build.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. from pathlib import Path
  5. # NOTE: Do NOT import anything here that needs be built (e.g. params)
  6. from openpilot.common.basedir import BASEDIR
  7. from openpilot.common.spinner import Spinner
  8. from openpilot.common.text_window import TextWindow
  9. from openpilot.system.hardware import AGNOS
  10. from openpilot.common.swaglog import cloudlog, add_file_handler
  11. from openpilot.system.version import is_dirty
  12. MAX_CACHE_SIZE = 4e9 if "CI" in os.environ else 2e9
  13. CACHE_DIR = Path("/data/scons_cache" if AGNOS else "/tmp/scons_cache")
  14. TOTAL_SCONS_NODES = 2560
  15. MAX_BUILD_PROGRESS = 100
  16. def build(spinner: Spinner, dirty: bool = False, minimal: bool = False) -> None:
  17. env = os.environ.copy()
  18. env['SCONS_PROGRESS'] = "1"
  19. nproc = os.cpu_count()
  20. if nproc is None:
  21. nproc = 2
  22. extra_args = ["--minimal"] if minimal else []
  23. # building with all cores can result in using too
  24. # much memory, so retry with less parallelism
  25. compile_output: list[bytes] = []
  26. for n in (nproc, nproc/2, 1):
  27. compile_output.clear()
  28. scons: subprocess.Popen = subprocess.Popen(["scons", f"-j{int(n)}", "--cache-populate", *extra_args], cwd=BASEDIR, env=env, stderr=subprocess.PIPE)
  29. assert scons.stderr is not None
  30. # Read progress from stderr and update spinner
  31. while scons.poll() is None:
  32. try:
  33. line = scons.stderr.readline()
  34. if line is None:
  35. continue
  36. line = line.rstrip()
  37. prefix = b'progress: '
  38. if line.startswith(prefix):
  39. i = int(line[len(prefix):])
  40. spinner.update_progress(MAX_BUILD_PROGRESS * min(1., i / TOTAL_SCONS_NODES), 100.)
  41. elif len(line):
  42. compile_output.append(line)
  43. print(line.decode('utf8', 'replace'))
  44. except Exception:
  45. pass
  46. if scons.returncode == 0:
  47. break
  48. if scons.returncode != 0:
  49. # Read remaining output
  50. if scons.stderr is not None:
  51. compile_output += scons.stderr.read().split(b'\n')
  52. # Build failed log errors
  53. error_s = b"\n".join(compile_output).decode('utf8', 'replace')
  54. add_file_handler(cloudlog)
  55. cloudlog.error("scons build failed\n" + error_s)
  56. # Show TextWindow
  57. spinner.close()
  58. if not os.getenv("CI"):
  59. with TextWindow("openpilot failed to build\n \n" + error_s) as t:
  60. t.wait_for_exit()
  61. exit(1)
  62. # enforce max cache size
  63. cache_files = [f for f in CACHE_DIR.rglob('*') if f.is_file()]
  64. cache_files.sort(key=lambda f: f.stat().st_mtime)
  65. cache_size = sum(f.stat().st_size for f in cache_files)
  66. for f in cache_files:
  67. if cache_size < MAX_CACHE_SIZE:
  68. break
  69. cache_size -= f.stat().st_size
  70. f.unlink()
  71. if __name__ == "__main__":
  72. spinner = Spinner()
  73. spinner.update_progress(0, 100)
  74. build(spinner, is_dirty(), minimal = AGNOS)