Skip to content

Commit d6998e2

Browse files
authored
Rollup merge of #160981 - bjorn3:proc_macro_refactors7, r=Mark-Simulacrum
Partially support building and locating wasm proc-macros For building this currently only removes the crate type check and doesn't implement crate metadata support for wasip2 or the proc-macro ABI. And for locating wasip2 needs support for loading crate metadata. Crate metadata building and loading should work for wasip1. Part of #160389 r? @Mark-Simulacrum
2 parents 34c47da + ea4e56a commit d6998e2

15 files changed

Lines changed: 98 additions & 15 deletions

File tree

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ fn print_crate_info(
659659
println_info!("{}", targets.join("\n"));
660660
}
661661
HostTuple => println_info!("{}", rustc_session::config::host_tuple()),
662+
WasmProcMacroTuple => println_info!("{}", sess.wasm_proc_macro_tuple),
662663
Sysroot => println_info!("{}", sess.opts.sysroot.path().display()),
663664
TargetLibdir => println_info!("{}", sess.target_tlib_path.dir.display()),
664665
TargetSpecJson => {

compiler/rustc_metadata/src/creader.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,21 @@ impl CStore {
718718
// Load the proc macro crate for the host
719719
proc_macro_locator.for_proc_macro(sess, path_kind);
720720

721-
let Some(host_result) =
721+
if let Some(host_result) =
722722
self.load(&mut proc_macro_locator, &mut CrateRejections::default())?
723-
else {
724-
return Ok(None);
725-
};
723+
{
724+
Ok(Some((host_result, None)))
725+
} else if sess.opts.unstable_opts.wasm_proc_macros {
726+
// Load the proc macro crate for wasm
727+
proc_macro_locator.for_wasm_proc_macro(sess, path_kind);
726728

727-
Ok(Some((host_result, None)))
729+
match self.load(&mut proc_macro_locator, &mut CrateRejections::default())? {
730+
Some(host_result) => Ok(Some((host_result, None))),
731+
None => Ok(None),
732+
}
733+
} else {
734+
Ok(None)
735+
}
728736
}
729737
}
730738

compiler/rustc_metadata/src/locator.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ impl<'a> CrateLocator<'a> {
360360
self.path_kind = path_kind;
361361
}
362362

363+
pub(crate) fn for_wasm_proc_macro(&mut self, sess: &'a Session, path_kind: PathKind) {
364+
self.is_proc_macro = true;
365+
self.target = &sess.wasm_proc_macro_target;
366+
self.tuple = sess.wasm_proc_macro_tuple.clone();
367+
self.filesearch = sess.wasm_proc_macro_filesearch();
368+
self.path_kind = path_kind;
369+
}
370+
363371
pub(crate) fn maybe_load_library_crate(
364372
&self,
365373
crate_rejections: &mut CrateRejections,
@@ -734,6 +742,8 @@ impl<'a> CrateLocator<'a> {
734742
&self,
735743
crate_rejections: &mut CrateRejections,
736744
) -> Result<Option<Library>, CrateError> {
745+
debug!("find_commandline_library {}", self.crate_name);
746+
737747
// First, filter out all libraries that look suspicious. We only accept
738748
// files which actually exist that have the correct naming scheme for
739749
// rlibs/dylibs.

compiler/rustc_session/src/config/print_request.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub enum PrintKind {
4848
TargetSpecJson,
4949
TargetSpecJsonSchema,
5050
TlsModels,
51+
WasmProcMacroTuple,
5152
// tidy-alphabetical-end
5253
}
5354

@@ -82,6 +83,7 @@ impl PrintKind {
8283
TargetSpecJson => "target-spec-json",
8384
TargetSpecJsonSchema => "target-spec-json-schema",
8485
TlsModels => "tls-models",
86+
WasmProcMacroTuple => "wasm-proc-macro-tuple",
8587
// tidy-alphabetical-end
8688
}
8789
}
@@ -118,6 +120,7 @@ impl PrintKind {
118120
SupportedCrateTypes => false,
119121
TargetSpecJson => false,
120122
TargetSpecJsonSchema => false,
123+
WasmProcMacroTuple => false,
121124
}
122125
}
123126

compiler/rustc_session/src/output.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ pub fn invalid_output_for_target(sess: &Session, crate_type: CrateType) -> bool
137137
return true;
138138
}
139139
}
140+
if crate_type == CrateType::ProcMacro {
141+
if sess.opts.target_triple == sess.wasm_proc_macro_tuple
142+
&& sess.opts.unstable_opts.wasm_proc_macros
143+
{
144+
return false;
145+
}
146+
}
140147
if let CrateType::ProcMacro | CrateType::Dylib = crate_type
141148
&& sess.target.only_cdylib
142149
{

compiler/rustc_session/src/session.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ impl PointerAuthConfig {
327327
pub struct Session {
328328
pub target: Target,
329329
pub host: Target,
330+
pub wasm_proc_macro_tuple: TargetTuple,
331+
pub wasm_proc_macro_target: Target,
330332
pub opts: config::Options,
331333
pub target_tlib_path: SearchPath,
332334
pub psess: ParseSess,
@@ -395,6 +397,7 @@ pub struct Session {
395397

396398
target_filesearch: Arc<FileSearch>,
397399
host_filesearch: Arc<FileSearch>,
400+
wasm_proc_macro_filesearch: Option<Arc<FileSearch>>,
398401

399402
/// The names of intrinsics that the current codegen backend replaces
400403
/// with its own implementations.
@@ -659,6 +662,9 @@ impl Session {
659662
pub fn host_filesearch(&self) -> &filesearch::FileSearch {
660663
&self.host_filesearch
661664
}
665+
pub fn wasm_proc_macro_filesearch(&self) -> &filesearch::FileSearch {
666+
self.wasm_proc_macro_filesearch.as_ref().expect("wasm_filesearch not set")
667+
}
662668

663669
/// Returns a list of directories where target-specific tool binaries are located. Some fallback
664670
/// directories are also returned, for example if `--sysroot` is used but tools are missing
@@ -1290,6 +1296,19 @@ pub fn build_session(
12901296
dcx.handle().warn(warning)
12911297
}
12921298

1299+
let wasm_proc_macro_tuple = TargetTuple::from_tuple("wasm32-wasip2");
1300+
let (wasm_proc_macro_target, target_warnings) = Target::search(
1301+
&wasm_proc_macro_tuple,
1302+
sopts.sysroot.path(),
1303+
sopts.unstable_opts.unstable_options,
1304+
)
1305+
.unwrap_or_else(|e| {
1306+
dcx.handle().fatal(format!("Error loading wasm proc-macro target specification: {e}"))
1307+
});
1308+
for warning in target_warnings.warning_messages() {
1309+
dcx.handle().warn(warning)
1310+
}
1311+
12931312
let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.unstable_opts.self_profile
12941313
{
12951314
let directory = if let Some(directory) = d { directory } else { std::path::Path::new(".") };
@@ -1318,6 +1337,8 @@ pub fn build_session(
13181337
// FIXME use host sysroot?
13191338
let host_tlib_path = SearchPath::from_sysroot_and_triple(sopts.sysroot.path(), host_triple);
13201339
let target_tlib_path = SearchPath::from_sysroot_and_triple(sopts.sysroot.path(), target_triple);
1340+
let wasm_proc_macro_tlib_path =
1341+
SearchPath::from_sysroot_and_triple(sopts.sysroot.path(), wasm_proc_macro_tuple.tuple());
13211342

13221343
let prof = SelfProfilerRef::new(
13231344
self_profiler,
@@ -1347,6 +1368,16 @@ pub fn build_session(
13471368
sopts.unstable_opts.implicit_sysroot_deps,
13481369
))
13491370
};
1371+
let wasm_proc_macro_filesearch = if sopts.unstable_opts.wasm_proc_macros {
1372+
Some(Arc::new(FileSearch::new(
1373+
&sopts.search_paths,
1374+
&wasm_proc_macro_tlib_path,
1375+
&wasm_proc_macro_target,
1376+
sopts.unstable_opts.implicit_sysroot_deps,
1377+
)))
1378+
} else {
1379+
None
1380+
};
13501381

13511382
let timings = TimingSectionHandler::new(sopts.json_timings);
13521383

@@ -1356,6 +1387,8 @@ pub fn build_session(
13561387
let sess = Session {
13571388
target,
13581389
host,
1390+
wasm_proc_macro_tuple,
1391+
wasm_proc_macro_target,
13591392
opts: sopts,
13601393
target_tlib_path,
13611394
psess,
@@ -1379,6 +1412,7 @@ pub fn build_session(
13791412
file_depinfo: Default::default(),
13801413
target_filesearch,
13811414
host_filesearch,
1415+
wasm_proc_macro_filesearch,
13821416
replaced_intrinsics: FxHashSet::default(), // filled by `run_compiler`
13831417
fallback_intrinsics: FxHashSet::default(), // filled by `run_compiler`
13841418
thin_lto_supported: true, // filled by `run_compiler`
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `print=wasm-proc-macro-tuple`
2+
3+
The tracking issue for this feature is: [#160389](https://github.com/rust-lang/rust/issues/160389).
4+
5+
------------------------
6+
7+
This option of the `--print` flag produces the target for which wasm proc-macros should be compiled.
8+
9+
Intended to be used like this:
10+
11+
```bash
12+
rustc --print=wasm-proc-macro-tuple -Zunstable-options
13+
```

src/tools/compiletest/src/runtest.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ fn disable_error_reporting<F: FnOnce() -> R, R>(f: F) -> R {
9191
}
9292

9393
/// The platform-specific library name
94-
fn get_lib_name(name: &str, aux_type: AuxType) -> Option<String> {
94+
fn get_lib_name(name: &str, aux_type: AuxType, wasm_proc_macros: bool) -> Option<String> {
9595
match aux_type {
9696
AuxType::Bin => None,
9797
// In some cases (e.g. MUSL), we build a static
9898
// library, rather than a dynamic library.
9999
// In this case, the only path we can pass
100100
// with '--extern-meta' is the '.rlib' file
101101
AuxType::Lib => Some(format!("lib{name}.rlib")),
102+
// FIXME maybe use `rustc --print file-names` instead?
103+
AuxType::ProcMacro if wasm_proc_macros => Some(format!("{name}.wasm")),
102104
AuxType::Dylib | AuxType::ProcMacro => Some(dylib_name(name)),
103105
}
104106
}
@@ -1248,7 +1250,8 @@ impl<'test> TestCx<'test> {
12481250
aux_name: &str,
12491251
aux_path: &str,
12501252
aux_type: AuxType| {
1251-
let lib_name = get_lib_name(&path_to_crate_name(aux_path), aux_type);
1253+
let lib_name =
1254+
get_lib_name(&path_to_crate_name(aux_path), aux_type, self.config.wasm_proc_macros);
12521255
if let Some(lib_name) = lib_name {
12531256
let modifiers_and_name = match extern_modifiers {
12541257
Some(modifiers) => format!("{modifiers}:{aux_name}"),
@@ -1279,7 +1282,11 @@ impl<'test> TestCx<'test> {
12791282
// to `-Zcodegen-backend` when compiling the test file.
12801283
if let Some(aux_file) = &self.props.aux.codegen_backend {
12811284
let aux_type = self.build_auxiliary(aux_file, aux_dir, None);
1282-
if let Some(lib_name) = get_lib_name(aux_file.trim_end_matches(".rs"), aux_type) {
1285+
if let Some(lib_name) = get_lib_name(
1286+
aux_file.trim_end_matches(".rs"),
1287+
aux_type,
1288+
self.config.wasm_proc_macros,
1289+
) {
12831290
let lib_path = aux_dir.join(&lib_name);
12841291
rustc.arg(format!("-Zcodegen-backend={}", lib_path));
12851292
}

tests/run-make/print-request-help-stable-unstable/help-diff.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
error: unknown print request: `xxx`
33
|
44
- = help: valid print requests are: `calling-conventions`, `cfg`, `code-models`, `crate-name`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `tls-models`
5-
+ = help: valid print requests are: `all-target-specs-json`, `backend-has-mnemonic`, `backend-has-zstd`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `target-spec-json-schema`, `tls-models`
5+
+ = help: valid print requests are: `all-target-specs-json`, `backend-has-mnemonic`, `backend-has-zstd`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `target-spec-json-schema`, `tls-models`, `wasm-proc-macro-tuple`
66
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
77

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unknown print request: `xxx`
22
|
3-
= help: valid print requests are: `all-target-specs-json`, `backend-has-mnemonic`, `backend-has-zstd`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `target-spec-json-schema`, `tls-models`
3+
= help: valid print requests are: `all-target-specs-json`, `backend-has-mnemonic`, `backend-has-zstd`, `calling-conventions`, `cfg`, `check-cfg`, `code-models`, `crate-name`, `crate-root-lint-levels`, `deployment-target`, `file-names`, `host-tuple`, `link-args`, `native-static-libs`, `relocation-models`, `split-debuginfo`, `stack-protector-strategies`, `supported-crate-types`, `sysroot`, `target-cpus`, `target-features`, `target-libdir`, `target-list`, `target-spec-json`, `target-spec-json-schema`, `tls-models`, `wasm-proc-macro-tuple`
44
= help: for more information, see the rustc book: https://doc.rust-lang.org/rustc/command-line-arguments.html#--print-print-compiler-information
55

0 commit comments

Comments
 (0)