From 589e298cf97ce5f646caedb2832f437594b471b2 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 25 Jul 2022 10:11:31 -0700 Subject: [PATCH 1/2] backends/ninja: run `ranlib -c $out` when using the apple ar Apple's AR is old, and doesn't add externed symbols to the symbol table, instead relying on the user calling ranlib with -c. We need to do that for the user --- mesonbuild/backend/ninjabackend.py | 13 ++++++++++++- mesonbuild/linkers/linkers.py | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 03f33d613523..6c739ed0a1bf 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -2350,7 +2350,7 @@ def generate_static_link_rules(self): if static_linker is None: continue rule = 'STATIC_LINKER{}'.format(self.get_rule_suffix(for_machine)) - cmdlist = [] + cmdlist: T.List[T.Union[str, NinjaCommandArg]] = [] args = ['$in'] # FIXME: Must normalize file names with pathlib.Path before writing # them out to fix this properly on Windows. See: @@ -2364,6 +2364,17 @@ def generate_static_link_rules(self): cmdlist += static_linker.get_exelist() cmdlist += ['$LINK_ARGS'] cmdlist += NinjaCommandArg.list(static_linker.get_output_args('$out'), Quoting.none) + # The default ar on MacOS (at least through version 12), does not + # add extern'd variables to the symbol table by default, and + # requires that apple's ranlib be called with a special flag + # instead after linking + if static_linker.id == 'applear': + # This is a bit of a hack, but we assume that that we won't need + # an rspfile on MacOS, otherwise the arguments are passed to + # ranlib, not to ar + cmdlist.extend(args) + args = [] + cmdlist.extend(['&&', 'ranlib', '-c', '$out']) description = 'Linking static target $out' if num_pools > 0: pool = 'pool = link_pool' diff --git a/mesonbuild/linkers/linkers.py b/mesonbuild/linkers/linkers.py index 9176090404a4..0c2bf73be77a 100644 --- a/mesonbuild/linkers/linkers.py +++ b/mesonbuild/linkers/linkers.py @@ -349,6 +349,8 @@ class VisualStudioLinker(VisualStudioLikeLinker, StaticLinker): """Microsoft's lib static linker.""" + id = 'lib' + def __init__(self, exelist: T.List[str], machine: str): StaticLinker.__init__(self, exelist) VisualStudioLikeLinker.__init__(self, machine) @@ -358,6 +360,8 @@ class IntelVisualStudioLinker(VisualStudioLikeLinker, StaticLinker): """Intel's xilib static linker.""" + id = 'xilib' + def __init__(self, exelist: T.List[str], machine: str): StaticLinker.__init__(self, exelist) VisualStudioLikeLinker.__init__(self, machine) From bddd1d9435bff6983fdb0c8620abf5ea919e6969 Mon Sep 17 00:00:00 2001 From: Ailin Nemui Date: Mon, 25 Jul 2022 10:24:09 -0700 Subject: [PATCH 2/2] tests: Test extern'd globals on MacOS with the Apple Archiver This forces the use of the Apple archiver, since that archiver doesn't add extern'd variables to the symbol table automatically, and instead requires that ranlib be used. A native file is used to ensure that Apple's ar is used even in the presence of llvm or gcc in the path with their superior archivers. Co-authored-by: Dylan Baker --- test cases/osx/9 global variable ar/libfile.c | 9 +++++++++ test cases/osx/9 global variable ar/libfile2.c | 7 +++++++ test cases/osx/9 global variable ar/meson.build | 6 ++++++ test cases/osx/9 global variable ar/nativefile.ini | 2 ++ test cases/osx/9 global variable ar/prog.c | 7 +++++++ 5 files changed, 31 insertions(+) create mode 100644 test cases/osx/9 global variable ar/libfile.c create mode 100644 test cases/osx/9 global variable ar/libfile2.c create mode 100644 test cases/osx/9 global variable ar/meson.build create mode 100644 test cases/osx/9 global variable ar/nativefile.ini create mode 100644 test cases/osx/9 global variable ar/prog.c diff --git a/test cases/osx/9 global variable ar/libfile.c b/test cases/osx/9 global variable ar/libfile.c new file mode 100644 index 000000000000..b258d7b8250f --- /dev/null +++ b/test cases/osx/9 global variable ar/libfile.c @@ -0,0 +1,9 @@ +// Source: https://lists.gnu.org/archive/html/libtool/2002-07/msg00025.html + +#include + +extern int l2; +void l1(void) +{ + printf("l1 %d\n", l2); +} diff --git a/test cases/osx/9 global variable ar/libfile2.c b/test cases/osx/9 global variable ar/libfile2.c new file mode 100644 index 000000000000..1499c4dc7f8a --- /dev/null +++ b/test cases/osx/9 global variable ar/libfile2.c @@ -0,0 +1,7 @@ +// Source: https://lists.gnu.org/archive/html/libtool/2002-07/msg00025.html + +int l2; +void l2_func(void) +{ + l2 = 77; +} diff --git a/test cases/osx/9 global variable ar/meson.build b/test cases/osx/9 global variable ar/meson.build new file mode 100644 index 000000000000..313dd1b37653 --- /dev/null +++ b/test cases/osx/9 global variable ar/meson.build @@ -0,0 +1,6 @@ +# Source: https://lists.gnu.org/archive/html/libtool/2002-07/msg00025.html + +project('global variable test', 'c') + +lib = static_library('mylib', 'libfile.c', 'libfile2.c') +test('global variable', executable('prog', 'prog.c', link_with: lib)) diff --git a/test cases/osx/9 global variable ar/nativefile.ini b/test cases/osx/9 global variable ar/nativefile.ini new file mode 100644 index 000000000000..4fb5e7f7db1b --- /dev/null +++ b/test cases/osx/9 global variable ar/nativefile.ini @@ -0,0 +1,2 @@ +[binaries] +ar = 'ar' diff --git a/test cases/osx/9 global variable ar/prog.c b/test cases/osx/9 global variable ar/prog.c new file mode 100644 index 000000000000..4665016f6149 --- /dev/null +++ b/test cases/osx/9 global variable ar/prog.c @@ -0,0 +1,7 @@ +// Source: https://lists.gnu.org/archive/html/libtool/2002-07/msg00025.html + +extern void l1(void); +int main(void) +{ + l1(); +}