v2 / vlib / v / tests / shared_library_system_link_test.v
56 lines · 53 sloc · 1.41 KB · 613a6c473fb61893f29132638cc8051ab0f4d019
Raw
1import os
2import rand
3
4const vexe = @VEXE
5
6fn test_shared_library_links_with_system_cc() {
7 if os.user_os() != 'linux' {
8 return
9 }
10 workdir := os.join_path(os.vtmp_dir(), 'v_shared_link_${rand.ulid()}')
11 os.mkdir_all(workdir) or { panic(err) }
12 defer {
13 os.rmdir_all(workdir) or {}
14 }
15 lib_src := os.join_path(workdir, 'libfoo.v')
16 lib_out := os.join_path(workdir, 'libfoo')
17 lib_so := '${lib_out}.so'
18 host_src := os.join_path(workdir, 'host.c')
19 host_bin := os.join_path(workdir, 'host')
20 os.write_file(lib_src, [
21 'module libfoo',
22 '',
23 'pub fn square(x int) int {',
24 '\treturn x * x',
25 '}',
26 ].join('\n')) or { panic(err) }
27 os.write_file(host_src, [
28 '#include <stdio.h>',
29 '',
30 'int libfoo__square(int);',
31 '',
32 'int main(void) {',
33 '\tprintf("%d\\n", libfoo__square(2));',
34 '\treturn 0;',
35 '}',
36 ].join('\n')) or { panic(err) }
37 run_cmd('${os.quoted_path(vexe)} -shared -o ${os.quoted_path(lib_out)} ${os.quoted_path(lib_src)}') or {
38 panic(err)
39 }
40 assert os.exists(lib_so)
41 run_cmd('cc ${os.quoted_path(host_src)} -L${os.quoted_path(workdir)} -lfoo -o ${os.quoted_path(host_bin)}') or {
42 panic(err)
43 }
44 res := run_cmd('LD_LIBRARY_PATH=${os.quoted_path(workdir)} ${os.quoted_path(host_bin)}') or {
45 panic(err)
46 }
47 assert res.output.trim_space() == '4'
48}
49
50fn run_cmd(cmd string) !os.Result {
51 res := os.execute(cmd)
52 if res.exit_code != 0 {
53 return error('command failed:\n${cmd}\n${res.output}')
54 }
55 return res
56}
57