|  |  | @ -1,18 +1,22 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #!/usr/bin/env python3 |  |  |  | #!/usr/bin/env python3 | 
			
		
	
		
		
			
				
					
					|  |  |  | import os |  |  |  | import os | 
			
		
	
		
		
			
				
					
					|  |  |  | import shutil |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | import subprocess |  |  |  | import subprocess | 
			
		
	
		
		
			
				
					
					|  |  |  | import sys |  |  |  | import sys | 
			
		
	
		
		
			
				
					
					|  |  |  | import time |  |  |  | import time | 
			
		
	
		
		
			
				
					
					|  |  |  | import textwrap |  |  |  | import textwrap | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | from pathlib import Path | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | # NOTE: Do NOT import anything here that needs be built (e.g. params) |  |  |  | # NOTE: Do NOT import anything here that needs be built (e.g. params) | 
			
		
	
		
		
			
				
					
					|  |  |  | from common.basedir import BASEDIR |  |  |  | from common.basedir import BASEDIR | 
			
		
	
		
		
			
				
					
					|  |  |  | from common.spinner import Spinner |  |  |  | from common.spinner import Spinner | 
			
		
	
		
		
			
				
					
					|  |  |  | from common.text_window import TextWindow |  |  |  | from common.text_window import TextWindow | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | from selfdrive.hardware import TICI | 
			
		
	
		
		
			
				
					
					|  |  |  | from selfdrive.swaglog import cloudlog, add_file_handler |  |  |  | from selfdrive.swaglog import cloudlog, add_file_handler | 
			
		
	
		
		
			
				
					
					|  |  |  | from selfdrive.version import dirty |  |  |  | from selfdrive.version import dirty | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | MAX_CACHE_SIZE = 2e9 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | CACHE_DIR = Path("/data/scons_cache" if TICI else "/tmp/scons_cache") | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | TOTAL_SCONS_NODES = 2405 |  |  |  | TOTAL_SCONS_NODES = 2405 | 
			
		
	
		
		
			
				
					
					|  |  |  | MAX_BUILD_PROGRESS = 100 |  |  |  | MAX_BUILD_PROGRESS = 100 | 
			
		
	
		
		
			
				
					
					|  |  |  | PREBUILT = os.path.exists(os.path.join(BASEDIR, 'prebuilt')) |  |  |  | PREBUILT = os.path.exists(os.path.join(BASEDIR, 'prebuilt')) | 
			
		
	
	
		
		
			
				
					|  |  | @ -26,7 +30,7 @@ def build(spinner, dirty=False): | 
			
		
	
		
		
			
				
					
					|  |  |  |   j_flag = "" if nproc is None else f"-j{nproc - 1}" |  |  |  |   j_flag = "" if nproc is None else f"-j{nproc - 1}" | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   for retry in [True, False]: |  |  |  |   for retry in [True, False]: | 
			
		
	
		
		
			
				
					
					|  |  |  |     scons = subprocess.Popen(["scons", j_flag], cwd=BASEDIR, env=env, stderr=subprocess.PIPE) |  |  |  |     scons = subprocess.Popen(["scons", j_flag, "--cache-populate"], cwd=BASEDIR, env=env, stderr=subprocess.PIPE) | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     compile_output = [] |  |  |  |     compile_output = [] | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -60,8 +64,6 @@ def build(spinner, dirty=False): | 
			
		
	
		
		
			
				
					
					|  |  |  |             print("....%d" % i) |  |  |  |             print("....%d" % i) | 
			
		
	
		
		
			
				
					
					|  |  |  |             time.sleep(1) |  |  |  |             time.sleep(1) | 
			
		
	
		
		
			
				
					
					|  |  |  |           subprocess.check_call(["scons", "-c"], cwd=BASEDIR, env=env) |  |  |  |           subprocess.check_call(["scons", "-c"], cwd=BASEDIR, env=env) | 
			
		
	
		
		
			
				
					
					|  |  |  |           shutil.rmtree("/tmp/scons_cache", ignore_errors=True) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |           shutil.rmtree("/data/scons_cache", ignore_errors=True) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         else: |  |  |  |         else: | 
			
		
	
		
		
			
				
					
					|  |  |  |           print("scons build failed after retry") |  |  |  |           print("scons build failed after retry") | 
			
		
	
		
		
			
				
					
					|  |  |  |           sys.exit(1) |  |  |  |           sys.exit(1) | 
			
		
	
	
		
		
			
				
					|  |  | @ -82,6 +84,16 @@ def build(spinner, dirty=False): | 
			
		
	
		
		
			
				
					
					|  |  |  |     else: |  |  |  |     else: | 
			
		
	
		
		
			
				
					
					|  |  |  |       break |  |  |  |       break | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   # 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() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | if __name__ == "__main__" and not PREBUILT: |  |  |  | if __name__ == "__main__" and not PREBUILT: | 
			
		
	
		
		
			
				
					
					|  |  |  |   spinner = Spinner() |  |  |  |   spinner = Spinner() | 
			
		
	
	
		
		
			
				
					|  |  | 
 |