| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import shutil, sys, subprocess, argparse, pathlib |
| 4 |
|
| 5 |
parser = argparse.ArgumentParser() |
| 6 |
|
| 7 |
parser.add_argument('--private-dir', required=True) |
| 8 |
parser.add_argument('-o', nargs='+', required=True) |
| 9 |
parser.add_argument('cmparr', nargs='+') |
| 10 |
|
| 11 |
contents = ['''#include<stdio.h> |
| 12 |
|
| 13 |
void flob_1() { |
| 14 |
printf("Now flobbing #1.\\n"); |
| 15 |
} |
| 16 |
''', '''#include<stdio.h> |
| 17 |
|
| 18 |
void flob_2() { |
| 19 |
printf("Now flobbing #2.\\n"); |
| 20 |
} |
| 21 |
'''] |
| 22 |
|
| 23 |
def generate_lib_gnulike(outfile, c_file, private_dir, compiler_array): |
| 24 |
if shutil.which('ar'): |
| 25 |
static_linker = 'ar' |
| 26 |
elif shutil.which('llvm-ar'): |
| 27 |
static_linker = 'llvm-ar' |
| 28 |
elif shutil.which('gcc-ar'): |
| 29 |
static_linker = 'gcc-ar' |
| 30 |
else: |
| 31 |
sys.exit('Could not detect a static linker.') |
| 32 |
o_file = c_file.with_suffix('.o') |
| 33 |
compile_cmd = compiler_array + ['-c', '-g', '-O2', '-o', str(o_file), str(c_file)] |
| 34 |
subprocess.check_call(compile_cmd) |
| 35 |
out_file = pathlib.Path(outfile) |
| 36 |
if out_file.exists(): |
| 37 |
out_file.unlink() |
| 38 |
link_cmd = [static_linker, 'csr', outfile, str(o_file)] |
| 39 |
subprocess.check_call(link_cmd) |
| 40 |
return 0 |
| 41 |
|
| 42 |
|
| 43 |
def generate_lib_msvc(outfile, c_file, private_dir, compiler_array): |
| 44 |
static_linker = 'lib' |
| 45 |
o_file = c_file.with_suffix('.obj') |
| 46 |
compile_cmd = compiler_array + ['/MDd', |
| 47 |
'/nologo', |
| 48 |
'/ZI', |
| 49 |
'/Ob0', |
| 50 |
'/Od', |
| 51 |
'/c', |
| 52 |
'/Fo' + str(o_file), |
| 53 |
str(c_file)] |
| 54 |
subprocess.check_call(compile_cmd) |
| 55 |
out_file = pathlib.Path(outfile) |
| 56 |
if out_file.exists(): |
| 57 |
out_file.unlink() |
| 58 |
link_cmd = [static_linker, |
| 59 |
'/nologo', |
| 60 |
'/OUT:' + str(outfile), |
| 61 |
str(o_file)] |
| 62 |
subprocess.check_call(link_cmd) |
| 63 |
return 0 |
| 64 |
|
| 65 |
def generate_lib(outfiles, private_dir, compiler_array): |
| 66 |
private_dir = pathlib.Path(private_dir) |
| 67 |
if not private_dir.exists(): |
| 68 |
private_dir.mkdir() |
| 69 |
|
| 70 |
for i, content in enumerate(contents): |
| 71 |
c_file = private_dir / ('flob_' + str(i + 1) + '.c') |
| 72 |
c_file.write_text(content) |
| 73 |
outfile = outfiles[i] |
| 74 |
|
| 75 |
cl_found = False |
| 76 |
for cl_arg in compiler_array: |
| 77 |
if (cl_arg.endswith('cl') or cl_arg.endswith('cl.exe')) and 'clang-cl' not in cl_arg: |
| 78 |
ret = generate_lib_msvc(outfile, c_file, private_dir, compiler_array) |
| 79 |
if ret > 0: |
| 80 |
return ret |
| 81 |
else: |
| 82 |
cl_found = True |
| 83 |
break |
| 84 |
if not cl_found: |
| 85 |
ret = generate_lib_gnulike(outfile, c_file, private_dir, compiler_array) |
| 86 |
if ret > 0: |
| 87 |
return ret |
| 88 |
return 0 |
| 89 |
|
| 90 |
if __name__ == '__main__': |
| 91 |
options = parser.parse_args() |
| 92 |
sys.exit(generate_lib(options.o, options.private_dir, options.cmparr)) |