diff --git a/cmake/colobot-lint.cmake b/cmake/colobot-lint.cmake index 67b42d7e..84023220 100644 --- a/cmake/colobot-lint.cmake +++ b/cmake/colobot-lint.cmake @@ -4,17 +4,20 @@ # This adds a new target with generated fake source files that include single header files # It is a workaround for how CMake and Clang handle header files in compilation database # And we need that to check each header file in the project exactly once, the same as .cpp modules +# Note: This is modifying an existing target by adding fake source files. ## -macro(add_fake_header_sources subdir) +macro(add_fake_header_sources subdir the_target) set(all_fake_header_src_files "") set(fake_headers_src_dir ${colobot_BINARY_DIR}/fake_header_sources) file(MAKE_DIRECTORY ${fake_headers_src_dir}) - file(GLOB_RECURSE all_header_files RELATIVE ${colobot_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.h) + get_target_property(the_target_sources ${the_target} SOURCES) - if(all_header_files) - foreach(header_file ${all_header_files}) + foreach(source_file IN LISTS the_target_sources) + string(REGEX MATCH [[^.*\.h$]] is_header_file ${source_file}) + if(is_header_file) + file(RELATIVE_PATH header_file ${colobot_SOURCE_DIR} ${colobot_SOURCE_DIR}/${subdir}/${source_file}) string(REGEX REPLACE "\\.h$" ".cpp" fake_header_src_file "${fake_headers_src_dir}/${header_file}") get_filename_component(fake_header_src_dir ${fake_header_src_file} PATH) @@ -22,10 +25,8 @@ macro(add_fake_header_sources subdir) file(WRITE ${fake_header_src_file} "#include \"${header_file}\"\n\n") - list(APPEND all_fake_header_src_files ${fake_header_src_file}) - endforeach() - - add_library(colobot_${subdir}_fake_header_srcs STATIC ${all_fake_header_src_files}) - target_include_directories(colobot_${subdir}_fake_header_srcs PUBLIC ${colobot_SOURCE_DIR}) - endif() + target_sources(${the_target} PRIVATE ${fake_header_src_file}) + target_include_directories(${the_target} PUBLIC ${colobot_SOURCE_DIR}) + endif() + endforeach() endmacro() diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index 896b2e5a..359175f0 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -164,3 +164,7 @@ target_sources(CBot PRIVATE ) target_include_directories(CBot PUBLIC ${CMAKE_CURRENT_LIST_DIR}/..) target_link_libraries(CBot PRIVATE Boost::headers) + +if(COLOBOT_LINT_BUILD) + add_fake_header_sources("src/CBot" CBot) +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6ea0bfab..a3ce5527 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -593,5 +593,6 @@ endif() # Linter-specific if(COLOBOT_LINT_BUILD) - add_fake_header_sources("src") + add_fake_header_sources("src" colobotbase) + add_fake_header_sources("src" colobot) endif() diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index 4923e4c8..5e63a289 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -7,4 +7,3 @@ add_executable(convert_model convert_model.cpp) target_link_libraries(convert_model PRIVATE Boost::headers) target_include_directories(convert_model PRIVATE . ..) - diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cd4c320b..508056b4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,8 +8,3 @@ add_subdirectory(unit) # CBot tests add_subdirectory(cbot) - - -if(COLOBOT_LINT_BUILD) - add_fake_header_sources("test") -endif() diff --git a/test/cbot/CMakeLists.txt b/test/cbot/CMakeLists.txt index 5be58335..473f3d73 100644 --- a/test/cbot/CMakeLists.txt +++ b/test/cbot/CMakeLists.txt @@ -11,3 +11,8 @@ target_link_directories(CBot_compile_graph PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ) target_link_libraries(CBot_compile_graph CBot) + +if(COLOBOT_LINT_BUILD) + add_fake_header_sources("test/cbot" CBot_console) + add_fake_header_sources("test/cbot" CBot_compile_graph) +endif() diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index ee7e20fb..6121021e 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -48,3 +48,7 @@ gtest_discover_tests(colobot_ut if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) target_compile_options(colobot_ut PRIVATE "-Wno-suggest-override") endif() + +if(COLOBOT_LINT_BUILD) + add_fake_header_sources("test/unit" colobot_ut) +endif()