2021-03-05 18:03:23 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import subprocess
|
2021-05-23 11:13:39 +08:00
|
|
|
from pathlib import Path
|
2023-08-23 11:58:43 +08:00
|
|
|
from typing import List
|
2021-03-05 18:03:23 +08:00
|
|
|
|
|
|
|
# NOTE: Do NOT import anything here that needs be built (e.g. params)
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.common.basedir import BASEDIR
|
|
|
|
from openpilot.common.spinner import Spinner
|
|
|
|
from openpilot.common.text_window import TextWindow
|
|
|
|
from openpilot.system.hardware import AGNOS
|
2023-12-07 09:27:51 +08:00
|
|
|
from openpilot.common.swaglog import cloudlog, add_file_handler
|
2023-08-21 11:49:55 +08:00
|
|
|
from openpilot.system.version import is_dirty
|
2021-03-05 18:03:23 +08:00
|
|
|
|
2021-12-01 06:51:31 +08:00
|
|
|
MAX_CACHE_SIZE = 4e9 if "CI" in os.environ else 2e9
|
2022-06-02 21:20:51 +08:00
|
|
|
CACHE_DIR = Path("/data/scons_cache" if AGNOS else "/tmp/scons_cache")
|
2021-05-23 11:13:39 +08:00
|
|
|
|
2023-08-15 08:34:19 +08:00
|
|
|
TOTAL_SCONS_NODES = 2560
|
2021-03-26 00:27:15 +08:00
|
|
|
MAX_BUILD_PROGRESS = 100
|
2021-03-05 18:03:23 +08:00
|
|
|
PREBUILT = os.path.exists(os.path.join(BASEDIR, 'prebuilt'))
|
|
|
|
|
2023-11-11 08:55:25 +08:00
|
|
|
def build(spinner: Spinner, dirty: bool = False, minimal: bool = False) -> None:
|
2021-03-05 18:03:23 +08:00
|
|
|
env = os.environ.copy()
|
|
|
|
env['SCONS_PROGRESS'] = "1"
|
|
|
|
nproc = os.cpu_count()
|
2023-08-23 11:58:43 +08:00
|
|
|
if nproc is None:
|
|
|
|
nproc = 2
|
|
|
|
|
2023-11-11 08:55:25 +08:00
|
|
|
extra_args = ["--minimal"] if minimal else []
|
|
|
|
|
2023-08-23 11:58:43 +08:00
|
|
|
# building with all cores can result in using too
|
|
|
|
# much memory, so retry with less parallelism
|
|
|
|
compile_output: List[bytes] = []
|
|
|
|
for n in (nproc, nproc/2, 1):
|
|
|
|
compile_output.clear()
|
2023-11-11 08:55:25 +08:00
|
|
|
scons: subprocess.Popen = subprocess.Popen(["scons", f"-j{int(n)}", "--cache-populate", *extra_args], cwd=BASEDIR, env=env, stderr=subprocess.PIPE)
|
2023-08-23 11:58:43 +08:00
|
|
|
assert scons.stderr is not None
|
|
|
|
|
|
|
|
# Read progress from stderr and update spinner
|
|
|
|
while scons.poll() is None:
|
|
|
|
try:
|
|
|
|
line = scons.stderr.readline()
|
|
|
|
if line is None:
|
|
|
|
continue
|
|
|
|
line = line.rstrip()
|
|
|
|
|
|
|
|
prefix = b'progress: '
|
|
|
|
if line.startswith(prefix):
|
|
|
|
i = int(line[len(prefix):])
|
|
|
|
spinner.update_progress(MAX_BUILD_PROGRESS * min(1., i / TOTAL_SCONS_NODES), 100.)
|
|
|
|
elif len(line):
|
|
|
|
compile_output.append(line)
|
|
|
|
print(line.decode('utf8', 'replace'))
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if scons.returncode == 0:
|
|
|
|
break
|
2022-06-28 23:39:22 +08:00
|
|
|
|
|
|
|
if scons.returncode != 0:
|
|
|
|
# Read remaining output
|
2023-08-23 11:58:43 +08:00
|
|
|
if scons.stderr is not None:
|
|
|
|
compile_output += scons.stderr.read().split(b'\n')
|
2022-06-28 23:39:22 +08:00
|
|
|
|
|
|
|
# Build failed log errors
|
2023-08-23 11:58:43 +08:00
|
|
|
error_s = b"\n".join(compile_output).decode('utf8', 'replace')
|
2022-06-28 23:39:22 +08:00
|
|
|
add_file_handler(cloudlog)
|
|
|
|
cloudlog.error("scons build failed\n" + error_s)
|
|
|
|
|
|
|
|
# Show TextWindow
|
|
|
|
spinner.close()
|
|
|
|
if not os.getenv("CI"):
|
|
|
|
with TextWindow("openpilot failed to build\n \n" + error_s) as t:
|
|
|
|
t.wait_for_exit()
|
|
|
|
exit(1)
|
|
|
|
|
2021-05-23 11:13:39 +08:00
|
|
|
# enforce max cache size
|
|
|
|
cache_files = [f for f in CACHE_DIR.rglob('*') if f.is_file()]
|
|
|
|
cache_files.sort(key=lambda f: f.stat().st_mtime)
|
|
|
|
cache_size = sum(f.stat().st_size for f in cache_files)
|
|
|
|
for f in cache_files:
|
|
|
|
if cache_size < MAX_CACHE_SIZE:
|
|
|
|
break
|
|
|
|
cache_size -= f.stat().st_size
|
|
|
|
f.unlink()
|
|
|
|
|
2021-03-05 18:03:23 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__" and not PREBUILT:
|
|
|
|
spinner = Spinner()
|
|
|
|
spinner.update_progress(0, 100)
|
2023-11-11 08:55:25 +08:00
|
|
|
build(spinner, is_dirty(), minimal = AGNOS)
|