Debian Codegraph @

test cases/common/138 C and CPP link/meson.build

raw
1 # Copyright © 2017 Dylan Baker
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 project('C and C++ static link test', ['c', 'cpp'])
16
17 if meson.backend() == 'xcode'
18 error('''MESON_SKIP_TEST: overriding link language is not supported in Xcode.
19
20 If you really need this, then patches are welcome. The only known way is
21 to create a dummy C++ file in the meson-private directory and adding
22 that to the target's source list when needed. The primitives exist
23 but may need some tweaking. Grep for language_stdlib_only_link_flags to find
24 where this is handled in other backends.''')
25 endif
26
27 # Verify that adding link arguments works.
28 add_global_link_arguments('', language : 'c')
29 add_project_link_arguments('', language : 'c')
30
31 libc = static_library('cfoo', ['foo.c', 'foo.h'])
32
33 # Test that linking C libs to external static C++ libs uses the C++ linker
34 # Since we can't depend on the test system to provide this, we create one
35 # ourselves at configure time and then 'find' it with cxx.find_library().
36 cxx = meson.get_compiler('cpp')
37
38 if cxx.get_argument_syntax() == 'msvc'
39 if cxx.get_id() == 'msvc'
40 static_linker = find_program('lib')
41 elif cxx.get_id() == 'clang-cl'
42 static_linker = find_program('llvm-lib')
43 elif cxx.get_id() == 'intel-cl'
44 static_linker = find_program('xilib')
45 else
46 error('unable to determine static linker to use with this compiler')
47 endif
48 compile_cmd = ['/c', '@INPUT@', '/Fo@OUTPUT@']
49 stlib_cmd = [static_linker, '/OUT:@OUTPUT@', '@INPUT@']
50 else
51 picflag = []
52 if not ['darwin', 'windows'].contains(host_machine.system())
53 picflag = ['-fPIC']
54 endif
55 compile_cmd = ['-c', picflag, '@INPUT@', '-o', '@OUTPUT@']
56 stlib_cmd = ['ar', 'csr', '@OUTPUT@', '@INPUT@']
57 endif
58
59 foo_cpp_o = configure_file(
60 input : 'foo.cpp',
61 output : 'foo.cpp.o',
62 command : cxx.cmd_array() + compile_cmd)
63
64 configure_file(
65 input : foo_cpp_o,
66 output : 'libstcppext.a',
67 command : stlib_cmd)
68
69 libstcppext = cxx.find_library('stcppext', dirs : meson.current_build_dir())
70 lib_type_name = libstcppext.type_name()
71 assert(lib_type_name == 'library', 'type name is ' + lib_type_name)
72
73 libfooext = shared_library(
74 'fooext',
75 ['foobar.c', 'foobar.h'],
76 link_with : libc,
77 dependencies : libstcppext,
78 )
79
80 # Test that linking C libs to internal static C++ libs uses the C++ linker
81 libcpp = static_library('cppfoo', ['foo.cpp', 'foo.hpp'])
82
83 libfoo = shared_library(
84 'foo',
85 ['foobar.c', 'foobar.h'],
86 link_with : [libc, libcpp],
87 )
88
89 # Test that link_whole is also honored
90 #
91 # VS2010 lacks the /WHOLEARCHIVE option that later versions of MSVC support, so
92 # don't run this tests on that backend.
93 if not (cxx.get_id() == 'msvc' and cxx.version().version_compare('<19'))
94 libfoowhole = shared_library(
95 'foowhole',
96 ['foobar.c', 'foobar.h'],
97 link_whole : [libc, libcpp],
98 )
99 endif
100
101 # Test sublinking (linking C and C++, then linking that to C)
102 libfoo_static = static_library(
103 'foo_static',
104 ['foobar.c', 'foobar.h'],
105 link_with : [libc, libcpp],
106 )
107
108 libsub = shared_library(
109 'sub',
110 ['sub.c', 'sub.h'],
111 link_with : libfoo_static,
112 )
113
114 if not (cxx.get_id() == 'msvc' and cxx.version().version_compare('<19'))
115 libsubwhole = shared_library(
116 'subwhole',
117 ['sub.c', 'sub.h'],
118 link_whole : libfoo_static,
119 )
120 endif
121
122 # Test that it really is recursive
123 libsub_static = static_library(
124 'sub_static',
125 ['sub.c', 'sub.h'],
126 link_with : libfoo_static,
127 )
128
129 libsubsub = shared_library(
130 'subsub',
131 ['dummy.c'],
132 link_with : libsub_static,
133 )