Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit 0253b68

Browse files
authored
Let build2cmake generate metadata.json (#328)
We were generating it from Nix first, but then we need the same logic on the Windows side. So, instead, let `build2cmake` generate file and let the Nix/Windows builders put it in place.
1 parent fc6344a commit 0253b68

File tree

11 files changed

+55
-13
lines changed

11 files changed

+55
-13
lines changed

build2cmake/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use config::{Backend, Build, BuildCompat};
2020
mod fileset;
2121
use fileset::FileSet;
2222

23+
mod metadata;
24+
2325
mod version;
2426

2527
#[derive(Parser, Debug)]

build2cmake/src/metadata.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Deserialize, Serialize)]
4+
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
5+
pub struct Metadata {
6+
python_depends: Vec<String>,
7+
}
8+
9+
impl Metadata {
10+
pub fn new(python_depends: impl Into<Vec<String>>) -> Self {
11+
Self {
12+
python_depends: python_depends.into(),
13+
}
14+
}
15+
}

build2cmake/src/templates/windows.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ function(add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME)
161161
${PYTHON_FILES}
162162
${LOCAL_INSTALL_DIR}/
163163

164+
# Copy metadata.json if it exists
165+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
166+
${CMAKE_SOURCE_DIR}/metadata.json
167+
${LOCAL_INSTALL_DIR}/
168+
164169
COMMENT "Copying shared library and Python files to ${LOCAL_INSTALL_DIR}"
165170
COMMAND_EXPAND_LISTS
166171
)

build2cmake/src/torch/common.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use itertools::Itertools;
33
use minijinja::{context, Environment};
44

55
use crate::config::{Backend, General};
6+
use crate::metadata::Metadata;
67
use crate::FileSet;
78

89
pub fn write_pyproject_toml(
@@ -32,3 +33,18 @@ pub fn write_pyproject_toml(
3233

3334
Ok(())
3435
}
36+
37+
pub fn write_metadata(backend: Backend, general: &General, file_set: &mut FileSet) -> Result<()> {
38+
let writer = file_set.entry("metadata.json");
39+
40+
let python_depends = general
41+
.python_depends()
42+
.chain(general.backend_python_depends(backend))
43+
.collect::<Result<Vec<_>>>()?;
44+
45+
let metadata = Metadata::new(python_depends);
46+
47+
serde_json::to_writer(writer, &metadata)?;
48+
49+
Ok(())
50+
}

build2cmake/src/torch/cpu.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::{common::write_pyproject_toml, kernel_ops_identifier};
88
use crate::{
99
config::{Backend, Build, Kernel, Torch},
1010
fileset::FileSet,
11+
torch::common::write_metadata,
1112
version::Version,
1213
};
1314

@@ -52,6 +53,8 @@ pub fn write_torch_ext_cpu(
5253

5354
write_torch_registration_macros(&mut file_set)?;
5455

56+
write_metadata(Backend::Cpu, &build.general, &mut file_set)?;
57+
5558
Ok(file_set)
5659
}
5760

build2cmake/src/torch/cuda.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use minijinja::{context, Environment};
1010
use super::common::write_pyproject_toml;
1111
use super::kernel_ops_identifier;
1212
use crate::config::{Backend, Build, Dependency, Kernel, Torch};
13+
use crate::torch::common::write_metadata;
1314
use crate::version::Version;
1415
use crate::FileSet;
1516

@@ -65,6 +66,8 @@ pub fn write_torch_ext_cuda(
6566

6667
write_torch_registration_macros(&mut file_set)?;
6768

69+
write_metadata(backend, &build.general, &mut file_set)?;
70+
6871
Ok(file_set)
6972
}
7073

build2cmake/src/torch/metal.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use super::{common::write_pyproject_toml, kernel_ops_identifier};
88
use crate::{
99
config::{Backend, Build, Kernel, Torch},
1010
fileset::FileSet,
11+
torch::common::write_metadata,
1112
version::Version,
1213
};
1314

@@ -54,6 +55,8 @@ pub fn write_torch_ext_metal(
5455

5556
write_torch_registration_macros(&mut file_set)?;
5657

58+
write_metadata(Backend::Metal, &build.general, &mut file_set)?;
59+
5760
Ok(file_set)
5861
}
5962

build2cmake/src/torch/noarch.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use minijinja::{context, Environment};
77
use crate::{
88
config::{Backend, Build, General, Torch},
99
fileset::FileSet,
10-
torch::kernel_ops_identifier,
10+
torch::{common::write_metadata, kernel_ops_identifier},
1111
};
1212

1313
pub fn write_torch_ext_noarch(
@@ -30,6 +30,8 @@ pub fn write_torch_ext_noarch(
3030
&mut file_set,
3131
)?;
3232

33+
write_metadata(backend, &build.general, &mut file_set)?;
34+
3335
Ok(file_set)
3436
}
3537

build2cmake/src/torch/xpu.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use minijinja::{context, Environment};
99
use super::common::write_pyproject_toml;
1010
use super::kernel_ops_identifier;
1111
use crate::config::{Backend, Build, Dependency, Kernel, Torch};
12+
use crate::torch::common::write_metadata;
1213
use crate::version::Version;
1314
use crate::FileSet;
1415

@@ -53,6 +54,8 @@ pub fn write_torch_ext_xpu(
5354

5455
write_torch_registration_macros(&mut file_set)?;
5556

57+
write_metadata(Backend::Xpu, &build.general, &mut file_set)?;
58+
5659
Ok(file_set)
5760
}
5861

lib/torch-extension/arch.nix

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ let
8383

8484
moduleName = builtins.replaceStrings [ "-" ] [ "_" ] kernelName;
8585

86-
metadata = builtins.toJSON {
87-
python-depends = pythonDeps;
88-
};
89-
90-
metadataFile = writeText "metadata.json" metadata;
91-
9286
# On Darwin, we need the host's xcrun for `xcrun metal` to compile Metal shaders.
9387
# It's not supported by the nixpkgs shim.
9488
xcrunHost = writeScriptBin "xcrunHost" ''
@@ -255,7 +249,7 @@ stdenv.mkDerivation (prevAttrs: {
255249
mkdir $out/${moduleName}
256250
cp ${./compat.py} $out/${moduleName}/__init__.py
257251
258-
cp ${metadataFile} $out/metadata.json
252+
cp ../metadata.json $out/
259253
''
260254
+ (lib.optionalString (stripRPath && stdenv.hostPlatform.isLinux)) ''
261255
find $out/ -name '*.so' \

0 commit comments

Comments
 (0)