-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSConstruct
271 lines (226 loc) · 8.2 KB
/
SConstruct
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
# \author Neil Turton <[email protected]>
# \brief The Nanotube project
# \date 2020-06-10
#
###########################################################################
# Copyright (C) 2023, Advanced Micro Devices, Inc. All rights reserved.
# SPDX-License-Identifier: MIT
###########################################################################
import os
import os.path
import re
import subprocess
import sys
top_dir = os.getcwd()
sys.path.append(os.path.join(top_dir, 'py-lib'))
# This is to work around a bug in Python. The project_defs file used
# to be in site_scons, but was moved to py-lib. If the .pyc exists in
# site_scons then it will be used in preference. Delete it to avoid
# the module becoming stale.
_project_defs_pyc = os.path.join(top_dir, "site_scons/project_defs.pyc")
if os.path.exists(_project_defs_pyc):
os.unlink(_project_defs_pyc)
import project_defs
EnsurePythonVersion(2,7)
EnsureSConsVersion(2,5,1)
SConscriptChdir(0)
# Check the compiler used to build LLVM matches the configured
# compiler.
def check_llvm_cc(env):
if env.get('LLVM_CONFIG') == None:
return
llvm_obj_root = env['LLVM_OBJ_ROOT']
cc_re = re.compile(r'CMAKE_C_COMPILER:(FILEPATH|STRING)=')
cxx_re = re.compile(r'CMAKE_CXX_COMPILER:(FILEPATH|STRING)=')
cmake_cache = "%s/CMakeCache.txt" % llvm_obj_root
if not os.path.exists(cmake_cache):
print("WARNING: Could not determine the C++ compiler used to build LLVM.")
print(" Looked in: %s" % (llvm_obj_root,))
return
f = open(cmake_cache, "r")
llvm_cc = None
llvm_cxx = None
for line in f.readlines():
m = cc_re.match(line)
if m:
llvm_cc = line[m.end():].rstrip()
m = cxx_re.match(line)
if m:
llvm_cxx = line[m.end():].rstrip()
f.close()
if llvm_cc == None:
sys.stderr.write("Could not determine C compiler used to build LLVM.\n")
sys.exit(1)
if llvm_cxx == None:
sys.stderr.write("Could not determine C++ compiler used to build LLVM.\n")
sys.exit(1)
if llvm_cc != env['CC']:
sys.stderr.write("C compiler used to build LLVM does not match configured compiler.\n")
sys.stderr.write(" LLVM: %s\n" % (llvm_cc,))
sys.stderr.write(" Configured: %s\n" % (env['CC'],))
sys.exit(1)
if llvm_cxx != env['CXX']:
sys.stderr.write("C++ compiler used to build LLVM does not match configured compiler.\n")
sys.stderr.write(" LLVM: %s\n" % (llvm_cxx,))
sys.stderr.write(" Configured: %s\n" % (env['CXX'],))
sys.exit(1)
def check_config(env):
check_llvm_cc(env)
def process_config(env):
c = config(str(env['BUILD_TOP']), ARGUMENTS)
for var,val in c.items():
env[var] = val
get_llvm_info(env)
if GetOption('clean'):
return
check_config(env)
if c.need_save():
c.save()
def capture_output(args):
try:
out = subprocess.check_output(args, universal_newlines=True)
except subprocess.CalledProcessError as e:
sys.stderr.write("Command %s returned exit code %s\n" %
(args, e.returncode))
sys.exit(1)
except Exception as e:
sys.stderr.write("Error running %s: %s\n" %
(args, e))
sys.exit(1)
return out.rstrip('\n')
Export('capture_output')
def get_llvm_info(env):
llvm_config = env.get('LLVM_CONFIG')
if llvm_config == None:
return
if not os.path.exists(llvm_config):
sys.stderr.write("ERROR: Could not find llvm-config at '%s'.\n" %
(llvm_config,))
sys.exit(1)
env['LLVM_VERSION'] = capture_output([llvm_config, "--version"])
env['LLVM_SRC_ROOT'] = capture_output([llvm_config, "--src-root"])
env['LLVM_OBJ_ROOT'] = capture_output([llvm_config, "--obj-root"])
env['LLVM_BIN_DIR'] = capture_output([llvm_config, "--bindir"])
env['LLVM_LIB_DIR'] = capture_output([llvm_config, "--libdir"])
env['LLVM_CXX_FLAGS'] = capture_output([llvm_config, "--cxxflags"])
env['LLVM_LD_FLAGS'] = capture_output([llvm_config, "--ldflags"])
env['CLANG'] = '$LLVM_BIN_DIR/clang'
env['LLC'] = '$LLVM_BIN_DIR/llc'
env['LLVM_AS'] = '$LLVM_BIN_DIR/llvm-as'
env['LLVM_DIS'] = '$LLVM_BIN_DIR/llvm-dis'
env['LLVM_LINK'] = '$LLVM_BIN_DIR/llvm-link'
for var in ('LLVM_CONFIG', 'LLVM_VERSION', 'LLVM_SRC_ROOT',):
print("%s = %s" % (var, env[var]))
def add_subdir(old_env, subdir):
env = old_env.Clone()
env['SOURCE_DIR'] = old_env['SOURCE_DIR'].Dir(subdir)
env['BUILD_DIR'] = old_env['BUILD_DIR'].Dir(subdir)
env.SConscript(env['BUILD_DIR'].File('SConscript'),
exports=['env'])
def add_libs(env, lib_name):
for t, n in project_defs.lib_defs[lib_name]:
if t == 'file':
env.Append(LIBS=[env['BUILD_TOP'].File(n)])
else:
assert t == 'lib'
env.Append(LIBS=[n])
def lib_deps(env, lib_name):
deps = []
for t, n in project_defs.lib_defs[lib_name]:
if t == 'file':
deps.append(env['BUILD_TOP'].File(n))
return deps
def add_libnanotube(env):
add_libs(env, 'nanotube')
def build_all(env):
build_top = env['BUILD_TOP']
# When cleaning the build directory, remove the build directory
# itself.
Clean(build_top, build_top)
# Build the subdirectories.
subdirs = (
'back_end',
'front_end',
'libnt',
'testing',
)
for subdir in subdirs:
env.VariantDir(env['BUILD_TOP'].Dir(subdir),
env['SOURCE_TOP'].Dir(subdir),
duplicate=0)
env.add_subdir(subdir)
env.Alias('all', '$BUILD_TOP')
Default('all')
def run_tests(env):
opts = ""
tests = "testing"
extra = env.get('EXTRA_TESTS')
if extra:
opts = "-D NT_DIR=" + top_dir
tests += " " + os.path.join(extra, "testing")
target = env.Command(
'run_tests',
[
'all',
],
[
( 'scripts/locate_tool --run llvm-lit ' + opts + ' -v ' + tests )
]
)
AlwaysBuild(target)
def set_pie(env, pie):
if pie:
# Enable PIC for llc builds
# NB: On Ubuntu, the linker (GCC) tries to build PIE by default. Instruct
# llc to do the same for the .o files it generates
env['LLCFLAGS'] = ['-relocation-model=pic']
env['LINKFLAGS'] = ['-pie']
else:
# Disable PIC / PIE in the linker
# NB: On Ubuntu, using GCC as the linker does PIE by default, but clang
# does not generate PIC / PIE object files => disable in the linking step
env['LLCFLAGS'] = ['-relocation-model=static']
env['LINKFLAGS'] = ['-no-pie']
def main():
source_top = Dir(".")
build_top = Dir("build")
env = Environment(tools = [
'default',
tool_bitfile,
tool_bitfile_obj,
tool_build_kernel_test,
tool_llvm_asm,
tool_nanotube_be,
tool_nanotube_opt,
tool_llvm_link,
])
env['SOURCE_TOP'] = source_top
env['SOURCE_DIR'] = source_top
env['BUILD_TOP'] = build_top
env['BUILD_TOP_ABS'] = build_top.abspath
env['BUILD_DIR'] = build_top
env['CPPPATH'] = [source_top.Dir('include')]
# Set the C/C++ compiler flags:
# CFLAGS - Used for C only
# CCFLAGS - Used for C and C++
# CXXFLAGS - Used for C++ only
env['CFLAGS'] = project_defs.CFLAGS
env['CCFLAGS'] = project_defs.CCFLAGS
env['CXXFLAGS'] = project_defs.CXXFLAGS
env['GCC_CCFLAGS'] = project_defs.GCC_CCFLAGS
set_pie(env, False);
env['NANOTUBE_OPT'] = build_top.File('nanotube_opt')
env['NANOTUBE_BE'] = build_top.File('nanotube_back_end')
env['LIB_NANOTUBE'] = build_top.File(project_defs.LIB_NANOTUBE)
env['BUILD_KERNEL_TEST'] = source_top.File('testing/scripts/build_kernel_test')
sconsign_file = str(build_top.File(".sconsign.dblite"))
if not GetOption('clean') or os.path.exists(sconsign_file):
SConsignFile(sconsign_file)
process_config(env)
env.AddMethod(add_subdir, 'add_subdir')
env.AddMethod(add_libs, 'add_libs')
env.AddMethod(lib_deps, 'lib_deps')
env.AddMethod(add_libnanotube, 'add_libnanotube')
build_all(env)
run_tests(env)
main()