forked from facebookarchive/caffe2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brewery.py
697 lines (619 loc) · 23.7 KB
/
brewery.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import cPickle as pickle
from collections import defaultdict
import multiprocessing
import glob
import hashlib
import os
import shlex
import shutil
import subprocess
import sys
import tempfile
import traceback
from build_env import Env
class Colors(object):
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def BuildDebug(message, *args):
# Note(Yangqing): if you want to know detailed message about the build,
# uncomment the following line.
print Colors.OKBLUE + 'DEBUG:', message % args, Colors.ENDC
return
def BuildLog(message, *args):
print Colors.OKGREEN + 'LOG:', message % args, Colors.ENDC
def BuildWarning(message, *args):
print Colors.WARNING + 'WARNING:', message % args, Colors.ENDC
def BuildFatal(message, *args):
print Colors.FAIL + 'FATAL:', message % args, Colors.ENDC
print Colors.FAIL + 'Build exiting.' + Colors.ENDC
Brewery.Finalize()
sys.exit(1)
def BuildFatalIf(command, message, *args):
if command:
BuildFatal(message, *args)
_single_command_env = os.environ
if 'PYTHONPATH' not in _single_command_env:
_single_command_env['PYTHONPATH'] = ''
_single_command_env['PYTHONPATH'] = (
Env.GENDIR + ':' + _single_command_env['PYTHONPATH'])
def RunSingleCommand(command):
BuildDebug(command)
try:
proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, env=_single_command_env)
stdout, _ = proc.communicate()
if proc.returncode:
print stdout
return proc.returncode
except: # all exceptions caught here.
e = sys.exc_info()[0]
return str(e)
def Glob(patterns):
"""Globs all files with the given patterns, relative to the path of the BREW
file."""
files = []
if type(patterns) is str:
patterns = [patterns]
for pattern in patterns:
full_pattern = os.path.join(Brewery.CWD, pattern)
files += glob.glob(full_pattern)
prefix_len = len(Brewery.CWD) + 1
return [f[prefix_len:] for f in files if os.path.isfile(f)]
def RectifyFileName(name):
"""Rectifies a build file name to its absolute name."""
if name.startswith("//"):
# Simply replace the "//" with the root folder.
out_name = name[2:]
else:
# Add the current working directory.
out_name = os.path.join(Brewery.CWD, name)
# check if the name exists.
BuildFatalIf(not os.path.exists(out_name), 'Cannot find file %s' % out_name)
return out_name
def RectifyFileNames(names):
return [RectifyFileName(n) for n in sorted(names)]
def RectifyTarget(name):
"""Rectifies a build target name."""
if name.startswith("//"):
return name
elif name.startswith(":"):
return Brewery.TARGET_PREFIX + name
else:
if Brewery.TARGET_PREFIX == '//':
return Brewery.TARGET_PREFIX + name
return Brewery.TARGET_PREFIX + ":" + name
def RectifyTargets(names):
return [RectifyTarget(n) for n in sorted(names)]
def MakeGenDirs(rectified_srcs):
for src in rectified_srcs:
dst = os.path.join(Env.GENDIR, src)
try:
os.makedirs(os.path.dirname(dst))
except OSError as e:
pass
def CopyToGenDir(rectified_srcs):
MakeGenDirs(rectified_srcs)
for src in rectified_srcs:
shutil.copyfile(src, GenFilename(src))
def GenFilename(name, new_ext=None, original_ext=None):
if new_ext:
if original_ext:
new_name = name[:name.rfind(original_ext)] + new_ext
else:
new_name = name[:name.rfind('.') + 1] + new_ext
else:
new_name = name
return os.path.join(Env.GENDIR, new_name)
def MergeOrderedObjs(dep_lists):
added = set()
output = []
for dep_list in dep_lists:
for item in dep_list[::-1]:
if item not in added:
added.add(item)
output.insert(0, item)
return output
class Brewery(object):
# Targets store the dictionary from the target name to the build objects.
_targets = dict()
# Success stores whether a target is successfully built.
_success = defaultdict(bool)
# deps_map is a dictionary mapping each target to its dependents.
_deps_map = dict()
# signature_map is the map that stores the signatures for build targets.
_signatures = defaultdict(str)
_signature_filename = 'brewery.signature'
# Pool is the compute pool that one can use to run a list of commands in
# parallel.
Pool = multiprocessing.Pool(Env.CPUS * 2)
#Pool = multiprocessing.Pool(1)
CWD = ''
TARGET_PREFIX = '//'
TMPDIR = ''
def __init__(self):
"""Brewery is a singleton and should not be instantiated."""
raise NotImplementedError(
'Build system error: there shall only be one brewery.')
@classmethod
def InitBrewery(cls):
"""Initializes the brewery, e.g. loads the signatures currently built."""
try:
os.makedirs(Env.GENDIR)
except OSError as e:
pass
cls.TMPDIR = tempfile.mkdtemp()
if os.path.exists(os.path.join(Env.GENDIR, cls._signature_filename)):
BuildDebug('Loading the signature file.')
cls._signatures = pickle.load(
open(os.path.join(Env.GENDIR, cls._signature_filename)))
cls.FindAndParseBuildFiles()
@classmethod
def Finalize(cls):
"""Finalizes the brew process."""
if os.path.exists(Env.GENDIR):
BuildDebug('Saving the signature file.')
pickle.dump(cls._signatures,
open(os.path.join(Env.GENDIR, cls._signature_filename), 'w'))
else:
BuildDebug('No gendir present. Exiting.')
shutil.rmtree(cls.TMPDIR)
@classmethod
def Get(cls, name):
return cls._targets[name]
@classmethod
def FindAndParseBuildFiles(cls):
"""Find and parse all the BREW files in the subfolders."""
build_files = [os.path.join(d[2:], f)
for (d, _, files) in os.walk('.') if not d.startswith(Env.GENDIR)
for f in files if f.endswith('BREW')]
for build_file in build_files:
# Set the current working directory of the environment, and parse the build
# file.
BuildDebug("Parsing %s" % build_file)
cls.SetCwd(os.path.dirname(build_file))
execfile(build_file)
cls.SetCwd('')
return
@classmethod
def SetCwd(cls, cwd):
if cwd and not os.path.isdir(cwd):
# cwd should either be empty, or is a directory.
raise RuntimeError('Setting an invalid cwd: %s' % cwd)
cls.CWD = cwd
cls.TARGET_PREFIX = '//' + cwd
@classmethod
def RunInParallel(cls, commands):
fail = any(cls.Pool.map(RunSingleCommand, commands))
sys.stdout.flush()
if fail:
BuildWarning('Command failed.')
return not fail
@classmethod
def Register(cls, name, target):
BuildFatalIf(name in cls._targets,
"%s already in build target.", name)
BuildDebug("Registered build target %s, deps %s", name, str(target.deps))
cls._targets[name] = target
cls._deps_map[name] = target.deps + target.optional_deps
@classmethod
def _GetExecutionChain(cls, targets):
"""Gets the execution chain."""
# First, verify all dependencies.
for t in cls._targets:
for d in cls._deps_map[t]:
BuildFatalIf(d not in cls._targets,
"Dependency %s for target %s does not exist.", d, t)
if len(targets) == 0:
targets = cls._targets
else:
# Get all targets that we need to build.
seen_targets = set(targets)
idx = 0
while idx < len(targets):
for d in cls._deps_map[targets[idx]]:
if d not in seen_targets:
seen_targets.add(d)
targets.append(d)
idx += 1
# Now, create a topological order.
inverse_deps_map = defaultdict(list)
# Get the graph of all targets
for t in targets:
for d in cls._deps_map[t]:
inverse_deps_map[d].append(t)
deps_count = dict((t, len(cls._deps_map[t])) for t in targets)
#BuildDebug("deps count: %s", str(deps_count))
frontier = set(t for t in deps_count if deps_count[t] == 0)
build_order = []
while frontier:
current = frontier.pop()
#BuildDebug("processing %s", current)
build_order.append(current)
for t in inverse_deps_map[current]:
deps_count[t] -= 1
if deps_count[t] == 0:
#BuildDebug('Add to frontier: %s', t)
frontier.add(t)
# If this does not cover all targets, the graph is not a DAG.
BuildFatalIf(len(build_order) != len(targets),
"There are cycles in the dependency graph!")
BuildDebug('Build order: %s', str(build_order))
return build_order
@classmethod
def Signature(cls, target):
# Returns the builtsignature of the current target.
return cls._signatures[target]
@classmethod
def Success(cls, target):
return cls._success[target]
@classmethod
def ClearSignature(cls, including_third_party=False):
if including_third_party:
cls._signatures = defaultdict(str)
else:
keys = cls._signatures.keys()
for k in keys:
if not k.startswith('//third_party'):
del cls._signatures[k]
@classmethod
def Build(cls, targets):
"""Build all the targets, using their topological order."""
BuildDebug("Start building.")
build_order = cls._GetExecutionChain(targets)
for t in build_order:
BuildLog("Building %s", t)
cls._success[t], changed, new_signature = (
cls._targets[t].SetUpAndBuild(cls._signatures[t]))
if cls._success[t]:
cls._signatures[t] = new_signature
# Finally, print a summary of the build results.
succeeded = [key for key in cls._success if cls._success[key]]
BuildDebug("Successfully built %d targets." % len(succeeded))
#for key in cls._success:
# if cls._success[key]:
# BuildDebug(key)
failed = [key for key in cls._success if not cls._success[key]]
if len(failed) > 0:
BuildWarning("Failed to build:")
for key in failed:
BuildWarning(key)
@classmethod
def Draw(cls):
import pydot
graph = pydot.Dot("brewery", rankdir="LR")
nodes = {}
node_style = {'shape': 'box', 'color': '#0F9D58', 'style': 'filled',
'fontcolor': '#FFFFFF'}
for target_name in cls._targets:
nodes[target_name] = pydot.Node('"' + target_name + '"', **node_style)
graph.add_node(nodes[target_name])
for target_name in cls._deps_map:
for dep_name in cls._deps_map[target_name]:
graph.add_edge(pydot.Edge(nodes[dep_name], nodes[target_name]))
graph.write(graph.get_name() + '.dot', format='raw')
with open(graph.get_name() + '.pdf', 'w') as fid:
subprocess.call(['dot', '-Tpdf', graph.get_name() + '.dot'], stdout=fid)
class BuildTarget(object):
"""A build target that can be executed with the Build() function."""
def __init__(self, name, srcs, other_files=[], deps=[], optional_deps=[]):
self.name = RectifyTarget(name)
self.srcs = RectifyFileNames(srcs)
self.files = sorted(self.srcs + other_files)
self.deps = sorted(RectifyTargets(deps))
self.optional_deps = sorted(RectifyTargets(optional_deps))
self.command_groups = []
Brewery.Register(self.name, self)
def GetSignature(self):
"""Generate the signature of the build object."""
src_digest = ''.join([hashlib.sha256(open(f, 'rb').read()).hexdigest()
for f in self.files])
dep_digest = ''.join([Brewery.Signature(d) for d in self.deps])
return hashlib.sha256(src_digest + dep_digest).hexdigest()
def SetUpAndBuild(self, built_signature):
# Add successful optional dependencies into deps.
self.deps += [dep for dep in self.optional_deps
if Brewery.Success(dep)]
self.SetUp()
signature = self.GetSignature()
if not all(Brewery.Success(d) for d in self.deps):
BuildWarning("Not all dependencies have succeeded. Skipping build. "
"Failed dependencies: ")
BuildWarning(str([d for d in self.deps if not Brewery.Success(d)]))
return False, True, signature
if signature != built_signature:
success = self.Build()
return success, True, signature
return True, False, signature
def SetUp(self):
"""Set up the build object's variables.
This will always run even if the target has already been built. Anything
that further dependencies will need should be implemented here.
If your target just emits a set of shell commands, in SetUp() you can set
self.command_groups and use the default Build function, which basically
sends the command groups to a execution pool.
"""
BuildFatal('Not implemented.')
def Build(self):
"""Builds the target."""
success = True
for command_group in self.command_groups:
success &= Brewery.RunInParallel(command_group)
if not success:
return False
return True
class proto_library(BuildTarget):
"""Builds a protobuffer library.
A protobuffer library builds a set of protobuffer source files to its cc and
python source files, as well as the static library named "libname.a".
"""
def __init__(self, name, srcs, deps=[], **kwargs):
BuildTarget.__init__(self, name, srcs, deps=deps, **kwargs)
def SetUp(self):
MakeGenDirs(self.srcs)
# proto_library depends on protoc, so it would need to add that to the
# includes folder.
pbcc_files = [GenFilename(filename, 'pb.cc') for filename in self.srcs]
pbo_files = [GenFilename(filename, 'pb.o') for filename in self.srcs]
proto_commands = [
' '.join([Env.PROTOC_BINARY, '-I.', '--cpp_out', Env.GENDIR,
'--python_out', Env.GENDIR, filename])
for filename in self.srcs]
cpp_commands = [
' '.join([Env.CC, Env.CFLAGS, Env.INCLUDES, '-c', pbcc, '-o', pbo])
for pbcc, pbo in zip(pbcc_files, pbo_files)]
self.cc_obj_files = pbo_files
self.cc_obj_files += MergeOrderedObjs(
[Brewery.Get(dep).cc_obj_files for dep in self.deps])
self.command_groups = [proto_commands, cpp_commands]
class cc_target(BuildTarget):
def __init__(self, name, srcs, hdrs=[], deps=[], cflags=[], external_libs=[],
build_binary=False, is_test=False, whole_archive=False,
shared=False, **kwargs):
self.hdrs = RectifyFileNames(hdrs)
self.cflags = cflags
self.external_libs = [
'-l' + s if not s.startswith('-') else s for s in external_libs]
self.build_binary = build_binary
self.is_test = is_test
self.whole_archive = whole_archive
self.shared = shared
BuildTarget.__init__(self, name, srcs, other_files=self.hdrs, deps=deps, **kwargs)
def OutputName(self, is_library=False, is_shared=False):
if len(self.srcs) == 0:
# This is just a collection of dependencies, so we will not need
# any output file. Returning an empty string.
return ''
name_split = self.name.split(':')
if is_library:
if is_shared:
return os.path.join(
Env.GENDIR, name_split[0][2:],
'lib' + name_split[1] + Env.SHARED_LIB_EXT)
else:
return os.path.join(
Env.GENDIR, name_split[0][2:], 'lib' + name_split[1] + '.a')
else:
return os.path.join(Env.GENDIR, name_split[0][2:], name_split[1])
def SetUp(self):
MakeGenDirs(self.srcs)
CopyToGenDir(self.hdrs)
archive_file = self.OutputName(is_library=True)
self.cc_obj_files = MergeOrderedObjs(
[Brewery.Get(dep).cc_obj_files for dep in self.deps] +
[self.external_libs])
if self.whole_archive:
self.cc_obj_files.insert(0, Env.WHOLE_ARCHIVE_TEMPLATE % archive_file)
else:
self.cc_obj_files.insert(0, archive_file)
if len(self.srcs) == 0:
# There is nothing to build if there is no source files.
self.command_groups = []
else:
obj_files = [GenFilename(src, 'o') for src in self.srcs]
cpp_commands = [
' '.join([Env.CC, Env.CFLAGS, Env.INCLUDES, ' '.join(self.cflags),
'-c', src, '-o', obj])
for src, obj in zip(self.srcs, obj_files)]
# Create the archive
link_commands = [
' '.join([Env.LINK_STATIC, archive_file] + obj_files)]
if self.build_binary:
link_binary_commands = [
' '.join([Env.LINK_BINARY, self.OutputName()] + self.cc_obj_files +
[Env.LINKFLAGS])]
self.command_groups = [cpp_commands, link_commands, link_binary_commands]
elif self.shared:
link_shared_commands = [' '.join(
[Env.LINK_SHARED, self.OutputName(is_library=True, is_shared=True)]
+ obj_files + self.cc_obj_files[1:] + [Env.LINKFLAGS])]
self.command_groups = [cpp_commands, link_commands, link_shared_commands]
else:
self.command_groups = [cpp_commands, link_commands]
if self.is_test:
# Add test command
self.command_groups.append([
' '.join([self.OutputName(), '--caffe_test_root',
os.path.abspath(Env.GENDIR),
'--gtest_filter=-*.LARGE_*'])])
def cc_library(*args, **kwargs):
return cc_target(*args, **kwargs)
def cc_binary(*args, **kwargs):
return cc_target(*args, build_binary=True, **kwargs)
def cc_test(*args, **kwargs):
if 'cflags' not in kwargs:
kwargs['cflags'] = []
kwargs['cflags'].append("-DGTEST_USE_OWN_TR1_TUPLE=1")
return cc_target(
*args, build_binary=True, is_test=True, whole_archive=True, **kwargs)
class mpi_test(cc_target):
def __init__(self, *args, **kwargs):
if 'cflags' not in kwargs:
kwargs['cflags'] = []
kwargs['cflags'].append("-DGTEST_USE_OWN_TR1_TUPLE=1")
kwargs['build_binary'] = True
kwargs['is_test'] = True
kwargs['whole_archive'] = True
cc_target.__init__(self, *args, **kwargs)
self.mpi_size = kwargs['mpi_size'] if 'mpi_size' in kwargs else 4
def SetUp(self):
cc_target.SetUp(self)
self.command_groups.append([
' '.join(['mpirun -n', str(self.mpi_size), self.OutputName(),
'--caffe_test_root', os.path.abspath(Env.GENDIR),
'--gtest_filter=-*.LARGE_*'])])
class cuda_library(BuildTarget):
def __init__(self, name, srcs, hdrs=[], deps=[], cflags=[],
whole_archive=False, **kwargs):
self.hdrs = RectifyFileNames(hdrs)
self.cflags = cflags
self.whole_archive = whole_archive
BuildTarget.__init__(self, name, srcs, other_files=self.hdrs, deps=deps, **kwargs)
def OutputName(self, is_library=False):
name_split = self.name.split(':')
if is_library:
return os.path.join(
Env.GENDIR, name_split[0][2:], 'lib' + name_split[1] + '.a')
else:
return os.path.join(Env.GENDIR, name_split[0][2:], name_split[1])
def SetUp(self):
MakeGenDirs(self.srcs)
CopyToGenDir(self.hdrs)
obj_files = [GenFilename(src, 'cuo') for src in self.srcs]
cpp_commands = [
' '.join([Env.NVCC, Env.NVCC_CFLAGS, Env.INCLUDES,
' '.join(self.cflags), '-c', src, '-o', obj])
for src, obj in zip(self.srcs, obj_files)]
archive_file = self.OutputName(is_library=True)
# Create the archive
link_commands = [
' '.join([Env.LINK_STATIC, archive_file]
+ obj_files)]
if self.whole_archive:
archive_file = Env.WHOLE_ARCHIVE_TEMPLATE % archive_file
self.cc_obj_files = MergeOrderedObjs(
[Brewery.Get(dep).cc_obj_files for dep in self.deps])
# We will need to add nvidia link targets as well
self.cc_obj_files.append(Env.NVCC_LINKS)
self.cc_obj_files.insert(0, archive_file)
self.command_groups = [cpp_commands, link_commands]
class filegroup(BuildTarget):
def __init__(self, name, srcs, deps=[], **kwargs):
self.cc_obj_files = []
BuildTarget.__init__(self, name, srcs, deps=deps, **kwargs)
def SetUp(self):
CopyToGenDir(self.srcs)
def py_library(*args, **kwargs):
return filegroup(*args, **kwargs)
def cc_headers(*args, **kwargs):
return filegroup(*args, **kwargs)
class py_test(BuildTarget):
def __init__(self, name, srcs, deps=[], **kwargs):
self.cc_obj_files = []
BuildTarget.__init__(self, name, srcs, deps=deps, **kwargs)
def SetUp(self):
CopyToGenDir(self.srcs)
if len(self.srcs) > 1:
raise RuntimeError('py_test should only take one python source file.')
# Add test command
self.command_groups = [
['python %s' % GenFilename(self.srcs[0])]]
class cc_thirdparty_target(BuildTarget):
"""thirdparty_target should only be used in third_party to build things with
a pre-defined script. Note that this will also set the following values:
cc_includes: the include folder needed for compiling dependent targets.
cc_obj_files: the object files produced by the target.
When building, this script will copy all stuff to a temporary directory, so
that the original source tree is not affected.
"""
def __init__(self, name, srcs, commands, cc_obj_files, deps=[], **kwargs):
self.cwd = Brewery.CWD
self.build_dir = os.path.join(Brewery.TMPDIR, Brewery.CWD)
self.commands = [
'SRCDIR=%s' % self.build_dir,
'DSTDIR=%s' % os.path.join(os.path.abspath(Env.GENDIR), "third_party"),
'CPUS=%d' % Env.CPUS,
'cd %s' % self.build_dir,
] + commands
self.cc_obj_files = [
os.path.join(Env.GENDIR, "third_party", f)
for f in cc_obj_files if not f.startswith('-l')] + [
f for f in cc_obj_files if f.startswith('-l')]
BuildTarget.__init__(self, name, srcs, deps=deps, **kwargs)
def SetUp(self):
self.cc_obj_files += MergeOrderedObjs(
[Brewery.Get(dep).cc_obj_files for dep in self.deps])
def Build(self):
# First, copy all things to the temp directory
if os.path.exists(self.build_dir):
shutil.rmtree(self.build_dir)
shutil.copytree(self.cwd, self.build_dir)
BuildDebug("script: %s" % str(self.commands))
proc = subprocess.Popen(' && '.join(self.commands), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
stdout, _ = proc.communicate()
if proc.returncode:
BuildWarning("Script failed.")
print stdout
return False
return True
class shell_script(BuildTarget):
"""Shell scripts are directly run to generate data files. It is run from the
root of the gendir.
"""
def __init__(self, name, srcs, commands, deps=[], **kwargs):
self.cwd = Brewery.CWD
self.commands = [
'GENDIR=%s' % os.path.abspath(Env.GENDIR),
'CWD=%s' % self.cwd,
'cd %s' % os.path.abspath(Env.GENDIR),
] + commands
BuildTarget.__init__(self, name, srcs, deps=deps, **kwargs)
def SetUp(self):
"""A shell script should produce no cc_obj_files. This is here just so that
a cc object can use shell_script as a data dependency.
"""
CopyToGenDir(self.srcs)
self.cc_obj_files = []
def Build(self):
BuildDebug("script: %s" % str(self.commands))
proc = subprocess.Popen(' && '.join(self.commands), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True)
stdout, _ = proc.communicate()
if proc.returncode:
BuildWarning("Script failed.")
print stdout
return False
return True
################################################################################
# Below are functions during the main entry.
################################################################################
def main(argv):
"""The main entry of the build script."""
BuildLog('Welcome to Caffe2. Running command: %s' % str(argv))
Brewery.InitBrewery()
if len(sys.argv) > 1:
if sys.argv[1] == 'clean':
for folder in ['caffe2', 'pycaffe2']:
os.system('rm -rf ' + os.path.join(Env.GENDIR, folder))
Brewery.ClearSignature()
elif sys.argv[1] == 'reallyclean':
os.system('rm -rf ' + Env.GENDIR)
BuildLog('Finished cleaning.')
elif sys.argv[1] == 'build':
# Build all targets.
targets = sys.argv[2:]
Brewery.Build(targets)
elif sys.argv[1] == 'draw':
# Draws the dependency graph.
Brewery.Draw()
else:
BuildFatal('Unknown command: %s' % sys.argv[1])
else:
BuildLog('Finished parsing all build files without error.')
Brewery.Finalize()
if __name__ == "__main__":
main(sys.argv)