Skip to content

Commit e89b2c6

Browse files
committed
Merge #136: cmake: Support being included with add_subdirectory
6adbb1d cmake: Support being included with add_subdirectory (Ryan Ofsky) 1103f86 cmake: Define and use MP_INCLUDE_DIR variable (Ryan Ofsky) Pull request description: This implements changes needed to make the cmake project work well when it is not the top level project and has been included with `add_subdirectory`. - Avoids defining "tests" and "check" targets when project is not at the top level to avoid clashes. Add new "mptests" and "mpcheck" alternatives instead. - Renames "example" target to "mpexamples" - Renames "util" target to "mputil" - Makes various internal changes needed to resolve other issues. See commit messages for details. Top commit has no ACKs. Tree-SHA512: c3acbba696bfda47284a0cf557b56552a4a1764cc000522010de3985824089b9973a98f82169986d238a073939df87d282a72698921adca804a6992135ec59d0
2 parents 3dea1d5 + 6adbb1d commit e89b2c6

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

CMakeLists.txt

+21-6
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,32 @@ include("cmake/compat_config.cmake")
2626
include("cmake/pthread_checks.cmake")
2727
include(GNUInstallDirs)
2828

29+
# Set convenience variables for subdirectories.
30+
set(MP_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
31+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
32+
set(MP_STANDALONE TRUE)
33+
else()
34+
# Set MP_INCLUDE_DIR for parent directories too, so target_capnp_sources calls
35+
# in parent directories can use it and not need to specify include directories
36+
# manually or see capnproto error "error: Import failed: /mp/proxy.capnp"
37+
set(MP_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE)
38+
set(MP_STANDALONE FALSE)
39+
endif()
40+
41+
# Prevent include directories from parent project from leaking into this one.
42+
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES "")
43+
2944
# Generated C++ preprocessor defines
3045
configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/config.h")
3146

3247
# Generated C++ Capn'Proto schema files
3348
capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp)
3449

3550
# util library
36-
add_library(util OBJECT src/mp/util.cpp)
37-
target_include_directories(util PRIVATE
38-
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
39-
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>
51+
add_library(mputil OBJECT src/mp/util.cpp)
52+
target_include_directories(mputil PRIVATE
53+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
54+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
4055
${CAPNP_INCLUDE_DIRECTORY})
4156

4257
# libmultiprocess.a runtime library
@@ -71,7 +86,7 @@ add_library(multiprocess STATIC
7186
${MP_PROXY_SRCS}
7287
${MP_PUBLIC_HEADERS}
7388
src/mp/proxy.cpp
74-
$<TARGET_OBJECTS:util>)
89+
$<TARGET_OBJECTS:mputil>)
7590
add_library(Libmultiprocess::multiprocess ALIAS multiprocess)
7691
target_include_directories(multiprocess PUBLIC
7792
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
@@ -89,7 +104,7 @@ install(TARGETS multiprocess EXPORT LibTargets
89104
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mp COMPONENT lib)
90105

91106
# mpgen code generator
92-
add_executable(mpgen src/mp/gen.cpp $<TARGET_OBJECTS:util>)
107+
add_executable(mpgen src/mp/gen.cpp $<TARGET_OBJECTS:mputil>)
93108
add_executable(Libmultiprocess::mpgen ALIAS mpgen)
94109
target_include_directories(mpgen PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>)
95110
target_include_directories(mpgen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

cmake/TargetCapnpSources.cmake

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function(target_capnp_sources target include_prefix)
6868
foreach(capnp_file IN LISTS TCS_UNPARSED_ARGUMENTS)
6969
add_custom_command(
7070
OUTPUT ${capnp_file}.c++ ${capnp_file}.h ${capnp_file}.proxy-client.c++ ${capnp_file}.proxy-types.h ${capnp_file}.proxy-server.c++ ${capnp_file}.proxy-types.c++ ${capnp_file}.proxy.h
71-
COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS}
71+
COMMAND Libmultiprocess::mpgen ${CMAKE_CURRENT_SOURCE_DIR} ${include_prefix} ${CMAKE_CURRENT_SOURCE_DIR}/${capnp_file} ${TCS_IMPORT_PATHS} ${MP_INCLUDE_DIR}
7272
DEPENDS ${capnp_file}
7373
VERBATIM
7474
)
@@ -89,7 +89,7 @@ function(target_capnp_sources target include_prefix)
8989
if(relative_path)
9090
string(APPEND build_include_prefix "/" "${relative_path}")
9191
endif()
92-
target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}>)
92+
target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${build_include_prefix}> ${MP_INCLUDE_DIR})
9393

9494
if(TARGET Libmultiprocess::multiprocess)
9595
target_link_libraries(${target} PRIVATE Libmultiprocess::multiprocess)

example/CMakeLists.txt

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,23 @@ include(${PROJECT_SOURCE_DIR}/cmake/TargetCapnpSources.cmake)
77
add_executable(mpcalculator
88
calculator.cpp
99
)
10-
target_capnp_sources(mpcalculator ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp
11-
IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include
12-
)
10+
target_capnp_sources(mpcalculator ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp)
1311
target_include_directories(mpcalculator PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
1412
target_link_libraries(mpcalculator PRIVATE Threads::Threads)
1513

1614
add_executable(mpprinter
1715
printer.cpp
1816
)
19-
target_capnp_sources(mpprinter ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp
20-
IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include
21-
)
17+
target_capnp_sources(mpprinter ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp)
2218
target_include_directories(mpprinter PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
2319
target_link_libraries(mpprinter PRIVATE Threads::Threads)
2420

2521
add_executable(mpexample
2622
example.cpp
2723
)
28-
target_capnp_sources(mpexample ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp
29-
IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include
30-
)
24+
target_capnp_sources(mpexample ${CMAKE_CURRENT_SOURCE_DIR} init.capnp calculator.capnp printer.capnp)
3125
target_include_directories(mpexample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
3226
target_link_libraries(mpexample PRIVATE Threads::Threads)
3327
target_link_libraries(mpexample PRIVATE stdc++fs)
3428

35-
add_custom_target(example DEPENDS mpexample mpcalculator mpprinter)
29+
add_custom_target(mpexamples DEPENDS mpexample mpcalculator mpprinter)

test/CMakeLists.txt

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ include(CTest)
1111
# that were previously built, without building anything itself. Define "make
1212
# tests" here as a custom target to build all available tests and "make check"
1313
# as a custom target to build and run them.
14-
add_custom_target(tests)
15-
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS tests)
14+
add_custom_target(mptests)
15+
add_custom_target(mpcheck COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS mptests)
16+
17+
# Only add more convenient tests and check targets if project is being built
18+
# standlone, to prevent clashes with external projects.
19+
if (MP_STANDALONE)
20+
add_custom_target(tests DEPENDS mptests)
21+
add_custom_target(check DEPENDS mpcheck)
22+
endif()
1623

1724
if(BUILD_TESTING AND TARGET CapnProto::kj-test)
1825
set_property(SOURCE ${MP_PROXY_HDRS} PROPERTY GENERATED 1)
@@ -24,13 +31,11 @@ if(BUILD_TESTING AND TARGET CapnProto::kj-test)
2431
mp/test/test.cpp
2532
)
2633
include(${PROJECT_SOURCE_DIR}/cmake/TargetCapnpSources.cmake)
27-
target_capnp_sources(mptest ${CMAKE_CURRENT_SOURCE_DIR} mp/test/foo.capnp
28-
IMPORT_PATHS ${CMAKE_SOURCE_DIR}/include
29-
)
34+
target_capnp_sources(mptest ${CMAKE_CURRENT_SOURCE_DIR} mp/test/foo.capnp)
3035
target_include_directories(mptest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
3136
target_link_libraries(mptest PRIVATE CapnProto::kj-test)
3237
target_link_libraries(mptest PRIVATE Threads::Threads)
3338

34-
add_dependencies(tests mptest)
39+
add_dependencies(mptests mptest)
3540
add_test(NAME mptest COMMAND mptest)
3641
endif()

0 commit comments

Comments
 (0)