wscript (2522B)
1 #! /usr/bin/env python 2 # encoding: utf-8 3 # Thomas Nagy, 2013 (ita) 4 5 VERSION='0.0.1' 6 APPNAME='dynamic_build3' 7 8 """ 9 An advanced dynamic build simulating a call to an external system. 10 11 That external build system produces a library which is then used in the current build. 12 """ 13 14 import os, shutil, sys 15 from waflib import Build, Errors, Logs 16 17 top = '.' 18 out = 'build' 19 20 def options(opt): 21 opt.load('compiler_c') 22 23 def configure(conf): 24 conf.load('compiler_c') 25 26 def build(bld): 27 bld.post_mode = Build.POST_LAZY 28 29 # declare the temporary build directory for the external library 30 # it is best to keep it under the project build directory 31 tmp_dir = bld.bldnode.make_node('external_lib') 32 33 # build the external library through an external process 34 bld(rule=some_fun, target=tmp_dir.make_node('flag.lock')) 35 36 # once it is done create a second build group 37 bld.add_group() 38 39 # read the library 40 bld.read_shlib('foo', paths=[tmp_dir], export_includes=[tmp_dir], export_defines=['A=1']) 41 42 # use this library for a target 43 # no additional build group needed since "app" will wait on "foo" through the use= system 44 bld.program(source='main.c', target='app', use='foo') 45 46 # ----------------------------------------------------------------------------------------- 47 # the following is a pointless exercise simulating the execution of an external buildsystem 48 # do not spend too much time on it :-) 49 50 SNIP = """ 51 top = '.' 52 out = '.' 53 def options(opt): 54 opt.load('compiler_c') 55 def configure(conf): 56 conf.load('compiler_c') 57 def build(bld): 58 bld.shlib(source='external.c', target='foo', includes='.') 59 """ 60 61 def some_fun(task): 62 # first, clean everything 63 output_dir = task.outputs[0].parent 64 shutil.rmtree(output_dir.abspath()) 65 os.makedirs(output_dir.abspath()) 66 67 # we have a clean directory, create a fake project in it 68 h_node = output_dir.make_node('external.h') 69 h_node.write('int zero();\n', flags='w') 70 71 c_node = output_dir.make_node('external.c') 72 c_node.write('int zero() { return 0; }\n', flags='w') 73 74 w_node = output_dir.make_node('wscript') 75 w_node.write(SNIP) 76 77 cmd = [sys.executable, sys.argv[0], 'configure', 'build'] 78 cwd = output_dir.abspath() 79 80 try: 81 task.generator.bld.cmd_and_log(cmd, cwd=cwd, quiet=0, output=0) 82 except Errors.WafError as e: 83 try: 84 print(e.stderr) 85 except AttributeError: 86 pass 87 Logs.error("Build of the external library failed") 88 return -1 89 90 Logs.info(' (the external library has been compiled)') 91 92 # write a lock file so that a rebuild occurs if files are removed manually 93 task.outputs[0].write('ok') 94