forked from commaai/openpilot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSConstruct
More file actions
389 lines (326 loc) · 11.3 KB
/
SConstruct
File metadata and controls
389 lines (326 loc) · 11.3 KB
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
import os
import subprocess
import sys
import sysconfig
import platform
import numpy as np
import SCons.Errors
SCons.Warnings.warningAsException(True)
# pending upstream fix - https://github.com/SCons/scons/issues/4461
# SetOption('warn', 'all')
Decider('MD5-timestamp')
SetOption('num_jobs', max(1, int(os.cpu_count() / 2)))
AddOption('--kaitai', action='store_true', help='Regenerate kaitai struct parsers')
AddOption('--asan', action='store_true', help='turn on ASAN')
AddOption('--ubsan', action='store_true', help='turn on UBSan')
AddOption('--coverage', action='store_true', help='build with test coverage options')
AddOption('--clazy', action='store_true', help='build with clazy')
AddOption('--ccflags', action='store', type='string', default='', help='pass arbitrary flags over the command line')
AddOption('--external-sconscript', action='store', metavar='FILE', dest='external_sconscript', help='add an external SConscript to the build')
AddOption('--mutation', action='store_true', help='generate mutation-ready code')
AddOption('--with-valhalla',
action='store_true',
dest='with_valhalla',
default=False,
help='Build local Valhalla routing engine (offline navigation)')
AddOption(
'--minimal',
action='store_false',
dest='extras',
default=os.path.exists(File('#.lfsconfig').abspath), # minimal by default on release branch (where there's no LFS)
help='the minimum build to run openpilot. no tests, tools, etc.',
)
## Architecture name breakdown (arch)
## - aarch64: linux rk3588/rk3576 aarch64
## - x86_64: linux pc x64
## - Darwin: mac x64 or arm64
real_arch = arch = subprocess.check_output(["uname", "-m"], encoding='utf8').rstrip()
if platform.system() == "Darwin":
arch = "Darwin"
brew_prefix = subprocess.check_output(['brew', '--prefix'], encoding='utf8').strip()
assert arch in ["aarch64", "x86_64", "Darwin"]
# Detect specific SoC for platform-specific optimizations (RK3576 vs RK3588)
soc = None
if arch == "aarch64":
try:
with open('/proc/device-tree/compatible', 'r') as f:
compatible = f.read()
if 'rk3588' in compatible:
soc = 'rk3588'
elif 'rk3576' in compatible:
soc = 'rk3576'
except (FileNotFoundError, OSError):
pass
# Both RK3576 and RK3588 use same build artifacts (aarch64), but soc can be used for future optimizations
lenv = {
"PATH": os.environ['PATH'],
"PYTHONPATH": Dir("#").abspath + ':' + Dir(f"#third_party/acados").abspath,
"ACADOS_SOURCE_DIR": Dir("#third_party/acados").abspath,
"ACADOS_PYTHON_INTERFACE_PATH": Dir("#third_party/acados/interfaces/acados_template").abspath,
"TERA_PATH": Dir("#").abspath + f"/third_party/acados/{arch}/t_renderer",
}
rpath = []
cflags = []
cxxflags = []
cpppath = []
rpath += []
# MacOS
if arch == "Darwin":
libpath = [
f"#third_party/libyuv/build/{arch}",
f"#third_party/catch2/build/lib",
f"#third_party/raylib/build/raylib",
f"#third_party/acados/{arch}/lib",
f"{brew_prefix}/lib",
f"{brew_prefix}/opt/openssl@3.0/lib",
"/System/Library/Frameworks/OpenGL.framework/Libraries",
]
cflags += ["-DGL_SILENCE_DEPRECATION"]
cxxflags += ["-DGL_SILENCE_DEPRECATION"]
cpppath += [
f"{brew_prefix}/include",
f"{brew_prefix}/opt/openssl@3.0/include",
]
# Linux
else:
libpath = [
f"#third_party/acados/{arch}/lib",
f"#third_party/libyuv/build/{arch}",
f"#third_party/catch2/build/lib",
f"#third_party/raylib/build/raylib",
"/usr/lib",
"/usr/local/lib",
]
if arch == "aarch64":
cflags += ["-DROCKCHIP"]
cxxflags += ["-DROCKCHIP"]
if GetOption('asan'):
ccflags = ["-fsanitize=address", "-fno-omit-frame-pointer"]
ldflags = ["-fsanitize=address"]
elif GetOption('ubsan'):
ccflags = ["-fsanitize=undefined"]
ldflags = ["-fsanitize=undefined"]
else:
ccflags = []
ldflags = []
# no --as-needed on mac linker
if arch != "Darwin":
ldflags += ["-Wl,--as-needed", "-Wl,--no-undefined"]
ccflags_option = GetOption('ccflags')
if ccflags_option:
ccflags += ccflags_option.split(' ')
env = Environment(
ENV=lenv,
CCFLAGS=[
"-g",
"-fPIC",
"-O2",
"-Wunused",
"-Werror",
"-Wshadow",
"-Wno-unknown-warning-option",
"-Wno-inconsistent-missing-override",
"-Wno-c99-designator",
"-Wno-reorder-init-list",
"-Wno-vla-cxx-extension",
]
+ cflags
+ ccflags,
CPPPATH=cpppath
+ [
"#",
"#third_party/acados/include",
"#third_party/acados/include/blasfeo/include",
"#third_party/acados/include/hpipm/include",
"#third_party/catch2/src",
"#third_party/libyuv/include",
"#third_party/json11",
"#third_party",
"#third_party/raylib/src",
"#msgq",
],
CC='clang',
CXX='clang++',
LINKFLAGS=ldflags,
RPATH=rpath,
CFLAGS=["-std=gnu11"] + cflags,
CXXFLAGS=["-std=c++1z"] + cxxflags,
LIBPATH=libpath
+ [
"#msgq_repo",
"#third_party",
"#selfdrive/pandad",
"#common",
"#rednose/helpers",
],
CYTHONCFILESUFFIX=".cpp",
COMPILATIONDB_USE_ABSPATH=True,
REDNOSE_ROOT="#",
tools=["default", "cython", "compilation_db", "rednose_filter"],
toolpath=["#site_scons/site_tools", "#rednose_repo/site_scons/site_tools"],
)
if arch == "Darwin":
# RPATH is not supported on macOS, instead use the linker flags
darwin_rpath_link_flags = [f"-Wl,-rpath,{path}" for path in env["RPATH"]]
env["LINKFLAGS"] += darwin_rpath_link_flags
env.CompilationDatabase('compile_commands.json')
# Setup cache dir
# Use /data for cache on embedded platforms (RK3588/RK3576); /tmp on dev PCs
cache_dir = '/data/scons_cache' if arch == "aarch64" else '/tmp/scons_cache'
CacheDir(cache_dir)
Clean(["."], cache_dir)
node_interval = 5
node_count = 0
def progress_function(node):
global node_count
node_count += node_interval
sys.stderr.write("progress: %d\n" % node_count)
if os.environ.get('SCONS_PROGRESS'):
Progress(progress_function, interval=node_interval)
# Cython build environment
py_include = sysconfig.get_paths()['include']
envCython = env.Clone()
envCython["CPPPATH"] += [py_include, np.get_include()]
envCython["CCFLAGS"] += ["-Wno-#warnings", "-Wno-shadow", "-Wno-deprecated-declarations"]
envCython["CCFLAGS"].remove("-Werror")
envCython["LIBS"] = []
if arch == "Darwin":
envCython["LINKFLAGS"] = ["-bundle", "-undefined", "dynamic_lookup"] + darwin_rpath_link_flags
else:
envCython["LINKFLAGS"] = ["-pthread", "-shared"]
np_version = SCons.Script.Value(np.__version__)
Export('envCython', 'np_version')
# Build third_party submodules that need compilation
# libyuv - CMake build
libyuv_build_dir = Dir("#third_party/libyuv/build").abspath
libyuv_lib = File(f"#third_party/libyuv/build/{arch}/libyuv.a")
if not libyuv_lib.exists():
print("Building libyuv from submodule...")
os.makedirs(libyuv_build_dir, exist_ok=True)
subprocess.run(["cmake", "-S", "third_party/libyuv", "-B", libyuv_build_dir,
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={arch}",
f"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY={arch}"], check=True)
subprocess.run(["cmake", "--build", libyuv_build_dir, "-j", str(os.cpu_count())], check=True)
# catch2 v3 - CMake build (header-only in v2, library in v3)
catch2_build_dir = Dir("#third_party/catch2/build").abspath
catch2_lib = File("#third_party/catch2/build/libCatch2.a")
if not catch2_lib.exists():
print("Building catch2 from submodule...")
os.makedirs(catch2_build_dir, exist_ok=True)
subprocess.run(["cmake", "-S", "third_party/catch2", "-B", catch2_build_dir,
"-DBUILD_TESTING=OFF", "-DCATCH_INSTALL_DOCS=OFF"], check=True)
subprocess.run(["cmake", "--build", catch2_build_dir, "-j", str(os.cpu_count())], check=True)
# raylib - CMake build
raylib_build_dir = Dir("#third_party/raylib/build").abspath
raylib_lib = File(f"#third_party/raylib/build/raylib/libraylib.a")
if not raylib_lib.exists():
print("Building raylib from submodule...")
os.makedirs(raylib_build_dir, exist_ok=True)
subprocess.run(["cmake", "-S", "third_party/raylib", "-B", raylib_build_dir,
"-DBUILD_EXAMPLES=OFF", "-DBUILD_GAMES=OFF", "-DPLATFORM=Desktop"], check=True)
subprocess.run(["cmake", "--build", raylib_build_dir, "-j", str(os.cpu_count())], check=True)
# Qt build environment
qt_env = env.Clone()
qt_modules = ["Widgets", "Gui", "Core", "Network", "Concurrent", "DBus", "Xml"]
qt_libs = []
if arch == "Darwin":
qt_env['QTDIR'] = f"{brew_prefix}/opt/qt@5"
qt_dirs = [
os.path.join(qt_env['QTDIR'], "include"),
]
qt_dirs += [f"{qt_env['QTDIR']}/include/Qt{m}" for m in qt_modules]
qt_env["LINKFLAGS"] += ["-F" + os.path.join(qt_env['QTDIR'], "lib")]
qt_env["FRAMEWORKS"] += [f"Qt{m}" for m in qt_modules] + ["OpenGL"]
qt_env.AppendENVPath('PATH', os.path.join(qt_env['QTDIR'], "bin"))
else:
qt_install_prefix = subprocess.check_output(['qmake', '-query', 'QT_INSTALL_PREFIX'], encoding='utf8').strip()
qt_install_headers = subprocess.check_output(['qmake', '-query', 'QT_INSTALL_HEADERS'], encoding='utf8').strip()
qt_env['QTDIR'] = qt_install_prefix
qt_dirs = [
f"{qt_install_headers}",
]
qt_gui_path = os.path.join(qt_install_headers, "QtGui")
qt_gui_dirs = [d for d in os.listdir(qt_gui_path) if os.path.isdir(os.path.join(qt_gui_path, d))]
qt_dirs += (
[
f"{qt_install_headers}/QtGui/{qt_gui_dirs[0]}/QtGui",
]
if qt_gui_dirs
else []
)
qt_dirs += [f"{qt_install_headers}/Qt{m}" for m in qt_modules]
qt_libs = [f"Qt5{m}" for m in qt_modules]
if arch != "Darwin":
qt_libs += ["GL"]
qt_env['QT3DIR'] = qt_env['QTDIR']
qt_env.Tool('qt3')
qt_env['CPPPATH'] += qt_dirs + ["#third_party/qrcode"]
qt_flags = [
"-D_REENTRANT",
"-DQT_NO_DEBUG",
"-DQT_WIDGETS_LIB",
"-DQT_GUI_LIB",
"-DQT_CORE_LIB",
"-DQT_MESSAGELOGCONTEXT",
]
qt_env['CXXFLAGS'] += qt_flags
qt_env['LIBPATH'] += [
'#selfdrive/ui',
]
qt_env['LIBS'] = qt_libs
if GetOption("clazy"):
checks = [
"level0",
"level1",
"no-range-loop",
"no-non-pod-global-static",
]
qt_env['CXX'] = 'clazy'
qt_env['ENV']['CLAZY_IGNORE_DIRS'] = qt_dirs[0]
qt_env['ENV']['CLAZY_CHECKS'] = ','.join(checks)
Export('env', 'qt_env', 'arch', 'real_arch')
# Build common module
SConscript(['common/SConscript'])
Import('_common', '_gpucommon')
common = [_common, 'json11', 'zmq']
gpucommon = [_gpucommon]
Export('common', 'gpucommon')
# Build messaging (cereal + msgq + socketmaster + their dependencies)
# Enable swaglog include in submodules
env_swaglog = env.Clone()
env_swaglog['CXXFLAGS'].append('-DSWAGLOG="\\"common/swaglog.h\\""')
SConscript(['msgq_repo/SConscript'], exports={'env': env_swaglog})
# opendbc_repo removed - using vehicled instead
SConscript(['cereal/SConscript'])
Import('socketmaster', 'msgq')
messaging = [
socketmaster,
msgq,
'capnp',
'kj',
]
Export('messaging')
# panda removed - safety moved to vehicled
# Build rednose library
SConscript(['rednose/SConscript'])
# Build Valhalla (optional, for offline routing)
if GetOption('with_valhalla'):
from site_scons.valhalla_build import build_valhalla
valhalla_bin_dir = build_valhalla(env)
if valhalla_bin_dir:
env['VALHALLA_BIN_DIR'] = valhalla_bin_dir
# Build system services
SConscript(
[
'system/ubloxd/SConscript',
'system/loggerd/SConscript',
]
)
# Build openpilot
SConscript(['third_party/SConscript'])
SConscript(['selfdrive/SConscript'])
if GetOption('extras'):
SConscript(['tools/replay/SConscript'])
external_sconscript = GetOption('external_sconscript')
if external_sconscript:
SConscript([external_sconscript])