From f480f2b4b6a3f216740738f50237c194f8d1e9bd Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 13 Dec 2024 19:26:24 -0800 Subject: [PATCH] Add link_syms parameter to build targets This parameter provides a place to add -Wl,--defsym=a=b and -Wl,--undefined=c parameters during linking. These should occur before any object files and must occur before any libraries so that the results will affect the link correctly. In the ninjabackend, this is done by adding these to the ARGS parameter during linking as that is placed in the command line before any input files or libraries. Signed-off-by: Keith Packard --- docs/yaml/functions/_build_target_base.yaml | 6 ++++++ mesonbuild/backend/ninjabackend.py | 8 ++++++-- mesonbuild/build.py | 5 +++++ mesonbuild/compilers/compilers.py | 3 +++ mesonbuild/interpreter/type_checking.py | 7 +++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/yaml/functions/_build_target_base.yaml b/docs/yaml/functions/_build_target_base.yaml index 1721b29cfe5a..d73bdf35ff16 100644 --- a/docs/yaml/functions/_build_target_base.yaml +++ b/docs/yaml/functions/_build_target_base.yaml @@ -103,6 +103,12 @@ kwargs: Flags to use during linking. You can use UNIX-style flags here for all platforms. + link_syms: + type: list[str] + description: | + Flags to define and undefine symbols during linking. You must use + build-system-appropriate flags here. + link_depends: type: str | file | custom_tgt | custom_idx description: | diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 5716ea29e351..311e80761066 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -3558,15 +3558,19 @@ def generate_link(self, target: build.BuildTarget, outname, obj_list, linker: T. elem.add_dep(dep_targets + custom_target_libraries) # Compiler args must be included in TI C28x linker commands. + compile_args = [] if linker.get_id() in {'c2000', 'c6000', 'ti'}: - compile_args = [] for for_machine in MachineChoice: clist = self.environment.coredata.compilers[for_machine] for langname, compiler in clist.items(): if langname in {'c', 'cpp'} and compiler.get_id() in {'c2000', 'c6000', 'ti'}: compile_args += self.generate_basic_compiler_args(target, compiler) - elem.add_item('ARGS', compile_args) + # Add -Wl,--defsym and -Wl,--undefined args + if not isinstance(target, build.StaticLibrary): + compile_args += linker.get_target_link_syms(target) + if compile_args: + elem.add_item('ARGS', compile_args) elem.add_item('LINK_ARGS', commands) self.create_target_linker_introspection(target, linker, commands) return elem diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 35f1f24a42f8..e1be289212a8 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -84,6 +84,7 @@ class DFeatures(TypedDict): 'link_with', 'link_whole', 'link_args', + 'link_syms', 'link_depends', 'implicit_include_directories', 'include_directories', @@ -1151,6 +1152,10 @@ def process_kwargs(self, kwargs): or build_rpath properties instead. This will become a hard error in a future Meson release. ''')) + self.link_syms = extract_as_list(kwargs, 'link_syms') + for i in self.link_syms: + if not isinstance(i, str): + raise InvalidArguments('Link_syms arguments must be strings.') self.process_link_depends(kwargs.get('link_depends', [])) # Target-specific include dirs must be added BEFORE include dirs from # internal deps (added inside self.add_deps()) to override them. diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 603a3eb484de..7a34f42b1afc 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -1038,6 +1038,9 @@ def get_soname_args(self, env: 'Environment', prefix: str, shlib_name: str, def get_target_link_args(self, target: 'BuildTarget') -> T.List[str]: return target.link_args + def get_target_link_syms(self, target: 'BuildTarget') -> T.List[str]: + return target.link_syms + def get_dependency_compile_args(self, dep: 'Dependency') -> T.List[str]: return dep.get_compile_args() diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py index ed34be950065..de9f601b6e0c 100644 --- a/mesonbuild/interpreter/type_checking.py +++ b/mesonbuild/interpreter/type_checking.py @@ -646,6 +646,13 @@ def _name_suffix_validator(arg: T.Optional[T.Union[str, T.List]]) -> T.Optional[ validator=in_set_validator(set(compilers.all_languages)), since='0.51.0', ), + KwargInfo( + 'link_syms', + (ContainerTypeInfo(list, str), NoneType), + listify=True, + default=None, + since='1.7.0', + ), ] def _validate_win_subsystem(value: T.Optional[str]) -> T.Optional[str]: