forked from ponylang/ponyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wscript
458 lines (397 loc) · 16.4 KB
/
wscript
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
#! /usr/bin/env python
# encoding: utf-8
# This script is used to configure and build the Pony compiler.
# Target definitions are in the build() function.
import sys, subprocess
from waflib import TaskGen
TaskGen.declare_chain(
rule = '${LLVM_LLC} -filetype=obj -o ${TGT} ${SRC}',
ext_in = '.ll',
ext_out = '.o'
)
# check if the operating system's description contains a string
def os_is(name):
return (sys.platform.lower().rfind(name) > -1)
# runs a shell command and captures its standard output
def cmd_output(cmd):
if (sys.version_info > (3,0)):
return str(subprocess.check_output(cmd), 'utf-8').strip('\n').strip('\'')
else:
return str(subprocess.check_output(cmd)).strip('\n').strip('\'')
# build ponyc version string
APPNAME = 'ponyc'
VERSION = '?'
with open('VERSION') as v:
VERSION = v.read().strip()
try:
rev = '-' + cmd_output(['git', 'rev-parse', '--short', 'HEAD'])
VERSION = VERSION + rev
except:
pass
# source and build directories
top = '.'
out = 'build'
# various configuration parameters
CONFIGS = [
'release',
'debug'
]
MSVC_VERSIONS = [ '15.6', '15.4', '15.0', '14.0' ]
# keep these in sync with the list in .appveyor.yml
LLVM_VERSIONS = [
'3.9.1',
'5.0.1',
'6.0.0'
]
WINDOWS_LIBS_TAG = "v1.7.0"
LIBRESSL_VERSION = "2.6.4"
PCRE2_VERSION = "10.31"
# Adds an option for specifying debug or release mode.
def options(ctx):
ctx.add_option('--config', action='store', default=CONFIGS[0], help='debug or release build')
ctx.add_option('--llvm', action='store', default=LLVM_VERSIONS[0], help='llvm version')
ctx.add_option('--msvc', action='store', default=None, help='MSVC version')
# This replaces the default versions of these context classes with subclasses
# that set their "variant", i.e. build directory, to the debug or release config.
def init(ctx):
from waflib.Build import BuildContext, CleanContext, InstallContext, UninstallContext
for c in (BuildContext, CleanContext, InstallContext, UninstallContext, testContext):
name = c.__name__.replace('Context','').lower()
class tmp(c):
cmd = name
variant = ctx.options.config.lower() + '-llvm-' + ctx.options.llvm
# sets up one-time configuration for the build variants
def configure(ctx):
ctx.env.append_value('DEFINES', [
'PONY_VERSION="' + VERSION + '"',
'PONY_USE_BIGINT',
])
if os_is('win32'):
import os
base_env = ctx.env
if ctx.options.msvc is None:
base_env.MSVC_VERSIONS = [ 'msvc ' + v for v in MSVC_VERSIONS ]
else:
base_env.MSVC_VERSIONS = [ 'msvc ' + ctx.options.msvc ]
base_env.MSVC_TARGETS = [ 'x64' ]
ctx.load('msvc')
base_env.CC_SRC_F = '/Tp' # force C++ for all files
base_env.CXX_SRC_F = '/Tp'
base_env.append_value('DEFINES', [
'_CRT_SECURE_NO_WARNINGS', '_MBCS',
'PLATFORM_TOOLS_VERSION=%d0' % base_env.MSVC_VERSION,
'BUILD_COMPILER="msvc-%d-x64"' % base_env.MSVC_VERSION
])
base_env.append_value('LIBPATH', [ base_env.LLVM_DIR ])
for llvm_version in LLVM_VERSIONS:
bld_env = base_env.derive()
bld_env.PONYLIBS_DIR = 'ThirdParty'
bld_env.LIBRESSL_DIR = os.path.join(bld_env.PONYLIBS_DIR, \
'lib', 'libressl-' + LIBRESSL_VERSION)
bld_env.PCRE2_DIR = os.path.join(bld_env.PONYLIBS_DIR, \
'lib', 'pcre2-' + PCRE2_VERSION)
bld_env.append_value('DEFINES', [
'LLVM_VERSION="' + llvm_version + '"',
'PONY_VERSION_STR="' + \
'%s [%s]\\ncompiled with: llvm %s -- msvc-%d-x64"' % \
(VERSION, ctx.options.config, \
llvm_version, base_env.MSVC_VERSION)
])
libs_name = 'PonyWinLibs' + \
'-LLVM-' + llvm_version + \
'-LibreSSL-' + LIBRESSL_VERSION + \
'-PCRE2-' + PCRE2_VERSION + '-' + \
WINDOWS_LIBS_TAG
# Debug configuration
bldName = 'debug-llvm-' + llvm_version
ctx.setenv(bldName, env = bld_env)
ctx.env.PONYLIBS_NAME = libs_name + '-Debug'
ctx.env.LLVM_DIR = os.path.join(ctx.env.PONYLIBS_DIR, \
'lib', 'LLVM-' + llvm_version + '-Debug')
ctx.env.append_value('DEFINES', [
'DEBUG',
'PONY_BUILD_CONFIG="debug"',
'_SCL_SECURE_NO_WARNINGS'
])
msvcDebugFlags = [
'/EHsc', '/MP', '/GS', '/W3', '/Zc:wchar_t', '/Zi',
'/Gm-', '/Od', '/Zc:inline', '/fp:precise', '/WX',
'/Zc:forScope', '/Gd', '/MDd', '/FS', '/DEBUG'
]
ctx.env.append_value('CFLAGS', msvcDebugFlags)
ctx.env.append_value('CXXFLAGS', msvcDebugFlags)
ctx.env.append_value('LINKFLAGS', [
'/NXCOMPAT', '/SUBSYSTEM:CONSOLE', '/DEBUG', '/ignore:4099'
])
ctx.env.PONYC_EXTRA_LIBS = [
'kernel32', 'user32', 'gdi32', 'winspool', 'comdlg32',
'advapi32', 'shell32', 'ole32', 'oleaut32', 'uuid',
'odbc32', 'odbccp32', 'vcruntimed', 'ucrtd', 'Ws2_32',
'dbghelp', 'Shlwapi'
]
# Release configuration
bldName = 'release-llvm-' + llvm_version
ctx.setenv(bldName, env = bld_env)
ctx.env.PONYLIBS_NAME = libs_name + '-Release'
ctx.env.LLVM_DIR = os.path.join(ctx.env.PONYLIBS_DIR, \
'lib', 'LLVM-' + llvm_version + '-Release')
ctx.env.append_value('DEFINES', [
'NDEBUG',
'PONY_BUILD_CONFIG="release"'
])
msvcReleaseFlags = [
'/EHsc', '/MP', '/GS', '/TC', '/W3', '/Gy', '/Zc:wchar_t',
'/Gm-', '/O2', '/Zc:inline', '/fp:precise', '/GF', '/WX',
'/Zc:forScope', '/Gd', '/Oi', '/MD', '/FS'
]
ctx.env.append_value('CFLAGS', msvcReleaseFlags)
ctx.env.append_value('CXXFLAGS', msvcReleaseFlags)
ctx.env.append_value('LINKFLAGS', [
'/NXCOMPAT', '/SUBSYSTEM:CONSOLE'
])
ctx.env.PONYC_EXTRA_LIBS = [
'kernel32', 'user32', 'gdi32', 'winspool', 'comdlg32',
'advapi32', 'shell32', 'ole32', 'oleaut32', 'uuid',
'odbc32', 'odbccp32', 'vcruntime', 'ucrt', 'Ws2_32',
'dbghelp', 'Shlwapi'
]
# specifies build targets
def build(ctx):
import os
buildDir = ctx.bldnode.abspath()
packagesDir = os.path.join(ctx.srcnode.abspath(), 'packages')
llvmIncludes = []
llvmLibs = []
llvmBuildMode = ''
sslIncludes = []
# download windows libraries needed for building
if (os_is('win32')):
libsName = ctx.env.PONYLIBS_NAME
libsDir = os.path.join(buildDir, ctx.env.PONYLIBS_DIR)
libresslDir = os.path.join(buildDir, ctx.env.LIBRESSL_DIR)
pcre2Dir = os.path.join(buildDir, ctx.env.PCRE2_DIR)
llvmDir = os.path.join(buildDir, ctx.env.LLVM_DIR)
if (ctx.cmd == 'clean'): # make sure to delete the libs directory
import shutil
if os.path.exists(libsDir):
shutil.rmtree(libsDir)
else:
llvmIncludes.append(os.path.join(llvmDir, 'include'))
ctx.env.append_value('LIBPATH', os.path.join(llvmDir, 'lib'))
sslIncludes.append(os.path.join(libresslDir, 'include'))
if not (os.path.exists(libsDir) \
and os.path.exists(libresslDir) \
and os.path.exists(pcre2Dir) \
and os.path.exists(llvmDir)):
libsZip = libsName + '.zip'
libsFname = os.path.join(buildDir, '..', libsZip)
if not os.path.isfile(libsFname):
libsUrl = 'https://github.com/kulibali/ponyc-windows-libs/releases/download/' \
+ WINDOWS_LIBS_TAG + '/' + libsZip
print('downloading ' + libsUrl)
if (sys.version_info > (3,0)):
import urllib.request
urllib.request.urlretrieve(libsUrl, libsFname)
else:
import urllib2
buf = urllib2.urlopen(libsUrl)
with open(libsFname, 'wb') as output:
while True:
data = buf.read(65536)
if data:
output.write(data)
else:
break
print('unzipping ' + libsZip)
import zipfile
if not (os.path.exists(libsDir)):
os.makedirs(libsDir)
with zipfile.ZipFile(libsFname) as zf:
zf.extractall(libsDir)
import shutil
if (ctx.options.config == 'debug'):
shutil.copy(os.path.join(pcre2Dir, 'pcre2-8d.lib'), buildDir)
shutil.copy(os.path.join(pcre2Dir, 'pcre2-8d.lib'), os.path.join(buildDir, 'pcre2-8.lib'))
else:
shutil.copy(os.path.join(pcre2Dir, 'pcre2-8.lib'), buildDir)
shutil.copy(os.path.join(libresslDir, 'lib', 'crypto.lib'), buildDir)
shutil.copy(os.path.join(libresslDir, 'lib', 'ssl.lib'), buildDir)
shutil.copy(os.path.join(libresslDir, 'lib', 'tls.lib'), buildDir)
llvmConfig = os.path.join(llvmDir, 'bin', 'llvm-config.exe')
ctx.env.LLVM_LLC = os.path.join(llvmDir, 'bin', 'llc.exe')
llvmLibFiles = cmd_output([llvmConfig, '--libs'])
import re
llvmLibs = [re.sub(r'.*[\\\/]([^\\\/)]+)$', r'\1', x) for x in llvmLibFiles.split('.lib') if x]
llvmBuildMode = cmd_output([llvmConfig, '--build-mode'])
if llvmBuildMode == 'Release':
llvmBuildMode = 'LLVM_BUILD_MODE_Release'
elif llvmBuildMode == 'RelWithDebInfo':
llvmBuildMode = 'LLVM_BUILD_MODE_RelWithDebInfo'
elif llvmBuildMode == 'Debug':
llvmBuildMode = 'LLVM_BUILD_MODE_Debug'
else:
print('Unknown llvm build-mode of {0}'.format(llvmBuildMode))
sys.exit(1)
llvmBuildMode = 'LLVM_BUILD_MODE={0}'.format(llvmBuildMode)
# build targets:
if ctx.options.llvm.startswith('4') or ctx.options.llvm.startswith('5') or ctx.options.llvm.startswith('6'):
print('WARNING: LLVM 4, 5 and 6 support is experimental and may result in decreased performance or crashes')
# gtest
ctx(
features = 'cxx cxxstlib seq',
target = 'gtest',
source = ctx.path.ant_glob('lib/gtest/*.cc'),
defines = [ '_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING' ]
)
# libgbenchmark
ctx(
features = 'cxx cxxstlib seq',
target = 'libgbenchmark',
source = ctx.path.ant_glob('lib/gbenchmark/src/*.cc'),
includes = [ 'lib/gbenchmark/include' ],
defines = [ 'HAVE_STD_REGEX', 'HAVE_STEADY_CLOCK' ]
)
# blake2
ctx(
features = 'c seq',
target = 'blake2',
source = ctx.path.ant_glob('lib/blake2/*.c'),
)
# libponyc
ctx(
features = 'c cxx cxxstlib seq',
target = 'libponyc',
source = ctx.path.ant_glob('src/libponyc/**/*.c') + \
ctx.path.ant_glob('src/libponyc/**/*.cc'),
includes = [ 'src/common', 'lib/blake2' ] + llvmIncludes + sslIncludes,
defines = [ 'PONY_ALWAYS_ASSERT', llvmBuildMode ],
use = [ 'blake2' ]
)
# libponyc.benchmarks
ctx(
features = 'cxx cxxprogram seq',
target = 'libponyc.benchmarks',
source = ctx.path.ant_glob('benchmark/libponyc/**/*.cc'),
includes = [ 'lib/gbenchmark/include', 'src/common', 'src/libponyrt' ],
defines = [ llvmBuildMode ],
use = [ 'libponyc', 'libgbenchmark' ],
lib = ctx.env.PONYC_EXTRA_LIBS
)
# libponyrt
ctx(
features = 'c cxx cxxstlib seq',
target = 'libponyrt',
source = ctx.path.ant_glob('src/libponyrt/**/*.c') + \
ctx.path.ant_glob('src/libponyrt/**/*.ll'),
includes = [ 'src/common', 'src/libponyrt' ] + sslIncludes
)
# libponyrt.benchmarks
ctx(
features = 'cxx cxxprogram seq',
target = 'libponyrt.benchmarks',
source = ctx.path.ant_glob('benchmark/libponyrt/**/*.cc'),
includes = [ 'lib/gbenchmark/include', 'src/common', 'src/libponyrt' ],
use = [ 'libponyrt', 'libgbenchmark' ],
lib = ctx.env.PONYC_EXTRA_LIBS
)
# ponyc
ctx(
features = 'c cxx cxxprogram seq',
target = 'ponyc',
source = ctx.path.ant_glob('src/ponyc/**/*.c'),
includes = [ 'src/common' ],
defines = [ 'PONY_ALWAYS_ASSERT' ],
use = [ 'libponyc', 'libponyrt' ],
lib = llvmLibs + ctx.env.PONYC_EXTRA_LIBS
)
# testc
testcUses = [ 'gtest', 'libponyc', 'libponyrt' ]
testcLibs = llvmLibs + ctx.env.PONYC_EXTRA_LIBS
if os_is('win32'):
testcUses = [ 'gtest', 'libponyc' ]
testcLibs = [ '/WHOLEARCHIVE:libponyrt' ] + testcLibs
ctx(
features = 'c cxx cxxprogram seq',
target = 'testc',
source = ctx.path.ant_glob('test/libponyc/**/*.cc'),
includes = [ 'src/common', 'src/libponyc', 'src/libponyrt',
'lib/gtest' ] + llvmIncludes,
defines = [
'PONY_ALWAYS_ASSERT',
'PONY_PACKAGES_DIR="' + packagesDir.replace('\\', '\\\\') + '"',
'_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING',
llvmBuildMode
],
use = testcUses,
lib = testcLibs,
linkflags = [ '/INCREMENTAL:NO' ]
)
# testrt
ctx(
features = 'c cxx cxxprogram seq',
target = 'testrt',
source = ctx.path.ant_glob('test/libponyrt/**/*.cc'),
includes = [ 'src/common', 'src/libponyrt', 'lib/gtest' ],
defines = [ '_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING' ],
use = [ 'gtest', 'libponyrt' ],
lib = ctx.env.PONYC_EXTRA_LIBS
)
# this command builds and runs the test suites
def test(ctx):
import os
# stdlib tests
stdlibTarget = 'stdlib'
if os_is('win32'):
stdlibTarget = 'stdlib.exe'
ctx(
features = 'seq',
rule = '"' + os.path.join(ctx.bldnode.abspath(), 'ponyc') + '" -d -s --checktree --verify ../../packages/stdlib',
target = stdlibTarget,
source = ctx.bldnode.ant_glob('ponyc*') + ctx.path.ant_glob('packages/**/*.pony'),
)
# grammar file
ctx(
features = 'seq',
rule = '"' + os.path.join(ctx.bldnode.abspath(), 'ponyc') + '" --antlr > pony.g.new',
target = 'pony.g.new',
)
def run_tests(ctx):
buildDir = ctx.bldnode.abspath()
sourceDir = ctx.srcnode.abspath()
total = 0
passed = 0
total = total + 1
testc = os.path.join(buildDir, 'testc')
returncode = subprocess.call([ testc ])
if returncode == 0:
passed = passed + 1
total = total + 1
testrt = os.path.join(buildDir, 'testrt')
print(testrt)
returncode = subprocess.call([ testrt ])
if returncode == 0:
passed = passed + 1
total = total + 1
stdlib = os.path.join(buildDir, 'stdlib')
print(stdlib)
returncode = subprocess.call([ stdlib, '--sequential' ])
if returncode == 0:
passed = passed + 1
print('')
print('{0} test suites; {1} passed; {2} failed'.format(total, passed, total - passed))
if passed < total:
sys.exit(1)
ponyg = os.path.join(sourceDir, 'pony.g')
ponygNew = os.path.join(buildDir, 'pony.g.new')
with open(ponyg, 'r') as pg:
with open(ponygNew, 'r') as pgn:
if pg.read() != pgn.read():
print('Grammar files differ')
sys.exit(1)
ctx.add_post_fun(run_tests)
# subclass the build context for the test command,
# otherwise the context wouldn't be set up
from waflib.Build import BuildContext
class testContext(BuildContext):
cmd = 'test'
fun = 'test'