summaryrefslogtreecommitdiffstats
path: root/test cases/common/208 link custom
diff options
context:
space:
mode:
Diffstat (limited to 'test cases/common/208 link custom')
-rwxr-xr-xtest cases/common/208 link custom/custom_stlib.py81
-rw-r--r--test cases/common/208 link custom/custom_target.c6
-rw-r--r--test cases/common/208 link custom/custom_target.py6
-rw-r--r--test cases/common/208 link custom/dummy.c1
-rw-r--r--test cases/common/208 link custom/lib.c7
-rw-r--r--test cases/common/208 link custom/meson.build86
-rw-r--r--test cases/common/208 link custom/outerlib.c3
-rw-r--r--test cases/common/208 link custom/prog.c6
8 files changed, 196 insertions, 0 deletions
diff --git a/test cases/common/208 link custom/custom_stlib.py b/test cases/common/208 link custom/custom_stlib.py
new file mode 100755
index 0000000..6a090f3
--- /dev/null
+++ b/test cases/common/208 link custom/custom_stlib.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+
+import shutil, sys, subprocess, argparse, pathlib
+import platform
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument('--private-dir', required=True)
+parser.add_argument('-o', required=True)
+parser.add_argument('cmparr', nargs='+')
+
+contents = '''#include<stdio.h>
+
+void flob(void) {
+ printf("Now flobbing.\\n");
+}
+'''
+
+def get_pic_args():
+ platname = platform.system().lower()
+ if platname in ['windows', 'darwin'] or sys.platform == 'cygwin':
+ return []
+ return ['-fPIC']
+
+def generate_lib_gnulike(outfile, c_file, private_dir, compiler_array):
+ if shutil.which('ar'):
+ static_linker = 'ar'
+ elif shutil.which('llvm-ar'):
+ static_linker = 'llvm-ar'
+ elif shutil.which('gcc-ar'):
+ static_linker = 'gcc-ar'
+ else:
+ sys.exit('Could not detect a static linker.')
+ o_file = c_file.with_suffix('.o')
+ compile_cmd = compiler_array + ['-c', '-g', '-O2', '-o', str(o_file), str(c_file)]
+ compile_cmd += get_pic_args()
+ subprocess.check_call(compile_cmd)
+ out_file = pathlib.Path(outfile)
+ if out_file.exists():
+ out_file.unlink()
+ link_cmd = [static_linker, 'csr', outfile, str(o_file)]
+ subprocess.check_call(link_cmd)
+ return 0
+
+
+def generate_lib_msvc(outfile, c_file, private_dir, compiler_array):
+ static_linker = 'lib'
+ o_file = c_file.with_suffix('.obj')
+ compile_cmd = compiler_array + ['/MDd',
+ '/nologo',
+ '/ZI',
+ '/Ob0',
+ '/Od',
+ '/c',
+ '/Fo' + str(o_file),
+ str(c_file)]
+ subprocess.check_call(compile_cmd)
+ out_file = pathlib.Path(outfile)
+ if out_file.exists():
+ out_file.unlink()
+ link_cmd = [static_linker,
+ '/nologo',
+ '/OUT:' + str(outfile),
+ str(o_file)]
+ subprocess.check_call(link_cmd)
+ return 0
+
+def generate_lib(outfile, private_dir, compiler_array):
+ private_dir = pathlib.Path(private_dir)
+ if not private_dir.exists():
+ private_dir.mkdir()
+ c_file = private_dir / 'flob.c'
+ c_file.write_text(contents)
+ for i in compiler_array:
+ if (i.endswith('cl') or i.endswith('cl.exe')) and 'clang-cl' not in i:
+ return generate_lib_msvc(outfile, c_file, private_dir, compiler_array)
+ return generate_lib_gnulike(outfile, c_file, private_dir, compiler_array)
+
+if __name__ == '__main__':
+ options = parser.parse_args()
+ sys.exit(generate_lib(options.o, options.private_dir, options.cmparr))
diff --git a/test cases/common/208 link custom/custom_target.c b/test cases/common/208 link custom/custom_target.c
new file mode 100644
index 0000000..1bbe82c
--- /dev/null
+++ b/test cases/common/208 link custom/custom_target.c
@@ -0,0 +1,6 @@
+void outer_lib_func(void);
+
+int main(void) {
+ outer_lib_func();
+ return 0;
+}
diff --git a/test cases/common/208 link custom/custom_target.py b/test cases/common/208 link custom/custom_target.py
new file mode 100644
index 0000000..c246344
--- /dev/null
+++ b/test cases/common/208 link custom/custom_target.py
@@ -0,0 +1,6 @@
+#!/usr/bin/env python3
+
+import shutil, sys
+
+if __name__ == '__main__':
+ shutil.copyfile(sys.argv[1], sys.argv[2])
diff --git a/test cases/common/208 link custom/dummy.c b/test cases/common/208 link custom/dummy.c
new file mode 100644
index 0000000..53a4a40
--- /dev/null
+++ b/test cases/common/208 link custom/dummy.c
@@ -0,0 +1 @@
+void inner_lib_func(void) {} \ No newline at end of file
diff --git a/test cases/common/208 link custom/lib.c b/test cases/common/208 link custom/lib.c
new file mode 100644
index 0000000..585b6c9
--- /dev/null
+++ b/test cases/common/208 link custom/lib.c
@@ -0,0 +1,7 @@
+void flob(void);
+
+int foo(void)
+{
+ flob();
+ return 0;
+}
diff --git a/test cases/common/208 link custom/meson.build b/test cases/common/208 link custom/meson.build
new file mode 100644
index 0000000..4d4f655
--- /dev/null
+++ b/test cases/common/208 link custom/meson.build
@@ -0,0 +1,86 @@
+project('linkcustom', 'c')
+
+# This would require passing the static linker to the build script or having
+# it detect it by itself. I'm too lazy to implement it now and it is not
+# really needed for testing that custom targets work. It is the responsibility
+# of the custom target to produce things in the correct format.
+assert(not meson.is_cross_build(),
+ 'MESON_SKIP_TEST cross checking not implemented.')
+
+cc = meson.get_compiler('c')
+genprog = find_program('custom_stlib.py')
+
+clib = custom_target('linkcustom',
+ output: 'libflob.a',
+ command: [genprog,
+ '-o', '@OUTPUT@',
+ '--private-dir', '@PRIVATE_DIR@'] + cc.cmd_array())
+
+# custom_target tests
+
+exe = executable('prog', 'prog.c', link_with: clib)
+test('linkcustom', exe)
+
+d = declare_dependency(link_with: clib)
+
+exe2 = executable('prog2', 'prog.c', dependencies: d)
+test('linkcustom2', exe2)
+
+# Link whole tests
+
+if meson.backend() == 'xcode'
+ message('Xcode does not support link whole so skipping.')
+else
+ exe3 = executable('prog3', 'prog.c', link_whole: clib)
+ test('linkwhole', exe)
+
+ d2 = declare_dependency(link_whole: clib)
+
+ exe4 = executable('prog4', 'prog.c', dependencies: d2)
+ test('linkwhole2', exe2)
+endif
+
+# custom_target[i] tests
+
+exe_i = executable('prog_i', 'prog.c', link_with: clib[0])
+test('linkcustom', exe_i)
+
+d_i = declare_dependency(link_with: clib[0])
+
+exe2_i = executable('prog2_i', 'prog.c', dependencies: d_i)
+test('linkcustom2_i', exe2_i)
+
+# Link whole tests
+
+if meson.backend() == 'xcode'
+ message('Xcode does not support link whole so skipping.')
+else
+ shared_library('lib1', 'lib.c', link_whole: clib)
+
+ exe3_i = executable('prog3_i', 'prog.c', link_whole: clib[0])
+ test('linkwhole', exe)
+
+ d2_i = declare_dependency(link_whole: clib[0])
+
+ exe4_i = executable('prog4_i', 'prog.c', dependencies: d2_i)
+ test('linkwhole2_i', exe2_i)
+endif
+
+# Link with custom target
+
+dummy = static_library('dummy', 'dummy.c')
+
+custom_prog = find_program('custom_target.py')
+t = custom_target('custom', input: dummy, output: 'libcustom.a', command: [custom_prog, '@INPUT@', '@OUTPUT@'])
+
+dep1 = declare_dependency(link_with: t)
+dep2 = declare_dependency(link_with: t[0])
+
+lib1 = static_library('lib1', 'outerlib.c', dependencies: dep1)
+lib2 = static_library('lib2', 'outerlib.c', dependencies: dep2)
+
+exe1 = executable('exe1', 'custom_target.c', link_with: lib1)
+test('custom_target_1', exe1)
+
+exe1_2 = executable('exe1_2', 'custom_target.c', link_with: lib2)
+test('custom_target_2', exe2) \ No newline at end of file
diff --git a/test cases/common/208 link custom/outerlib.c b/test cases/common/208 link custom/outerlib.c
new file mode 100644
index 0000000..6861f8d
--- /dev/null
+++ b/test cases/common/208 link custom/outerlib.c
@@ -0,0 +1,3 @@
+void inner_lib_func(void);
+
+void outer_lib_func(void) { inner_lib_func(); } \ No newline at end of file
diff --git a/test cases/common/208 link custom/prog.c b/test cases/common/208 link custom/prog.c
new file mode 100644
index 0000000..efecbef
--- /dev/null
+++ b/test cases/common/208 link custom/prog.c
@@ -0,0 +1,6 @@
+void flob(void);
+
+int main(void) {
+ flob();
+ return 0;
+}