-
Notifications
You must be signed in to change notification settings - Fork 3
/
sng.py
321 lines (271 loc) · 9.06 KB
/
sng.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
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""A ninja generator for simple projects. No configuration files, replaced by
ridiculously regimented project structure that you probably won't like.
"""
import glob
import optparse
import os
import re
import subprocess
import sys
def Run(args, cwd=None):
assert isinstance(args, list)
subprocess.check_call(args, shell=True, cwd=cwd)
def GitPullOrClone(remote):
base = os.path.splitext(os.path.basename(remote))[0]
if not os.path.exists('third_party/%s/.git' % base):
Run(['git', 'clone', remote], cwd='third_party')
else:
Run(['git', 'pull'], cwd='third_party/%s' % base)
def DEP_ninja(action):
if action == 'sync':
GitPullOrClone('https://github.com/martine/ninja.git')
if os.path.exists('third_party/ninja/ninja.bootstrap.exe'):
Run(['ninja.bootstrap.exe'], cwd='third_party/ninja')
elif os.path.exists('third_party/ninja/ninja.exe'):
Run(['ninja.exe'], cwd='third_party/ninja')
elif os.path.exists('third_party/ninja/ninja'):
Run(['ninja'], cwd='third_party/ninja')
else:
Run([sys.executable, 'configure.py', '--bootstrap'],
cwd='third_party/ninja')
def DEP_googletest(action):
if action == 'sync':
GitPullOrClone('https://github.com/google/googletest.git')
CFLAGS = [
'/Zi', '/W4', '/WX', '/GR-',
'/wd4100',
'/wd4530',
'/wd4800',
'/D_WIN32_WINNT=0x0600',
'/D_CRT_SECURE_NO_WARNINGS',
'/DNOMINMAX',
'/Isrc',
]
CFLAGS_DEBUG = [
'/D_DEBUG',
'/MDd',
]
CFLAGS_RELEASE = [
'/Ox',
'/GL',
'/DNDEBUG',
'/MD',
]
ARFLAGS = [
]
ARFLAGS_DEBUG = [
]
ARFLAGS_RELEASE = [
'/LTCG',
]
LDFLAGS = [
'/DEBUG',
]
LDFLAGS_DEBUG = [
]
LDFLAGS_RELEASE = [
'/LTCG',
]
ALL_AUX_SNG = glob.glob('sng_*.py')
for helper in ALL_AUX_SNG:
exec(open(helper).read(), globals())
def UpdateDeps():
if not os.path.exists('third_party'):
os.makedirs('third_party')
for name, func in globals().items():
if name.startswith('DEP_'):
func('sync')
def GetFromDeps(into, action):
for name, func in globals().items():
if name.startswith('DEP_'):
result = func(action)
if result:
into.extend(result)
def ForwardSlash(path):
return os.path.normpath(path).replace('\\', '/')
def ScanForIncludeAndUpdateLibs(src_path, libs, libtags):
with open(src_path, 'r') as f:
contents = f.read()
for libtag, libname in libtags:
if libtag in contents:
libs.add(libname)
def ScanForRCDATA(src_path):
deps = []
with open(src_path, 'r') as f:
contents = f.read()
for mo in re.finditer(r'RCDATA "(.*)"', contents):
deps.append(ForwardSlash(mo.group(1)))
return deps
def Compile(n, target, libtags):
objs = []
libs = set()
for topdir, dirnames, filenames in os.walk(os.path.join('src', target)):
for filename in filenames:
ext = os.path.splitext(filename)[1]
src_path = ForwardSlash(os.path.join(topdir, filename))
if ext in ('.cc', '.cpp', '.cxx'):
ScanForIncludeAndUpdateLibs(src_path, libs, libtags)
assert src_path.startswith('src')
obj_path = '$builddir/' + ForwardSlash(
src_path[4:] + '.obj').replace('/', '.')
variables = None
if '_test' in filename:
variables = (('cflags', '$cflags_test'),)
n.build(obj_path, 'cxx', src_path, variables=variables)
objs.append(obj_path)
elif ext == '.rc':
obj_path = '$builddir/' + ForwardSlash(
src_path[4:] + '.res').replace('/', '.')
used = ScanForRCDATA(src_path)
n.build(obj_path, 'rc', src_path, implicit=used)
objs.append(obj_path)
return objs, list(libs)
def BinaryAndRuleForTarget(target):
if target.endswith('_dll'):
return '$builddir/' + target[:-4] + '.dll', 'linkdll'
elif target.endswith('_exe'):
return '$builddir/' + target[:-4] + '.exe', 'link'
else:
return '$builddir/' + target + '.lib', 'lib'
def BuildLibTags(target, targets):
libtags = []
for t in targets:
if target == t:
continue
if not t.endswith('_dll') and not t.endswith('_exe'):
libtags.append(('#include "%s/' % t, '$builddir/%s.lib' % t))
return libtags
def Generate(is_debug):
if not os.path.exists('out'):
os.makedirs('out')
sys.path.append('third_party/ninja/misc')
import ninja_syntax
with open('build.ninja', 'w') as output_file:
n = ninja_syntax.Writer(output_file)
n.comment('Generated by sng.py')
n.newline()
n.variable('ninja_required_version', '1.3')
n.newline()
n.comment('The arguments passed to configure.py, for rerunning it.')
n.variable('configure_args', ' '.join(sys.argv[1:]))
assert sys.platform == 'win32'
n.variable('builddir', 'out')
cflags = CFLAGS
have_common = os.path.exists('src/common')
GetFromDeps(cflags, 'cflags')
if is_debug:
cflags += CFLAGS_DEBUG
GetFromDeps(cflags, 'cflags_debug')
else:
cflags += CFLAGS_RELEASE
GetFromDeps(cflags, 'cflags_release')
n.variable('cflags', cflags)
n.variable('rcflags', [x for x in cflags
if x.startswith('/I') or x.startswith('/D')] + [
'/I.'])
cflags_test = cflags[:]
cflags_test += [
'/Ithird_party/googletest/googletest/include',
'/Ithird_party/googletest/googletest',
'/D_VARIADIC_MAX=10',
]
GetFromDeps(cflags_test, 'cflags_test')
n.variable('cflags_test', cflags_test)
n.newline()
arflags = ARFLAGS
GetFromDeps(arflags, 'arflags')
if is_debug:
arflags += ARFLAGS_DEBUG
GetFromDeps(arflags, 'arflags_debug')
else:
arflags += ARFLAGS_RELEASE
GetFromDeps(arflags, 'arflags_release')
n.variable('arflags', arflags)
n.newline()
ldflags = LDFLAGS
GetFromDeps(ldflags, 'ldflags')
if is_debug:
ldflags += LDFLAGS_DEBUG
GetFromDeps(ldflags, 'ldflags_debug')
else:
ldflags += LDFLAGS_RELEASE
GetFromDeps(ldflags, 'ldflags_release')
n.variable('ldflags', ldflags)
n.newline()
n.rule('cxx',
command=('cl /nologo /showIncludes $cflags -c $in '
'/Fo$out /Fd$out.pdb'),
description='CXX $out',
deps='msvc')
n.rule('lib',
command='lib /nologo $arflags /out:$out $in',
description='LIB $out')
n.rule('link',
command='link /nologo $in $libs $ldflags /out:$out /pdb:$out.pdb',
description='LINK $out')
n.rule('linkdll',
command=('link /nologo $in $libs $ldflags /DLL '
'/out:$out /pdb:$out.pdb'),
description='LINK DLL $out')
n.rule('rc',
command='rc /nologo /r /fo$out $rcflags $in',
description='RC $out')
n.newline()
targets = [os.path.basename(x) for x in glob.glob('src/*')]
all_binaries = []
all_test_objs = []
all_test_libs = []
for target in targets:
objs, libs = Compile(n, target, BuildLibTags(target, targets))
no_test_objs = [x for x in objs if '_test' not in x]
test_objs = [x for x in objs if '_test' in x]
all_test_objs += test_objs
name, rule = BinaryAndRuleForTarget(target)
n.build(name, rule, no_test_objs + libs)
if test_objs:
all_test_libs.append(name)
all_binaries.append(name)
if all_test_objs:
gtest_obj = '$builddir/gtest-all.obj'
gtest_main = '$builddir/gtest_main.obj'
gtest_cflags = '$cflags_test /wd4100 /Ithird_party/googletest'
n.build(gtest_obj, 'cxx',
'third_party/googletest/googletest/src/gtest-all.cc',
variables=(('cflags', gtest_cflags),))
n.build(gtest_main, 'cxx',
'third_party/googletest/googletest/src/gtest_main.cc',
variables=(('cflags', gtest_cflags),))
test_binary = '$builddir/tests.exe'
n.build(test_binary, 'link',
all_test_objs + [gtest_obj, gtest_main] + all_test_libs)
all_binaries.append(test_binary)
n.newline()
n.comment('Regenerate build file if build script changes.')
n.rule('configure',
command=sys.executable + ' sng.py ' + '$configure_args',
generator=1)
build_files = ['sng.py', 'third_party/ninja/misc/ninja_syntax.py'] + \
ALL_AUX_SNG
n.build('build.ninja', 'configure', implicit=build_files)
n.newline()
n.build('all', 'phony', all_binaries)
def main():
parser = optparse.OptionParser()
parser.add_option("-d", "--debug",
action="store_true", dest="debug", default=False,
help="build Debug build (default Release)")
parser.add_option("-u", "--update-deps",
action="store_true", dest="update_deps", default=False,
help="Pull or update third party dependencies")
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error('unexpected args')
if options.update_deps:
UpdateDeps()
Generate(options.debug)
return 0
if __name__ == '__main__':
sys.exit(main())