-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathAMBuildScript
569 lines (468 loc) · 18.1 KB
/
AMBuildScript
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
# vim: set ts=4 sw=4 tw=99 et ft=python:
#
# Copyright (C) 2004-2012 David Anderson
#
# This file is part of SourcePawn.
#
# SourcePawn is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# SourcePawn. If not, see http://www.gnu.org/licenses/.
#
import copy
import os
import subprocess
import sys
from ambuild2.frontend.system import System
def Normalize(path):
return os.path.abspath(os.path.normpath(path))
class LipoEntry(object):
def __init__(self, binary, target):
self.binary = binary
self.target = target
def SetArchFlags(compiler):
if compiler.like('gcc'):
if compiler.target.arch == 'x86':
if not compiler.like('emscripten'):
compiler.cflags += ['-msse']
else:
compiler.cflags += ['-fPIC']
class Config(object):
def __init__(self):
super(Config, self).__init__()
self.enable_coverage = getattr(builder.options, 'enable_coverage', False)
self.enable_asan = getattr(builder.options, 'enable_asan', False)
self.targets = []
self.asan_libs = {}
self.generated_headers = None
def configure(self):
if builder.options.targets:
targets = builder.options.targets.split(',')
dupes = set()
for target in targets:
if target in dupes:
continue
dupes.add(target)
cxx = builder.DetectCxx(target=target)
self.configure_cxx(cxx)
self.targets.append(cxx)
else:
cxx = builder.DetectCxx()
self.configure_cxx(cxx)
self.targets.append(cxx)
if builder.backend == 'amb2':
self.generate_version_header()
def configure_cxx(self, cxx):
if cxx.like('gcc'):
self.configure_gcc(cxx)
elif cxx.like('msvc'):
self.configure_msvc(cxx)
# Optimization
if builder.options.opt == '1':
cxx.defines += ['NDEBUG']
if cxx.like('gcc'):
if self.enable_asan:
cxx.cflags += ['-O1']
else:
cxx.cflags += ['-O3']
if cxx.like('emscripten'):
emflags = ['--closure', '1', '-flto', '--llvm-lto', '1']
cxx.cflags += emflags
cxx.linkflags += ['-O3'] + emflags
elif cxx.like('msvc'):
cxx.cflags += ['/Ox']
cxx.linkflags += ['/OPT:ICF', '/OPT:REF']
# Debugging
if builder.options.debug == '1':
cxx.defines += ['DEBUG', '_DEBUG']
if cxx.like('msvc'):
cxx.cflags += ['/Od', '/RTC1']
elif cxx.like('emscripten'):
emflags = [
'-s', 'ASSERTIONS=2', '-s', 'SAFE_HEAP=1', '-s', 'STACK_OVERFLOW_CHECK=2', '-s',
'DEMANGLE_SUPPORT=1'
]
cxx.cflags += emflags
cxx.linkflags += emflags
# This needs to be after our optimization flags which could otherwise disable it.
if cxx.like('msvc'):
# Don't omit the frame pointer.
cxx.cflags += ['/Oy-']
# Platform-specifics
if not cxx.like('emscripten'):
if cxx.target.platform == 'linux':
cxx.postlink += ['-lpthread', '-lrt']
elif cxx.target.platform == 'mac':
cxx.cflags += ['-mmacosx-version-min=10.15']
cxx.linkflags += ['-mmacosx-version-min=10.15']
elif cxx.target.platform == 'windows':
cxx.defines += ['WIN32', '_WINDOWS']
cxx.cxxdefines += ['NOMINMAX']
cxx.defines += ['KE_THREADSAFE']
if getattr(builder.options, 'enable_spew', False):
cxx.defines += ['JIT_SPEW']
if builder.options.amtl:
amtl_path = builder.options.amtl
else:
amtl_path = os.path.join(builder.sourcePath, 'third_party', 'amtl')
amtl_path = Normalize(amtl_path)
if not os.path.isdir(amtl_path):
raise Exception('Could not find AMTL at: {0}'.format(amtl_path))
self.amtl = amtl_path
cxx.cxxincludes += [
self.amtl,
os.path.join(builder.sourcePath, 'include'),
]
def configure_gcc(self, cxx):
cxx.cflags += [
'-pipe',
'-Wall',
'-Werror',
'-Wno-switch',
]
cxx.cxxflags += ['-std=c++17']
if builder.options.debug == '1':
cxx.cflags += ['-gdwarf-4']
cxx.cxxflags += ['-D_GLIBCXX_DEBUG']
have_gcc = cxx.family == 'gcc'
have_clang = cxx.family == 'clang' or cxx.family == 'emscripten'
if have_clang or have_gcc:
cxx.cflags += ['-fvisibility=hidden']
cxx.cxxflags += ['-fvisibility-inlines-hidden']
if have_clang or (have_gcc and cxx.version >= '4.7'):
cxx.cxxflags += ['-Wno-delete-non-virtual-dtor']
if not (have_gcc and cxx.version <= 6.3):
cxx.cxxflags += ['-Wno-unused-private-field']
if self.enable_coverage:
if cxx.family == 'clang':
coverage_argv = [
'-fprofile-instr-generate',
'-fcoverage-mapping',
]
cxx.cflags += coverage_argv
cxx.linkflags += coverage_argv
else:
raise Exception('Code coverage support is not implemented for {0}.'.format(
cxx.family))
# Disable some stuff we don't use, that gives us better binary
# compatibility on Linux.
cxx.cxxflags += [
'-fno-rtti',
'-Wno-non-virtual-dtor',
'-Wno-overloaded-virtual',
]
if have_gcc:
if cxx.target.arch in ['x86', 'x86_64']:
cxx.cflags += ['-mfpmath=sse']
cxx.cxxflags += ['-Wformat-truncation=0']
cxx.postlink += ['-lm']
is_wsl = False
if os.path.exists('/proc/sys/kernel/osrelease'):
with open('/proc/sys/kernel/osrelease', 'rb') as fp:
contents = fp.read().decode("utf-8")
is_wsl = 'Microsoft' in contents
if cxx.like('emscripten'):
emflags = ['-s', 'PRECISE_F32=1']
cxx.cflags += emflags
cxx.linkflags += emflags
cxx.defines += ['__linux__']
if self.enable_asan:
if not have_clang:
raise Exception('--enable-asan only supported when using Clang')
self.configure_asan(cxx)
def configure_msvc(self, cxx):
if builder.options.debug == '1':
cxx.cflags += ['/MTd']
cxx.linkflags += ['/NODEFAULTLIB:libcmt']
else:
cxx.cflags += ['/MT']
cxx.defines += [
'_CRT_SECURE_NO_DEPRECATE',
'_CRT_SECURE_NO_WARNINGS',
'_CRT_NONSTDC_NO_DEPRECATE',
'_ITERATOR_DEBUG_LEVEL=0',
]
cxx.cflags += [
'/W3',
'/wd4351',
]
cxx.cxxflags += [
'/EHsc',
'/GR-',
'/TP',
'/std:c++17',
]
cxx.linkflags += [
'kernel32.lib',
'user32.lib',
'gdi32.lib',
'winspool.lib',
'comdlg32.lib',
'advapi32.lib',
'shell32.lib',
'ole32.lib',
'oleaut32.lib',
'uuid.lib',
'odbc32.lib',
'odbccp32.lib',
]
if self.enable_asan:
raise Exception('--enable-asan only supported when using Clang')
def configure_asan(self, cxx):
if cxx.target.platform != 'linux':
raise Exception('--enable-asan only supported on Linux')
cxx.cflags += ['-fsanitize=address']
cxx.linkflags += ['-fsanitize=address']
if cxx.target.arch == 'x86':
libclang_rt = 'libclang_rt.asan-i386.so'
else:
libclang_rt = 'libclang_rt.asan-x86_64.so'
try:
argv = cxx.cxx_argv + ['--print-file-name', libclang_rt]
output = subprocess.check_output(argv)
output = output.decode('utf-8')
output = output.strip()
except:
raise
raise Exception('Could not find {}'.format(libclang_rt))
self.asan_libs[cxx.target.arch] = os.path.dirname(output)
def generate_version_header(self):
includes = builder.AddFolder('includes')
script = os.path.join(builder.sourcePath, 'tools', 'generate_version_header.py')
inputs = [
os.path.join(builder.sourcePath, 'product.version'),
script,
]
outputs = [
os.path.join(builder.buildFolder, 'includes', 'sourcemod_version.h')
]
argv = [
sys.executable,
script,
inputs[0],
outputs[0],
]
self.generated_headers = builder.AddCommand(inputs = [], argv = argv, outputs = outputs)
def ProgramBuilder(self, compiler, name):
binary = compiler.Program(name)
if binary.compiler.like('msvc'):
binary.compiler.linkflags.append('/SUBSYSTEM:CONSOLE')
return binary
def LibraryBuilder(self, compiler, name):
binary = compiler.Library(name)
if binary.compiler.like('msvc'):
binary.compiler.linkflags.append('/SUBSYSTEM:WINDOWS')
# Dumb clang behavior means we have to manually find libclang_rt.
if self.enable_asan:
binary.compiler.linkflags += [
'-shared-libsan',
'-Wl,-rpath={}'.format(self.asan_libs[binary.compiler.target.arch]),
]
return binary
def StaticLibraryBuilder(self, compiler, name):
return compiler.StaticLibrary(name)
def Program(self, context, name):
compiler = context.cxx.clone()
SetArchFlags(compiler)
return self.ProgramBuilder(compiler, name)
def Library(self, context, name):
compiler = context.cxx.clone()
SetArchFlags(compiler)
return self.LibraryBuilder(compiler, name)
def StaticLibrary(self, context, name):
compiler = context.cxx.clone()
SetArchFlags(compiler)
return self.StaticLibraryBuilder(compiler, name)
class SourcePawn(object):
def __init__(self, root, amtl):
super(SourcePawn, self).__init__()
self.root = root
self.amtl = amtl
self.vars = {
'Root': self.root,
'SP': self,
}
self.spshell = {}
self.spcomp = []
self.zlib = None
self.libamtl = None
self.static_libsp = {}
self.dynamic_libsp = {}
# Embedders (aka SourceMod) use this.
self.libsourcepawn = {}
def BuildCore(self):
self.EnsureZlib()
self.EnsureAmtl()
for cxx in self.root.targets:
builder.CallBuilder(lambda builder: self.BuildCoreForArch(cxx, builder))
builder.CallBuilder(lambda builder: self.BuildFatSpcomp(builder))
def SetupBinForArch(self, binary, builder):
if binary.compiler.like('gcc'):
binary.compiler.cflags += [
'-Wno-invalid-offsetof',
]
if binary.compiler.target.platform == 'linux':
binary.compiler.defines += ['_GNU_SOURCE']
elif binary.compiler.target.platform == 'mac':
binary.compiler.defines += ['DARWIN']
binary.compiler.includes += [
os.path.join(builder.currentSourcePath),
os.path.join(builder.currentSourcePath, 'include'),
os.path.join(builder.currentSourcePath, 'third_party'),
self.amtl,
]
generated_headers = getattr(self.root, 'generated_headers', None)
if generated_headers:
for entry in generated_headers:
include_path = os.path.join(builder.buildPath, os.path.dirname(entry.path))
binary.compiler.includes += [include_path]
binary.compiler.sourcedeps += generated_headers
def BuildCoreForArch(self, cxx, builder):
builder.cxx = cxx
self.BuildStaticCoreLib(builder)
self.BuildDynamicCoreLib(builder)
self.BuildSpcomp(builder)
self.BuildSpshell(builder)
self.BuildVerifier(builder)
def BuildStaticCoreLib(self, builder):
cxx = builder.cxx
binary = self.root.StaticLibrary(builder, 'libsourcepawn_static')
self.SetupBinForArch(binary, builder)
vars = self.vars.copy()
vars['binary'] = binary
builder.Build('compiler/AMBuilder', vars)
builder.Build('libsmx/AMBuilder', vars)
builder.Build('vm/AMBuilder', vars)
cppnode = builder.Add(binary)
self.static_libsp[cxx.target.arch] = cppnode.binary
def BuildDynamicCoreLib(self, builder):
cxx = builder.cxx
binary = self.root.Library(builder, 'libsourcepawn')
self.SetupBinForArch(binary, builder)
vars = self.vars.copy()
vars['binary'] = binary
if binary.compiler.like('gcc') and binary.compiler.target.platform == 'mac':
binary.compiler.cxxflags += [
'-Wno-implicit-exception-spec-mismatch',
]
binary.sources = [
'vm/dll_exports.cpp',
]
self.AddStaticLibraries(binary)
cppnode = builder.Add(binary)
self.libsourcepawn[cxx.target.arch] = cppnode
self.dynamic_libsp[cxx.target.arch] = cppnode.binary
def BuildSpcomp(self, builder):
binary = self.root.Program(builder, 'spcomp')
self.SetupBinForArch(binary, builder)
binary.sources = [
'compiler/driver.cpp',
]
if binary.compiler.target.platform == 'windows':
binary.sources += ['compiler/version.rc']
self.AddStaticLibraries(binary)
cppnodes = builder.Add(binary)
self.spcomp.append(cppnodes)
def BuildSpshell(self, builder):
binary = self.root.Program(builder, 'spshell')
self.SetupBinForArch(binary, builder)
binary.sources = [
'vm/shell/shell.cpp',
]
binary.compiler.cxxincludes += [
os.path.join(builder.currentSourcePath, 'vm'),
]
self.AddStaticLibraries(binary)
cppnodes = builder.Add(binary)
self.spshell[builder.cxx.target.arch] = cppnodes
def BuildVerifier(self, builder):
binary = self.root.Program(builder, 'verifier')
self.SetupBinForArch(binary, builder)
binary.sources = [
'tools/verifier/verifier.cpp',
]
self.AddStaticLibraries(binary)
builder.Add(binary)
def AddStaticLibraries(self, binary):
if binary.compiler.linker.like('gcc') and binary.compiler.target.platform == 'linux':
binary.compiler.linkflags += ['-Wl,--start-group']
arch = binary.compiler.target.arch
binary.compiler.linkflags += [
self.zlib[arch],
self.libamtl[arch],
self.static_libsp[arch],
]
if binary.compiler.linker.like('gcc') and binary.compiler.target.platform == 'linux':
binary.compiler.linkflags += ['-Wl,--end-group']
def BuildFatSpcomp(self, builder):
# Building fat binaries is currently only supported on macOS hosts
# building macOS targets.
if builder.host.platform != 'mac':
return
mac_targets = []
for spcomp in self.spcomp:
if spcomp.target.platform == 'mac':
mac_targets.append(spcomp.binary)
if len(mac_targets) == 0:
return
lipo_argv = [
'lipo', '-create',
]
for spcomp in mac_targets:
lipo_argv += [os.path.join(builder.buildPath, spcomp.path)]
lipo_argv += ['-output', 'spcomp']
out = builder.AddCommand(inputs = mac_targets,
argv = lipo_argv,
outputs = ['spcomp'],
folder = builder.AddFolder('spcomp/mac-universal'))
self.spcomp.append(LipoEntry(out[0], System('mac', 'universal')))
def BuildSuite(self):
self.BuildCore()
def EnsureAmtl(self):
if self.libamtl is not None:
return
self.libamtl = getattr(self.root, 'libamtl', None)
if self.libamtl:
return
self.libamtl = {}
for cxx in self.root.targets:
def do_config(builder, name):
return self.root.StaticLibrary(builder, name)
extra_vars = {'Configure': do_config}
amtl = os.path.relpath(self.amtl, builder.currentSourcePath)
amtl_builder = os.path.join(amtl, 'amtl', 'AMBuilder')
libamtl = self.BuildForArch(amtl_builder, cxx, extra_vars)
self.libamtl[cxx.target.arch] = libamtl.binary
def EnsureZlib(self):
if self.zlib is not None:
return
self.zlib = {}
for cxx in self.root.targets:
self.zlib[cxx.target.arch] = self.BuildForArch("third_party/zlib/AMBuilder", cxx)
self.included_zlib = True
def BuildForArch(self, scripts, cxx, extra_vars={}):
new_vars = copy.copy(self.vars)
for key in extra_vars:
new_vars[key] = extra_vars[key]
builder.cxx = cxx
try:
rvalue = builder.Build(scripts, new_vars)
finally:
builder.cxx = None
return rvalue
if builder.parent is None:
root = Config()
root.configure()
sp = SourcePawn(root, root.amtl)
else:
sp = SourcePawn(external_root, external_amtl)
sp.BuildCore()
if builder.parent is not None:
rvalue = sp