1414import shutil
1515import subprocess
1616import sys
17+ from typing import Dict , List , ItemsView
1718from contextlib import contextmanager
1819
1920from install_requirements import (
@@ -167,29 +168,45 @@ def build_args_parser() -> argparse.ArgumentParser:
167168 )
168169 return parser
169170
171+ # Helper to keep track of cmake define flags.
172+ class CmakeDefineFlags :
173+ def __init__ (self ):
174+ self ._values : Dict [str , str ] = {}
175+
176+ def add (self , name , value ):
177+ self ._values [name ] = value
170178
171- def handle_pybind (args , cmake_args , executorch_build_pybind ):
179+ def get (self , name ) -> str :
180+ return self ._values [name ]
181+
182+ def cmake_args (self ) -> List [str ]:
183+ return [f"-D{ name } ={ value } " for name , value in self ._values .items ()]
184+
185+ def items (self ) -> ItemsView [str , str ]:
186+ return self ._values .items ()
187+
188+
189+ def handle_pybind (args , cmake_define_flags ):
172190 # Flatten list of lists.
173191 args .pybind = list (itertools .chain (* args .pybind ))
174192 if "off" in args .pybind :
175193 if len (args .pybind ) != 1 :
176194 raise Exception (f"Cannot combine `off` with other pybinds: { args .pybind } " )
177- executorch_build_pybind = "OFF"
195+ cmake_define_flags . add ( "EXECUTORCH_BUILD_PYBIND" , "OFF" )
178196 else :
179197 for pybind_arg in args .pybind :
180198 if pybind_arg not in VALID_PYBINDS :
181199 raise Exception (
182200 f"Unrecognized pybind argument { pybind_arg } ; valid options are: { ', ' .join (VALID_PYBINDS )} "
183201 )
184202 if pybind_arg == "training" :
185- cmake_args += " -DEXECUTORCH_BUILD_EXTENSION_TRAINING=ON"
186- os .environ ["EXECUTORCH_BUILD_TRAINING" ] = "ON"
203+ cmake_define_flags .add ("EXECUTORCH_BUILD_EXTENSION_TRAINING" , "ON" )
187204 elif pybind_arg == "mps" :
188- cmake_args += " -DEXECUTORCH_BUILD_MPS= ON"
205+ cmake_define_flags . add ( "EXECUTORCH_BUILD_MPS" , " ON")
189206 else :
190- cmake_args += f" -DEXECUTORCH_BUILD_ { pybind_arg .upper ()} = ON"
191- executorch_build_pybind = "ON"
192- return executorch_build_pybind , cmake_args
207+ cmake_define_flags . add ( f"EXECUTORCH_BUILD_ { pybind_arg .upper ()} " , " ON")
208+
209+ cmake_define_flags . add ( "EXECUTORCH_BUILD_PYBIND" , "ON" )
193210
194211
195212def main (args ):
@@ -199,14 +216,14 @@ def main(args):
199216 parser = build_args_parser ()
200217 args = parser .parse_args ()
201218
202- EXECUTORCH_BUILD_PYBIND = ""
203- CMAKE_ARGS = os .getenv ("CMAKE_ARGS" , "" )
219+ cmake_define_flags = CmakeDefineFlags ()
220+ cmake_define_flags .add ("EXECUTORCH_BUILD_PYBIND" , "" )
221+
222+ cmake_args = [os .getenv ("CMAKE_ARGS" , "" )]
204223 use_pytorch_nightly = True
205224
206225 if args .pybind :
207- EXECUTORCH_BUILD_PYBIND , CMAKE_ARGS = handle_pybind (
208- args , CMAKE_ARGS , EXECUTORCH_BUILD_PYBIND
209- )
226+ handle_pybind (args , cmake_define_flags )
210227
211228 if args .clean :
212229 clean ()
@@ -221,15 +238,15 @@ def main(args):
221238 # If --pybind is not set explicitly for backends (e.g., --pybind xnnpack)
222239 # or is not turned off explicitly (--pybind off)
223240 # then install XNNPACK by default.
224- if EXECUTORCH_BUILD_PYBIND == "" :
225- EXECUTORCH_BUILD_PYBIND = "ON"
226- CMAKE_ARGS += " -DEXECUTORCH_BUILD_XNNPACK= ON"
241+ if cmake_define_flags . get ( " EXECUTORCH_BUILD_PYBIND" ) == "" :
242+ cmake_define_flags . add ( " EXECUTORCH_BUILD_PYBIND" , "ON" )
243+ cmake_define_flags . add ( "EXECUTORCH_BUILD_XNNPACK" , " ON")
227244
228245 # Use ClangCL on Windows.
229246 # ClangCL is an alias to Clang that configures it to work in an MSVC-compatible
230247 # mode. Using it on Windows to avoid compiler compatibility issues for MSVC.
231248 if os .name == "nt" :
232- CMAKE_ARGS += " -T ClangCL"
249+ cmake_args . append ( " -T ClangCL")
233250
234251 #
235252 # Install executorch pip package. This also makes `flatc` available on the path.
@@ -238,8 +255,10 @@ def main(args):
238255 #
239256
240257 # Set environment variables
241- os .environ ["EXECUTORCH_BUILD_PYBIND" ] = EXECUTORCH_BUILD_PYBIND
242- os .environ ["CMAKE_ARGS" ] = CMAKE_ARGS
258+ for key , value in cmake_define_flags .items ():
259+ os .environ [key ] = value
260+
261+ os .environ ["CMAKE_ARGS" ] = " " .join (cmake_args + cmake_define_flags .cmake_args ())
243262
244263 # Check if the required submodules are present and update them if not
245264 check_and_update_submodules ()
0 commit comments