Skip to content

Commit 0f36bd2

Browse files
lukasm91havogt
authored andcommitted
Fix CMake for custom_test_generated_repository(#1125)
Description: - More idiomatic usage of `add_custom_command` and `add_custom_target` - Does not recompile each time anymore
1 parent 8e9e701 commit 0f36bd2

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

Diff for: unit_tests/interface/repository/CMakeLists.txt

+16-17
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ macro(generate_preprocessed_file preprocessed_file_out cpp_file)
88
target_compile_options( ${cpp_file}_OBJECTS BEFORE PUBLIC -E ${GT_CXX_FLAGS})
99
target_link_libraries( ${cpp_file}_OBJECTS common )
1010

11-
# now use the preprocessed file to generate a library (which we will never use) ...
12-
add_library( dummy_${cpp_file} STATIC EXCLUDE_FROM_ALL $<TARGET_OBJECTS:${cpp_file}_OBJECTS>)
13-
# ... but before linking we copy the preprocessed file to the given file "preprocessed_file_out"
14-
# this trick is necessary because $<TARGET_OBJECTS:XXX> is only defined within add_library
15-
add_custom_command( TARGET dummy_${cpp_file} PRE_LINK
16-
COMMAND ${CMAKE_COMMAND} -E copy '$<TARGET_OBJECTS:${cpp_file}_OBJECTS>' ${preprocessed_file_out}
17-
)
18-
1911
# add a target for the generated file
20-
add_custom_command( OUTPUT ${preprocessed_file_out} COMMAND "" DEPENDS dummy_${cpp_file})
12+
add_custom_command( OUTPUT ${preprocessed_file_out}
13+
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_OBJECTS:${cpp_file}_OBJECTS> ${preprocessed_file_out}
14+
DEPENDS ${cpp_file}_OBJECTS $<TARGET_OBJECTS:${cpp_file}_OBJECTS>
15+
COMMENT "Extract generated code"
16+
)
17+
18+
get_filename_component(generated_target_name ${preprocessed_file_out} NAME_WE)
19+
add_custom_target(${generated_target_name}
20+
DEPENDS ${preprocessed_file_out}
21+
)
2122
else()
2223
message( ERROR "In macro generate_preprocessed_file: ${cpp_file} is not a file" )
2324
endif()
@@ -29,22 +30,20 @@ if (GT_ENABLE_TARGET_X86)
2930
generate_preprocessed_file( ${GENERATED_REPOSITORY} plain_repository_generator.cpp )
3031

3132
# clean the preprocessed file from comments and from everything before our class starts
32-
add_custom_target( process_generated_repository
33+
add_custom_command(OUTPUT ${GENERATED_REPOSITORY}
3334
# remove all lines starting with '#'
3435
# remove everything before "class my_repository"
3536
COMMAND sed "/^#/d" ${GENERATED_REPOSITORY} | awk "/class my_repository/,0" > ${GENERATED_REPOSITORY}_tmp
3637
COMMAND cp ${GENERATED_REPOSITORY}_tmp ${GENERATED_REPOSITORY}
37-
DEPENDS ${GENERATED_REPOSITORY}
38+
APPEND
3839
)
3940

4041
# format the generated file if we can
4142
if( CLANG_FORMAT_FOUND )
42-
add_custom_target( format_generated_repository COMMAND ${CLANG_FORMAT_BIN} -i ${GENERATED_REPOSITORY}
43-
DEPENDS process_generated_repository
43+
add_custom_command(OUTPUT ${GENERATED_REPOSITORY}
44+
COMMAND ${CLANG_FORMAT_BIN} -i ${GENERATED_REPOSITORY}
45+
APPEND
4446
)
45-
else()
46-
#dummy target for dependency
47-
add_custom_target( format_generated_repository COMMAND "" DEPENDS process_generated_repository)
4847
endif()
4948

5049
# generate the driver
@@ -59,7 +58,7 @@ if (GT_ENABLE_TARGET_X86)
5958
LABELS unittest_x86 target_x86
6059
ENVIRONMENT ${TEST_HOST_ENVIRONMENT}
6160
)
62-
add_dependencies( custom_test_generated_repository format_generated_repository )
61+
add_dependencies(custom_test_generated_repository generated_repository)
6362
endif()
6463

6564
# collect test cases

0 commit comments

Comments
 (0)