Skip to content

Commit 0392d18

Browse files
authored
Merge pull request #328 from Smephite/flake-slang-fix
flake: fix build with slang feature
2 parents bb5f891 + cd6f11a commit 0392d18

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

crates/bender-slang/build.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ fn main() {
4545
vec!["-std=c++20"]
4646
};
4747

48+
// Allow overriding FetchContent source directories via environment variables.
49+
// This is used by Nix (and other sandboxed build systems) where network access
50+
// is blocked and dependencies must be pre-fetched.
51+
let slang_src_dir = std::env::var("SLANG_SRC_DIR").ok();
52+
let fmt_src_dir = std::env::var("FMT_SRC_DIR").ok();
53+
let mimalloc_src_dir = std::env::var("MIMALLOC_SRC_DIR").ok();
54+
4855
// Apply cmake configuration for Slang library
4956
slang_lib
5057
.define("SLANG_INCLUDE_TESTS", "OFF")
@@ -57,6 +64,16 @@ fn main() {
5764
.define("CMAKE_DISABLE_FIND_PACKAGE_Boost", "ON")
5865
.profile(cmake_profile);
5966

67+
if let Some(ref dir) = slang_src_dir {
68+
slang_lib.define("FETCHCONTENT_SOURCE_DIR_SLANG", dir);
69+
}
70+
if let Some(ref dir) = fmt_src_dir {
71+
slang_lib.define("FETCHCONTENT_SOURCE_DIR_FMT", dir);
72+
}
73+
if let Some(ref dir) = mimalloc_src_dir {
74+
slang_lib.define("FETCHCONTENT_SOURCE_DIR_MIMALLOC", dir);
75+
}
76+
6077
// Apply common defines and flags
6178
for (def, value) in common_cxx_defines.iter() {
6279
slang_lib.define(def, *value);
@@ -70,10 +87,17 @@ fn main() {
7087
let dst = slang_lib.build();
7188
// With FetchContent, cmake builds slang in a _deps subdirectory rather than
7289
// installing it. Point directly at the FetchContent build/source directories.
90+
// When source dirs are overridden, include paths come from those instead.
7391
let slang_lib_dir = dst.join("build/_deps/slang-build/lib");
74-
let slang_include_dir = dst.join("build/_deps/slang-src/include");
92+
let slang_include_dir = match slang_src_dir {
93+
Some(dir) => std::path::PathBuf::from(dir).join("include"),
94+
None => dst.join("build/_deps/slang-src/include"),
95+
};
7596
let slang_generated_include_dir = dst.join("build/_deps/slang-build/source");
76-
let fmt_include_dir = dst.join("build/_deps/fmt-src/include");
97+
let fmt_include_dir = match fmt_src_dir {
98+
Some(dir) => std::path::PathBuf::from(dir).join("include"),
99+
None => dst.join("build/_deps/fmt-src/include"),
100+
};
77101

78102
// Generate cpp/compile_flags.txt for clangd IDE support
79103
if !in_publish {
@@ -138,6 +162,9 @@ fn main() {
138162
println!("cargo:rerun-if-changed=cpp/rewriter.cpp");
139163
println!("cargo:rerun-if-changed=cpp/print.cpp");
140164
println!("cargo:rerun-if-changed=cpp/analysis.cpp");
165+
println!("cargo:rerun-if-env-changed=SLANG_SRC_DIR");
166+
println!("cargo:rerun-if-env-changed=FMT_SRC_DIR");
167+
println!("cargo:rerun-if-env-changed=MIMALLOC_SRC_DIR");
141168
}
142169

143170
// Generates cpp/compile_flags.txt so that clangd gets the correct include paths

flake.nix

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,78 @@
1818
system:
1919
let
2020
pkgs = nixpkgs.legacyPackages.${system};
21-
teraTemplFilter = path: _type: builtins.match ".*src/script_fmt/.*tera" path != null;
22-
benderFilter = path: type: ((craneLib.filterCargoSources path type) || (teraTemplFilter path type));
21+
22+
# Pre-fetched sources for bender-slang's cmake FetchContent dependencies.
23+
# The Nix sandbox blocks network access, so we fetch these ourselves and
24+
# inject them via FETCHCONTENT_SOURCE_DIR_* variables in build.rs.
25+
slangSrc = pkgs.fetchFromGitHub {
26+
owner = "MikePopoloski";
27+
repo = "slang";
28+
tag = "v11.0";
29+
hash = "sha256-popHzwX0qwv2POAl7/qX3e//OwJRXGtSl9xogpSn2LI=";
30+
};
31+
fmtSrc = pkgs.fetchFromGitHub {
32+
owner = "fmtlib";
33+
repo = "fmt";
34+
tag = "12.1.0";
35+
hash = "sha256-ZmI1Dv0ZabPlxa02OpERI47jp7zFfjpeWCy1WyuPYZ0=";
36+
};
37+
mimallocSrc = pkgs.fetchFromGitHub {
38+
owner = "microsoft";
39+
repo = "mimalloc";
40+
tag = "v3.3.2";
41+
hash = "sha256-GZ37qQVDe9jgMb4Coe5oKvgaLTspZDlSkS5rdy1MfUU=";
42+
};
2343

2444
craneLib = crane.mkLib pkgs;
45+
46+
# Source filter: cargo sources + tera templates + bender-slang C++/CMake files + test fixtures
47+
slangCrateFilter = path: _type:
48+
builtins.match ".*/crates/bender-slang/(cpp/.*|CMakeLists\\.txt|build\\.rs)" path != null;
49+
teraTemplFilter = path: _type: builtins.match ".*src/script_fmt/.*tera" path != null;
50+
testFixtureFilter = path: _type: builtins.match ".*/tests/.*" path != null;
51+
benderFilter = path: type:
52+
(craneLib.filterCargoSources path type)
53+
|| (teraTemplFilter path type)
54+
|| (slangCrateFilter path type)
55+
|| (testFixtureFilter path type);
56+
2557
src = pkgs.lib.cleanSourceWith {
2658
src = ./.;
2759
filter = benderFilter;
2860
name = "bender-source";
2961
};
3062

31-
bender = craneLib.buildPackage {
63+
commonArgs = {
3264
inherit src;
3365
strictDeps = true;
66+
nativeBuildInputs = with pkgs; [
67+
cmake
68+
python3
69+
];
3470
};
71+
72+
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
73+
74+
bender = craneLib.buildPackage (commonArgs // {
75+
inherit cargoArtifacts;
76+
77+
nativeCheckInputs = [ pkgs.gitMinimal ];
78+
79+
# Point build.rs at pre-fetched FetchContent sources
80+
SLANG_SRC_DIR = slangSrc;
81+
FMT_SRC_DIR = fmtSrc;
82+
MIMALLOC_SRC_DIR = mimallocSrc;
83+
84+
# owo-colors wraps test assertions in ANSI codes when TERM is set,
85+
# causing string-matching tests to fail in the sandbox.
86+
preCheck = "export NO_COLOR=1";
87+
88+
postCheck = ''
89+
patchShebangs --build tests
90+
BENDER="$PWD/target/''${CARGO_BUILD_TARGET:-}/release/bender" tests/run_all.sh
91+
'';
92+
});
3593
in
3694
{
3795
packages = {

0 commit comments

Comments
 (0)