From 1fef03c0f04e86300bee5ff3f628752d9e27304d Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Mon, 10 Jul 2023 13:21:41 -0400 Subject: [PATCH] build: dependencies should come after link_with on link command This fixes regression caused by https://github.com/mesonbuild/meson/commit/3162b901cab46d66a30c66a4406195523714ecdc that changes the order in which libraries are put on the link command. In addition, that commit was wrong because libraries from dependencies were processed before process_compiler() is called, which that commit wanted to avoid. --- mesonbuild/build.py | 19 +++++++++++++++---- .../unit/113 complex link cases/meson.build | 11 +++++++++++ unittests/linuxliketests.py | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index eb13a8976..3cb4a2237 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -755,8 +755,19 @@ class BuildTarget(Target): self.process_objectlist(objects) self.process_kwargs(kwargs) self.missing_languages = self.process_compilers() - self.link(extract_as_list(kwargs, 'link_with')) - self.link_whole(extract_as_list(kwargs, 'link_whole')) + + # self.link_targets and self.link_whole_targets contains libraries from + # dependencies (see add_deps()). They have not been processed yet because + # we have to call process_compilers() first and we need to process libraries + # from link_with and link_whole first. + # See https://github.com/mesonbuild/meson/pull/11957#issuecomment-1629243208. + link_targets = extract_as_list(kwargs, 'link_with') + self.link_targets + link_whole_targets = extract_as_list(kwargs, 'link_whole') + self.link_whole_targets + self.link_targets.clear() + self.link_whole_targets.clear() + self.link(link_targets) + self.link_whole(link_whole_targets) + if not any([self.sources, self.generated, self.objects, self.link_whole_targets, self.structured_sources, kwargs.pop('_allow_no_sources', False)]): mlog.warning(f'Build target {name} has no sources. ' @@ -1337,8 +1348,8 @@ class BuildTarget(Target): self.extra_files.extend(f for f in dep.extra_files if f not in self.extra_files) self.add_include_dirs(dep.include_directories, dep.get_include_type()) self.objects.extend(dep.objects) - self.link(dep.libraries) - self.link_whole(dep.whole_libraries) + self.link_targets.extend(dep.libraries) + self.link_whole_targets.extend(dep.whole_libraries) if dep.get_compile_args() or dep.get_link_args(): # Those parts that are external. extpart = dependencies.InternalDependency('undefined', diff --git a/test cases/unit/113 complex link cases/meson.build b/test cases/unit/113 complex link cases/meson.build index 31753edad..04e628177 100644 --- a/test cases/unit/113 complex link cases/meson.build +++ b/test cases/unit/113 complex link cases/meson.build @@ -47,3 +47,14 @@ s1 = static_library('t7-s1', 's1.c') s2 = static_library('t7-s2', 's2.c') s3 = static_library('t7-s3', 's3.c', link_with: [s1, s2], install: true) e = executable('t7-e1', 'main.c', link_with: s3) + +# Regression test: s3 should come last in the linker command. This seems to be +# required for at least backward compatibility reasons: +# https://github.com/mesonbuild/meson/pull/11957#issuecomment-1629243208 +s1 = static_library('t8-s1', 's1.c') +s2 = static_library('t8-s2', 's2.c') +s3 = static_library('t8-s3', 's3.c') +e = executable('t8-e1', 'main.c', + link_with: [s1, s2], + dependencies: declare_dependency(link_with: s3), +) diff --git a/unittests/linuxliketests.py b/unittests/linuxliketests.py index 745ee35c2..f322b0200 100644 --- a/unittests/linuxliketests.py +++ b/unittests/linuxliketests.py @@ -1846,3 +1846,4 @@ class LinuxlikeTests(BasePlatformTests): self.assertIn('build t5-e1: c_LINKER t5-e1.p/main.c.o | libt5-s1.so.p/libt5-s1.so.symbols libt5-s3.a\n', content) self.assertIn('build t6-e1: c_LINKER t6-e1.p/main.c.o | libt6-s2.a libt6-s3.a\n', content) self.assertIn('build t7-e1: c_LINKER t7-e1.p/main.c.o | libt7-s3.a\n', content) + self.assertIn('build t8-e1: c_LINKER t8-e1.p/main.c.o | libt8-s1.a libt8-s2.a libt8-s3.a\n', content)