Merge pull request #5989 from xclaesse/cmake-part2

cmake: Add support for add_custom_target() and add_dependencies()
pull/6258/head
Jussi Pakkanen 5 years ago committed by GitHub
commit 575a2c34f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      mesonbuild/cmake/data/run_ctgt.py
  2. 129
      mesonbuild/cmake/interpreter.py
  3. 30
      mesonbuild/cmake/traceparser.py
  4. 14
      test cases/cmake/8 custom command/subprojects/cmMod/CMakeLists.txt
  5. 18
      test cases/cmake/8 custom command/subprojects/cmMod/args_test.cpp
  6. 5
      test cases/cmake/8 custom command/subprojects/cmMod/cmMod.cpp
  7. 14
      test cases/cmake/8 custom command/subprojects/cmMod/macro_name.cpp

@ -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(SEPARATOR))
# 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)
@ -37,7 +40,15 @@ 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'):
os.utime(dummy_target, None)
exit(0)
# Copy outputs
zipped_outputs = zip(args.outputs, args.original_outputs)

@ -93,8 +93,6 @@ target_type_map = {
'INTERFACE_LIBRARY': 'header_only'
}
target_type_requires_trace = ['INTERFACE_LIBRARY']
skip_targets = ['UTILITY']
blacklist_compiler_flags = [
@ -128,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)
@ -152,6 +152,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]
@ -170,6 +172,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 +205,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:
@ -227,16 +229,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 +341,12 @@ 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:
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)]
@ -354,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())
@ -375,6 +403,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)))
@ -401,7 +430,11 @@ 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.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 = {}
@ -411,10 +444,12 @@ class ConverterCustomTarget:
self.inputs = []
self.depends = []
ConverterCustomTarget.tgt_counter += 1
# 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
@ -472,8 +507,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)
@ -488,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:
@ -717,15 +772,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.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:
@ -740,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:
@ -813,6 +886,7 @@ class CMakeInterpreter:
root_cb.lines += [assign(run_script_var, function('find_program', [[run_script]], {'required': True}))]
# Add the targets
processing = []
processed = {}
name_map = {}
@ -826,6 +900,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:
@ -834,6 +913,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]
@ -841,6 +922,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 +933,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 +1008,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}))
@ -952,6 +1043,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:
@ -959,6 +1051,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)
@ -970,7 +1066,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 ';;;'

@ -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: {}'
@ -51,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):
@ -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
@ -235,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
@ -250,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]
@ -295,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
@ -302,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
@ -398,6 +403,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)

@ -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")
@ -44,3 +45,16 @@ 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(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)

@ -0,0 +1,18 @@
#include <iostream>
#include <fstream>
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;
}
ifstream in1("macro_name.txt");
ofstream out1("cmModLib.hpp");
out1 << "#define " << in1.rdbuf() << " = \"plop\"";
return 0;
}

@ -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;

@ -0,0 +1,14 @@
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
using namespace std;
int main() {
this_thread::sleep_for(chrono::seconds(1));
ofstream out1("macro_name.txt");
out1 << "FOO";
return 0;
}
Loading…
Cancel
Save