-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCpp.py
452 lines (394 loc) · 16 KB
/
Cpp.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# system lib
import sys
sys.path.append('flame/utils')
import commands
import os
import socket
from datetime import datetime
# scons lib
from SCons.Script import ARGUMENTS
from SCons.Script import Action
from SCons.Script import Builder
# self-defined lib
import Flags
import Path
import Util
import cpplint
from Access import AccessChecker
from LanguageBuilder import LanguageBuilder
from LanguageBuilder import RegisterObj
"""Cpp build registerers"""
def _cc_internal(name, srcs, deps, data, copt, libs,
cflags, link_flags, build_type):
opt = {}
if copt != None and len(copt) > 0:
opt['copt'] = copt
if data != None and len(data) > 0:
opt['data'] = data
if libs != None and len(libs) > 0:
opt['libs'] = libs
if cflags != None and len(cflags) > 0:
opt['cflags'] = cflags
if link_flags != None and len(link_flags) > 0:
opt['link_flags'] = link_flags
RegisterObj(name, srcs, deps, opt, build_type)
def cc_library(name = None, srcs = [], deps = [], data = [], copt = [],
libs = [], cflags = [], link_flags = []):
_cc_internal(name, srcs, deps, data, copt,
libs, cflags, link_flags, 'cc_library')
def cc_binary(name = None, srcs = [], deps = [],
data = [], copt = [], libs = [], cflags = [], link_flags = []):
_cc_internal(name, srcs, deps, data, copt, libs,
cflags, link_flags, 'cc_binary')
def cc_test(name = None, srcs = [], deps = [], data = [],
copt = [], libs = [], cflags = [], link_flags = []):
_cc_internal(name, srcs, deps, data, copt, libs,
cflags, link_flags, 'cc_test')
def cc_benchmark(name = None, srcs = [], deps = [],
data = [], copt = [], libs = [], cflags = [], link_flags = []):
_cc_internal(name, srcs, deps, data, copt, libs,
cflags, link_flags, 'cc_benchmark')
def CheckSpecialDependency(obj):
obj.has_thrift_dep = obj.name_.endswith('_thrift')
obj.has_proto_dep = obj.name_.endswith('_proto')
obj.has_cuda_dep = obj.name_.endswith('_cuda')
obj.has_bison_dep = obj.name_.endswith('_bison')
for d in obj.depends_:
if d.endswith('_thrift'):
obj.has_thrift_dep = True
elif d.endswith('_proto'):
obj.has_proto_dep = True
elif d.endswith('_cuda'):
obj.has_cuda_dep = True
elif d.endswith('_bison'):
obj.has_bison_dep = True
def GetCppInclude(obj):
result = ['.', Path.GetOutputDir(), Path.GetGlobalDir()]
# add thirdparty include to cpp include path.
for dep in obj.depends_:
if dep.startswith('//thirdparty/'):
result.append('./thirdparty/')
result.append(Path.GetAbsPath('thirdparty/boost/include'))
result.append('./thirdparty/gtest/')
result.append('./thirdparty/gtest/internal/')
result.append('./thirdparty/gmock/')
result.append('./thirdparty/sofa-rpc/include/')
result.append('./thirdparty/snappy/include/')
result.append('./thirdparty/boost/include/')
result.append('./thirdparty/json/include/')
result.append('./thirdparty/idict/include/')
result.append('./thirdparty/ullib_v2/include/')
result.append('./thirdparty/iconf/include/')
result.append('./thirdparty/dnn/include/')
result.append('./thirdparty/glog/include/')
result.append('./thirdparty/mkl/include/')
result.append('./thirdparty/openmp/include/')
result.append('./thirdparty/zmq/include/')
result.append('./thirdparty/zlib/include/')
break
if not hasattr(obj, 'has_proto_dep'):
CheckSpecialDependency(obj)
if obj.has_thrift_dep:
result.append(Path.GetAbsPath(Flags.THRIFT_INC))
result.append(Path.GetThriftOutPath())
result.append(Path.GetAbsPath('thirdparty/boost'))
result.append(Path.GetAbsPath('thirdparty/libevent'))
if obj.has_proto_dep:
result.append(Path.GetProtoOutPath())
result.append(Path.GetAbsPath(Flags.PROTO_INC))
# result.append(Path.GetAbsPath(Flags.GRPC_INC))
# TODO(xujian): check following language
if obj.has_cuda_dep:
result.append(Flags.CUDA_INC)
result.append(Flags.CUDA_SDK_COMMON_INC)
result.append(Flags.CUDA_SDK_SHARED_INC)
if obj.has_bison_dep:
result.insert(0, Path.GetBisonOutPath())
return result
class CppBuilder(LanguageBuilder):
"""C++ code builders"""
def __init__(self):
LanguageBuilder.__init__(self)
self._is_svn_client = Path.IsSVNClient()
self._is_git_client = Path.IsGITClient()
cpplint._cpplint_state.ResetErrorCounts()
self._opend_files = set()
self._checked_dir = set()
self._always_test = ARGUMENTS.get('always_test', 'on')
self._build_mode = ARGUMENTS.get('c', 'dbg')
# self._check_lib_dep = ARGUMENTS.get('check_dep', 'on')
self._check_style = ARGUMENTS.get('check_cpp_style', 'on')
self._test_suffix = 'passed'
self._access_checker = AccessChecker()
self._lib_name_map = {}
self._binaries = set()
#p = 'libs/gtest/lib%s.a'
p = 'thirdparty/gtest/libs/lib%s.a'
lib_base = self._GetStaticLib('//common/base:base', abort = False)
if not lib_base:
lib_base = self._GetLibName('//common/base:base')
# self._gtest_lib_source = [Path.GetAbsPath(x, abort = False) for x in
# (p % 'gtest_main', p % 'gmock', p % 'gtest')] + [lib_base]
self._gtest_lib_source = [Path.GetAbsPath((p % 'gtest'), abort = False)] + [lib_base]
self._benchmark_lib_source = [Path.GetAbsPath(x, abort = False) for x in
(p % 'benchmark_main', p % 'gtest')] + [lib_base]
self._main_lib = [Path.GetAbsPath(Flags.MAIN_LIB, abort = False)]
def _HasCopt(self, obj, opt):
"""Checks if an obj is specified one c/c++ option."""
return obj.option_.has_key('copt') and opt in obj.option_['copt']
def _GetGccVersion(self):
#return commands.getoutput('gcc -dumpversion')
return commands.getoutput('gcc --version')
def GetBuildRegisterers(self):
return ['cc_benchmark',
'cc_binary',
'cc_library',
'cc_test']
def RegisterSConsBuilders(self):
return {'UnitTest' : Builder(action = Action('$CCTESTCOM', '$CCTESTCOMSTR'),
suffix = self._test_suffix)
}
def _GetOpenedFiles(self, path):
if self._check_style != 'on': return
d = os.path.dirname(path)
if d in self._checked_dir:
return
self._checked_dir.add(d)
output = []
if self._is_svn_client and not Path.IsInDir(d, Path.GetGlobalDir()):
output = os.popen('svn st %s' % d).read().split('\n')
elif self._is_git_client:
output = os.popen('git status --porcelain %s' % d).read().split('\n')
for f in output:
seg = f.strip().split(' ')
if seg[0] == 'D' or seg[0] == '!':
continue
f = seg[len(seg)-1]
# not check style for third party code
# (xujian): not check style for base code.
if f.startswith(Path.GetBaseDir() + '/thirdparty/') or f.startswith(Path.GetBaseDir() + '/base/'):
continue
if f.endswith('.h') or f.endswith('.cc') or f.endswith('.cpp'):
self._opend_files.add(f)
pass
def _StyleCheck(self):
# not check style for third party code
for f in self._opend_files:
cpplint.ProcessFile(f, cpplint._cpplint_state.verbose_level, False)
if cpplint._cpplint_state.error_count > 0:
print "\n\n"
print Util.BuildMessage(
'There\'re %d style warnings in the opend files, '
'please try fixing them before submit the code!' %
cpplint._cpplint_state.error_count, 'WARNING')
pass
def _GenerateBuildingEnv(self, env):
content = open(Path.GetAbsPath(Flags.CPP_BLD_ENV_IN)).read()
content = content.replace('BD_TIME', '%s' % datetime.now())
content = content.replace('BD_HOST', Util.GetLocalIp())
cxx = env['CXX'].split('/')
cxx.reverse()
content = content.replace('BD_COM', '-'.join((cxx[0], env['CXXVERSION'])))
content = content.replace('BD_MODE', self._build_mode)
content = content.replace('BD_USERNAME', Util.GetUsername())
(sys, d1, release, d2, machine) = os.uname()
content = content.replace('BD_PLATFORM', '-'.join((sys, release, machine)))
base = ''
rev = ''
if self._is_svn_client:
info = os.popen('svn info .').read().split('\n')
for line in info:
if line.startswith('URL'):
base = line.split(': ')[1].strip()
elif line.startswith('Revision:') or line.startswith('版本'):
rev = line.split(':')[1].strip()
content = content.replace('BD_SVN_BASE', base)
content = content.replace('BD_SVN_REV', rev)
# build binary map
targets = ''
for b in self._binaries:
if len(targets) > 0: targets += ','
targets += '%s:%s' % (os.path.basename(b), b)
content = content.replace('BD_TARGET', targets)
path = os.path.join(Path.GetOutputDir(), Flags.CPP_BLD_ENV_OUT)
Util.MkDir(os.path.dirname(path))
open(path, 'w').write(content)
def GenerateEnv(self, env):
env['LINK'] = 'g++'
env['CCTESTCOM'] = ('bash %s $SOURCE $TARGET $CCTESTTIMEOUTFLAGS '
'$CCTESTHEAPCHECKFLAGS' % Flags.CPP_UNITTEST_SCRIPT)
env['CUDA_LIB'] = str('%s %s %s %s' %
(Flags.CUDA_SHRUTIL_LIB,
Flags.CUDA_UTIL_LIB,
Flags.CUDA_FFT_LIB,
Flags.CUDA_RT_LIB))
cc_flags = ('-m64 -fPIC -Wall -Werror -Wwrite-strings -Wno-sign-compare -g '
'-Wuninitialized -Wno-error=deprecated-declarations ')
# these is just for using proftools
cc_flags += ('-fno-builtin-malloc -fno-builtin-free -fno-builtin-realloc '
'-fno-builtin-calloc -fno-builtin-cfree -fno-builtin-memalign '
'-fno-builtin-posix_memalign -fno-builtin-valloc '
'-fno-builtin-pvalloc -fno-omit-frame-pointer -fopenmp')
link_flags = ['-pthread', '-fopenmp']
if self._GetGccVersion() >= '4.5': # only available after gcc4.5
link_flags.append(' -static-libstdc++ ')
else:
cc_flags += '-fno-strict-aliasing '
if self._build_mode != 'gprof':
pass
else:
link_flags.append(' -pg ')
env.Replace(LINKFLAGS = ' '.join(link_flags))
if self._build_mode == 'dbg':
env.Replace(CCFLAGS = ' '.join([cc_flags]))
elif self._build_mode == 'gprof':
env.Replace(CCFLAGS = ' '.join([cc_flags, '-pg -O2 -DNDEBUG']))
elif self._build_mode == 'opt':
env.Replace(CCFLAGS = ' '.join([cc_flags, '-O2 -DNDEBUG']))
else:
assert False, 'wrong build strategy: %s' % self._build_mode
def _GetSourcePath(self, sources):
result = []
for x in sources:
p = Path.GetAbsPath(x)
if Path.IsInDir(p, Path.GetBaseDir()):
result.append(Path.GetBuiltPath(x))
else:
result.append(p)
return result
def _GetLibPath(self, path):
lib_name = 'lib' + os.path.basename(path) + '.a'
return os.path.join(os.path.dirname(path), lib_name)
def _GetLibName(self, name):
try:
lib_name = self._lib_name_map[name]
except:
build_name = Path.GetBuiltPath(name)
lib_name = self._GetLibPath(build_name)
self._lib_name_map[name] = lib_name
return lib_name
def _GetStaticLib(self, path, abort = True):
p = os.path.join(Flags.STATIC_LIB_PATH, Path.GetRelativePath(path))
return Path.GetAbsPath(self._GetLibPath(p), abort)
def _GetLibNameForObj(self, obj):
if obj.is_private_:
return self._GetStaticLib(obj.name_)
return self._GetLibName(obj.name_)
def _GetFlags(self, obj, env):
source = []
always_link_libs = ''
libs = ['dl', 'rt', 'crypto', 'z', 'cares', 'ssl']
if 'libs' in obj.option_:
libs += obj.option_['libs']
path = []
# check dependent obj's special attributes
obj.has_thrift_dep = obj.name_.endswith('_thrift')
obj.has_proto_dep = obj.name_.endswith('_proto')
obj.has_cuda_dep = obj.name_.endswith('_cuda')
obj.has_bison_dep = obj.name_.endswith('_bison')
for d in obj.depends_:
if d.endswith('_thrift'):
obj.has_thrift_dep = True
elif d.endswith('_proto'):
obj.has_proto_dep = True
elif d.endswith('_cuda'):
obj.has_cuda_dep = True
elif d.endswith('_bison'):
obj.has_bison_dep = True
try:
dep_obj = self.build_manager_.GetObjByName(d)
except:
if Path.IsStaticLib(d):
d = d.replace(':', ':lib') + '.a'
source.append(Path.GetAbsPath(d))
continue
d_lib = self._GetLibNameForObj(dep_obj)
if self._HasCopt(dep_obj, 'always_link'):
always_link_libs += ' %s' % d_lib
# add explicit dependency since Scons could not figure out such dep
# relationship for always link lib
if obj.build_type_ in ['cc_test', 'cc_benchmark', 'cc_binary']:
obj_name = Path.GetBuiltPath(obj.name_)
elif (obj.build_type_ in ['cc_library']):
obj_name = self._GetLibName(obj.name_)
env.Depends(obj_name, d_lib)
else:
source.append(d_lib)
# detect thrift / proto dependency
if obj.has_thrift_dep:
libs += ['thriftnb', 'thriftz', 'thrift', 'event']
path.append(Path.GetAbsPath('libs/thirdparty/thrift'))
path.append(Path.GetAbsPath('libs/thirdparty/libevent'))
if obj.has_proto_dep:
libs += ['protobuf']
# xujian
path.append(Path.GetAbsPath(Flags.PROTO_LIB))
# for gtest
if obj.build_type_ == 'cc_test':
source += self._gtest_lib_source
if obj.build_type_ == 'cc_benchmark':
source += self._benchmark_lib_source
link_flags = env['LINKFLAGS']
if len(always_link_libs) > 0:
link_flags += (' -Wl,--whole-archive%s -Wl,--no-whole-archive' %
always_link_libs)
if 'link_flags' in obj.option_:
link_flags += ' '
link_flags += ' '.join(obj.option_['link_flags'])
# add cuda .so to the tail fo source
if obj.has_cuda_dep:
cuda_lib_list = env['CUDA_LIB'].split()
source += cuda_lib_list
return source, libs, path, link_flags
def BuildObject(self, env, obj):
self._GetOpenedFiles(obj.path_)
# convert to SCons dendency graph
target = Path.GetBuiltPath(obj.name_)
source = self._GetSourcePath(obj.sources_)
relative_source = [Path.GetRelativePath(x) for x in obj.sources_]
cpp_path = GetCppInclude(obj)
lib_sources, libs, libpath, link_flags = self._GetFlags(obj, env)
cc_flags = env['CCFLAGS']
if 'cflags' in obj.option_:
cc_flags += ' '
cc_flags += ' '.join(obj.option_['cflags'])
if obj.has_thrift_dep and self._GetGccVersion() >= '4.6':
cc_flags += ' -Wno-return-type '
CXX_value = env['CXX']
if obj.build_type_ in ['cc_binary', 'cc_test', 'cc_benchmark']:
if obj.build_type_ == 'cc_binary':
self._binaries.add(Path.GetRelativePath(obj.name_))
binary = env.Program(target = target,
source = source + lib_sources,
LIBS = libs,
LIBPATH = libpath,
CPPPATH = cpp_path,
LINKFLAGS = link_flags,
CCFLAGS = cc_flags,
CXX = CXX_value)
if (obj.build_type_ == 'cc_test' and
obj.name_ in self.build_manager_.GetTestTargets()):
heapcheck_str = 'on'
if self._HasCopt(obj, 'disable_heap_check'):
print Util.BuildMessage('Not do heap check for %s' % obj.name_,
'WARNING')
heapcheck_str = ''
timeout = 30
if self._HasCopt(obj, 'large_test'):
timeout = 300
if self._always_test == 'on':
os.system('rm -rf %s.%s' % (binary[0], self._test_suffix))
env.UnitTest(binary,
CCTESTHEAPCHECKFLAGS = heapcheck_str,
CCTESTTIMEOUTFLAGS = '%d' % timeout)
elif obj.build_type_ in ['cc_library']:
result = env.StaticLibrary(target = target,
source = source,
CPPPATH = cpp_path,
CCFLAGS = cc_flags,
CXX = CXX_value)
def Finish(self, env):
self._StyleCheck()
self._GenerateBuildingEnv(env)