From 6844ad374e0a333434782927e099b16ab1b2dfd0 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 2 Oct 2019 11:58:29 -0400 Subject: [PATCH 1/7] cmake: Add support for add_dependencies() Closes: #5983 --- mesonbuild/cmake/interpreter.py | 35 ++++++++++++++++++++++++++------- mesonbuild/cmake/traceparser.py | 15 ++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index 8098bdb9c..718e70acc 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -93,8 +93,6 @@ target_type_map = { 'INTERFACE_LIBRARY': 'header_only' } -target_type_requires_trace = ['INTERFACE_LIBRARY'] - skip_targets = ['UTILITY'] blacklist_compiler_flags = [ @@ -152,6 +150,8 @@ class ConverterTarget: self.install_dir = '' self.link_libraries = target.link_libraries self.link_flags = target.link_flags + target.link_lang_flags + self.depends_raw = [] + self.depends = [] if target.install_paths: self.install_dir = target.install_paths[0] @@ -227,16 +227,18 @@ class ConverterTarget: self.pie = True # Use the CMake trace, if required - if self.type.upper() in target_type_requires_trace: - if self.cmake_name in trace.targets: - props = trace.targets[self.cmake_name].properties + tgt = trace.targets.get(self.cmake_name) + if tgt: + self.depends_raw = trace.targets[self.cmake_name].depends + if self.type.upper() == 'INTERFACE_LIBRARY': + props = tgt.properties self.includes += props.get('INTERFACE_INCLUDE_DIRECTORIES', []) self.public_compile_opts += props.get('INTERFACE_COMPILE_DEFINITIONS', []) self.public_compile_opts += props.get('INTERFACE_COMPILE_OPTIONS', []) self.link_flags += props.get('INTERFACE_LINK_OPTIONS', []) - else: - mlog.warning('CMake: Target', mlog.bold(self.cmake_name), 'not found in CMake trace. This can lead to build errors') + elif self.type.upper() not in ['EXECUTABLE', 'OBJECT_LIBRARY']: + mlog.warning('CMake: Target', mlog.bold(self.cmake_name), 'not found in CMake trace. This can lead to build errors') # Fix link libraries def try_resolve_link_with(path: str) -> Optional[str]: @@ -337,6 +339,11 @@ class ConverterTarget: self.link_libraries = [x for x in self.link_libraries if x.lower() not in blacklist_link_libs] self.link_flags = [x for x in self.link_flags if check_flag(x)] + for i in self.depends_raw: + tgt = output_target_map.get(_target_key(i)) + if tgt: + self.depends.append(tgt) + def process_object_libs(self, obj_target_list: List['ConverterTarget']): # Try to detect the object library(s) from the generated input sources temp = [x for x in self.generated if isinstance(x, str)] @@ -841,6 +848,7 @@ class CMakeInterpreter: generated = [] generated_filenames = [] custom_targets = [] + dependencies = [] for i in tgt.link_with: assert(isinstance(i, ConverterTarget)) if i.name not in processed: @@ -851,6 +859,12 @@ class CMakeInterpreter: if i.name not in processed: process_target(i) objec_libs += [extract_tgt(i)] + for i in tgt.depends: + if not isinstance(i, ConverterCustomTarget): + continue + if i.name not in processed: + process_custom_target(i) + dependencies += [extract_tgt(i)] # Generate the source list and handle generated sources for i in tgt.sources + tgt.generated: @@ -920,6 +934,9 @@ class CMakeInterpreter: 'include_directories': id_node(inc_var), } + if dependencies: + generated += dependencies + # Generate the function nodes dir_node = assign(dir_var, function('include_directories', tgt.includes)) sys_node = assign(sys_var, function('include_directories', tgt.sys_includes, {'is_system': True})) @@ -959,6 +976,10 @@ class CMakeInterpreter: if x.name not in processed: process_target(x) return extract_tgt(x) + if isinstance(x, ConverterCustomTarget): + if x.name not in processed: + process_custom_target(x) + return extract_tgt(x) elif isinstance(x, CustomTargetReference): if x.ctgt.name not in processed: process_custom_target(x.ctgt) diff --git a/mesonbuild/cmake/traceparser.py b/mesonbuild/cmake/traceparser.py index 7daef0b1c..c5e0d93a9 100644 --- a/mesonbuild/cmake/traceparser.py +++ b/mesonbuild/cmake/traceparser.py @@ -43,6 +43,7 @@ class CMakeTarget: self.properties = properties self.imported = imported self.tline = tline + self.depends = [] def __repr__(self): s = 'CMake TARGET:\n -- name: {}\n -- type: {}\n -- imported: {}\n -- properties: {{\n{} }}\n -- tline: {}' @@ -89,6 +90,7 @@ class CMakeTraceParser: 'target_compile_options': self._cmake_target_compile_options, 'target_include_directories': self._cmake_target_include_directories, 'target_link_options': self._cmake_target_link_options, + 'add_dependencies': self._cmake_add_dependencies, } # Primary pass -- parse everything @@ -398,6 +400,19 @@ class CMakeTraceParser: self.targets[i].properties[name] = value + def _cmake_add_dependencies(self, tline: CMakeTraceLine) -> None: + # DOC: https://cmake.org/cmake/help/latest/command/add_dependencies.html + args = list(tline.args) + + if len(args) < 2: + return self._gen_exception('add_dependencies', 'takes at least 2 arguments', tline) + + target = self.targets.get(args[0]) + if not target: + return self._gen_exception('add_dependencies', 'target not found', tline) + + target.depends += args[1:] + def _cmake_target_compile_definitions(self, tline: CMakeTraceLine) -> None: # DOC: https://cmake.org/cmake/help/latest/command/target_compile_definitions.html self._parse_common_target_options('target_compile_definitions', 'COMPILE_DEFINITIONS', 'INTERFACE_COMPILE_DEFINITIONS', tline) From daeee32d9bf6a136fc3de6dade065a918290b429 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Tue, 1 Oct 2019 20:37:35 -0400 Subject: [PATCH 2/7] cmake: Add support for add_custom_target() with a command The command could have no output, in which case we create a dummy one. --- mesonbuild/cmake/data/run_ctgt.py | 14 +++++++++++--- mesonbuild/cmake/interpreter.py | 16 ++++++++++++---- mesonbuild/cmake/traceparser.py | 15 +++++++++------ 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/mesonbuild/cmake/data/run_ctgt.py b/mesonbuild/cmake/data/run_ctgt.py index d4e325922..4e9428335 100755 --- a/mesonbuild/cmake/data/run_ctgt.py +++ b/mesonbuild/cmake/data/run_ctgt.py @@ -13,13 +13,16 @@ SEPARATOR = ';;;' parser = argparse.ArgumentParser(description='Wrapper for add_custom_command') parser.add_argument('-d', '--directory', type=str, metavar='D', required=True, help='Working directory to cwd to') parser.add_argument('-o', '--outputs', nargs='+', metavar='O', required=True, help='Expected output files') -parser.add_argument('-O', '--original-outputs', nargs='+', metavar='O', required=True, help='Output files expected by CMake') -parser.add_argument('commands', nargs=argparse.REMAINDER, help='A "{}" separated list of commands'.format(SEPARATOR)) +parser.add_argument('-O', '--original-outputs', nargs='*', metavar='O', default=[], help='Output files expected by CMake') +parser.add_argument('commands', nargs=argparse.REMAINDER, help='A "{}" seperated list of commands'.format(SEPERATOR)) # Parse args = parser.parse_args() -if len(args.outputs) != len(args.original_outputs): +dummy_target = None +if len(args.outputs) == 1 and len(args.original_outputs) == 0: + dummy_target = args.outputs[0] +elif len(args.outputs) != len(args.original_outputs): print('Length of output list and original output list differ') sys.exit(1) @@ -39,6 +42,11 @@ for i in commands: subprocess.run(i, cwd=args.directory) +if dummy_target: + with open(dummy_target, 'a'): + os.utime(dummy_target, None) + exit(0) + # Copy outputs zipped_outputs = zip(args.outputs, args.original_outputs) for expected, generated in zipped_outputs: diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index 718e70acc..5fd8ce0ed 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -408,7 +408,10 @@ class ConverterCustomTarget: out_counter = 0 # type: int def __init__(self, target: CMakeGeneratorTarget): - self.name = 'custom_tgt_{}'.format(ConverterCustomTarget.tgt_counter) + self.name = target.name + if not self.name: + self.name = 'custom_tgt_{}'.format(ConverterCustomTarget.tgt_counter) + ConverterCustomTarget.tgt_counter += 1 self.original_outputs = list(target.outputs) self.outputs = [os.path.basename(x) for x in self.original_outputs] self.conflict_map = {} @@ -418,8 +421,6 @@ class ConverterCustomTarget: self.inputs = [] self.depends = [] - ConverterCustomTarget.tgt_counter += 1 - def __repr__(self) -> str: return '<{}: {}>'.format(self.__class__.__name__, self.outputs) @@ -729,6 +730,7 @@ class CMakeInterpreter: for j in i.artifacts: output_target_map[os.path.basename(j)] = i for i in self.custom_targets: + output_target_map[_target_key(i.name)] = i for j in i.original_outputs: output_target_map[_generated_file_key(j)] = i object_libs = [] @@ -971,6 +973,11 @@ class CMakeInterpreter: tgt_var = tgt.name # type: str + # If the custom target does not declare any output, create a dummy + # one that can be used as dependency. + if not tgt.outputs: + tgt.outputs = [tgt.name + '.h'] + def resolve_source(x: Any) -> Any: if isinstance(x, ConverterTarget): if x.name not in processed: @@ -991,7 +998,8 @@ class CMakeInterpreter: command = [] command += [id_node(run_script_var)] command += ['-o', '@OUTPUT@'] - command += ['-O'] + tgt.original_outputs + if tgt.original_outputs: + command += ['-O'] + tgt.original_outputs command += ['-d', tgt.working_dir] # Generate the commands. Subcommands are separated by ';;;' diff --git a/mesonbuild/cmake/traceparser.py b/mesonbuild/cmake/traceparser.py index c5e0d93a9..50cabab7c 100644 --- a/mesonbuild/cmake/traceparser.py +++ b/mesonbuild/cmake/traceparser.py @@ -52,12 +52,12 @@ class CMakeTarget: propSTR += " '{}': {}\n".format(i, self.properties[i]) return s.format(self.name, self.type, self.imported, propSTR, self.tline) -class CMakeGeneratorTarget: - def __init__(self): +class CMakeGeneratorTarget(CMakeTarget): + def __init__(self, name): + super().__init__(name, 'CUSTOM', {}) self.outputs = [] # type: List[str] self.command = [] # type: List[List[str]] self.working_dir = None # type: Optional[str] - self.depends = [] # type: List[str] class CMakeTraceParser: def __init__(self, permissive: bool = False): @@ -237,7 +237,7 @@ class CMakeTraceParser: else: self.targets[args[0]] = CMakeTarget(args[0], 'NORMAL', {}, tline=tline) - def _cmake_add_custom_command(self, tline: CMakeTraceLine): + def _cmake_add_custom_command(self, tline: CMakeTraceLine, name=None): # DOC: https://cmake.org/cmake/help/latest/command/add_custom_command.html args = list(tline.args) # Make a working copy @@ -252,7 +252,7 @@ class CMakeTraceParser: 'IMPLICIT_DEPENDS', 'WORKING_DIRECTORY', 'COMMENT', 'DEPFILE', 'JOB_POOL', 'VERBATIM', 'APPEND', 'USES_TERMINAL', 'COMMAND_EXPAND_LISTS'] - target = CMakeGeneratorTarget() + target = CMakeGeneratorTarget(name) def handle_output(key: str, target: CMakeGeneratorTarget) -> None: target.outputs += [key] @@ -297,6 +297,8 @@ class CMakeTraceParser: target.command = [self._guess_files(x) for x in target.command] self.custom_targets += [target] + if name: + self.targets[name] = target def _cmake_add_custom_target(self, tline: CMakeTraceLine): # DOC: https://cmake.org/cmake/help/latest/command/add_custom_target.html @@ -304,7 +306,8 @@ class CMakeTraceParser: if len(tline.args) < 1: return self._gen_exception('add_custom_target', 'requires at least one argument', tline) - self.targets[tline.args[0]] = CMakeTarget(tline.args[0], 'CUSTOM', {}, tline=tline) + # It's pretty much the same as a custom command + self._cmake_add_custom_command(tline, tline.args[0]) def _cmake_set_property(self, tline: CMakeTraceLine) -> None: # DOC: https://cmake.org/cmake/help/latest/command/set_property.html From f73a1dff0eaa631f9b674ebbafe43376b9675d9d Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 2 Oct 2019 20:17:26 -0400 Subject: [PATCH 3/7] cmake: Propagate error when running command --- mesonbuild/cmake/data/run_ctgt.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mesonbuild/cmake/data/run_ctgt.py b/mesonbuild/cmake/data/run_ctgt.py index 4e9428335..3786c5ddb 100755 --- a/mesonbuild/cmake/data/run_ctgt.py +++ b/mesonbuild/cmake/data/run_ctgt.py @@ -40,7 +40,10 @@ for i in commands: if not i: continue - subprocess.run(i, cwd=args.directory) + try: + subprocess.run(i, cwd=args.directory, check=True) + except subprocess.CalledProcessError: + exit(1) if dummy_target: with open(dummy_target, 'a'): From 8efbcb1e80d58fa39501a883968229270c4e4bec Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Mon, 7 Oct 2019 20:27:57 -0400 Subject: [PATCH 4/7] cmake: Add unit test for add_custom_target() and add_dependency() Also test commands with args separated by ';' --- .../subprojects/cmMod/CMakeLists.txt | 10 ++++++++++ .../subprojects/cmMod/args_test.cpp | 17 +++++++++++++++++ .../subprojects/cmMod/cmMod.cpp | 5 +++++ 3 files changed, 32 insertions(+) create mode 100644 test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp 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 259151c2c..ec56105df 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt +++ b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt @@ -44,3 +44,13 @@ add_custom_command( add_library(cmModLib SHARED cmMod.cpp genTest.cpp cpyBase.cpp cpyBase.hpp) include(GenerateExportHeader) generate_export_header(cmModLib) + +set(ARGS_TEST arg1) +set(ARGS_TEST ${ARGS_TEST} arg2) + +add_executable(args_test args_test.cpp) +add_custom_target(args_test_cmd + COMMAND args_test ARGS ${ARGS_TEST} +) + +add_dependencies(cmModLib args_test_cmd) diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp new file mode 100644 index 000000000..243e59706 --- /dev/null +++ b/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp @@ -0,0 +1,17 @@ +#include +#include + +using namespace std; + +int main(int argc, const char *argv[]) { + if(argc != 3 || string(argv[1]) != "arg1" || string(argv[2]) != "arg2") { + cerr << argv[0] << " requires 2 args" << endl; + return 1; + } + + ofstream out1("cmModLib.hpp"); + out1 << "#define FOO = \"plop\""; + + + return 0; +} diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp index 0fb6aa724..b7e8200d7 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp +++ b/test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp @@ -1,6 +1,11 @@ #include "cmMod.hpp" #include "genTest.hpp" #include "cpyBase.hpp" +#include "cmModLib.hpp" + +#ifndef FOO +#error FOO not declared +#endif using namespace std; From b2b9c102b5610112231a321fca4479c09a6ba234 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Wed, 20 Nov 2019 22:16:17 +0100 Subject: [PATCH 5/7] cmake: Some minor fixup --- mesonbuild/cmake/data/run_ctgt.py | 2 +- mesonbuild/cmake/interpreter.py | 38 +++++++++++++------ .../subprojects/cmMod/CMakeLists.txt | 4 ++ .../subprojects/cmMod/args_test.cpp | 3 +- .../subprojects/cmMod/macro_name.cpp | 14 +++++++ 5 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp diff --git a/mesonbuild/cmake/data/run_ctgt.py b/mesonbuild/cmake/data/run_ctgt.py index 3786c5ddb..954f8cea6 100755 --- a/mesonbuild/cmake/data/run_ctgt.py +++ b/mesonbuild/cmake/data/run_ctgt.py @@ -14,7 +14,7 @@ parser = argparse.ArgumentParser(description='Wrapper for add_custom_command') parser.add_argument('-d', '--directory', type=str, metavar='D', required=True, help='Working directory to cwd to') parser.add_argument('-o', '--outputs', nargs='+', metavar='O', required=True, help='Expected output files') parser.add_argument('-O', '--original-outputs', nargs='*', metavar='O', default=[], help='Output files expected by CMake') -parser.add_argument('commands', nargs=argparse.REMAINDER, help='A "{}" seperated list of commands'.format(SEPERATOR)) +parser.add_argument('commands', nargs=argparse.REMAINDER, help='A "{}" seperated list of commands'.format(SEPARATOR)) # Parse args = parser.parse_args() diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index 5fd8ce0ed..548acb194 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -170,6 +170,10 @@ class ConverterTarget: # Project default override options (c_std, cpp_std, etc.) self.override_options = [] + # Convert the target name to a valid meson target name + self.name = self.name.replace('-', '_') + self.name = generated_target_name_prefix + self.name + for i in target.files: # Determine the meson language lang = ConverterTarget.lang_cmake_to_meson.get(i.language.lower(), 'c') @@ -199,10 +203,6 @@ class ConverterTarget: std_regex = re.compile(r'([-]{1,2}std=|/std:v?|[-]{1,2}std:)(.*)') def postprocess(self, output_target_map: dict, root_src_dir: str, subdir: str, install_prefix: str, trace: CMakeTraceParser) -> None: - # Convert the target name to a valid meson target name - self.name = self.name.replace('-', '_') - self.name = generated_target_name_prefix + self.name - # Detect setting the C and C++ standard for i in ['c', 'cpp']: if i not in self.compile_opts: @@ -339,6 +339,7 @@ class ConverterTarget: self.link_libraries = [x for x in self.link_libraries if x.lower() not in blacklist_link_libs] self.link_flags = [x for x in self.link_flags if check_flag(x)] + # Handle explicit CMake add_dependency() calls for i in self.depends_raw: tgt = output_target_map.get(_target_key(i)) if tgt: @@ -382,6 +383,7 @@ class ConverterTarget: mlog.log(' -- generated: ', mlog.bold(str(self.generated))) mlog.log(' -- pie: ', mlog.bold('true' if self.pie else 'false')) mlog.log(' -- override_opts: ', mlog.bold(str(self.override_options))) + mlog.log(' -- depends: ', mlog.bold(str(self.depends))) mlog.log(' -- options:') for key, val in self.compile_opts.items(): mlog.log(' -', key, '=', mlog.bold(str(val))) @@ -412,6 +414,7 @@ class ConverterCustomTarget: if not self.name: self.name = 'custom_tgt_{}'.format(ConverterCustomTarget.tgt_counter) ConverterCustomTarget.tgt_counter += 1 + self.cmake_name = str(self.name) self.original_outputs = list(target.outputs) self.outputs = [os.path.basename(x) for x in self.original_outputs] self.conflict_map = {} @@ -421,8 +424,12 @@ class ConverterCustomTarget: self.inputs = [] self.depends = [] + # Convert the target name to a valid meson target name + self.name = self.name.replace('-', '_') + self.name = generated_target_name_prefix + self.name + def __repr__(self) -> str: - return '<{}: {}>'.format(self.__class__.__name__, self.outputs) + return '<{}: {} {}>'.format(self.__class__.__name__, self.name, self.outputs) def postprocess(self, output_target_map: dict, root_src_dir: str, subdir: str, build_dir: str, all_outputs: List[str]) -> None: # Default the working directory to the CMake build dir. This @@ -480,8 +487,15 @@ class ConverterCustomTarget: commands += [cmd] self.command = commands + # If the custom target does not declare any output, create a dummy + # one that can be used as dependency. + if not self.outputs: + self.outputs = [self.name + '.h'] + # Check dependencies and input files for i in self.depends_raw: + if not i: + continue tgt_key = _target_key(i) gen_key = _generated_file_key(i) @@ -725,16 +739,21 @@ class CMakeInterpreter: # generate the output_target_map output_target_map = {} output_target_map.update({x.full_name: x for x in self.targets}) - output_target_map.update({_target_key(x.name): x for x in self.targets}) + output_target_map.update({_target_key(x.cmake_name): x for x in self.targets}) for i in self.targets: for j in i.artifacts: output_target_map[os.path.basename(j)] = i for i in self.custom_targets: - output_target_map[_target_key(i.name)] = i + output_target_map[_target_key(i.cmake_name)] = i for j in i.original_outputs: output_target_map[_generated_file_key(j)] = i object_libs = [] + # Sometimes an empty string can be inserted (no full name, etc.) + # Delete the entry in this case + if '' in output_target_map: + del output_target_map[''] + # First pass: Basic target cleanup custom_target_outputs = [] # type: List[str] for i in self.custom_targets: @@ -973,11 +992,6 @@ class CMakeInterpreter: tgt_var = tgt.name # type: str - # If the custom target does not declare any output, create a dummy - # one that can be used as dependency. - if not tgt.outputs: - tgt.outputs = [tgt.name + '.h'] - def resolve_source(x: Any) -> Any: if isinstance(x, ConverterTarget): if x.name not in processed: 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 ec56105df..3c3297e10 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt +++ b/test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.5) project(cmMod) set (CMAKE_CXX_STANDARD 14) +set (CMAKE_CXX_STANDARD_REQUIRED ON) include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_definitions("-DDO_NOTHING_JUST_A_FLAG=1") @@ -48,9 +49,12 @@ generate_export_header(cmModLib) set(ARGS_TEST arg1) set(ARGS_TEST ${ARGS_TEST} arg2) +add_executable(macro_name macro_name.cpp) add_executable(args_test args_test.cpp) add_custom_target(args_test_cmd COMMAND args_test ARGS ${ARGS_TEST} ) +add_custom_target(macro_name_cmd COMMAND macro_name) add_dependencies(cmModLib args_test_cmd) +add_dependencies(args_test_cmd macro_name_cmd) diff --git a/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp b/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp index 243e59706..abb8a4266 100644 --- a/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp +++ b/test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp @@ -9,8 +9,9 @@ int main(int argc, const char *argv[]) { return 1; } + ifstream in1("macro_name.txt"); ofstream out1("cmModLib.hpp"); - out1 << "#define FOO = \"plop\""; + out1 << "#define " << in1.rdbuf() << " = \"plop\""; return 0; 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 new file mode 100644 index 000000000..790557b3a --- /dev/null +++ b/test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + this_thread::sleep_for(chrono::seconds(1)); + ofstream out1("macro_name.txt"); + out1 << "FOO"; + + return 0; +} From 3cdc386cde5ee489cb33d7c759cba989cbbf2448 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Thu, 28 Nov 2019 11:10:27 +0100 Subject: [PATCH 6/7] cmake: Detect dependenciy cycles and avoid infinite recursion --- mesonbuild/cmake/interpreter.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index 548acb194..c6bcbdf47 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -841,6 +841,7 @@ class CMakeInterpreter: root_cb.lines += [assign(run_script_var, function('find_program', [[run_script]], {'required': True}))] # Add the targets + processing = [] processed = {} name_map = {} @@ -854,6 +855,11 @@ class CMakeInterpreter: res_var = processed[tgt_name]['tgt'] return id_node(res_var) if res_var else None + def detect_cycle(tgt: Union[ConverterTarget, ConverterCustomTarget]) -> None: + if tgt.name in processing: + raise CMakeException('Cycle in CMake inputs/dependencies detected') + processing.append(tgt.name) + def resolve_ctgt_ref(ref: CustomTargetReference) -> BaseNode: tgt_var = extract_tgt(ref) if len(ref.ctgt.outputs) == 1: @@ -862,6 +868,8 @@ class CMakeInterpreter: return indexed(tgt_var, ref.index) def process_target(tgt: ConverterTarget): + detect_cycle(tgt) + # First handle inter target dependencies link_with = [] objec_libs = [] # type: List[IdNode] @@ -990,6 +998,7 @@ class CMakeInterpreter: # commands in order. This additionally allows setting the working # directory. + detect_cycle(tgt) tgt_var = tgt.name # type: str def resolve_source(x: Any) -> Any: From 82df046bc17435e38907cb605e17a52609c00e36 Mon Sep 17 00:00:00 2001 From: Daniel Mensinger Date: Thu, 28 Nov 2019 11:11:15 +0100 Subject: [PATCH 7/7] cmake: Reassign dependencies from header-only targets --- mesonbuild/cmake/interpreter.py | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/mesonbuild/cmake/interpreter.py b/mesonbuild/cmake/interpreter.py index c6bcbdf47..58794ea0f 100644 --- a/mesonbuild/cmake/interpreter.py +++ b/mesonbuild/cmake/interpreter.py @@ -126,6 +126,8 @@ blacklist_link_libs = [ generated_target_name_prefix = 'cm_' +transfer_dependencies_from = ['header_only'] + # Utility functions to generate local keys def _target_key(tgt_name: str) -> str: return '__tgt_{}__'.format(tgt_name) @@ -362,6 +364,24 @@ class ConverterTarget: # Filter out object files from the sources self.generated = [x for x in self.generated if not isinstance(x, str) or not any([x.endswith('.' + y) for y in obj_suffixes])] + def process_inter_target_dependencies(self): + # Move the dependencies from all transfer_dependencies_from to the target + to_process = list(self.depends) + processed = [] + new_deps = [] + for i in to_process: + processed += [i] + if isinstance(i, ConverterTarget) and i.meson_func() in transfer_dependencies_from: + to_process += [x for x in i.depends if x not in processed] + else: + new_deps += [i] + self.depends = list(set(new_deps)) + + def cleanup_dependencies(self): + # Clear the dependencies from targets that where moved from + if self.meson_func() in transfer_dependencies_from: + self.depends = [] + def meson_func(self) -> str: return target_type_map.get(self.type.upper()) @@ -510,6 +530,19 @@ class ConverterCustomTarget: elif os.path.isabs(i) and os.path.exists(i) and os.path.commonpath([i, root_src_dir]) == root_src_dir: self.inputs += [os.path.relpath(i, root_src_dir)] + def process_inter_target_dependencies(self): + # Move the dependencies from all transfer_dependencies_from to the target + to_process = list(self.depends) + processed = [] + new_deps = [] + for i in to_process: + processed += [i] + if isinstance(i, ConverterTarget) and i.meson_func() in transfer_dependencies_from: + to_process += [x for x in i.depends if x not in processed] + else: + new_deps += [i] + self.depends = list(set(new_deps)) + def get_ref(self, fname: str) -> Optional[CustomTargetReference]: try: if fname in self.conflict_map: @@ -768,6 +801,18 @@ class CMakeInterpreter: for i in self.targets: i.process_object_libs(object_libs) + # Third pass: Reassign dependencies to avoid some loops + for i in self.targets: + i.process_inter_target_dependencies() + for i in self.custom_targets: + i.process_inter_target_dependencies() + i.log() + + # Fourth pass: Remove rassigned dependencies + for i in self.targets: + i.cleanup_dependencies() + i.log() + mlog.log('CMake project', mlog.bold(self.project_name), 'has', mlog.bold(str(len(self.targets) + len(self.custom_targets))), 'build targets.') def pretend_to_be_meson(self) -> CodeBlockNode: