From 433ea2037b210751f984f12eef2d926dfded6897 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Sat, 8 Aug 2020 15:37:51 +0200 Subject: cmake: Detect custom command targets in compiler args This is required to make `-include /path/to/custom/target.hpp` work. This setup is used by wxWidgets and this PR is required to use wxWidgets as a CMake subproject. --- mesonbuild/cmake/interpreter.py | 9 ++++++++- .../cmake/8 custom command/subprojects/cmMod/CMakeLists.txt | 10 ++++++++++ .../cmake/8 custom command/subprojects/cmMod/cpyInc.hpp.am | 3 +++ .../cmake/8 custom command/subprojects/cmMod/macro_name.cpp | 6 ++++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test cases/cmake/8 custom command/subprojects/cmMod/cpyInc.hpp.am diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index 9749c4c..c2affd0 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -279,7 +279,7 @@ class ConverterTarget: std_regex = re.compile(r'([-]{1,2}std=|/std:v?|[-]{1,2}std:)(.*)') def postprocess(self, output_target_map: OutputTargetMap, root_src_dir: str, subdir: str, install_prefix: str, trace: CMakeTraceParser) -> None: - # Detect setting the C and C++ standard + # Detect setting the C and C++ standard and do additional compiler args manipulation for i in ['c', 'cpp']: if i not in self.compile_opts: continue @@ -287,6 +287,7 @@ class ConverterTarget: temp = [] for j in self.compile_opts[i]: m = ConverterTarget.std_regex.match(j) + ctgt = output_target_map.generated(j) if m: std = m.group(2) supported = self._all_lang_stds(i) @@ -301,6 +302,12 @@ class ConverterTarget: self.override_options += ['{}_std={}'.format(i, std)] elif j in ['-fPIC', '-fpic', '-fPIE', '-fpie']: self.pie = True + elif isinstance(ctgt, ConverterCustomTarget): + # Sometimes projects pass generated source files as compiler + # flags. Add these as generated sources to ensure that the + # corresponding custom target is run.2 + self.generated += [j] + temp += [j] elif j in blacklist_compiler_flags: pass else: diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt index 1498c36..199c2e9 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt +++ b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt @@ -134,6 +134,16 @@ add_custom_target(args_test_cmd ) add_custom_target(macro_name_cmd COMMAND macro_name) +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + message(STATUS "Running the -include test case on macro_name") + add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cpyInc.hpp" + COMMAND mycpy "${CMAKE_CURRENT_SOURCE_DIR}/cpyInc.hpp.am" "${CMAKE_CURRENT_BINARY_DIR}/cpyInc.hpp" + DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/cpyInc.hpp.am" + ) + target_compile_options(macro_name PUBLIC -DTEST_CMD_INCLUDE -include "${CMAKE_CURRENT_BINARY_DIR}/cpyInc.hpp") +endif() + # Only executable targets are replaced in the command # all other target names are kept as is add_custom_target(clang-format COMMAND clang-format -i cmMod.cpp) diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/cpyInc.hpp.am b/test cases/cmake/8 custom command/subprojects/cmMod/cpyInc.hpp.am new file mode 100644 index 0000000..07c8ff7 --- /dev/null +++ b/test cases/cmake/8 custom command/subprojects/cmMod/cpyInc.hpp.am @@ -0,0 +1,3 @@ +#pragma once + +#define CPY_INC_WAS_INCLUDED 1 diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp index 790557b..964062f 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp +++ b/test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp @@ -5,6 +5,12 @@ using namespace std; +#ifdef TEST_CMD_INCLUDE +#if CPY_INC_WAS_INCLUDED != 1 +#error "cpyInc.hpp was not included" +#endif +#endif + int main() { this_thread::sleep_for(chrono::seconds(1)); ofstream out1("macro_name.txt"); -- cgit v1.1