-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
612 lines (468 loc) · 17.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import os
print "Import easymake"
############################################################
class Config(object):
def __init__(self,
buildconfig,
platform,
):
self.buildconfig = buildconfig
self.platform = platform
print "config=%s, platform=%s" %(buildconfig, platform)
############################################################
class Settings(object):
def __init__(self, config):
platform = config.platform
buildconfig = config.buildconfig
self.OBJPATH = "obj/%s-%s" % (platform, buildconfig)
self.LIBPATH = "lib/%s-%s" % (platform, buildconfig)
self.DLLPATH = "dll/%s-%s" % (platform, buildconfig)
self.APPPATH = "bin/%s-%s" % (platform, buildconfig)
self.sysincdirs = []
self.syslibpaths = []
self.syslibs = []
self.dllsyslibs = []
self.CXX = None
self.CXXFLAGS = []
self.CC = None
self.CFLAGS = []
self.AR = None
self.RANLIB = None
self.LIBTOOL = None
self.DLLTOOL = None
self.DLLFLAGS = []
self.LINK = None
self.LINKFLAGS = []
self.srcexts = ['c', 'cpp']
self.make_appfile = lambda c, s, name: s.APPPATH+"/"+name+".bin"
self.scons_make_objects = None
self.scons_make_staticlib = None
self.scons_make_dynamiclib = None
self.scons_make_application = None
# Settings for this platform
exec('import config_'+config.platform+ ' as _config')
_config._config(config, self)
############################################################
def _make_str_array(stringOrArray):
if isinstance(stringOrArray, str):
return [ stringOrArray ]
elif isinstance(stringOrArray, list):
return stringOrArray
elif stringOrArray is None:
return []
else:
raise "Object %s cannot be converted to array of string" % stringOrArray
############################################################
# Config is passed to the platform-specific config file, which fills
# out a settings objects.
# Functions:
# scons_make_objects
# scons_make_staticlib
# scons_make_dynamiclib
# Take args: (module, env, config, settings)
# Return: [ <targets> ]
############################################################
#
#
#
class NoModule(object):
def __init__(self):
pass
#
#
#
class Module(object):
# _name
# _srcdirs
# _incdirs
# _deps
# _fulldeps
def __init__(self, name,
srcdirs,
incdirs,
deps,
srcfiles,
srcexcludes,
incdirs_internal,
cxxflags,
ldflags):
self._name = name
if (srcfiles != []) and (srcdirs != []):
raise "Cannot use both srcdirs and srcfiles attributes"
self._srcdirs = _make_str_array(srcdirs)
self._incdirs = _make_str_array(incdirs)
self._srcfiles = _make_str_array(srcfiles)
self._srcexcludes = _make_str_array(srcexcludes)
self._incdirs_internal = _make_str_array(incdirs_internal)
self._cxxflags = _make_str_array(cxxflags)
self._ldflags = _make_str_array(ldflags)
#print "mod %s has ldflags: %s" % (name, self._ldflags)
# Deps get resolved later
self._deps = deps
# For now, keep these as None
self._fulldeps = None
self._objects = None
self._target = None
#
def _calcdeps(self):
if not self._fulldeps is None:
return
self._fulldeps = []
for dep in self._deps:
# Filter out any dummy modules
if isinstance(dep, NoModule):
continue
if isinstance(dep, str):
#print "dep is name: %s" % dep
depmod = allmodules[dep]
elif isinstance(dep, Module):
#print "dep is module: %s" % dep
depmod = dep
else:
raise "Unknown module %s" % dep
depmod._calcdeps()
# Add any dependencies of depmod. We always track from
# right to left, adding on the left. This keeps a given
# module to the left of (i.e. before) it's dependencies.
#print "Recursively adding dependencies of %s" % depmod._name
for depDep in reversed(depmod._fulldeps):
if not depDep in self._fulldeps:
self._fulldeps = [ depDep ] + self._fulldeps
# Add depmod itself
if not depmod in self._fulldeps:
self._fulldeps = [ depmod ] + self._fulldeps
#print "Module %s has deps: %s" %(self._name, [a._name for a in self._fulldeps])
#
def _defineobjects(self, env, config, settings):
# Use platform specific version if it exists
if not settings.scons_make_objects is None:
fn = settings.scons_make_objects
self._objects = fn(self, env, config, settings)
return self._objects
# Regular scons version. Collect src files, filtering out
# excludes.
allsrc = []
globres = []
for sd in self._srcdirs:
for e in settings.srcexts:
globres += env.Glob(sd + "/*."+e)
srcglob = []
for g in globres:
if not str(g) in self._srcexcludes:
srcglob += [ g ]
allsrc += srcglob
for sf in self._srcfiles:
if not sf in self._srcexcludes:
allsrc += env.Glob(sf)
srcS = [str(s) for s in allsrc]
#print "Mod %s: src: %s" %(self._name, srcS)
# Object directory
objectpath = settings.OBJPATH + "/" + self._name
# Full include paths for this module
includepaths = [] + settings.sysincdirs
#print "Just sys includes: %s" % includepaths
for d in self._fulldeps:
#print " Dep %s has includes: %s" % (d._name, d._incdirs)
includepaths += d._incdirs
includepaths += self._incdirs
includepaths += self._incdirs_internal
#print "Full includes: %s" % includepaths
# Flags
flags = [] + settings.CXXFLAGS
for d in self._fulldeps:
flags += d._cxxflags
flags += self._cxxflags
# Create the rules
objects = []
for src in allsrc:
s = str(src)
sbase = os.path.basename(s)
(objname, ext) = os.path.splitext(sbase)
obj=objectpath+"/"+objname
#print "Object: %s source: %s" % (obj, s)
objects += env.Object(source=s,
target=obj,
CPPPATH=includepaths,
CXXFLAGS=flags)
self._objects = objects
return objects
def _definescons(self, env, config, settings):
if not self._target is None:
return
# Make sure all deps targets are created
for d in self._fulldeps:
d._definescons(env, config, settings)
# Call subclass
self._target = self._do_definescons(env, config, settings)
# Set alias for the Module name
env.Alias(self._name, self._target)
def _do_post_definescons(self, env, config, settings):
pass
#
#
#
class Library(Module):
def __init__(self, name,
srcdirs=[],
incdirs=[],
deps=[],
srcfiles = [],
srcexcludes = [],
incdirs_internal = [],
cxxflags = [],
ldflags = [],
includedeps = False):
super(Library, self).__init__(name,
srcdirs,
incdirs,
deps,
srcfiles,
srcexcludes,
incdirs_internal,
cxxflags,
ldflags)
self._includedeps = includedeps
if name in libs:
print "Library %s already defined" % name
libs[name] = self
#
def _do_definescons(self, env, config, settings):
# Make sure objects have been defined for this module
objects = self._defineobjects(env, config, settings)
if self._includedeps:
for d in self._fulldeps:
objects += [ d._objects ]
# Custom static lib creation?
if not settings.scons_make_staticlib is None:
fn = settings.scons_make_staticlib
return fn(mod, env, config, settings)
# Not much to do for static libs
libfile = settings.LIBPATH+"/"+self._name
target = env.StaticLibrary(target=libfile,
source=objects)
return target
#
#
#
class DynamicLibrary(Module):
def __init__(self, name,
srcdirs=[],
incdirs=[],
deps=[],
srcfiles = [],
srcexcludes = [],
incdirs_internal = [],
cxxflags = [],
ldflags = []):
super(DynamicLibrary, self).__init__(name,
srcdirs,
incdirs,
deps,
srcfiles,
srcexcludes,
incdirs_internal,
cxxflags,
ldflags)
if name in dlls:
print "DynamicLibrary %s already defined" % name
dlls[name] = self
def _do_definescons(self, env, config, settings):
# Get object list
objects = self._defineobjects(env, config, settings)
# Platform-specific version?
if not settings.scons_make_dynamiclib is None:
fn = settings.scons_make_dynamiclib
return fn(self, env, config, settings)
# Standard version. Get all lib requirements
deplibs = []
depldflags = self._ldflags
for d in self._fulldeps:
deplibs += d._target
depldflags += d._ldflags
deplibs += settings.dllsyslibs
dllout = settings.DLLPATH + "/lib" + self._name + ".so"
syslibpaths = settings.syslibpaths
return env.SharedLibrary(target = dllout,
source = objects,
LIBS = deplibs,
LIBPATH = syslibpaths,
LINKFLAGS = settings.DLLFLAGS + depldflags)
############################################################
#
#
#
class Application(Module):
def __init__(self, name,
srcdirs=[],
incdirs=[],
deps=[],
srcfiles = [],
srcexcludes = [],
incdirs_internal = [],
cxxflags = [],
ldflags = [],
cwd = ".",
args = []):
super(Application, self).__init__(name,
srcdirs,
incdirs,
deps,
srcfiles,
srcexcludes,
incdirs_internal,
cxxflags,
ldflags)
self._cwd = cwd
self._args = args
if name in apps:
print "Application %s already defined" % name
apps[name] = self
def _do_definescons(self, env, config, settings):
# Get object list
objects = self._defineobjects(env, config, settings)
# Platform-specific version?
if not settings.scons_make_application is None:
fn = settings.scons_make_application
return fn(self, env, config, settings)
# Standard version. Get all lib requirements
deplibs = []
depldflags = self._ldflags
for d in self._fulldeps:
deplibs += d._target
depldflags += d._ldflags
deplibs += settings.syslibs
appout = settings.make_appfile(config, settings, self._name)
syslibpaths = settings.syslibpaths
prog = env.Program(target = appout,
source = objects,
LIBS = deplibs,
LIBPATH = syslibpaths,
LINKFLAGS = depldflags + settings.LINKFLAGS)
# Define '<name>_run' as a secondary alias
cmd = ""
if self._cwd != ".":
cmd += "cd "+self._cwd+" && "
cmd += os.path.abspath(str(prog[0]))
cmd += " ".join(self._args)
self._run = env.AlwaysBuild(env.Alias(self._name+"_run", prog, cmd))
return prog
############################################################
tests = []
testtarget = None
class Test(Application):
def __init__(self, name,
srcdirs=[],
incdirs=[],
deps=[],
srcfiles = [],
srcexcludes = [],
incdirs_internal = [],
cxxflags = [],
ldflags = [],
cwd = ".",
args = []):
super(Test, self).__init__(name,
srcdirs,
incdirs,
deps,
srcfiles,
srcexcludes,
incdirs_internal,
cxxflags,
ldflags)
tests.append(self)
def _do_definescons(self, env, config, settings):
Application._do_definescons(self, env, config, settings)
def _do_post_definescons(self, env, config, settings):
global testtarget
if testtarget is None:
print "Defining 'tests' ..."
testtarget = env.AlwaysBuild(env.Alias("tests", [t._run for t in tests]))
############################################################
class ExternalCommand(Module):
def __init__(self, name, output,
command,
deps=[],
srcfiles=[],
):
super(ExternalCommand, self).__init__(name,
[],
[],
deps,
[],
[],
[],
[],
[])
self._cmdsrc = _make_str_array(srcfiles)
self._cmd = command
self._output = _make_str_array(output)
externalcommands[name] = self
def _do_definescons(self, env, config, settings):
# Require ALL dependency targets to be built before running
# the command
mydeps = []
for d in self._fulldeps:
#print "%s" % d._name
mydeps += d._target
#print "Command %s deps: %s" % (self._name, [str(i) for i in mydeps])
if len(self._output) == 0:
target = env.AlwaysBuild(env.Alias(self._name+"always", self._cmdsrc+mydeps, self._cmd))
else:
target = env.Command(self._output, self._cmdsrc+mydeps, self._cmd)
env.Depends(target, mydeps)
return target
############################################################
def add_default(env, mod):
env.Default(mod._target)
############################################################
libs = {}
dlls = {}
apps = {}
externalcommands = {}
allmodules = {}
############################################################
############################################################
# Build
############################################################
def setenv(env, name, value):
if value is None:
return
if isinstance(value, list) and len(value) == 0:
return
#print "setting env['%s'] = %s" % (name, value)
env[name] = value
def build(env, config, settings = None):
if settings is None:
settings = Settings(config)
# Set up the env
setenv(env, 'CXX', settings.CXX)
setenv(env, 'CXXFLAGS', settings.CXXFLAGS)
setenv(env, 'CC', settings.CC)
setenv(env, 'CFLAGS', settings.CFLAGS)
setenv(env, 'AR', settings.AR)
setenv(env, 'RANLIB', settings.RANLIB)
setenv(env, 'LIBTOOL', settings.LIBTOOL)
setenv(env, 'LINK', settings.LINK)
setenv(env, 'LINKFLAGS', settings.LINKFLAGS)
# List of all modules
for m in libs:
allmodules[m] = libs[m]
for m in dlls:
allmodules[m] = dlls[m]
for m in apps:
allmodules[m] = apps[m]
for m in externalcommands:
allmodules[m] = externalcommands[m]
# Calculate full dependencies
for m in allmodules:
mod = allmodules[m]
mod._calcdeps()
# Define Scons rules
for m in allmodules:
mod = allmodules[m]
mod._definescons(env, config, settings)
# Post-define rules
for m in allmodules:
mod = allmodules[m]
mod._do_post_definescons(env, config, settings)