meson: fix checking of linker args
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 25 Apr 2017 01:03:35 +0000 (21:03 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 25 Apr 2017 12:49:16 +0000 (08:49 -0400)
Previous checks did nothing, because cc.has_argument only does compilation,
without any linking. Unfortunately cc.links() cannot be used, because it does
not accept any options. Providing the test file as a static source is easiest,
even if not every elegant.

https://github.com/mesonbuild/meson/issues/1676

meson.build
src/systemd/meson.build
tools/meson-check-compilation.sh
tools/meson-link-test.c [new file with mode: 0644]

index 991836a..778fe63 100644 (file)
@@ -222,6 +222,7 @@ substs.set('RC_LOCAL_SCRIPT_PATH_STOP',                       get_option('halt-l
 
 cc = meson.get_compiler('c')
 pkgconfig = import('pkgconfig')
+check_compilation_sh = find_program('tools/meson-check-compilation.sh')
 
 foreach arg : ['-Wundef',
                '-Wlogical-op',
@@ -291,6 +292,8 @@ if cc.get_id() == 'clang'
         endforeach
 endif
 
+link_test_c = files('tools/meson-link-test.c')
+
 # --as-needed and --no-undefined are provided by meson by default,
 # run mesonconf to see what is enabled
 foreach arg : ['-Wl,-z,relro',
@@ -298,7 +301,12 @@ foreach arg : ['-Wl,-z,relro',
                '-pie',
                '-Wl,-fuse-ld=gold',
               ]
-        if cc.has_argument(arg)
+
+        have = run_command(check_compilation_sh,
+                           cc.cmd_array(), '-x', 'c', arg,
+                           '-include', link_test_c).returncode() == 0
+        message('Linking with @0@ supported: @1@'.format(arg, have ? 'yes' : 'no'))
+        if have
                 add_project_link_arguments(arg, language : 'c')
         endif
 endforeach
@@ -312,7 +320,11 @@ if get_option('buildtype') != 'debug'
         endforeach
 
         foreach arg : ['-Wl,--gc-sections']
-                if cc.has_argument(arg)
+                have = run_command(check_compilation_sh,
+                                   cc.cmd_array(), '-x', 'c', arg,
+                                   '-include', link_test_c).returncode() == 0
+                message('Linking with @0@ supported: @1@'.format(arg, have ? 'yes' : 'no'))
+                if have
                         add_project_link_arguments(arg, language : 'c')
                 endif
         endforeach
@@ -429,7 +441,6 @@ etags = find_program('etags', required : false)
 
 meson_make_symlink = meson.source_root() + '/tools/meson-make-symlink.sh'
 mkdir_p = 'mkdir -p $DESTDIR/@0@'
-check_compilation_sh = find_program('tools/meson-check-compilation.sh')
 test_efi_create_disk_sh = find_program('test/test-efi-create-disk.sh')
 splash_bmp = files('test/splash.bmp')
 
index 0f5a853..43fd013 100644 (file)
@@ -47,7 +47,7 @@ foreach header : _systemd_headers
                 name = ''.join([header] + opt)
                 test('cc-' + name,
                      check_compilation_sh,
-                     args : cc.cmd_array() + ['-x', 'c'] + opt +
+                     args : cc.cmd_array() + ['-x', 'c', '-c'] + opt +
                             ['-Werror', '-include',
                              join_paths(meson.current_source_dir(), header)])
         endforeach
index e241942..d3b2a31 100755 (executable)
@@ -1,3 +1,3 @@
 #!/bin/sh -eu
 
-"$@" '-' '-c' -o/dev/null </dev/null
+"$@" '-' -o/dev/null </dev/null
diff --git a/tools/meson-link-test.c b/tools/meson-link-test.c
new file mode 100644 (file)
index 0000000..825bbff
--- /dev/null
@@ -0,0 +1 @@
+int main(void) {return 0;}