Skip to content

Commit b323237

Browse files
audreytclaude
andcommitted
Add wasm64-unknown-unknown target support
- Parameterize target triple in cargo_build_wasm() and cargo_build_wasm_tests() - Extract --target from extra_options in build and test commands - Add --enable-memory64 to wasm-opt args for wasm64 targets - Construct CARGO_TARGET_*_RUNNER env var dynamically from target triple - Pass target triple through to cargo_test_wasm() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 74c8aab commit b323237

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

src/build/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn cargo_build_wasm(
7979
path: &Path,
8080
profile: BuildProfile,
8181
extra_options: &[String],
82+
target_triple: &str,
8283
) -> Result<String> {
8384
let msg = format!("{}Compiling to Wasm...", emoji::CYCLONE);
8485
PBAR.info(&msg);
@@ -111,9 +112,7 @@ pub fn cargo_build_wasm(
111112
}
112113
}
113114

114-
// If user has specified a custom --target in Cargo options, we shouldn't override it.
115-
// Otherwise, default to wasm32-unknown-unknown.
116-
cmd.env("CARGO_BUILD_TARGET", "wasm32-unknown-unknown");
115+
cmd.env("CARGO_BUILD_TARGET", target_triple);
117116

118117
// The `cargo` command is executed inside the directory at `path`, so relative paths set via extra options won't work.
119118
// To remedy the situation, all detected paths are converted to absolute paths.
@@ -192,7 +191,7 @@ pub fn cargo_build_wasm(
192191
/// * `path`: Path to the crate directory to build tests.
193192
/// * `debug`: Whether to build tests in `debug` mode.
194193
/// * `extra_options`: Additional parameters to pass to `cargo` when building tests.
195-
pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String]) -> Result<()> {
194+
pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String], target_triple: &str) -> Result<()> {
196195
let mut cmd = Command::new("cargo");
197196

198197
cmd.current_dir(path).arg("build").arg("--tests");
@@ -205,7 +204,7 @@ pub fn cargo_build_wasm_tests(path: &Path, debug: bool, extra_options: &[String]
205204
cmd.arg("--release");
206205
}
207206

208-
cmd.env("CARGO_BUILD_TARGET", "wasm32-unknown-unknown");
207+
cmd.env("CARGO_BUILD_TARGET", target_triple);
209208

210209
cmd.args(extra_options);
211210

src/command/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl Build {
388388
fn step_build_wasm(&mut self) -> Result<()> {
389389
info!("Building wasm...");
390390
let wasm_path =
391-
build::cargo_build_wasm(&self.crate_path, self.profile.clone(), &self.extra_options)?;
391+
build::cargo_build_wasm(&self.crate_path, self.profile.clone(), &self.extra_options, &self.target_triple)?;
392392
info!("wasm built at {wasm_path:#?}.");
393393
self.wasm_path = Some(wasm_path);
394394
Ok(())
@@ -475,6 +475,9 @@ impl Build {
475475
if self.reference_types {
476476
args.push("--enable-reference-types".into());
477477
}
478+
if self.target_triple.starts_with("wasm64") {
479+
args.push("--enable-memory64".into());
480+
}
478481
info!("executing wasm-opt with {:?}", args);
479482
wasm_opt::run(
480483
&self.cache,

src/command/test.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ pub struct Test {
9999
release: bool,
100100
test_runner_path: Option<PathBuf>,
101101
extra_options: Vec<String>,
102+
target_triple: String,
102103
}
103104

104105
type TestStep = fn(&mut Test) -> Result<()>;
@@ -138,6 +139,17 @@ impl Test {
138139
let crate_data = manifest::CrateData::new(&crate_path, None)?;
139140
let any_browser = chrome || firefox || safari;
140141

142+
let target_triple = {
143+
let mut iter = extra_options.iter();
144+
if iter.by_ref().any(|option| option == "--target") {
145+
iter.next().map(|s| s.as_str())
146+
} else {
147+
None
148+
}
149+
.unwrap_or("wasm32-unknown-unknown")
150+
.to_owned()
151+
};
152+
141153
if !node && !any_browser {
142154
bail!("Must specify at least one of `--node`, `--chrome`, `--firefox`, or `--safari`")
143155
}
@@ -164,6 +176,7 @@ impl Test {
164176
headless,
165177
release,
166178
test_runner_path: None,
179+
target_triple,
167180
extra_options,
168181
})
169182
}
@@ -251,7 +264,7 @@ impl Test {
251264

252265
fn step_check_for_wasm_target(&mut self) -> Result<()> {
253266
info!("Adding wasm-target...");
254-
build::wasm_target::check_for_wasm_target("wasm32-unknown-unknown")?;
267+
build::wasm_target::check_for_wasm_target(&self.target_triple)?;
255268
info!("Adding wasm-target was successful.");
256269
Ok(())
257270
}
@@ -267,7 +280,7 @@ impl Test {
267280
} else {
268281
&self.extra_options
269282
};
270-
build::cargo_build_wasm_tests(&self.crate_path, !self.release, extra_options)?;
283+
build::cargo_build_wasm_tests(&self.crate_path, !self.release, extra_options, &self.target_triple)?;
271284

272285
info!("Finished compiling tests to wasm.");
273286
Ok(())
@@ -311,17 +324,20 @@ impl Test {
311324
fn step_test_node(&mut self) -> Result<()> {
312325
assert!(self.node);
313326
info!("Running tests in node...");
327+
let runner_env = format!(
328+
"CARGO_TARGET_{}_RUNNER",
329+
self.target_triple.replace('-', "_").to_uppercase()
330+
);
331+
let runner_path = self.test_runner_path.as_ref().unwrap().to_str().unwrap().to_string();
314332
test::cargo_test_wasm(
315333
&self.crate_path,
316334
self.release,
317335
vec![
318-
(
319-
"CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER",
320-
&**self.test_runner_path.as_ref().unwrap(),
321-
),
322-
("WASM_BINDGEN_TEST_ONLY_NODE", "1".as_ref()),
336+
(runner_env, runner_path),
337+
("WASM_BINDGEN_TEST_ONLY_NODE".to_string(), "1".to_string()),
323338
],
324339
&self.extra_options,
340+
&self.target_triple,
325341
)?;
326342
info!("Finished running tests in node.");
327343
Ok(())
@@ -339,16 +355,15 @@ impl Test {
339355

340356
fn step_test_chrome(&mut self) -> Result<()> {
341357
let chromedriver = self.chromedriver.as_ref().unwrap().display().to_string();
342-
let chromedriver = chromedriver.as_str();
343358
info!(
344359
"Running tests in Chrome with chromedriver at {}",
345360
chromedriver
346361
);
347362

348363
let mut envs = self.webdriver_env();
349-
envs.push(("CHROMEDRIVER", chromedriver));
364+
envs.push(("CHROMEDRIVER".to_string(), chromedriver));
350365

351-
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
366+
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options, &self.target_triple)?;
352367
Ok(())
353368
}
354369

@@ -364,16 +379,15 @@ impl Test {
364379

365380
fn step_test_firefox(&mut self) -> Result<()> {
366381
let geckodriver = self.geckodriver.as_ref().unwrap().display().to_string();
367-
let geckodriver = geckodriver.as_str();
368382
info!(
369383
"Running tests in Firefox with geckodriver at {}",
370384
geckodriver
371385
);
372386

373387
let mut envs = self.webdriver_env();
374-
envs.push(("GECKODRIVER", geckodriver));
388+
envs.push(("GECKODRIVER".to_string(), geckodriver));
375389

376-
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
390+
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options, &self.target_triple)?;
377391
Ok(())
378392
}
379393

@@ -386,28 +400,31 @@ impl Test {
386400

387401
fn step_test_safari(&mut self) -> Result<()> {
388402
let safaridriver = self.safaridriver.as_ref().unwrap().display().to_string();
389-
let safaridriver = safaridriver.as_str();
390403
info!(
391404
"Running tests in Safari with safaridriver at {}",
392405
safaridriver
393406
);
394407

395408
let mut envs = self.webdriver_env();
396-
envs.push(("SAFARIDRIVER", safaridriver));
409+
envs.push(("SAFARIDRIVER".to_string(), safaridriver));
397410

398-
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options)?;
411+
test::cargo_test_wasm(&self.crate_path, self.release, envs, &self.extra_options, &self.target_triple)?;
399412
Ok(())
400413
}
401414

402-
fn webdriver_env(&self) -> Vec<(&'static str, &str)> {
403-
let test_runner = self.test_runner_path.as_ref().unwrap().to_str().unwrap();
415+
fn webdriver_env(&self) -> Vec<(String, String)> {
416+
let test_runner = self.test_runner_path.as_ref().unwrap().to_str().unwrap().to_string();
404417
info!("Using wasm-bindgen test runner at {}", test_runner);
418+
let runner_env = format!(
419+
"CARGO_TARGET_{}_RUNNER",
420+
self.target_triple.replace('-', "_").to_uppercase()
421+
);
405422
let mut envs = vec![
406-
("CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", test_runner),
407-
("WASM_BINDGEN_TEST_ONLY_WEB", "1"),
423+
(runner_env, test_runner),
424+
("WASM_BINDGEN_TEST_ONLY_WEB".to_string(), "1".to_string()),
408425
];
409426
if !self.headless {
410-
envs.push(("NO_HEADLESS", "1"));
427+
envs.push(("NO_HEADLESS".to_string(), "1".to_string()));
411428
}
412429
envs
413430
}

src/manifest/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ impl CrateData {
557557
return Ok(());
558558
}
559559
bail!(
560-
"crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \
560+
"crate-type must be cdylib to compile to WebAssembly. Add the following to your \
561561
Cargo.toml file:\n\n\
562562
[lib]\n\
563563
crate-type = [\"cdylib\", \"rlib\"]"

src/test/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn cargo_test_wasm<I, K, V>(
1616
release: bool,
1717
envs: I,
1818
extra_options: &[String],
19+
target_triple: &str,
1920
) -> Result<()>
2021
where
2122
I: IntoIterator<Item = (K, V)>,
@@ -35,7 +36,7 @@ where
3536
cmd.arg("--release");
3637
}
3738

38-
cmd.arg("--target").arg("wasm32-unknown-unknown");
39+
cmd.arg("--target").arg(target_triple);
3940

4041
cmd.args(extra_options);
4142

0 commit comments

Comments
 (0)