v2 / vlib / v / gen / c / link_generated_c_files_test.v
92 lines · 84 sloc · 2.43 KB · 0c6c80c92cf02525e375e2f6a973dcf2fdec6e92
Raw
1module c
2
3import os
4import v.builder
5import v.pref
6
7const test_vexe = os.quoted_path(@VEXE)
8
9fn test_is_o_generated_c_files_can_be_linked_together() {
10 cc_path := os.find_abs_path_of_executable('cc') or { return }
11 cc := os.quoted_path(cc_path)
12 workdir := os.join_path(os.vtmp_dir(), 'v_cgen_is_o_link_${os.getpid()}')
13 os.mkdir_all(workdir)!
14 defer {
15 os.rmdir_all(workdir) or {}
16 }
17 a_v := os.join_path(workdir, 'a.v')
18 b_v := os.join_path(workdir, 'b.v')
19 host_c := os.join_path(workdir, 'main.c')
20 a_c := os.join_path(workdir, 'a.c')
21 b_c := os.join_path(workdir, 'b.c')
22 prog := os.join_path(workdir, 'prog')
23 os.write_file(a_v, 'module main
24
25@[markused]
26pub fn a() int {
27 return 1
28}
29')!
30 os.write_file(b_v, 'module main
31
32@[markused]
33pub fn b() int {
34 return 2
35}
36')!
37 os.write_file(host_c, '#include <stdio.h>
38int main__a(void);
39int main__b(void);
40
41int main(void) {
42 printf("%d %d\\n", main__a(), main__b());
43 return 0;
44}
45')!
46 for cmd in [
47 '${test_vexe} -gc none -no-skip-unused -is_o -o ${os.quoted_path(a_c)} ${os.quoted_path(a_v)}',
48 '${test_vexe} -gc none -no-skip-unused -is_o -o ${os.quoted_path(b_c)} ${os.quoted_path(b_v)}',
49 '${cc} -o ${os.quoted_path(prog)} ${os.quoted_path(a_c)} ${os.quoted_path(b_c)} ${os.quoted_path(host_c)} -lm',
50 ] {
51 res := os.execute(cmd)
52 assert res.exit_code == 0, '${cmd}\n${res.output}'
53 }
54 res := os.execute(os.quoted_path(prog))
55 assert res.exit_code == 0, res.output
56 assert res.output.trim_space() == '1 2'
57}
58
59fn test_parallel_cc_windows_header_keeps_vv_loc_external() {
60 tmp_dir := os.join_path(os.vtmp_dir(), 'parallel_cc_windows_linkage_${os.getpid()}')
61 os.mkdir_all(tmp_dir)!
62 defer {
63 os.rmdir_all(tmp_dir) or {}
64 }
65 source_path := os.join_path(tmp_dir, 'main.v')
66 os.write_file(source_path, 'module main
67fn helper() string {
68 return "ok"
69}
70
71fn main() {
72 println(helper())
73}
74')!
75 mut prefs, _ := pref.parse_args_and_show_errors([], [
76 '',
77 '-parallel-cc',
78 '-os',
79 'windows',
80 source_path,
81 ], false)
82 mut b := builder.new_builder(prefs)
83 mut files := b.get_builtin_files()
84 files << b.get_user_files()
85 b.set_module_lookup_paths()
86 b.front_and_middle_stages(files)!
87 result := gen(b.parsed_files, mut b.table, b.pref)
88 header := result.header.replace('\r\n', '\n')
89 assert header.contains('#define _VPARALLELCC (1)'), header
90 assert header.contains('#ifdef _VPARALLELCC\n\t\t#define VV_LOC\n\t#else\n\t\t#define VV_LOC static\n\t#endif'), header
91 assert header.contains('VV_LOC string main__helper(void);'), header
92}
93