14
14
import shutil
15
15
import subprocess
16
16
import sys
17
+ from typing import Dict , List , Iterator , Tuple
17
18
from contextlib import contextmanager
18
19
19
20
from install_requirements import (
@@ -167,29 +168,45 @@ def build_args_parser() -> argparse.ArgumentParser:
167
168
)
168
169
return parser
169
170
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
170
178
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 ) -> Iterator [Tuple [str , str ]]:
186
+ return ((name , value ) for name , value in self ._values .items ())
187
+
188
+
189
+ def handle_pybind (args , cmake_define_flags ):
172
190
# Flatten list of lists.
173
191
args .pybind = list (itertools .chain (* args .pybind ))
174
192
if "off" in args .pybind :
175
193
if len (args .pybind ) != 1 :
176
194
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" )
178
196
else :
179
197
for pybind_arg in args .pybind :
180
198
if pybind_arg not in VALID_PYBINDS :
181
199
raise Exception (
182
200
f"Unrecognized pybind argument { pybind_arg } ; valid options are: { ', ' .join (VALID_PYBINDS )} "
183
201
)
184
202
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" )
187
204
elif pybind_arg == "mps" :
188
- cmake_args += " -DEXECUTORCH_BUILD_MPS= ON"
205
+ cmake_define_flags . add ( "EXECUTORCH_BUILD_MPS" , " ON")
189
206
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" )
193
210
194
211
195
212
def main (args ):
@@ -199,14 +216,14 @@ def main(args):
199
216
parser = build_args_parser ()
200
217
args = parser .parse_args ()
201
218
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" , "" )]
204
223
use_pytorch_nightly = True
205
224
206
225
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 )
210
227
211
228
if args .clean :
212
229
clean ()
@@ -221,15 +238,15 @@ def main(args):
221
238
# If --pybind is not set explicitly for backends (e.g., --pybind xnnpack)
222
239
# or is not turned off explicitly (--pybind off)
223
240
# 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")
227
244
228
245
# Use ClangCL on Windows.
229
246
# ClangCL is an alias to Clang that configures it to work in an MSVC-compatible
230
247
# mode. Using it on Windows to avoid compiler compatibility issues for MSVC.
231
248
if os .name == "nt" :
232
- CMAKE_ARGS += " -T ClangCL"
249
+ cmake_args . append ( " -T ClangCL")
233
250
234
251
#
235
252
# Install executorch pip package. This also makes `flatc` available on the path.
@@ -238,8 +255,10 @@ def main(args):
238
255
#
239
256
240
257
# 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 ())
243
262
244
263
# Check if the required submodules are present and update them if not
245
264
check_and_update_submodules ()
0 commit comments