Merge pull request #5161 from TheQwertiest/feature/custom_target_link
Can link against custom_target[i]pull/5254/head
commit
7059c47aad
11 changed files with 336 additions and 12 deletions
@ -0,0 +1,90 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
import shutil, sys, subprocess, argparse, pathlib |
||||
|
||||
parser = argparse.ArgumentParser() |
||||
|
||||
parser.add_argument('--private-dir', required=True) |
||||
parser.add_argument('-o', nargs='+', required=True) |
||||
parser.add_argument('cmparr', nargs='+') |
||||
|
||||
contents = [''' |
||||
int flob() { |
||||
return 0; |
||||
} |
||||
''', ''' |
||||
int flob() { |
||||
return 1; |
||||
} |
||||
'''] |
||||
|
||||
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)] |
||||
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(outfiles, private_dir, compiler_array): |
||||
private_dir = pathlib.Path(private_dir) |
||||
if not private_dir.exists(): |
||||
private_dir.mkdir() |
||||
|
||||
for i, content in enumerate(contents): |
||||
c_file = private_dir / ('flob_' + str(i + 1) + '.c') |
||||
c_file.write_text(content) |
||||
outfile = outfiles[i] |
||||
|
||||
cl_found = False |
||||
for cl_arg in compiler_array: |
||||
if (cl_arg.endswith('cl') or cl_arg.endswith('cl.exe')) and 'clang-cl' not in cl_arg: |
||||
ret = generate_lib_msvc(outfile, c_file, private_dir, compiler_array) |
||||
if ret > 0: |
||||
return ret |
||||
else: |
||||
cl_found = True |
||||
break |
||||
if not cl_found: |
||||
ret = generate_lib_gnulike(outfile, c_file, private_dir, compiler_array) |
||||
if ret > 0: |
||||
return ret |
||||
return 0 |
||||
|
||||
if __name__ == '__main__': |
||||
options = parser.parse_args() |
||||
sys.exit(generate_lib(options.o, options.private_dir, options.cmparr)) |
@ -0,0 +1,37 @@ |
||||
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('generate_conflicting_stlibs.py') |
||||
|
||||
clib = custom_target('linkcustom', |
||||
output: ['libflob_1.a', 'libflob_2.a'], |
||||
command: [genprog, |
||||
'-o', '@OUTPUT@', |
||||
'--private-dir', '@PRIVATE_DIR@'] + cc.cmd_array()) |
||||
|
||||
clib_2 = clib[1] |
||||
|
||||
exe = executable('prog', 'prog.c', link_with: clib_2) |
||||
test('linkcustom', exe) |
||||
|
||||
d = declare_dependency(link_with: clib_2) |
||||
|
||||
exe2 = executable('prog2', 'prog.c', dependencies: d) |
||||
test('linkcustom2', exe2) |
||||
|
||||
# Link whole tests |
||||
|
||||
exe3 = executable('prog3', 'prog.c', link_whole: clib_2) |
||||
test('linkwhole', exe) |
||||
|
||||
d2 = declare_dependency(link_whole: clib_2) |
||||
|
||||
exe4 = executable('prog4', 'prog.c', dependencies: d2) |
||||
test('linkwhole2', exe2) |
@ -0,0 +1,5 @@ |
||||
int flob(); |
||||
|
||||
int main(int argc, char **argv) { |
||||
return (flob() == 1 ? 0 : 1); |
||||
} |
@ -0,0 +1,92 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
import shutil, sys, subprocess, argparse, pathlib |
||||
|
||||
parser = argparse.ArgumentParser() |
||||
|
||||
parser.add_argument('--private-dir', required=True) |
||||
parser.add_argument('-o', nargs='+', required=True) |
||||
parser.add_argument('cmparr', nargs='+') |
||||
|
||||
contents = ['''#include<stdio.h> |
||||
|
||||
void flob_1() { |
||||
printf("Now flobbing #1.\\n"); |
||||
} |
||||
''', '''#include<stdio.h> |
||||
|
||||
void flob_2() { |
||||
printf("Now flobbing #2.\\n"); |
||||
} |
||||
'''] |
||||
|
||||
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)] |
||||
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(outfiles, private_dir, compiler_array): |
||||
private_dir = pathlib.Path(private_dir) |
||||
if not private_dir.exists(): |
||||
private_dir.mkdir() |
||||
|
||||
for i, content in enumerate(contents): |
||||
c_file = private_dir / ('flob_' + str(i + 1) + '.c') |
||||
c_file.write_text(content) |
||||
outfile = outfiles[i] |
||||
|
||||
cl_found = False |
||||
for cl_arg in compiler_array: |
||||
if (cl_arg.endswith('cl') or cl_arg.endswith('cl.exe')) and 'clang-cl' not in cl_arg: |
||||
ret = generate_lib_msvc(outfile, c_file, private_dir, compiler_array) |
||||
if ret > 0: |
||||
return ret |
||||
else: |
||||
cl_found = True |
||||
break |
||||
if not cl_found: |
||||
ret = generate_lib_gnulike(outfile, c_file, private_dir, compiler_array) |
||||
if ret > 0: |
||||
return ret |
||||
return 0 |
||||
|
||||
if __name__ == '__main__': |
||||
options = parser.parse_args() |
||||
sys.exit(generate_lib(options.o, options.private_dir, options.cmparr)) |
@ -0,0 +1,37 @@ |
||||
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('generate_stlibs.py') |
||||
|
||||
clib = custom_target('linkcustom', |
||||
output: ['libflob_1.a', 'libflob_2.a'], |
||||
command: [genprog, |
||||
'-o', '@OUTPUT@', |
||||
'--private-dir', '@PRIVATE_DIR@'] + cc.cmd_array()) |
||||
|
||||
clibs = [clib[0], clib[1]] |
||||
|
||||
exe = executable('prog', 'prog.c', link_with: clibs) |
||||
test('linkcustom', exe) |
||||
|
||||
d = declare_dependency(link_with: clibs) |
||||
|
||||
exe2 = executable('prog2', 'prog.c', dependencies: d) |
||||
test('linkcustom2', exe2) |
||||
|
||||
# Link whole tests |
||||
|
||||
exe3 = executable('prog3', 'prog.c', link_whole: clibs) |
||||
test('linkwhole', exe) |
||||
|
||||
d2 = declare_dependency(link_whole: clibs) |
||||
|
||||
exe4 = executable('prog4', 'prog.c', dependencies: d2) |
||||
test('linkwhole2', exe2) |
@ -0,0 +1,8 @@ |
||||
void flob_1(); |
||||
void flob_2(); |
||||
|
||||
int main(int argc, char **argv) { |
||||
flob_1(); |
||||
flob_2(); |
||||
return 0; |
||||
} |
Loading…
Reference in new issue