-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
130 lines (114 loc) · 3.98 KB
/
Copy pathbuild.rs
File metadata and controls
130 lines (114 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let workspace_root = manifest_dir.as_path();
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=Cargo.lock");
println!("cargo:rerun-if-changed=crates/mcp-run/Cargo.toml");
println!("cargo:rerun-if-changed=crates/mcp-run/src");
println!("cargo:rerun-if-env-changed=CLADDING_MCP_RUN_BIN");
println!("cargo:rerun-if-env-changed=CLADDING_RUN_REMOTE_BIN");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// Escape hatch: when prebuilt Linux helpers are provided (e.g. a macOS
// release build that cannot run a Linux container), copy them straight in
// and skip building mcp-run. Both must be set together.
if let (Ok(mcp_run), Ok(run_remote)) = (
env::var("CLADDING_MCP_RUN_BIN"),
env::var("CLADDING_RUN_REMOTE_BIN"),
) {
copy_bin(Path::new(&mcp_run), &out_dir.join("mcp-run"));
copy_bin(Path::new(&run_remote), &out_dir.join("run-remote"));
return;
}
let target_triple = env::var("TARGET").ok();
let build_target = env::var("CARGO_BUILD_TARGET").ok();
let effective_target = build_target.or(target_triple);
let (target_dir, release_target) = if cfg!(target_os = "linux") {
let target_dir = out_dir.join("mcp-run-target");
build_locally(workspace_root, &target_dir, effective_target.as_deref());
(target_dir, effective_target.as_deref())
} else {
let crate_dir = workspace_root.join("crates").join("mcp-run");
let target_dir = crate_dir.join("target");
build_with_podman(workspace_root);
(target_dir, None)
};
let release_dir = release_dir(&target_dir, release_target);
copy_bin(
&release_dir.join(bin_name("mcp-run")),
&out_dir.join("mcp-run"),
);
copy_bin(
&release_dir.join(bin_name("run-remote")),
&out_dir.join("run-remote"),
);
}
fn release_dir(target_dir: &Path, target: Option<&str>) -> PathBuf {
match target {
Some(target) => target_dir.join(target).join("release"),
None => target_dir.join("release"),
}
}
fn copy_bin(src: &Path, dst: &Path) {
fs::copy(src, dst).unwrap_or_else(|err| {
panic!(
"failed to copy {} to {}: {err}",
src.display(),
dst.display()
)
});
}
fn build_locally(workspace_root: &Path, target_dir: &Path, target: Option<&str>) {
let mut cargo = Command::new("cargo");
cargo.current_dir(workspace_root);
cargo.arg("build").arg("-p").arg("mcp-run").arg("--release");
cargo.arg("--bin").arg("mcp-run");
cargo.arg("--bin").arg("run-remote");
cargo.arg("--target-dir").arg(target_dir);
if let Some(target) = target {
cargo.arg("--target").arg(target);
}
let status = cargo
.status()
.expect("failed to run cargo build for mcp-run");
if !status.success() {
panic!("cargo build -p mcp-run failed");
}
}
fn build_with_podman(workspace_root: &Path) {
let status = Command::new("podman")
.arg("run")
.arg("--rm")
.arg("-e")
.arg("CARGO_TARGET_DIR=/work/crates/mcp-run/target")
.arg("-v")
.arg(format!("{}:/work", workspace_root.display()))
.arg("-w")
.arg("/work")
.arg("docker.io/library/rust:latest")
.arg("cargo")
.arg("build")
.arg("--manifest-path")
.arg("/work/Cargo.toml")
.arg("--release")
.arg("--locked")
.arg("--bin")
.arg("mcp-run")
.arg("--bin")
.arg("run-remote")
.status()
.expect("failed to run podman build for mcp-run");
if !status.success() {
panic!("podman cargo build for mcp-run failed");
}
}
fn bin_name(base: &str) -> String {
if cfg!(windows) {
format!("{base}.exe")
} else {
base.to_string()
}
}