Skip to content

Commit c9b3c64

Browse files
authored
🚸 only add CMake targets if they do not exist (#159)
1 parent c1c0d70 commit c9b3c64

File tree

3 files changed

+74
-62
lines changed

3 files changed

+74
-62
lines changed

extern/dd_package/CMakeLists.txt

+46-40
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,61 @@ include(cmake/PreventInSourceBuilds.cmake)
1717
include(cmake/CheckSubmodule.cmake)
1818
include(cmake/PackageAddTest.cmake)
1919

20-
# Use the warnings specified in CompilerWarnings.cmake
21-
add_library(project_warnings INTERFACE)
20+
if(NOT TARGET project_warnings)
21+
# Use the warnings specified in CompilerWarnings.cmake
22+
add_library(project_warnings INTERFACE)
2223

23-
# Standard compiler warnings
24-
include(cmake/CompilerWarnings.cmake)
25-
set_project_warnings(project_warnings)
24+
# Standard compiler warnings
25+
include(cmake/CompilerWarnings.cmake)
26+
set_project_warnings(project_warnings)
27+
endif()
2628

27-
# Interface library to set project options
28-
add_library(project_options INTERFACE)
29+
if(NOT TARGET project_options)
30+
# Use the options specified in CompilerOptions.cmake
31+
add_library(project_options INTERFACE)
2932

30-
# Compiler options
31-
include(cmake/CompilerOptions.cmake)
32-
enable_project_options(project_options)
33+
# Standard compiler options
34+
include(cmake/CompilerOptions.cmake)
35+
enable_project_options(project_options)
3336

34-
# Sanitizer options if supported by compiler
35-
include(cmake/Sanitizers.cmake)
36-
enable_sanitizers(project_options)
37+
# Sanitizer options if supported by compiler
38+
include(cmake/Sanitizers.cmake)
39+
enable_sanitizers(project_options)
40+
endif()
3741

3842
# add main library code
39-
add_library(
40-
${PROJECT_NAME}
41-
INTERFACE
42-
include/dd/Complex.hpp
43-
include/dd/ComplexCache.hpp
44-
include/dd/ComplexNumbers.hpp
45-
include/dd/ComplexTable.hpp
46-
include/dd/ComplexValue.hpp
47-
include/dd/ComputeTable.hpp
48-
include/dd/Control.hpp
49-
include/dd/Definitions.hpp
50-
include/dd/Edge.hpp
51-
include/dd/Export.hpp
52-
include/dd/GateMatrixDefinitions.hpp
53-
include/dd/Node.hpp
54-
include/dd/Package.hpp
55-
include/dd/ToffoliTable.hpp
56-
include/dd/UnaryComputeTable.hpp
57-
include/dd/DensityNoiseTable.hpp
58-
include/dd/StochasticNoiseOperationTable.hpp
59-
include/dd/UniqueTable.hpp)
43+
if(NOT TARGET ${PROJECT_NAME})
44+
add_library(
45+
${PROJECT_NAME}
46+
INTERFACE
47+
include/dd/Complex.hpp
48+
include/dd/ComplexCache.hpp
49+
include/dd/ComplexNumbers.hpp
50+
include/dd/ComplexTable.hpp
51+
include/dd/ComplexValue.hpp
52+
include/dd/ComputeTable.hpp
53+
include/dd/Control.hpp
54+
include/dd/Definitions.hpp
55+
include/dd/Edge.hpp
56+
include/dd/Export.hpp
57+
include/dd/GateMatrixDefinitions.hpp
58+
include/dd/Node.hpp
59+
include/dd/Package.hpp
60+
include/dd/ToffoliTable.hpp
61+
include/dd/UnaryComputeTable.hpp
62+
include/dd/DensityNoiseTable.hpp
63+
include/dd/StochasticNoiseOperationTable.hpp
64+
include/dd/UniqueTable.hpp)
6065

61-
# add options and warnings to library
62-
target_link_libraries(${PROJECT_NAME} INTERFACE project_options project_warnings)
66+
# add options and warnings to library
67+
target_link_libraries(${PROJECT_NAME} INTERFACE project_options project_warnings)
6368

64-
# set include directories
65-
target_include_directories(${PROJECT_NAME} INTERFACE include ${PROJECT_BINARY_DIR}/include)
69+
# set include directories
70+
target_include_directories(${PROJECT_NAME} INTERFACE include ${PROJECT_BINARY_DIR}/include)
6671

67-
# add MQT alias
68-
add_library(MQT::DDPackage ALIAS ${PROJECT_NAME})
72+
# add MQT alias
73+
add_library(MQT::DDPackage ALIAS ${PROJECT_NAME})
74+
endif()
6975

7076
# add test code
7177
option(BUILD_DD_PACKAGE_TESTS "Also build tests for DD package")
+12-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# macro to add a test executable for one of the project libraries
22
macro(PACKAGE_ADD_TEST testname linklibs)
3-
# create an executable in which the tests will be stored
4-
add_executable(${testname} ${ARGN})
5-
# link the Google test infrastructure and a default main function to the test executable.
6-
target_link_libraries(${testname} PRIVATE ${linklibs} gmock gtest_main)
7-
# discover tests
8-
gtest_discover_tests(
9-
${testname}
10-
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
11-
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" DISCOVERY_TIMEOUT 60)
12-
set_target_properties(${testname} PROPERTIES FOLDER tests)
3+
if(NOT TARGET ${testname})
4+
# create an executable in which the tests will be stored
5+
add_executable(${testname} ${ARGN})
6+
# link the Google test infrastructure and a default main function to the test executable.
7+
target_link_libraries(${testname} PRIVATE ${linklibs} gmock gtest_main)
8+
# discover tests
9+
gtest_discover_tests(
10+
${testname}
11+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
12+
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" DISCOVERY_TIMEOUT 60)
13+
set_target_properties(${testname} PROPERTIES FOLDER tests)
14+
endif()
1315
endmacro()

extern/dd_package/test/CMakeLists.txt

+16-12
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@ if(NOT TARGET benchmark::benchmark)
1717
set_target_properties(benchmark benchmark_main PROPERTIES FOLDER extern)
1818
endif()
1919

20-
add_executable(${PROJECT_NAME}_example main.cpp)
21-
target_link_libraries(${PROJECT_NAME}_example PRIVATE ${PROJECT_NAME})
22-
set_target_properties(${PROJECT_NAME}_example PROPERTIES FOLDER tests)
20+
if(NOT TARGET ${PROJECT_NAME}_example)
21+
add_executable(${PROJECT_NAME}_example main.cpp)
22+
target_link_libraries(${PROJECT_NAME}_example PRIVATE ${PROJECT_NAME})
23+
set_target_properties(${PROJECT_NAME}_example PROPERTIES FOLDER tests)
24+
25+
# include dd_example in test suite
26+
add_test(NAME ${PROJECT_NAME}_example COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_example)
27+
endif()
2328

2429
# add unit tests
2530
package_add_test(${PROJECT_NAME}_test ${PROJECT_NAME} test_complex.cpp test_package.cpp)
2631

27-
# include dd_example in test suite
28-
add_test(NAME ${PROJECT_NAME}_example COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_example)
29-
30-
# add benchmark executable
31-
add_executable(${PROJECT_NAME}_bench EXCLUDE_FROM_ALL bench_package.cpp)
32-
# link the Google benchmark infrastructure and a default main function to the benchmark executable.
33-
target_link_libraries(${PROJECT_NAME}_bench PRIVATE ${PROJECT_NAME} benchmark::benchmark_main
34-
Threads::Threads)
35-
set_target_properties(${PROJECT_NAME}_bench PROPERTIES FOLDER tests)
32+
if(NOT TARGET ${PROJECT_NAME}_bench)
33+
# add benchmark executable
34+
add_executable(${PROJECT_NAME}_bench EXCLUDE_FROM_ALL bench_package.cpp)
35+
# link the Google benchmark infrastructure and a default main function
36+
target_link_libraries(${PROJECT_NAME}_bench PRIVATE ${PROJECT_NAME} benchmark::benchmark_main
37+
Threads::Threads)
38+
set_target_properties(${PROJECT_NAME}_bench PROPERTIES FOLDER tests)
39+
endif()

0 commit comments

Comments
 (0)