Skip to content

Commit 6adbb1d

Browse files
committed
cmake: Support being included with add_subdirectory
Make changes needed to make the cmake project work well when it is not the top level project and has been included with add_subdirectory: - Avoid defining "tests" and "check" targets when project is not at the top level to avoid clashes. Add new "mptests" and "mpcheck" alternatives instead. - Rename "example" target to "mpexamples" - Rename "util" target to "mputil" - Export MP_INCLUDE_DIR variable to parent scope so target_capnp_sources() function can work well there. - Replace uses of CMAKE_SOURCE_DIR with CMAKE_CURRENT_SOURCE_DIR - Reset INCLUDE_DIRECTORIES property so include_directories calls in the parent project do not affect this project. This was causing errors because the bitcoin core build was adding global include directories and causing its init.h file to take precedence over local one.
1 parent 1103f86 commit 6adbb1d

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

CMakeLists.txt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,20 @@ include("cmake/compat_config.cmake")
2626
include("cmake/pthread_checks.cmake")
2727
include(GNUInstallDirs)
2828

29+
# Set convenience variables for subdirectories.
2930
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 "")
3043

3144
# Generated C++ preprocessor defines
3245
configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/config.h")
@@ -35,10 +48,10 @@ configure_file(include/mp/config.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/mp/co
3548
capnp_generate_cpp(MP_PROXY_SRCS MP_PROXY_HDRS include/mp/proxy.capnp)
3649

3750
# util library
38-
add_library(util OBJECT src/mp/util.cpp)
39-
target_include_directories(util PRIVATE
40-
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
41-
$<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>
4255
${CAPNP_INCLUDE_DIRECTORY})
4356

4457
# libmultiprocess.a runtime library
@@ -52,7 +65,7 @@ add_library(multiprocess STATIC
5265
${MP_PROXY_SRCS}
5366
${MP_PUBLIC_HEADERS}
5467
src/mp/proxy.cpp
55-
$<TARGET_OBJECTS:util>)
68+
$<TARGET_OBJECTS:mputil>)
5669
add_library(Libmultiprocess::multiprocess ALIAS multiprocess)
5770
target_include_directories(multiprocess PUBLIC
5871
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
@@ -70,7 +83,7 @@ install(TARGETS multiprocess EXPORT LibTargets
7083
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mp COMPONENT lib)
7184

7285
# mpgen code generator
73-
add_executable(mpgen src/mp/gen.cpp $<TARGET_OBJECTS:util>)
86+
add_executable(mpgen src/mp/gen.cpp $<TARGET_OBJECTS:mputil>)
7487
add_executable(Libmultiprocess::mpgen ALIAS mpgen)
7588
target_include_directories(mpgen PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>)
7689
target_include_directories(mpgen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

example/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ target_include_directories(mpexample PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
2626
target_link_libraries(mpexample PRIVATE Threads::Threads)
2727
target_link_libraries(mpexample PRIVATE stdc++fs)
2828

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

test/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
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)
@@ -29,6 +36,6 @@ if(BUILD_TESTING AND TARGET CapnProto::kj-test)
2936
target_link_libraries(mptest PRIVATE CapnProto::kj-test)
3037
target_link_libraries(mptest PRIVATE Threads::Threads)
3138

32-
add_dependencies(tests mptest)
39+
add_dependencies(mptests mptest)
3340
add_test(NAME mptest COMMAND mptest)
3441
endif()

0 commit comments

Comments
 (0)