Skip to content

Commit 81ba4a7

Browse files
committed
rustc: Add obj_is_bitcode to TargetOptions
This tells trans::back::write not to LLVM codegen to create .o files but to put LLMV bitcode in .o files. Emscripten's emcc supports .o in this format, and this is, I think, slightly easier than making rlibs work without .o files.
1 parent d6c0d85 commit 81ba4a7

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/librustc_back/target/asmjs_unknown_emscripten.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn target() -> Target {
2222
linker_is_gnu: true,
2323
allow_asm: false,
2424
archive_format: "gnu".to_string(),
25+
obj_is_bitcode: true,
2526
.. Default::default()
2627
};
2728
Target {

src/librustc_back/target/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ pub struct TargetOptions {
204204
/// Flag indicating whether ELF TLS (e.g. #[thread_local]) is available for
205205
/// this target.
206206
pub has_elf_tls: bool,
207+
// This is mainly for easy compatibility with emscripten.
208+
// If we give emcc .o files that are actually .bc files it
209+
// will 'just work'.
210+
pub obj_is_bitcode: bool,
207211
}
208212

209213
impl Default for TargetOptions {
@@ -251,6 +255,7 @@ impl Default for TargetOptions {
251255
exe_allocation_crate: "alloc_system".to_string(),
252256
allow_asm: true,
253257
has_elf_tls: false,
258+
obj_is_bitcode: false,
254259
}
255260
}
256261
}

src/librustc_trans/back/write.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ pub struct ModuleConfig {
244244
emit_ir: bool,
245245
emit_asm: bool,
246246
emit_obj: bool,
247-
248247
// Miscellaneous flags. These are mostly copied from command-line
249248
// options.
250249
no_verify: bool,
@@ -254,7 +253,11 @@ pub struct ModuleConfig {
254253
vectorize_loop: bool,
255254
vectorize_slp: bool,
256255
merge_functions: bool,
257-
inline_threshold: Option<usize>
256+
inline_threshold: Option<usize>,
257+
// Instead of creating an object file by doing LLVM codegen, just
258+
// make the object file bitcode. Provides easy compatibility with
259+
// emscripten's ecc compiler, when used as the linker.
260+
obj_is_bitcode: bool,
258261
}
259262

260263
unsafe impl Send for ModuleConfig { }
@@ -272,6 +275,7 @@ impl ModuleConfig {
272275
emit_ir: false,
273276
emit_asm: false,
274277
emit_obj: false,
278+
obj_is_bitcode: false,
275279

276280
no_verify: false,
277281
no_prepopulate_passes: false,
@@ -290,6 +294,7 @@ impl ModuleConfig {
290294
self.no_builtins = trans.no_builtins;
291295
self.time_passes = sess.time_passes();
292296
self.inline_threshold = sess.opts.cg.inline_threshold;
297+
self.obj_is_bitcode = sess.target.target.options.obj_is_bitcode;
293298

294299
// Copy what clang does by turning on loop vectorization at O2 and
295300
// slp vectorization at O3. Otherwise configure other optimization aspects
@@ -530,11 +535,21 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
530535
f(cpm);
531536
}
532537

533-
if config.emit_bc {
534-
let ext = format!("{}.bc", name_extra);
535-
let out = output_names.with_extension(&ext);
536-
let out = path2cstr(&out);
537-
llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr());
538+
// Change what we write and cleanup based on whether obj files are
539+
// just llvm bitcode. In that case write bitcode, and possibly
540+
// delete the bitcode if it wasn't requested. Don't generate the
541+
// machine code, instead copy the .o file from the .bc
542+
let write_bc = config.emit_bc || config.obj_is_bitcode;
543+
let rm_bc = !config.emit_bc && config.obj_is_bitcode;
544+
let write_obj = config.emit_obj && !config.obj_is_bitcode;
545+
let copy_bc_to_obj = config.emit_obj && config.obj_is_bitcode;
546+
547+
let bc_out = output_names.with_extension(&format!("{}.bc", name_extra));
548+
let obj_out = output_names.with_extension(&format!("{}.o", name_extra));
549+
550+
if write_bc {
551+
let bc_out_c = path2cstr(&bc_out);
552+
llvm::LLVMWriteBitcodeToFile(llmod, bc_out_c.as_ptr());
538553
}
539554

540555
time(config.time_passes, &format!("codegen passes [{}]", cgcx.worker), || {
@@ -568,14 +583,27 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext,
568583
}
569584
}
570585

571-
if config.emit_obj {
572-
let path = output_names.with_extension(&format!("{}.o", name_extra));
586+
if write_obj {
573587
with_codegen(tm, llmod, config.no_builtins, |cpm| {
574-
write_output_file(cgcx.handler, tm, cpm, llmod, &path, llvm::ObjectFileType);
588+
write_output_file(cgcx.handler, tm, cpm, llmod, &obj_out, llvm::ObjectFileType);
575589
});
576590
}
577591
});
578592

593+
if copy_bc_to_obj {
594+
debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out);
595+
if let Err(e) = fs::copy(&bc_out, &obj_out) {
596+
cgcx.handler.err(&format!("failed to copy bitcode to object file: {}", e));
597+
}
598+
}
599+
600+
if rm_bc {
601+
debug!("removing_bitcode {:?}", bc_out);
602+
if let Err(e) = fs::remove_file(&bc_out) {
603+
cgcx.handler.err(&format!("failed to remove bitcode: {}", e));
604+
}
605+
}
606+
579607
llvm::LLVMDisposeModule(llmod);
580608
llvm::LLVMContextDispose(llcx);
581609
llvm::LLVMRustDisposeTargetMachine(tm);

0 commit comments

Comments
 (0)