Skip to content

Commit 872060f

Browse files
authored
Improve consistency of environment variables for build (#2524)
1 parent 8aeaa88 commit 872060f

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ build-wheel:
3232
build-wheel-debug:
3333
BUILD_MODE=debug uv build --wheel
3434

35+
.PHONY: build-dry-run
36+
build-dry-run:
37+
DRY_RUN=true uv run --active --no-sync build.py
38+
3539
.PHONY: clean
3640
clean:
3741
find . -type d -name "__pycache" -print0 | xargs -0 rm -rf
@@ -55,7 +59,7 @@ format:
5559

5660
.PHONY: pre-commit
5761
pre-commit:
58-
uv run --active --no-sync pre-commit run --all-files
62+
CC=clang CXX=clang++ VIRTUAL_ENV="/home/twitu/Code/nautilus_trader/.venv" uv run --active --no-sync pre-commit run --all-files
5963

6064
.PHONY: ruff
6165
ruff:

build.py

+53-11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
COPY_TO_SOURCE = os.getenv("COPY_TO_SOURCE", "true").lower() == "true"
4242
# If PyO3 only then don't build C extensions to reduce compilation time
4343
PYO3_ONLY = os.getenv("PYO3_ONLY", "").lower() != ""
44+
# If dry run only print the commands that would be executed
45+
DRY_RUN = bool(os.getenv("DRY_RUN", ""))
4446

4547
# Precision mode configuration
4648
# https://nautilustrader.io/docs/nightly/getting_started/installation#precision-mode
@@ -120,6 +122,13 @@
120122
RUST_LIBS: list[str] = [str(path) for path in RUST_LIB_PATHS]
121123

122124

125+
def _set_feature_flags() -> list[str]:
126+
if HIGH_PRECISION:
127+
return ["--all-features"]
128+
else:
129+
return ["--features", "ffi,python,extension-module"]
130+
131+
123132
def _build_rust_libs() -> None:
124133
print("Compiling Rust libraries...")
125134

@@ -130,11 +139,7 @@ def _build_rust_libs() -> None:
130139

131140
build_options = " --release" if BUILD_MODE == "release" else ""
132141

133-
if HIGH_PRECISION:
134-
features = ["--all-features"]
135-
else:
136-
# Enable features needed for main build, but not high_precision
137-
features = ["--features", "ffi,python,extension-module"]
142+
features = _set_feature_flags()
138143

139144
cmd_args = [
140145
"cargo",
@@ -372,6 +377,38 @@ def _strip_unneeded_symbols() -> None:
372377
raise RuntimeError(f"Error when stripping symbols.\n{e}") from e
373378

374379

380+
def show_rustanalyzer_settings() -> None:
381+
"""
382+
Show appropriate vscode settings for the build.
383+
"""
384+
import json
385+
386+
# Set environment variables
387+
settings: dict[str, object] = {}
388+
for key in [
389+
"rust-analyzer.check.extraEnv",
390+
"rust-analyzer.runnables.extraEnv",
391+
"rust-analyzer.cargo.features",
392+
]:
393+
settings[key] = {
394+
"CC": os.environ["CC"],
395+
"CXX": os.environ["CXX"],
396+
"VIRTUAL_ENV": os.environ["VIRTUAL_ENV"],
397+
}
398+
399+
# Set features
400+
features = _set_feature_flags()
401+
if features[0] == "--all-features":
402+
settings["rust-analyzer.cargo.features"] = "all"
403+
settings["rust-analyzer.check.features"] = "all"
404+
else:
405+
settings["rust-analyzer.cargo.features"] = features[1].split(",")
406+
settings["rust-analyzer.check.features"] = features[1].split(",")
407+
408+
print("Set these rust analyzer settings in .vscode/settings.json")
409+
print(json.dumps(settings, indent=2))
410+
411+
375412
def build() -> None:
376413
"""
377414
Construct the extensions and distribution.
@@ -435,9 +472,14 @@ def print_env_var_if_exists(key: str) -> None:
435472
print_env_var_if_exists("LDFLAGS")
436473
print_env_var_if_exists("LD_LIBRARY_PATH")
437474
print_env_var_if_exists("RUSTFLAGS")
438-
439-
print("\nStarting build...")
440-
ts_start = dt.datetime.now(dt.UTC)
441-
build()
442-
print(f"Build time: {dt.datetime.now(dt.UTC) - ts_start}")
443-
print("\033[32m" + "Build completed" + "\033[0m")
475+
print_env_var_if_exists("DRY_RUN")
476+
477+
if DRY_RUN:
478+
print("Dry run")
479+
show_rustanalyzer_settings()
480+
else:
481+
print("\nStarting build...")
482+
ts_start = dt.datetime.now(dt.UTC)
483+
build()
484+
print(f"Build time: {dt.datetime.now(dt.UTC) - ts_start}")
485+
print("\033[32m" + "Build completed" + "\033[0m")

0 commit comments

Comments
 (0)