From 169f8b515ef8919aa95c694ee829ad3f76c7599b Mon Sep 17 00:00:00 2001 From: gerioldman Date: Sun, 8 Oct 2023 18:30:53 +0200 Subject: [PATCH 1/3] Add TASKING to docs --- docs/markdown/Builtin-options.md | 1 + docs/markdown/Reference-tables.md | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/docs/markdown/Builtin-options.md b/docs/markdown/Builtin-options.md index f16a46ffebea..a14f99f29a62 100644 --- a/docs/markdown/Builtin-options.md +++ b/docs/markdown/Builtin-options.md @@ -223,6 +223,7 @@ available on all platforms or with all compilers: | b_staticpic | true | true, false | Build static libraries as position independent | | b_pie | false | true, false | Build position-independent executables (since 0.49.0) | | b_vscrt | from_buildtype | none, md, mdd, mt, mtd, from_buildtype, static_from_buildtype | VS runtime library to use (since 0.48.0) (static_from_buildtype since 0.56.0) | +| b_tasking_mil_link | false | true, false | Use MIL linking for the TASKING VX-tools compiler family (since 1.?.?) | The value of `b_sanitize` can be one of: `none`, `address`, `thread`, `undefined`, `memory`, `leak`, `address,undefined`, but note that some diff --git a/docs/markdown/Reference-tables.md b/docs/markdown/Reference-tables.md index a5d0d5cdcc18..3fd0ab1d82d2 100644 --- a/docs/markdown/Reference-tables.md +++ b/docs/markdown/Reference-tables.md @@ -49,6 +49,11 @@ These are return values of the `get_id` (Compiler family) and | armasm | Microsoft Macro Assembler for ARM and AARCH64 (Since 0.64.0) | | | mwasmarm | Metrowerks Assembler for Embedded ARM | | | mwasmeppc | Metrowerks Assembler for Embedded PowerPC | | +| cctc | TASKING VX-toolset for TriCore compiler | | +| ccarm | TASKING VX-toolset for ARM compiler | | +| cc51 | TASKING VX-toolset for 8051 compiler | | +| ccmsc | TASKING VX-toolset for MCS compiler | | +| ccpcp | TASKING VX-toolset for PCP compiler | | ## Linker ids @@ -80,6 +85,11 @@ These are return values of the `get_linker_id` method in a compiler object. | ccomp | CompCert used as the linker driver | | mwldarm | The Metrowerks Linker with the ARM interface, used with mwccarm only | | mwldeppc | The Metrowerks Linker with the PowerPC interface, used with mwcceppc only | +| ltc | TASKING VX-toolset for TriCore linker | +| lkarm | TASKING VX-toolset for ARM linker | +| lk51 | TASKING VX-toolset for 8051 linker | +| lmsc | TASKING VX-toolset for MCS linker | +| lpcp | TASKING VX-toolset for PCP linker | For languages that don't have separate dynamic linkers such as C# and Java, the `get_linker_id` will return the compiler name. @@ -139,6 +149,7 @@ set in the cross file. | wasm64 | 64 bit Webassembly | | x86 | 32 bit x86 processor | | x86_64 | 64 bit x86 processor | +| tricore | Tricore 32 bit processor | Any cpu family not listed in the above list is not guaranteed to From a4b964efb2ea7f230c5c753f6b8c783326c9362b Mon Sep 17 00:00:00 2001 From: gerioldman Date: Sat, 23 Sep 2023 00:33:39 +0200 Subject: [PATCH 2/3] Add TASKING compiler support --- docs/markdown/Builtin-options.md | 2 +- docs/markdown/Reference-tables.md | 2 +- mesonbuild/backend/ninjabackend.py | 99 +++++++++++++++++-- mesonbuild/build.py | 2 + mesonbuild/compilers/c.py | 25 +++++ mesonbuild/compilers/compilers.py | 8 ++ mesonbuild/compilers/detect.py | 43 +++++++++ mesonbuild/compilers/mixins/tasking.py | 126 +++++++++++++++++++++++++ mesonbuild/envconfig.py | 1 + mesonbuild/linkers/base.py | 1 + mesonbuild/linkers/linkers.py | 96 +++++++++++++++++++ 11 files changed, 397 insertions(+), 8 deletions(-) create mode 100644 mesonbuild/compilers/mixins/tasking.py diff --git a/docs/markdown/Builtin-options.md b/docs/markdown/Builtin-options.md index a14f99f29a62..8d1772a43d20 100644 --- a/docs/markdown/Builtin-options.md +++ b/docs/markdown/Builtin-options.md @@ -223,7 +223,7 @@ available on all platforms or with all compilers: | b_staticpic | true | true, false | Build static libraries as position independent | | b_pie | false | true, false | Build position-independent executables (since 0.49.0) | | b_vscrt | from_buildtype | none, md, mdd, mt, mtd, from_buildtype, static_from_buildtype | VS runtime library to use (since 0.48.0) (static_from_buildtype since 0.56.0) | -| b_tasking_mil_link | false | true, false | Use MIL linking for the TASKING VX-tools compiler family (since 1.?.?) | +| b_tasking_mil_link | false | true, false | Use MIL linking for the TASKING VX-tools compiler family (since 1.4.0) | The value of `b_sanitize` can be one of: `none`, `address`, `thread`, `undefined`, `memory`, `leak`, `address,undefined`, but note that some diff --git a/docs/markdown/Reference-tables.md b/docs/markdown/Reference-tables.md index 3fd0ab1d82d2..06dbeb443577 100644 --- a/docs/markdown/Reference-tables.md +++ b/docs/markdown/Reference-tables.md @@ -52,7 +52,7 @@ These are return values of the `get_id` (Compiler family) and | cctc | TASKING VX-toolset for TriCore compiler | | | ccarm | TASKING VX-toolset for ARM compiler | | | cc51 | TASKING VX-toolset for 8051 compiler | | -| ccmsc | TASKING VX-toolset for MCS compiler | | +| ccmcs | TASKING VX-toolset for MCS compiler | | | ccpcp | TASKING VX-toolset for PCP compiler | | ## Linker ids diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index cb3552d7f0c1..e850eb8e62d1 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -245,7 +245,7 @@ def _quoter(x: NinjaCommandArg, qf: T.Callable[[str], str] = quote_func) -> str: def write(self, outfile: T.TextIO) -> None: rspfile_args = self.args rspfile_quote_func: T.Callable[[str], str] - if self.rspfile_quote_style is RSPFileSyntax.MSVC: + if self.rspfile_quote_style in {RSPFileSyntax.MSVC, RSPFileSyntax.TASKING}: rspfile_quote_func = cmd_quote rspfile_args = [NinjaCommandArg('$in_newline', arg.quoting) if arg.s == '$in' else arg for arg in rspfile_args] else: @@ -260,7 +260,10 @@ def rule_iter() -> T.Iterable[str]: for rsp in rule_iter(): outfile.write(f'rule {self.name}{rsp}\n') if rsp == '_RSP': - outfile.write(' command = {} @$out.rsp\n'.format(' '.join([self._quoter(x) for x in self.command]))) + if self.rspfile_quote_style is RSPFileSyntax.TASKING: + outfile.write(' command = {} --option-file=$out.rsp\n'.format(' '.join([self._quoter(x) for x in self.command]))) + else: + outfile.write(' command = {} @$out.rsp\n'.format(' '.join([self._quoter(x) for x in self.command]))) outfile.write(' rspfile = $out.rsp\n') outfile.write(' rspfile_content = {}\n'.format(' '.join([self._quoter(x, rspfile_quote_func) for x in rspfile_args]))) else: @@ -412,7 +415,7 @@ def write(self, outfile: T.TextIO) -> None: outfile.write(line) if use_rspfile: - if self.rule.rspfile_quote_style is RSPFileSyntax.MSVC: + if self.rule.rspfile_quote_style in {RSPFileSyntax.MSVC, RSPFileSyntax.TASKING}: qf = cmd_quote else: qf = gcc_rsp_quote @@ -729,6 +732,12 @@ def generate_compdb(self) -> None: for ext in ['', '_RSP']] rules += [f"{rule}{ext}" for rule in [self.compiler_to_pch_rule_name(compiler)] for ext in ['', '_RSP']] + # Add custom MIL link rules to get the files compiled by the TASKING compiler family to MIL files included in the database + key = OptionKey('b_tasking_mil_link') + if key in compiler.base_options: + rule = self.get_compiler_rule_name('tasking_mil_compile', compiler.for_machine) + rules.append(rule) + rules.append(f'{rule}_RSP') compdb_options = ['-x'] if mesonlib.version_compare(self.ninja_version, '>=1.9') else [] ninja_compdb = self.ninja_command + ['-t', 'compdb'] + compdb_options + rules builddir = self.environment.get_build_dir() @@ -1074,8 +1083,19 @@ def generate_target(self, target) -> None: # Skip the link stage for this special type of target return linker, stdlib_args = self.determine_linker_and_stdlib_args(target) - if isinstance(target, build.StaticLibrary) and target.prelink: + # For prelinking and TASKING mil linking there needs to be an additional link target and the object list is modified + if not isinstance(target, build.StaticLibrary): + final_obj_list = obj_list + elif target.prelink: final_obj_list = self.generate_prelink(target, obj_list) + elif 'c' in target.compilers: + key = OptionKey('b_tasking_mil_link') + if key not in target.get_options() or key not in target.compilers['c'].base_options: + final_obj_list = obj_list + elif target.get_option(key): + final_obj_list = self.generate_mil_link(target, obj_list) + else: + final_obj_list = obj_list else: final_obj_list = obj_list elem = self.generate_link(target, outname, final_obj_list, linker, pch_objects, stdlib_args=stdlib_args) @@ -2479,6 +2499,33 @@ def generate_llvm_ir_compile_rule(self, compiler) -> None: self.add_rule(NinjaRule(rule, command, args, description, **options)) self.created_llvm_ir_rule[compiler.for_machine] = True + def generate_tasking_mil_compile_rules(self, compiler: Compiler) -> None: + rule = self.get_compiler_rule_name('tasking_mil_compile', compiler.for_machine) + depargs = NinjaCommandArg.list(compiler.get_dependency_gen_args('$out', '$DEPFILE'), Quoting.none) + command = compiler.get_exelist() + args = ['$ARGS'] + depargs + NinjaCommandArg.list(compiler.get_output_args('$out'), Quoting.none) + ['-cm', '$in'] + description = 'Compiling to C object $in' + if compiler.get_argument_syntax() == 'msvc': + deps = 'msvc' + depfile = None + else: + deps = 'gcc' + depfile = '$DEPFILE' + + options = self._rsp_options(compiler) + + self.add_rule(NinjaRule(rule, command, args, description, **options, deps=deps, depfile=depfile)) + + def generate_tasking_mil_link_rules(self, compiler: Compiler) -> None: + rule = self.get_compiler_rule_name('tasking_mil_link', compiler.for_machine) + command = compiler.get_exelist() + args = ['$ARGS', '--mil-link'] + NinjaCommandArg.list(compiler.get_output_args('$out'), Quoting.none) + ['-c', '$in'] + description = 'MIL linking object $out' + + options = self._rsp_options(compiler) + + self.add_rule(NinjaRule(rule, command, args, description, **options)) + def generate_compile_rule_for(self, langname: str, compiler: Compiler) -> None: if langname == 'java': self.generate_java_compile_rule(compiler) @@ -2569,6 +2616,9 @@ def generate_compile_rules(self) -> None: for langname, compiler in clist.items(): if compiler.get_id() == 'clang': self.generate_llvm_ir_compile_rule(compiler) + if OptionKey('b_tasking_mil_link') in compiler.base_options: + self.generate_tasking_mil_compile_rules(compiler) + self.generate_tasking_mil_link_rules(compiler) self.generate_compile_rule_for(langname, compiler) self.generate_pch_rule_for(langname, compiler) for mode in compiler.get_modes(): @@ -3009,6 +3059,11 @@ def generate_single_compile(self, target: build.BuildTarget, src, else: raise InvalidArguments(f'Invalid source type: {src!r}') obj_basename = self.object_filename_from_source(target, src) + # If mil linking is enabled for the target, then compilation output has to be MIL files instead of object files + if compiler.get_language() == 'c': + key = OptionKey('b_tasking_mil_link') + if key in compiler.base_options and target.get_option(key) and src.rsplit('.', 1)[1] in compilers.lang_suffixes['c']: + obj_basename = f'{os.path.splitext(obj_basename)[0]}.mil' rel_obj = os.path.join(self.get_target_private_dir(target), obj_basename) dep_file = compiler.depfile_for_object(rel_obj) @@ -3029,8 +3084,14 @@ def generate_single_compile(self, target: build.BuildTarget, src, i = os.path.join(self.get_target_private_dir(target), compiler.get_pch_name(pchlist[0])) arr.append(i) pch_dep = arr - - compiler_name = self.compiler_to_rule_name(compiler) + # If TASKING compiler family is used and MIL linking is enabled for the target, + # then compilation rule name is a special one to output MIL files + # instead of object files for .c files + key = OptionKey('b_tasking_mil_link') + if key in compiler.base_options and target.get_option(key) and src.rsplit('.', 1)[1] in compilers.lang_suffixes['c']: + compiler_name = self.get_compiler_rule_name('tasking_mil_compile', compiler.for_machine) + else: + compiler_name = self.compiler_to_rule_name(compiler) extra_deps = [] if compiler.get_language() == 'fortran': # Can't read source file to scan for deps if it's generated later @@ -3415,6 +3476,29 @@ def generate_prelink(self, target, obj_list): self.add_build(elem) return [prelink_name] + def generate_mil_link(self, target: build.StaticLibrary, obj_list: T.List[str]) -> T.List[str]: + assert isinstance(target, build.StaticLibrary) + + mil_linked_name = os.path.join(self.get_target_private_dir(target), target.name + '-mil_link.o') + mil_link_list = [] + obj_file_list = [] + for obj in obj_list: + if obj.endswith('.mil'): + mil_link_list.append(obj) + else: + obj_file_list.append(obj) + obj_file_list.append(mil_linked_name) + compiler = get_compiler_for_source(target.compilers.values(), mil_link_list[0][:-3] + '.c') + commands = self._generate_single_compile_base_args(target, compiler) + commands += self._generate_single_compile_target_args(target, compiler) + commands = commands.compiler.compiler_args(commands) + + elem = NinjaBuildElement(self.all_outputs, [mil_linked_name], self.get_compiler_rule_name('tasking_mil_link', compiler.for_machine), mil_link_list) + elem.add_item('ARGS', commands) + self.add_build(elem) + + return obj_file_list + def generate_link(self, target: build.BuildTarget, outname, obj_list, linker: T.Union['Compiler', 'StaticLinker'], extra_args=None, stdlib_args=None): extra_args = extra_args if extra_args is not None else [] stdlib_args = stdlib_args if stdlib_args is not None else [] @@ -3446,6 +3530,9 @@ def generate_link(self, target: build.BuildTarget, outname, obj_list, linker: T. linker, isinstance(target, build.SharedModule), self.environment.get_build_dir()) + # Add --mil-link if the option is enabled + if isinstance(target, build.Executable) and 'c' in target.compilers and OptionKey('b_tasking_mil_link') in target.get_options(): + commands += target.compilers['c'].get_tasking_mil_link_args(target.get_option(OptionKey('b_tasking_mil_link'))) # Add -nostdlib if needed; can't be overridden commands += self.get_no_stdlib_link_args(target, linker) # Add things like /NOLOGO; usually can't be overridden diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 460ed549be92..a2e89396a70c 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -2008,6 +2008,8 @@ def post_init(self) -> None: elif ('c' in self.compilers and self.compilers['c'].get_id() in {'mwccarm', 'mwcceppc'} or 'cpp' in self.compilers and self.compilers['cpp'].get_id() in {'mwccarm', 'mwcceppc'}): self.suffix = 'nef' + elif ('c' in self.compilers and self.compilers['c'].get_id() in {'cctc', 'ccarm', 'cc51', 'ccmcs', 'ccpcp'}): + self.suffix = 'elf' else: self.suffix = machine.get_exe_suffix() self.filename = self.name diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index 5868211a6f15..b72c5b5d2eb5 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -27,6 +27,7 @@ from .mixins.emscripten import EmscriptenMixin from .mixins.metrowerks import MetrowerksCompiler from .mixins.metrowerks import mwccarm_instruction_set_args, mwcceppc_instruction_set_args +from .mixins.tasking import TaskingCompiler from .compilers import ( gnu_winlibs, msvc_winlibs, @@ -830,3 +831,27 @@ def get_option_compile_args(self, options: 'KeyedOptionDictType') -> T.List[str] if std != 'none': args.append('-lang ' + std) return args + +class _TaskingCCompiler(TaskingCompiler, CCompiler): + def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, + is_cross: bool, info: 'MachineInfo', + linker: T.Optional['DynamicLinker'] = None, + full_version: T.Optional[str] = None): + CCompiler.__init__(self, ccache, exelist, version, for_machine, is_cross, + info, linker=linker, full_version=full_version) + TaskingCompiler.__init__(self) + +class TaskingTricoreCCompiler(_TaskingCCompiler): + id = 'cctc' + +class TaskingArmCCompiler(_TaskingCCompiler): + id = 'ccarm' + +class Tasking8051CCompiler(_TaskingCCompiler): + id = 'cc51' + +class TaskingMCSCCompiler(_TaskingCCompiler): + id = 'ccmcs' + +class TaskingPCPCCompiler(_TaskingCCompiler): + id = 'ccpcp' diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 603a3eb484de..58032b487fff 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -247,6 +247,7 @@ def init_option(self, name: OptionKey) -> options._U: OptionKey('b_bitcode'): BaseOption(options.UserBooleanOption, 'Generate and embed bitcode (only macOS/iOS/tvOS)', False), OptionKey('b_vscrt'): BaseOption(options.UserComboOption, 'VS run-time library type to use.', 'from_buildtype', choices=MSCRT_VALS + ['from_buildtype', 'static_from_buildtype']), + OptionKey('b_tasking_mil_link'): BaseOption(options.UserBooleanOption, 'Use TASKING compiler families MIL linking feature', False), } base_options = {key: base_opt.init_option(key) for key, base_opt in BASE_OPTIONS.items()} @@ -1349,6 +1350,13 @@ def get_preprocessor(self) -> Compiler: def form_compileropt_key(self, basename: str) -> OptionKey: return OptionKey(f'{self.language}_{basename}', machine=self.for_machine) + def get_tasking_mil_link_args(self, option_enabled: bool) -> T.List[str]: + """ + Argument for enabling TASKING's MIL link feature, + for most compilers, this will return nothing. + """ + return [] + def get_global_options(lang: str, comp: T.Type[Compiler], for_machine: MachineChoice, diff --git a/mesonbuild/compilers/detect.py b/mesonbuild/compilers/detect.py index 7542fb6283a2..365f7f546a4a 100644 --- a/mesonbuild/compilers/detect.py +++ b/mesonbuild/compilers/detect.py @@ -240,6 +240,17 @@ def detect_static_linker(env: 'Environment', compiler: Compiler) -> StaticLinker return linkers.MetrowerksStaticLinkerARM(linker) else: return linkers.MetrowerksStaticLinkerEmbeddedPowerPC(linker) + if 'TASKING VX-toolset' in err: + if 'TriCore' in err: + return linkers.TaskingTricoreStaticLinker(linker) + if 'ARM' in err: + return linkers.TaskingARMStaticLinker(linker) + if '8051' in err: + return linkers.Tasking8051StaticLinker(linker) + if 'PCP' in err: + return linkers.TaskingPCPStaticLinker(linker) + else: + return linkers.TaskingMCSStaticLinker(linker) if p.returncode == 0: return linkers.ArLinker(compiler.for_machine, linker) if p.returncode == 1 and err.startswith('usage'): # OSX @@ -605,6 +616,38 @@ def sanitize(p: T.Optional[str]) -> T.Optional[str]: return cls( ccache, compiler, compiler_version, for_machine, is_cross, info, full_version=full_version, linker=linker) + if 'TASKING VX-toolset' in err: + if 'TriCore' in err or 'AURIX Development Studio' in err: + cls = c.TaskingTricoreCCompiler + lnk = linkers.TaskingTricoreLinker + elif 'ARM' in err: + cls = c.TaskingArmCCompiler + lnk = linkers.TaskingARMLinker + elif '8051' in err: + cls = c.Tasking8051CCompiler + lnk = linkers.Tasking8051Linker + elif 'PCP' in err: + cls = c.TaskingPCPCCompiler + lnk = linkers.TaskingPCPLinker + elif 'MCS' in err: + cls = c.TaskingMCSCCompiler + lnk = linkers.TaskingMCSLinker + else: + raise EnvironmentException('Failed to detect linker for TASKING VX-toolset compiler. Please update your cross file(s).') + + tasking_ver_match = re.search(r'v([0-9]+)\.([0-9]+)r([0-9]+) Build ([0-9]+)', err) + assert tasking_ver_match is not None, 'for mypy' + tasking_version = '.'.join(x for x in tasking_ver_match.groups() if x is not None) + + env.coredata.add_lang_args(cls.language, cls, for_machine, env) + ld = env.lookup_binary_entry(for_machine, cls.language + '_ld') + if ld is None: + raise MesonException(f'{cls.language}_ld was not properly defined in your cross file') + + linker = lnk(ld, for_machine, version=tasking_version) + return cls( + ccache, compiler, tasking_version, for_machine, is_cross, info, + full_version=full_version, linker=linker) _handle_exceptions(popen_exceptions, compilers) raise EnvironmentException(f'Unknown compiler {compilers}') diff --git a/mesonbuild/compilers/mixins/tasking.py b/mesonbuild/compilers/mixins/tasking.py new file mode 100644 index 000000000000..7675dbc75329 --- /dev/null +++ b/mesonbuild/compilers/mixins/tasking.py @@ -0,0 +1,126 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright 2012-2023 The Meson development team +from __future__ import annotations + +"""Representations specific to the TASKING embedded C/C++ compiler family.""" + +import os +import typing as T + +from ...mesonlib import EnvironmentException +from ...options import OptionKey + +if T.TYPE_CHECKING: + from ...compilers.compilers import Compiler +else: + # This is a bit clever, for mypy we pretend that these mixins descend from + # Compiler, so we get all of the methods and attributes defined for us, but + # for runtime we make them descend from object (which all classes normally + # do). This gives us DRYer type checking, with no runtime impact + Compiler = object + +tasking_buildtype_args: T.Mapping[str, T.List[str]] = { + 'plain': [], + 'debug': [], + 'debugoptimized': [], + 'release': [], + 'minsize': [], + 'custom': [] +} + +tasking_optimization_args: T.Mapping[str, T.List[str]] = { + 'plain': [], + '0': ['-O0'], + 'g': ['-O1'], # There is no debug specific level, O1 is recommended by the compiler + '1': ['-O1'], + '2': ['-O2'], + '3': ['-O3'], + 's': ['-Os'] +} + +tasking_debug_args: T.Mapping[bool, T.List[str]] = { + False: [], + True: ['-g3'] +} + +class TaskingCompiler(Compiler): + ''' + Functionality that is common to all TASKING family compilers. + ''' + + LINKER_PREFIX = '-Wl' + + def __init__(self) -> None: + if not self.is_cross: + raise EnvironmentException(f'{id} supports only cross-compilation.') + + self.base_options = { + OptionKey(o) for o in [ + 'b_tasking_mil_link', + 'b_staticpic', + 'b_ndebug' + ] + } + + default_warn_args = [] # type: T.List[str] + self.warn_args = {'0': [], + '1': default_warn_args, + '2': default_warn_args + [], + '3': default_warn_args + [], + 'everything': default_warn_args + []} # type: T.Dict[str, T.List[str]] + # TODO: add additional compilable files so that meson can detect it + self.can_compile_suffixes.add('asm') + + def get_pic_args(self) -> T.List[str]: + return ['--pic'] + + def get_buildtype_args(self, buildtype: str) -> T.List[str]: + return tasking_buildtype_args[buildtype] + + def get_debug_args(self, is_debug: bool) -> T.List[str]: + return tasking_debug_args[is_debug] + + def get_compile_only_args(self) -> T.List[str]: + return ['-c'] + + def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: + return [f'--dep-file={outfile}'] + + def get_depfile_suffix(self) -> str: + return 'dep' + + def get_no_stdinc_args(self) -> T.List[str]: + return ['--no-stdinc'] + + def get_werror_args(self) -> T.List[str]: + return ['--warnings-as-errors'] + + def get_no_stdlib_link_args(self) -> T.List[str]: + return ['--no-default-libraries'] + + def get_output_args(self, outputname: str) -> T.List[str]: + return ['-o', outputname] + + def get_include_args(self, path: str, is_system: bool) -> T.List[str]: + if path == '': + path = '.' + return ['-I' + path] + + def get_optimization_args(self, optimization_level: str) -> T.List[str]: + return tasking_optimization_args[optimization_level] + + def get_no_optimization_args(self) -> T.List[str]: + return ['-O0'] + + def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]: + for idx, i in enumerate(parameter_list): + if i[:2] == '-I' or i[:2] == '-L': + parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:])) + + return parameter_list + + def get_tasking_mil_link_args(self, option_enabled: bool) -> T.List[str]: + return ['--mil-link'] if option_enabled else [] + + def get_preprocess_only_args(self) -> T.List[str]: + return ['-E'] diff --git a/mesonbuild/envconfig.py b/mesonbuild/envconfig.py index 86bad9be23ee..1ecdf2a71c6e 100644 --- a/mesonbuild/envconfig.py +++ b/mesonbuild/envconfig.py @@ -64,6 +64,7 @@ 'wasm64', 'x86', 'x86_64', + 'tricore' ) # It would feel more natural to call this "64_BIT_CPU_FAMILIES", but diff --git a/mesonbuild/linkers/base.py b/mesonbuild/linkers/base.py index c8efc9d6d82c..68fdb2ea3a70 100644 --- a/mesonbuild/linkers/base.py +++ b/mesonbuild/linkers/base.py @@ -18,6 +18,7 @@ class RSPFileSyntax(enum.Enum): MSVC = enum.auto() GCC = enum.auto() + TASKING = enum.auto() class ArLikeLinker: diff --git a/mesonbuild/linkers/linkers.py b/mesonbuild/linkers/linkers.py index d0ffc56182ee..ba7b9228d718 100644 --- a/mesonbuild/linkers/linkers.py +++ b/mesonbuild/linkers/linkers.py @@ -526,6 +526,38 @@ class MetrowerksStaticLinkerARM(MetrowerksStaticLinker): class MetrowerksStaticLinkerEmbeddedPowerPC(MetrowerksStaticLinker): id = 'mwldeppc' +class TaskingStaticLinker(StaticLinker): + + def __init__(self, exelist: T.List[str]): + super().__init__(exelist) + + def can_linker_accept_rsp(self) -> bool: + return True + + def rsp_file_syntax(self) -> RSPFileSyntax: + return RSPFileSyntax.TASKING + + def get_output_args(self, target: str) -> T.List[str]: + return ['-n', target] + + def get_linker_always_args(self) -> T.List[str]: + return ['-r'] + +class TaskingTricoreStaticLinker(TaskingStaticLinker): + id = 'artc' + +class TaskingARMStaticLinker(TaskingStaticLinker): + id = 'ararm' + +class Tasking8051StaticLinker(TaskingStaticLinker): + id = 'ar51' + +class TaskingMCSStaticLinker(TaskingStaticLinker): + id = 'armcs' + +class TaskingPCPStaticLinker(TaskingStaticLinker): + id = 'arpcp' + def prepare_rpaths(raw_rpaths: T.Tuple[str, ...], build_dir: str, from_dir: str) -> T.List[str]: # The rpaths we write must be relative if they point to the build dir, # because otherwise they have different length depending on the build @@ -1663,3 +1695,67 @@ class MetrowerksLinkerARM(MetrowerksLinker): class MetrowerksLinkerEmbeddedPowerPC(MetrowerksLinker): id = 'mwldeppc' + +class TaskingLinker(DynamicLinker): + + _OPTIMIZATION_ARGS: T.Dict[str, T.List[str]] = { + 'plain': [], + '0': ['-O0'], + 'g': ['-O1'], # There is no debug specific level, O1 is recommended by the compiler + '1': ['-O1'], + '2': ['-O2'], + '3': ['-O2'], # There is no 3rd level optimization for the linker + 's': ['-Os'], + } + + def __init__(self, exelist: T.List[str], for_machine: mesonlib.MachineChoice, + *, version: str = 'unknown version'): + super().__init__(exelist, for_machine, '', [], + version=version) + + def get_accepts_rsp(self) -> bool: + return True + + def get_lib_prefix(self) -> str: + return "" + + def get_allow_undefined_args(self) -> T.List[str]: + return [] + + def invoked_by_compiler(self) -> bool: + return True + + def get_search_args(self, dirname: str) -> T.List[str]: + return self._apply_prefix('-L' + dirname) + + def get_output_args(self, outputname: str) -> T.List[str]: + return ['-o', outputname] + + def rsp_file_syntax(self) -> RSPFileSyntax: + return RSPFileSyntax.TASKING + + def fatal_warnings(self) -> T.List[str]: + """Arguments to make all warnings errors.""" + return self._apply_prefix('--warnings-as-errors') + + def get_link_whole_for(self, args: T.List[str]) -> T.List[str]: + args = mesonlib.listify(args) + l: T.List[str] = [] + for a in args: + l.extend(self._apply_prefix('-Wl--whole-archive=' + a)) + return l + +class TaskingTricoreLinker(TaskingLinker): + id = 'ltc' + +class TaskingARMLinker(TaskingLinker): + id = 'lkarm' + +class Tasking8051Linker(TaskingLinker): + id = 'lk51' + +class TaskingMCSLinker(TaskingLinker): + id = 'lmsc' + +class TaskingPCPLinker(TaskingLinker): + id = 'lpcp' From e965caa9d0f67b333da5a8d03513452df5c3324a Mon Sep 17 00:00:00 2001 From: gerioldman Date: Wed, 28 Aug 2024 02:57:56 +0200 Subject: [PATCH 3/3] First draft version of Tasking MIL linking with b_lto and prelinking --- docs/markdown/Builtin-options.md | 1 - mesonbuild/backend/backends.py | 11 +++-- mesonbuild/backend/ninjabackend.py | 62 +++++++------------------- mesonbuild/backend/vs2010backend.py | 4 +- mesonbuild/build.py | 5 ++- mesonbuild/compilers/compilers.py | 17 ++++--- mesonbuild/compilers/mixins/apple.py | 5 ++- mesonbuild/compilers/mixins/elbrus.py | 5 ++- mesonbuild/compilers/mixins/gnu.py | 5 ++- mesonbuild/compilers/mixins/tasking.py | 37 ++++++++++++--- mesonbuild/linkers/linkers.py | 3 ++ 11 files changed, 83 insertions(+), 72 deletions(-) diff --git a/docs/markdown/Builtin-options.md b/docs/markdown/Builtin-options.md index 8d1772a43d20..f16a46ffebea 100644 --- a/docs/markdown/Builtin-options.md +++ b/docs/markdown/Builtin-options.md @@ -223,7 +223,6 @@ available on all platforms or with all compilers: | b_staticpic | true | true, false | Build static libraries as position independent | | b_pie | false | true, false | Build position-independent executables (since 0.49.0) | | b_vscrt | from_buildtype | none, md, mdd, mt, mtd, from_buildtype, static_from_buildtype | VS runtime library to use (since 0.48.0) (static_from_buildtype since 0.56.0) | -| b_tasking_mil_link | false | true, false | Use MIL linking for the TASKING VX-tools compiler family (since 1.4.0) | The value of `b_sanitize` can be one of: `none`, `address`, `thread`, `undefined`, `memory`, `leak`, `address,undefined`, but note that some diff --git a/mesonbuild/backend/backends.py b/mesonbuild/backend/backends.py index a4be50f664b1..864eec011358 100644 --- a/mesonbuild/backend/backends.py +++ b/mesonbuild/backend/backends.py @@ -25,6 +25,7 @@ from .. import mesonlib from .. import mlog from ..compilers import LANGUAGES_USING_LDFLAGS, detect +from ..utils.universal import get_compiler_for_source from ..mesonlib import ( File, MachineChoice, MesonException, OrderedSet, ExecutableSerialisation, classify_unity_sources, @@ -836,7 +837,7 @@ def canonicalize_filename(fname: str) -> str: fname = fname.replace(ch, '_') return hashed + fname - def object_filename_from_source(self, target: build.BuildTarget, source: 'FileOrString', targetdir: T.Optional[str] = None) -> str: + def object_filename_from_source(self, target: build.BuildTarget, compiler: Compiler, source: 'FileOrString', targetdir: T.Optional[str] = None) -> str: assert isinstance(source, mesonlib.File) if isinstance(target, build.CompileTarget): return target.sources_map[source] @@ -867,7 +868,10 @@ def object_filename_from_source(self, target: build.BuildTarget, source: 'FileOr gen_source = os.path.relpath(os.path.join(build_dir, rel_src), os.path.join(self.environment.get_source_dir(), target.get_subdir())) machine = self.environment.machines[target.for_machine] - ret = self.canonicalize_filename(gen_source) + '.' + machine.get_object_suffix() + object_suffix = machine.get_object_suffix() + object_suffix_override = compiler.get_object_suffix_override(target, source) + object_suffix = object_suffix if object_suffix_override is None else object_suffix_override + ret = self.canonicalize_filename(gen_source) + '.' + object_suffix if targetdir is not None: return os.path.join(targetdir, ret) return ret @@ -924,7 +928,8 @@ def _determine_ext_objs(self, extobj: 'build.ExtractedObjects', proj_dir_to_buil sources.append(_src) for osrc in sources: - objname = self.object_filename_from_source(extobj.target, osrc, targetdir) + compiler = get_compiler_for_source(extobj.target.compilers.values(), osrc) + objname = self.object_filename_from_source(extobj.target, compiler, osrc, targetdir) objpath = os.path.join(proj_dir_to_build_root, objname) result.append(objpath) diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index e850eb8e62d1..f9ad0e73b1fe 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -1083,19 +1083,11 @@ def generate_target(self, target) -> None: # Skip the link stage for this special type of target return linker, stdlib_args = self.determine_linker_and_stdlib_args(target) - # For prelinking and TASKING mil linking there needs to be an additional link target and the object list is modified + if not isinstance(target, build.StaticLibrary): final_obj_list = obj_list elif target.prelink: final_obj_list = self.generate_prelink(target, obj_list) - elif 'c' in target.compilers: - key = OptionKey('b_tasking_mil_link') - if key not in target.get_options() or key not in target.compilers['c'].base_options: - final_obj_list = obj_list - elif target.get_option(key): - final_obj_list = self.generate_mil_link(target, obj_list) - else: - final_obj_list = obj_list else: final_obj_list = obj_list elem = self.generate_link(target, outname, final_obj_list, linker, pch_objects, stdlib_args=stdlib_args) @@ -2616,9 +2608,8 @@ def generate_compile_rules(self) -> None: for langname, compiler in clist.items(): if compiler.get_id() == 'clang': self.generate_llvm_ir_compile_rule(compiler) - if OptionKey('b_tasking_mil_link') in compiler.base_options: + if compiler.get_id() in {'cctc', 'ccarm', 'cc51', 'ccmcs', 'ccpcp'}: self.generate_tasking_mil_compile_rules(compiler) - self.generate_tasking_mil_link_rules(compiler) self.generate_compile_rule_for(langname, compiler) self.generate_pch_rule_for(langname, compiler) for mode in compiler.get_modes(): @@ -3058,12 +3049,7 @@ def generate_single_compile(self, target: build.BuildTarget, src, raise AssertionError(f'BUG: broken generated source file handling for {src!r}') else: raise InvalidArguments(f'Invalid source type: {src!r}') - obj_basename = self.object_filename_from_source(target, src) - # If mil linking is enabled for the target, then compilation output has to be MIL files instead of object files - if compiler.get_language() == 'c': - key = OptionKey('b_tasking_mil_link') - if key in compiler.base_options and target.get_option(key) and src.rsplit('.', 1)[1] in compilers.lang_suffixes['c']: - obj_basename = f'{os.path.splitext(obj_basename)[0]}.mil' + obj_basename = self.object_filename_from_source(target, compiler, src) rel_obj = os.path.join(self.get_target_private_dir(target), obj_basename) dep_file = compiler.depfile_for_object(rel_obj) @@ -3087,9 +3073,12 @@ def generate_single_compile(self, target: build.BuildTarget, src, # If TASKING compiler family is used and MIL linking is enabled for the target, # then compilation rule name is a special one to output MIL files # instead of object files for .c files - key = OptionKey('b_tasking_mil_link') - if key in compiler.base_options and target.get_option(key) and src.rsplit('.', 1)[1] in compilers.lang_suffixes['c']: - compiler_name = self.get_compiler_rule_name('tasking_mil_compile', compiler.for_machine) + key = OptionKey('b_lto') + if compiler.get_id() in {'cctc', 'ccarm', 'cc51', 'ccmcs', 'ccpcp'}: + if ((isinstance(target, build.StaticLibrary) and target.prelink) or target.get_option(key)) and src.rsplit('.', 1)[1] in compilers.lang_suffixes['c']: + compiler_name = self.get_compiler_rule_name('tasking_mil_compile', compiler.for_machine) + else: + compiler_name = self.compiler_to_rule_name(compiler) else: compiler_name = self.compiler_to_rule_name(compiler) extra_deps = [] @@ -3468,36 +3457,19 @@ def generate_prelink(self, target, obj_list): prelinker = target.get_prelinker() cmd = prelinker.exelist[:] - cmd += prelinker.get_prelink_args(prelink_name, obj_list) + obj_list, args = prelinker.get_prelink_args(target, prelink_name, obj_list) + cmd += args + if prelinker.get_prelink_append_compile_args(): + compile_args = self._generate_single_compile_base_args(target, prelinker) + compile_args += self._generate_single_compile_target_args(target, prelinker) + compile_args = compile_args.compiler.compiler_args(compile_args) + cmd += compile_args.to_native() cmd = self.replace_paths(target, cmd) elem.add_item('COMMAND', cmd) elem.add_item('description', f'Prelinking {prelink_name}') self.add_build(elem) - return [prelink_name] - - def generate_mil_link(self, target: build.StaticLibrary, obj_list: T.List[str]) -> T.List[str]: - assert isinstance(target, build.StaticLibrary) - - mil_linked_name = os.path.join(self.get_target_private_dir(target), target.name + '-mil_link.o') - mil_link_list = [] - obj_file_list = [] - for obj in obj_list: - if obj.endswith('.mil'): - mil_link_list.append(obj) - else: - obj_file_list.append(obj) - obj_file_list.append(mil_linked_name) - compiler = get_compiler_for_source(target.compilers.values(), mil_link_list[0][:-3] + '.c') - commands = self._generate_single_compile_base_args(target, compiler) - commands += self._generate_single_compile_target_args(target, compiler) - commands = commands.compiler.compiler_args(commands) - - elem = NinjaBuildElement(self.all_outputs, [mil_linked_name], self.get_compiler_rule_name('tasking_mil_link', compiler.for_machine), mil_link_list) - elem.add_item('ARGS', commands) - self.add_build(elem) - - return obj_file_list + return obj_list def generate_link(self, target: build.BuildTarget, outname, obj_list, linker: T.Union['Compiler', 'StaticLinker'], extra_args=None, stdlib_args=None): extra_args = extra_args if extra_args is not None else [] diff --git a/mesonbuild/backend/vs2010backend.py b/mesonbuild/backend/vs2010backend.py index 08a19c659e44..0fb30a7b91b9 100644 --- a/mesonbuild/backend/vs2010backend.py +++ b/mesonbuild/backend/vs2010backend.py @@ -1725,7 +1725,7 @@ def path_normalize_add(path, lis): self.add_preprocessor_defines(lang, inc_cl, file_defines) self.add_include_dirs(lang, inc_cl, file_inc_dirs) ET.SubElement(inc_cl, 'ObjectFileName').text = "$(IntDir)" + \ - self.object_filename_from_source(target, s) + self.object_filename_from_source(target, compiler, s) for s in gen_src: if path_normalize_add(s, previous_sources): inc_cl = ET.SubElement(inc_src, 'CLCompile', Include=s) @@ -1739,7 +1739,7 @@ def path_normalize_add(path, lis): self.add_include_dirs(lang, inc_cl, file_inc_dirs) s = File.from_built_file(target.get_subdir(), s) ET.SubElement(inc_cl, 'ObjectFileName').text = "$(IntDir)" + \ - self.object_filename_from_source(target, s) + self.object_filename_from_source(target, compiler, s) for lang, headers in pch_sources.items(): impl = headers[1] if impl and path_normalize_add(impl, previous_sources): diff --git a/mesonbuild/build.py b/mesonbuild/build.py index a2e89396a70c..b7b888b2e667 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -2165,7 +2165,10 @@ def post_init(self) -> None: elif self.rust_crate_type == 'staticlib': self.suffix = 'a' else: - self.suffix = 'a' + if 'c' in self.compilers and self.compilers['c'].get_id() in {'cctc', 'ccarm', 'cc51', 'ccmcs', 'ccpcp'}: + self.suffix = 'ma' if self.options.get_value('b_lto') and not self.prelink else 'a' + else: + self.suffix = 'a' self.filename = self.prefix + self.name + '.' + self.suffix self.outputs[0] = self.filename diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 58032b487fff..7c9d99228acd 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -247,7 +247,6 @@ def init_option(self, name: OptionKey) -> options._U: OptionKey('b_bitcode'): BaseOption(options.UserBooleanOption, 'Generate and embed bitcode (only macOS/iOS/tvOS)', False), OptionKey('b_vscrt'): BaseOption(options.UserComboOption, 'VS run-time library type to use.', 'from_buildtype', choices=MSCRT_VALS + ['from_buildtype', 'static_from_buildtype']), - OptionKey('b_tasking_mil_link'): BaseOption(options.UserBooleanOption, 'Use TASKING compiler families MIL linking feature', False), } base_options = {key: base_opt.init_option(key) for key, base_opt in BASE_OPTIONS.items()} @@ -429,8 +428,8 @@ class CompileResult(HoldableObject): output_name: T.Optional[str] = field(default=None, init=False) cached: bool = field(default=False, init=False) - class Compiler(HoldableObject, metaclass=abc.ABCMeta): + # Libraries to ignore in find_library() since they are provided by the # compiler or the C library. Currently only used for MSVC. ignore_libs: T.List[str] = [] @@ -1324,9 +1323,13 @@ def get_feature_args(self, kwargs: DFeatures, build_to_src: str) -> T.List[str]: # TODO: using a TypeDict here would improve this raise EnvironmentException(f'{self.id} does not implement get_feature_args') - def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.List[str]: + def get_prelink_args(self, target: BuildTarget, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: raise EnvironmentException(f'{self.id} does not know how to do prelinking.') + def get_prelink_append_compile_args(self) -> bool: + """Controls whether compile args have to be used for prelinking or not""" + return False + def rsp_file_syntax(self) -> 'RSPFileSyntax': """The format of the RSP file that this compiler supports. @@ -1350,12 +1353,8 @@ def get_preprocessor(self) -> Compiler: def form_compileropt_key(self, basename: str) -> OptionKey: return OptionKey(f'{self.language}_{basename}', machine=self.for_machine) - def get_tasking_mil_link_args(self, option_enabled: bool) -> T.List[str]: - """ - Argument for enabling TASKING's MIL link feature, - for most compilers, this will return nothing. - """ - return [] + def get_object_suffix_override(self, target: BuildTarget, source: mesonlib.File) -> T.Optional[str]: + return None def get_global_options(lang: str, comp: T.Type[Compiler], diff --git a/mesonbuild/compilers/mixins/apple.py b/mesonbuild/compilers/mixins/apple.py index fc93d38a5673..ca1b50a37475 100644 --- a/mesonbuild/compilers/mixins/apple.py +++ b/mesonbuild/compilers/mixins/apple.py @@ -9,6 +9,7 @@ from ...mesonlib import MesonException if T.TYPE_CHECKING: + from ...build import BuildTarget from ..._typing import ImmutableListProtocol from ...environment import Environment from ..compilers import Compiler @@ -56,6 +57,6 @@ def openmp_link_flags(self, env: Environment) -> T.List[str]: raise MesonException("Couldn't find libomp") return self.__BASE_OMP_FLAGS + link - def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.List[str]: + def get_prelink_args(self, target: BuildTarget, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: # The objects are prelinked through the compiler, which injects -lSystem - return ['-nostdlib', '-r', '-o', prelink_name] + obj_list + return [prelink_name], ['-nostdlib', '-r', '-o', prelink_name] + obj_list diff --git a/mesonbuild/compilers/mixins/elbrus.py b/mesonbuild/compilers/mixins/elbrus.py index 66f419cf02d8..464756e1b57c 100644 --- a/mesonbuild/compilers/mixins/elbrus.py +++ b/mesonbuild/compilers/mixins/elbrus.py @@ -15,6 +15,7 @@ from .gnu import gnu_optimization_args from ...mesonlib import Popen_safe from ...options import OptionKey +from ...build import BuildTarget if T.TYPE_CHECKING: from ...environment import Environment @@ -76,8 +77,8 @@ def get_default_include_dirs(self) -> T.List[str]: def get_optimization_args(self, optimization_level: str) -> T.List[str]: return gnu_optimization_args[optimization_level] - def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.List[str]: - return ['-r', '-nodefaultlibs', '-nostartfiles', '-o', prelink_name] + obj_list + def get_prelink_args(self, target: BuildTarget, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: + return [prelink_name], ['-r', '-nodefaultlibs', '-nostartfiles', '-o', prelink_name] + obj_list def get_pch_suffix(self) -> str: # Actually it's not supported for now, but probably will be supported in future diff --git a/mesonbuild/compilers/mixins/gnu.py b/mesonbuild/compilers/mixins/gnu.py index 62f55543a0a7..ebacbeb9e6d8 100644 --- a/mesonbuild/compilers/mixins/gnu.py +++ b/mesonbuild/compilers/mixins/gnu.py @@ -17,6 +17,7 @@ from ... import mesonlib from ... import mlog from ...options import OptionKey +from ...build import BuildTarget from mesonbuild.compilers.compilers import CompileCheckMode if T.TYPE_CHECKING: @@ -607,8 +608,8 @@ def get_has_func_attribute_extra_args(self, name: str) -> T.List[str]: # error. return ['-Werror=attributes'] - def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.List[str]: - return ['-r', '-o', prelink_name] + obj_list + def get_prelink_args(self, target: BuildTarget, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: + return [prelink_name], ['-r', '-o', prelink_name] + obj_list def get_lto_compile_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]: if threads == 0: diff --git a/mesonbuild/compilers/mixins/tasking.py b/mesonbuild/compilers/mixins/tasking.py index 7675dbc75329..78db6e8a15a4 100644 --- a/mesonbuild/compilers/mixins/tasking.py +++ b/mesonbuild/compilers/mixins/tasking.py @@ -7,8 +7,10 @@ import os import typing as T -from ...mesonlib import EnvironmentException +from ...mesonlib import EnvironmentException, File from ...options import OptionKey +from ...build import BuildTarget, StaticLibrary +from ...compilers import lang_suffixes if T.TYPE_CHECKING: from ...compilers.compilers import Compiler @@ -56,7 +58,7 @@ def __init__(self) -> None: self.base_options = { OptionKey(o) for o in [ - 'b_tasking_mil_link', + 'b_lto', 'b_staticpic', 'b_ndebug' ] @@ -112,6 +114,21 @@ def get_optimization_args(self, optimization_level: str) -> T.List[str]: def get_no_optimization_args(self) -> T.List[str]: return ['-O0'] + def get_prelink_args(self, target: BuildTarget, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: + mil_link_list = [] + obj_file_list = [] + for obj in obj_list: + if obj.endswith('.mil'): + mil_link_list.append(obj) + else: + obj_file_list.append(obj) + obj_file_list.append(prelink_name) + + return obj_file_list, ['--mil-link', '-o', prelink_name, '-c'] + mil_link_list + + def get_prelink_append_compile_args(self) -> bool: + return True + def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]: for idx, i in enumerate(parameter_list): if i[:2] == '-I' or i[:2] == '-L': @@ -119,8 +136,18 @@ def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], bu return parameter_list - def get_tasking_mil_link_args(self, option_enabled: bool) -> T.List[str]: - return ['--mil-link'] if option_enabled else [] - def get_preprocess_only_args(self) -> T.List[str]: return ['-E'] + + # This is required for MIL linking, if b_lto is enabled on the target + def get_object_suffix_override(self, target: BuildTarget, source: File) -> T.Optional[str]: + if target.get_option(OptionKey('b_lto')) or (isinstance(target, StaticLibrary) and target.prelink): + if not source.rsplit('.', 1)[1] in lang_suffixes['c']: + if isinstance(target, StaticLibrary) and not target.prelink: + raise EnvironmentException('Tried using MIL linking for a static library with a assembly file. This can only be done if the static library is prelinked or disable \'b_lto\'.') + else: + return None + else: + return 'mil' + else: + return None diff --git a/mesonbuild/linkers/linkers.py b/mesonbuild/linkers/linkers.py index ba7b9228d718..7341300d0c22 100644 --- a/mesonbuild/linkers/linkers.py +++ b/mesonbuild/linkers/linkers.py @@ -1731,6 +1731,9 @@ def get_search_args(self, dirname: str) -> T.List[str]: def get_output_args(self, outputname: str) -> T.List[str]: return ['-o', outputname] + def get_lto_args(self) -> T.List[str]: + return ['--mil-link'] + def rsp_file_syntax(self) -> RSPFileSyntax: return RSPFileSyntax.TASKING