Debian Codegraph @

test cases/common/95 manygen/subdir/manygen.py

raw
1 #!/usr/bin/env python3
2
3
4 # Generates a static library, object file, source
5 # file and a header file.
6
7 import sys, os
8 import subprocess
9
10 with open(sys.argv[1]) as f:
11 funcname = f.readline().strip()
12 outdir = sys.argv[2]
13 buildtype_args = sys.argv[3]
14 compiler_type = sys.argv[4]
15 compiler = sys.argv[5:]
16
17 if not os.path.isdir(outdir):
18 print('Outdir does not exist.')
19 sys.exit(1)
20
21 if compiler_type == 'msvc':
22 libsuffix = '.lib'
23 is_vs = True
24 if any(['clang-cl' in c for c in compiler]):
25 linker = 'llvm-lib'
26 else:
27 linker = 'lib'
28 else:
29 libsuffix = '.a'
30 is_vs = False
31 linker = 'ar'
32
33 objsuffix = '.o'
34
35 outo = os.path.join(outdir, funcname + objsuffix)
36 outa = os.path.join(outdir, funcname + libsuffix)
37 outh = os.path.join(outdir, funcname + '.h')
38 outc = os.path.join(outdir, funcname + '.c')
39
40 tmpc = 'diibadaaba.c'
41 tmpo = 'diibadaaba' + objsuffix
42
43 with open(outc, 'w', encoding='utf-8') as f:
44 f.write('''#include"{}.h"
45 int {}_in_src(void) {{
46 return 0;
47 }}
48 '''.format(funcname, funcname))
49
50 with open(outh, 'w', encoding='utf-8') as f:
51 f.write('''#pragma once
52 int {}_in_lib(void);
53 int {}_in_obj(void);
54 int {}_in_src(void);
55 '''.format(funcname, funcname, funcname))
56
57 with open(tmpc, 'w', encoding='utf-8') as f:
58 f.write('''int %s_in_obj(void) {
59 return 0;
60 }
61 ''' % funcname)
62
63 if is_vs:
64 subprocess.check_call(compiler + ['/nologo', '/c', buildtype_args, '/Fo' + outo, tmpc])
65 else:
66 subprocess.check_call(compiler + ['-c', '-o', outo, tmpc])
67
68 with open(tmpc, 'w', encoding='utf-8') as f:
69 f.write('''int %s_in_lib() {
70 return 0;
71 }
72 ''' % funcname)
73
74 if is_vs:
75 subprocess.check_call(compiler + ['/nologo', '/c', '/Fo' + tmpo, tmpc])
76 subprocess.check_call([linker, '/NOLOGO', '/OUT:' + outa, tmpo])
77 else:
78 subprocess.check_call(compiler + ['-c', '-o', tmpo, tmpc])
79 subprocess.check_call([linker, 'csr', outa, tmpo])
80
81 os.unlink(tmpo)
82 os.unlink(tmpc)