Skip to content

Commit 827a6d8

Browse files
committed
Use the LLVM rustc backend as external assembler
The LLVM backend is generally available, while the gnu assembler is not on Windows and many other platforms by default.
1 parent 6d30a7d commit 827a6d8

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

src/global_asm.rs

+58-12
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,66 @@ pub(crate) fn compile_global_asm(
134134
.join("\n");
135135
global_asm.push('\n');
136136

137-
let output_object_file = config.output_filenames.temp_path(OutputType::Object, Some(cgu_name));
137+
let global_asm_object_file = add_file_stem_postfix(
138+
config.output_filenames.temp_path(OutputType::Object, Some(cgu_name)),
139+
".asm",
140+
);
138141

139142
// Assemble `global_asm`
140-
let global_asm_object_file = add_file_stem_postfix(output_object_file, ".asm");
141-
let mut child = Command::new(&config.assembler)
142-
.arg("-o")
143-
.arg(&global_asm_object_file)
144-
.stdin(Stdio::piped())
145-
.spawn()
146-
.expect("Failed to spawn `as`.");
147-
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
148-
let status = child.wait().expect("Failed to wait for `as`.");
149-
if !status.success() {
150-
return Err(format!("Failed to assemble `{}`", global_asm));
143+
if false {
144+
let mut child = Command::new(&config.assembler)
145+
.arg("-o")
146+
.arg(&global_asm_object_file)
147+
.stdin(Stdio::piped())
148+
.spawn()
149+
.expect("Failed to spawn `as`.");
150+
child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap();
151+
let status = child.wait().expect("Failed to wait for `as`.");
152+
if !status.success() {
153+
return Err(format!("Failed to assemble `{}`", global_asm));
154+
}
155+
} else {
156+
let mut child = Command::new(std::env::current_exe().unwrap())
157+
.arg("--target")
158+
.arg(&config.target)
159+
.arg("--crate-type")
160+
.arg("staticlib")
161+
.arg("--emit")
162+
.arg("obj")
163+
.arg("-o")
164+
.arg(&global_asm_object_file)
165+
.arg("-")
166+
.arg("-Abad_asm_style")
167+
.stdin(Stdio::piped())
168+
.spawn()
169+
.expect("Failed to spawn `as`.");
170+
let mut stdin = child.stdin.take().unwrap();
171+
stdin
172+
.write_all(
173+
br####"
174+
#![feature(decl_macro, no_core, rustc_attrs)]
175+
#![allow(internal_features)]
176+
#![no_core]
177+
#[rustc_builtin_macro]
178+
#[rustc_macro_transparency = "semitransparent"]
179+
macro global_asm() { /* compiler built-in */ }
180+
global_asm!(r###"
181+
"####,
182+
)
183+
.unwrap();
184+
stdin.write_all(global_asm.as_bytes()).unwrap();
185+
stdin
186+
.write_all(
187+
br####"
188+
"###);
189+
"####,
190+
)
191+
.unwrap();
192+
std::mem::drop(stdin);
193+
let status = child.wait().expect("Failed to wait for `as`.");
194+
if !status.success() {
195+
return Err(format!("Failed to assemble `{}`", global_asm));
196+
}
151197
}
152198

153199
Ok(Some(global_asm_object_file))

0 commit comments

Comments
 (0)