commit
5905533fcd
11 changed files with 179 additions and 61 deletions
@ -0,0 +1,16 @@ |
||||
## Can link against custom targets |
||||
|
||||
The output of `custom_target` can be used in `link_with` and |
||||
`link_whole` keyword arguments. This is useful for integrating custom |
||||
code generator steps, but note that there are many limitations: |
||||
|
||||
- Meson can not know about link dependencies of the custom target. If |
||||
the target requires further link libraries, you need to add them manually |
||||
|
||||
- The user is responsible for ensuring that the code produced by |
||||
different toolchains are compatible. |
||||
|
||||
- The custom target can only have one output file. |
||||
|
||||
- The output file must have the correct file name extension. |
||||
|
@ -0,0 +1,73 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
import shutil, sys, subprocess, argparse, pathlib |
||||
|
||||
parser = argparse.ArgumentParser() |
||||
|
||||
parser.add_argument('--private-dir', required=True) |
||||
parser.add_argument('-o', required=True) |
||||
parser.add_argument('cmparr', nargs='+') |
||||
|
||||
contents = '''#include<stdio.h> |
||||
|
||||
void flob() { |
||||
printf("Now flobbing.\\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(outfile, private_dir, compiler_array): |
||||
private_dir = pathlib.Path(private_dir) |
||||
if not private_dir.exists(): |
||||
private_dir.mkdir() |
||||
c_file = private_dir / 'flob.c' |
||||
c_file.write_text(contents) |
||||
for i in compiler_array: |
||||
if (i.endswith('cl') or i.endswith('cl.exe')) and 'clang-cl' not in i: |
||||
return generate_lib_msvc(outfile, c_file, private_dir, compiler_array) |
||||
return generate_lib_gnulike(outfile, c_file, private_dir, compiler_array) |
||||
|
||||
if __name__ == '__main__': |
||||
options = parser.parse_args() |
||||
sys.exit(generate_lib(options.o, options.private_dir, options.cmparr)) |
@ -0,0 +1,35 @@ |
||||
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('custom_stlib.py') |
||||
|
||||
clib = custom_target('linkcustom', |
||||
output: 'libflob.a', |
||||
command: [genprog, |
||||
'-o', '@OUTPUT@', |
||||
'--private-dir', '@PRIVATE_DIR@'] + cc.cmd_array()) |
||||
|
||||
exe = executable('prog', 'prog.c', link_with: clib) |
||||
test('linkcustom', exe) |
||||
|
||||
d = declare_dependency(link_with: clib) |
||||
|
||||
exe2 = executable('prog2', 'prog.c', dependencies: d) |
||||
test('linkcustom2', exe2) |
||||
|
||||
# Link whole tests |
||||
|
||||
exe3 = executable('prog3', 'prog.c', link_whole: clib) |
||||
test('linkwhole', exe) |
||||
|
||||
d2 = declare_dependency(link_whole: clib) |
||||
|
||||
exe4 = executable('prog4', 'prog.c', dependencies: d2) |
||||
test('linkwhole2', exe2) |
@ -0,0 +1,6 @@ |
||||
void flob(); |
||||
|
||||
int main(int argc, char **argv) { |
||||
flob(); |
||||
return 0; |
||||
} |
@ -1,5 +0,0 @@ |
||||
int func_in_foo(); |
||||
|
||||
int main(int argc, char **argv) { |
||||
return func_in_foo(); |
||||
} |
@ -1,3 +0,0 @@ |
||||
int func_in_foo() { |
||||
return 0; |
||||
} |
@ -1,24 +0,0 @@ |
||||
#!/usr/bin/env python3 |
||||
|
||||
# Mimic a binary that generates a static library |
||||
|
||||
import os |
||||
import subprocess |
||||
import sys |
||||
|
||||
if __name__ == '__main__': |
||||
if len(sys.argv) != 4: |
||||
print(sys.argv[0], 'compiler input_file output_file') |
||||
sys.exit(1) |
||||
compiler = sys.argv[1] |
||||
ifile = sys.argv[2] |
||||
ofile = sys.argv[3] |
||||
tmp = ifile + '.o' |
||||
if compiler.endswith('cl'): |
||||
subprocess.check_call([compiler, '/nologo', '/MDd', '/Fo' + tmp, '/c', ifile]) |
||||
subprocess.check_call(['lib', '/nologo', '/OUT:' + ofile, tmp]) |
||||
else: |
||||
subprocess.check_call([compiler, '-c', ifile, '-o', tmp]) |
||||
subprocess.check_call(['ar', 'csr', ofile, tmp]) |
||||
|
||||
os.unlink(tmp) |
@ -1,23 +0,0 @@ |
||||
project('link_with custom target', ['c']) |
||||
|
||||
# |
||||
# libraries created by a custom_target currently can be used in sources: (see |
||||
# common/100 manygen/ for an example of that), but not in link_with: |
||||
# |
||||
|
||||
lib_generator = find_program('lib_generator.py') |
||||
|
||||
cc = meson.get_compiler('c').cmd_array().get(-1) |
||||
|
||||
libfoo_target = custom_target( |
||||
'libfoo', |
||||
input: ['foo.c'], |
||||
output: ['libfoo.a'], |
||||
command: [lib_generator, cc, '@INPUT@', '@OUTPUT@'] |
||||
) |
||||
|
||||
libfoo = declare_dependency( |
||||
link_with: libfoo_target, |
||||
) |
||||
|
||||
executable('demo', ['demo.c'], dependencies: [libfoo]) |
Loading…
Reference in new issue