5 Compile whole groups of C/C++ files at once
6 (C and C++ files are processed independently though).
11 opt.load('compiler_cxx')
13 bld.load('compiler_cxx unity')
15 To enable for specific task generators only::
18 bld(features='c cprogram unity', source='main.c', ...)
20 The file order is often significant in such builds, so it can be
21 necessary to adjust the order of source files and the batch sizes.
22 To control the amount of files processed in a batch per target
26 bld(features='c cprogram', unity_size=20)
30 from waflib
import Task
, Options
31 from waflib
.Tools
import c_preproc
32 from waflib
import TaskGen
37 EXTS_CXX
= ('.cpp','.cc','.cxx','.C','.c++')
41 opt
.add_option('--batchsize', action
='store', dest
='batchsize', type='int', default
=MAX_BATCH
,
42 help='default unity batch size (0 disables unity builds)')
44 @TaskGen.taskgen_method
46 default
= getattr(Options
.options
, 'batchsize', MAX_BATCH
)
49 return getattr(self
, 'unity_size', default
)
52 class unity(Task
.Task
):
55 def to_include(self
, node
):
56 ret
= node
.path_from(self
.outputs
[0].parent
)
57 ret
= ret
.replace('\\', '\\\\').replace('"', '\\"')
60 lst
= ['#include "%s"\n' % self
.to_include(node
) for node
in self
.inputs
]
62 self
.outputs
[0].write(txt
)
64 node
= self
.outputs
[0]
65 return node
.path_from(node
.ctx
.launch_node())
67 def bind_unity(obj
, cls_name
, exts
):
68 if not 'mappings' in obj
.__dict
__:
69 obj
.mappings
= dict(obj
.mappings
)
73 if fun
.__name
__ == 'unity_fun':
74 raise ValueError('Attempt to bind unity mappings multiple times %r' % j
)
76 def unity_fun(self
, node
):
77 cnt
= self
.batch_size()
79 return fun(self
, node
)
80 x
= getattr(self
, 'master_%s' % cls_name
, None)
81 if not x
or len(x
.inputs
) >= cnt
:
82 x
= self
.create_task('unity')
83 setattr(self
, 'master_%s' % cls_name
, x
)
85 cnt_cur
= getattr(self
, 'cnt_%s' % cls_name
, 0)
86 c_node
= node
.parent
.find_or_declare('unity_%s_%d_%d.%s' % (self
.idx
, cnt_cur
, cnt
, cls_name
))
88 setattr(self
, 'cnt_%s' % cls_name
, cnt_cur
+ 1)
92 obj
.mappings
[j
] = unity_fun
94 @TaskGen.feature('unity')
95 @TaskGen.before('process_source')
96 def single_unity(self
):
97 lst
= self
.to_list(self
.features
)
99 bind_unity(self
, 'c', EXTS_C
)
101 bind_unity(self
, 'cxx', EXTS_CXX
)
105 bind_unity(TaskGen
.task_gen
, 'c', EXTS_C
)
107 bind_unity(TaskGen
.task_gen
, 'cxx', EXTS_CXX
)