Skip to content

Commit 93f6112

Browse files
committed
remove env vars
1 parent 644b7dd commit 93f6112

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

install_executorch.py

+31-31
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import subprocess
1616
import sys
1717
from contextlib import contextmanager
18+
from typing import List
1819

1920
from install_requirements import (
2021
install_requirements,
@@ -168,28 +169,24 @@ def build_args_parser() -> argparse.ArgumentParser:
168169
return parser
169170

170171

171-
def handle_pybind(args, cmake_args, executorch_build_pybind):
172+
def _list_pybind_defines(args) -> List[str]:
173+
cmake_args = []
172174
# Flatten list of lists.
173175
args.pybind = list(itertools.chain(*args.pybind))
174-
if "off" in args.pybind:
175-
if len(args.pybind) != 1:
176-
raise Exception(f"Cannot combine `off` with other pybinds: {args.pybind}")
177-
executorch_build_pybind = "OFF"
178-
else:
179-
for pybind_arg in args.pybind:
180-
if pybind_arg not in VALID_PYBINDS:
181-
raise Exception(
182-
f"Unrecognized pybind argument {pybind_arg}; valid options are: {', '.join(VALID_PYBINDS)}"
183-
)
184-
if pybind_arg == "training":
185-
cmake_args += " -DEXECUTORCH_BUILD_EXTENSION_TRAINING=ON"
186-
os.environ["EXECUTORCH_BUILD_TRAINING"] = "ON"
187-
elif pybind_arg == "mps":
188-
cmake_args += " -DEXECUTORCH_BUILD_MPS=ON"
189-
else:
190-
cmake_args += f" -DEXECUTORCH_BUILD_{pybind_arg.upper()}=ON"
191-
executorch_build_pybind = "ON"
192-
return executorch_build_pybind, cmake_args
176+
if ("off" in args.pybind) and (len(args.pybind) != 1):
177+
raise Exception(f"Cannot combine `off` with other pybinds: {args.pybind}")
178+
179+
for pybind_arg in args.pybind:
180+
if pybind_arg not in VALID_PYBINDS:
181+
raise Exception(
182+
f"Unrecognized pybind argument {pybind_arg}; valid options are: {', '.join(VALID_PYBINDS)}"
183+
)
184+
if pybind_arg == "training":
185+
cmake_args.append("-DEXECUTORCH_BUILD_EXTENSION_TRAINING=ON")
186+
else:
187+
cmake_args.append(f"-DEXECUTORCH_BUILD_{pybind_arg.upper()}=ON")
188+
189+
return cmake_args
193190

194191

195192
def main(args):
@@ -199,14 +196,15 @@ def main(args):
199196
parser = build_args_parser()
200197
args = parser.parse_args()
201198

202-
EXECUTORCH_BUILD_PYBIND = ""
203-
CMAKE_ARGS = os.getenv("CMAKE_ARGS", "")
199+
has_pybindings = False
200+
cmake_args = [os.getenv("CMAKE_ARGS", "")]
204201
use_pytorch_nightly = True
205202

206203
if args.pybind:
207-
EXECUTORCH_BUILD_PYBIND, CMAKE_ARGS = handle_pybind(
208-
args, CMAKE_ARGS, EXECUTORCH_BUILD_PYBIND
209-
)
204+
pybind_defines = _list_pybind_defines(args)
205+
if len(pybind_defines) > 0:
206+
has_pybindings = True
207+
cmake_args += pybind_defines
210208

211209
if args.clean:
212210
clean()
@@ -221,15 +219,15 @@ def main(args):
221219
# If --pybind is not set explicitly for backends (e.g., --pybind xnnpack)
222220
# or is not turned off explicitly (--pybind off)
223221
# then install XNNPACK by default.
224-
if EXECUTORCH_BUILD_PYBIND == "":
225-
EXECUTORCH_BUILD_PYBIND = "ON"
226-
CMAKE_ARGS += " -DEXECUTORCH_BUILD_XNNPACK=ON"
222+
if not has_pybindings:
223+
has_pybindings = True
224+
cmake_args.append("-DEXECUTORCH_BUILD_XNNPACK=ON")
227225

228226
# Use ClangCL on Windows.
229227
# ClangCL is an alias to Clang that configures it to work in an MSVC-compatible
230228
# mode. Using it on Windows to avoid compiler compatibility issues for MSVC.
231229
if os.name == "nt":
232-
CMAKE_ARGS += " -T ClangCL"
230+
cmake_args.append("-T ClangCL")
233231

234232
#
235233
# Install executorch pip package. This also makes `flatc` available on the path.
@@ -238,8 +236,10 @@ def main(args):
238236
#
239237

240238
# Set environment variables
241-
os.environ["EXECUTORCH_BUILD_PYBIND"] = EXECUTORCH_BUILD_PYBIND
242-
os.environ["CMAKE_ARGS"] = CMAKE_ARGS
239+
if has_pybindings:
240+
cmake_args.append("-DEXECUTORCH_BUILD_PYBIND=ON")
241+
242+
os.environ["CMAKE_ARGS"] = " ".join(cmake_args)
243243

244244
# Check if the required submodules are present and update them if not
245245
check_and_update_submodules()

setup.py

+31-10
Original file line numberDiff line numberDiff line change
@@ -60,42 +60,63 @@
6060

6161
from distutils import log
6262
from distutils.sysconfig import get_python_lib
63+
from functools import cache
6364
from pathlib import Path
64-
from typing import List, Optional
65+
from typing import Dict, List, Optional
6566

6667
from setuptools import Extension, setup
6768
from setuptools.command.build import build
6869
from setuptools.command.build_ext import build_ext
6970
from setuptools.command.build_py import build_py
7071

7172

73+
@cache
74+
def _cmake_args_defines() -> Dict[str, str]:
75+
result = {}
76+
77+
args = re.split(r"\s+", os.environ.get("CMAKE_ARGS", ""))
78+
for arg in args:
79+
if arg.startswith("-D") and "=" in arg:
80+
arg_key, value = arg.split("=")
81+
key = arg_key[2:] # Remove the leading "-D"
82+
result[key] = value
83+
84+
return result
85+
86+
7287
class ShouldBuild:
7388
"""Indicates whether to build various components."""
7489

7590
@staticmethod
76-
def _is_env_enabled(env_var: str, default: bool = False) -> bool:
77-
val = os.environ.get(env_var, None)
78-
if val is None:
79-
return default
80-
if val in ("OFF", "0", ""):
91+
def _is_truthy(value: Optional[str]) -> bool:
92+
if (value is None) or (value.lower() in ("off", "0", "")):
8193
return False
8294
return True
8395

96+
@staticmethod
97+
def _is_cmake_arg_enabled(var: str, default: bool) -> bool:
98+
value = _cmake_args_defines().get(var, None)
99+
if value is None:
100+
return default
101+
return ShouldBuild._is_truthy(value)
102+
84103
@classmethod
85104
def pybindings(cls) -> bool:
86-
return cls._is_env_enabled("EXECUTORCH_BUILD_PYBIND", default=False)
105+
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_PYBIND", default=False)
87106

88107
@classmethod
89108
def training(cls) -> bool:
90-
return cls._is_env_enabled("EXECUTORCH_BUILD_TRAINING", default=False)
109+
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_TRAINING", default=False)
91110

92111
@classmethod
93112
def llama_custom_ops(cls) -> bool:
94-
return cls._is_env_enabled("EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT", default=True)
113+
return cls._is_cmake_arg_enabled(
114+
"EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT", default=True
115+
)
95116

96117
@classmethod
97118
def flatc(cls) -> bool:
98-
return cls._is_env_enabled("EXECUTORCH_BUILD_FLATC", default=True)
119+
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_FLATC", default=True)
99120

100121

101122
class Version:

0 commit comments

Comments
 (0)