import glob
Import ( ' env ' , ' envCython ' , ' arch ' , ' cereal ' , ' messaging ' , ' common ' , ' gpucommon ' , ' visionipc ' , ' transformations ' )
lenv = env . Clone ( )
lenvCython = envCython . Clone ( )
libs = [ cereal , messaging , visionipc , gpucommon , common , ' capnp ' , ' kj ' , ' pthread ' ]
frameworks = [ ]
common_src = [
" models/commonmodel.cc " ,
" transforms/loadyuv.cc " ,
" transforms/transform.cc " ,
]
# OpenCL is a framework on Mac
if arch == " Darwin " :
frameworks + = [ ' OpenCL ' ]
else :
libs + = [ ' OpenCL ' ]
# Set path definitions
for pathdef , fn in { ' TRANSFORM ' : ' transforms/transform.cl ' , ' LOADYUV ' : ' transforms/loadyuv.cl ' } . items ( ) :
for xenv in ( lenv , lenvCython ) :
xenv [ ' CXXFLAGS ' ] . append ( f ' -D { pathdef } _PATH= \\ " { File ( fn ) . abspath } \\ " ' )
# Compile cython
cython_libs = envCython [ " LIBS " ] + libs
commonmodel_lib = lenv . Library ( ' commonmodel ' , common_src )
lenvCython . Program ( ' models/commonmodel_pyx.so ' , ' models/commonmodel_pyx.pyx ' , LIBS = [ commonmodel_lib , * cython_libs ] , FRAMEWORKS = frameworks )
tinygrad_files = [ " # " + x for x in glob . glob ( env . Dir ( " #tinygrad_repo " ) . relpath + " /** " , recursive = True , root_dir = env . Dir ( " # " ) . abspath ) if ' pycache ' not in x ]
# Get model metadata
fn = File ( " models/supercombo " ) . abspath
script_files = [ File ( Dir ( " #selfdrive/modeld " ) . File ( " get_model_metadata.py " ) . abspath ) ]
cmd = f ' python3 { Dir ( " #selfdrive/modeld " ) . abspath } /get_model_metadata.py { fn } .onnx '
lenv . Command ( fn + " _metadata.pkl " , [ fn + " .onnx " ] + tinygrad_files + script_files , cmd )
# Compile tinygrad model
pythonpath_string = ' PYTHONPATH= " $ {PYTHONPATH} : ' + env . Dir ( " #tinygrad_repo " ) . abspath + ' " '
if arch == ' larch64 ' :
device_string = ' QCOM=1 '
else :
device_string = ' CLANG=1 IMAGE=0 '
for model_name in [ ' supercombo ' , ' dmonitoring_model ' ] :
fn = File ( f " models/ { model_name } " ) . abspath
cmd = f ' { pythonpath_string } { device_string } python3 { Dir ( " #tinygrad_repo " ) . abspath } /examples/openpilot/compile3.py { fn } .onnx { fn } _tinygrad.pkl '
lenv . Command ( fn + " _tinygrad.pkl " , [ fn + " .onnx " ] + tinygrad_files , cmd )