From 3bfc36c275fadb73a5ca406fbb529ad10297fbc3 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 26 Nov 2015 19:05:10 +0000 Subject: [PATCH 1/3] Add the asmjs-unknown-emscripten triple. Add cfgs to libs. Backtraces, and the compilation of libbacktrace for asmjs, are disabled. This port doesn't use jemalloc so, like pnacl, it disables jemalloc *for all targets* in the configure file. It updates libc to not link to -m and -rt since all emscripten runtime components are part of libc. Emscripten libc is musl. It disables stack protection. --- configure | 6 +++ mk/cfg/asmjs-unknown-emscripten.mk | 27 ++++++++++++++ mk/rt.mk | 7 ++++ src/jemalloc | 2 +- src/liballoc_system/lib.rs | 3 +- src/liblibc | 2 +- .../target/asmjs_unknown_emscripten.rs | 37 +++++++++++++++++++ src/librustc_back/target/mod.rs | 3 +- src/librustc_trans/trans/cabi.rs | 2 + src/librustc_trans/trans/cabi_asmjs.rs | 22 +++++++++++ src/librustc_trans/trans/mod.rs | 1 + src/libstd/dynamic_lib.rs | 3 +- src/libstd/env.rs | 16 ++++++++ src/libstd/os/linux/raw.rs | 3 +- src/libstd/os/mod.rs | 5 +++ src/libstd/sys/common/args.rs | 3 +- src/libstd/sys/common/libunwind.rs | 4 ++ src/libstd/sys/common/mod.rs | 2 +- src/libstd/sys/unix/backtrace/printing/mod.rs | 6 ++- src/libstd/sys/unix/mod.rs | 1 + src/libstd/sys/unix/os.rs | 8 ++-- src/libstd/sys/unix/process.rs | 2 +- src/libstd/sys/unix/thread.rs | 4 +- src/libtest/lib.rs | 6 ++- src/llvm | 2 +- 25 files changed, 159 insertions(+), 18 deletions(-) create mode 100644 mk/cfg/asmjs-unknown-emscripten.mk create mode 100644 src/librustc_back/target/asmjs_unknown_emscripten.rs create mode 100644 src/librustc_trans/trans/cabi_asmjs.rs diff --git a/configure b/configure index 6e2d3060bc3c0..8d81ce9260cca 100755 --- a/configure +++ b/configure @@ -1290,6 +1290,12 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake putvar CFG_DISABLE_JEMALLOC ;; + *-emscripten) + step_msg "targeting emscripten, disabling jemalloc" + CFG_DISABLE_JEMALLOC=1 + putvar CFG_DISABLE_JEMALLOC + ;; + *) ;; esac diff --git a/mk/cfg/asmjs-unknown-emscripten.mk b/mk/cfg/asmjs-unknown-emscripten.mk new file mode 100644 index 0000000000000..a1ec2daf84674 --- /dev/null +++ b/mk/cfg/asmjs-unknown-emscripten.mk @@ -0,0 +1,27 @@ +# asmjs-unknown-emscripten configuration +CC_asmjs-unknown-emscripten=emcc +CXX_asmjs-unknown-emscripten=em++ +CPP_asmjs-unknown-emscripten=$(CPP) +AR_asmjs-unknown-emscripten=emar +CFG_LIB_NAME_asmjs-unknown-emscripten=lib$(1).so +CFG_STATIC_LIB_NAME_asmjs-unknown-emscripten=lib$(1).a +CFG_LIB_GLOB_asmjs-unknown-emscripten=lib$(1)-*.so +CFG_LIB_DSYM_GLOB_asmjs-unknown-emscripten=lib$(1)-*.dylib.dSYM +CFG_JEMALLOC_CFLAGS_asmjs-unknown-emscripten := -m32 $(CFLAGS) +# NB: The EMSCRIPTEN environment variable is set by the emscripten SDK. +# This is only needed here to so the compiler-rt build can find unwind.h, +# but the asmjs target *doesn't even link to compiler-rt*. +CFG_GCCISH_CFLAGS_asmjs-unknown-emscripten := -Wall -Werror -g -fPIC -m32 $(CFLAGS) \ + -I$(EMSCRIPTEN)/system/lib/libcxxabi/include +CFG_GCCISH_CXXFLAGS_asmjs-unknown-emscripten := -fno-rtti $(CXXFLAGS) +CFG_GCCISH_LINK_FLAGS_asmjs-unknown-emscripten := -shared -fPIC -ldl -pthread -lrt -g -m32 +CFG_GCCISH_DEF_FLAG_asmjs-unknown-emscripten := -Wl,--export-dynamic,--dynamic-list= +CFG_LLC_FLAGS_asmjs-unknown-emscripten := +CFG_INSTALL_NAME_asmjs-unknown-emscripten = +CFG_EXE_SUFFIX_asmjs-unknown-emscripten = +CFG_WINDOWSY_asmjs-unknown-emscripten := +CFG_UNIXY_asmjs-unknown-emscripten := 1 +CFG_LDPATH_asmjs-unknown-emscripten := +CFG_RUN_asmjs-unknown-emscripten=$(2) +CFG_RUN_TARG_asmjs-unknown-emscripten=$(call CFG_RUN_asmjs-unknown-emscripten,,$(2)) +CFG_GNU_TRIPLE_asmjs-unknown-emscripten := asmjs-unknown-emscripten diff --git a/mk/rt.mk b/mk/rt.mk index 9dbbcbebb979d..6bd8df7081fb9 100644 --- a/mk/rt.mk +++ b/mk/rt.mk @@ -301,6 +301,12 @@ $$(BACKTRACE_LIB_$(1)): touch $$@ else +ifeq ($$(findstring emscripten,$(1)),emscripten) +# FIXME: libbacktrace doesn't understand the emscripten triple +$$(BACKTRACE_LIB_$(1)): + touch $$@ +else + ifdef CFG_ENABLE_FAST_MAKE BACKTRACE_DEPS := $(S)/.gitmodules else @@ -348,6 +354,7 @@ $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS) INCDIR=$(S)src/libbacktrace $$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@ +endif # endif for emscripten endif # endif for msvc endif # endif for ios endif # endif for darwin diff --git a/src/jemalloc b/src/jemalloc index f84e30927284b..e24a1a025a1f2 160000 --- a/src/jemalloc +++ b/src/jemalloc @@ -1 +1 @@ -Subproject commit f84e30927284b0c500ed3eaf09e8e159da20ddaf +Subproject commit e24a1a025a1f214e40eedafe3b9c7b1d69937922 diff --git a/src/liballoc_system/lib.rs b/src/liballoc_system/lib.rs index fccc024603ebd..e66914a337356 100644 --- a/src/liballoc_system/lib.rs +++ b/src/liballoc_system/lib.rs @@ -29,7 +29,8 @@ extern crate libc; target_arch = "arm", target_arch = "mips", target_arch = "mipsel", - target_arch = "powerpc")))] + target_arch = "powerpc", + target_arch = "asmjs")))] const MIN_ALIGN: usize = 8; #[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))] diff --git a/src/liblibc b/src/liblibc index e0c0bf439add6..fb83189000026 160000 --- a/src/liblibc +++ b/src/liblibc @@ -1 +1 @@ -Subproject commit e0c0bf439add63a6a25a25ba47e4aec9547bf9af +Subproject commit fb8318900002678298f050c8581c68baf187e648 diff --git a/src/librustc_back/target/asmjs_unknown_emscripten.rs b/src/librustc_back/target/asmjs_unknown_emscripten.rs new file mode 100644 index 0000000000000..4197ae7c661a8 --- /dev/null +++ b/src/librustc_back/target/asmjs_unknown_emscripten.rs @@ -0,0 +1,37 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use super::{Target, TargetOptions}; + +pub fn target() -> Target { + let opts = TargetOptions { + linker: "emcc".to_string(), + ar: "emar".to_string(), + + dynamic_linking: false, + executables: true, + exe_suffix: ".js".to_string(), + no_compiler_rt: true, + linker_is_gnu: true, + allow_asm: false, + archive_format: "gnu".to_string(), + .. Default::default() + }; + Target { + llvm_target: "asmjs-unknown-emscripten".to_string(), + target_endian: "little".to_string(), + target_pointer_width: "32".to_string(), + target_os: "emscripten".to_string(), + target_env: "".to_string(), + target_vendor: "unknown".to_string(), + arch: "asmjs".to_string(), + options: opts, + } +} diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs index 666903b4eed42..67ff13acd58dd 100644 --- a/src/librustc_back/target/mod.rs +++ b/src/librustc_back/target/mod.rs @@ -448,7 +448,8 @@ impl Target { x86_64_pc_windows_msvc, i686_pc_windows_msvc, - le32_unknown_nacl + le32_unknown_nacl, + asmjs_unknown_emscripten ); diff --git a/src/librustc_trans/trans/cabi.rs b/src/librustc_trans/trans/cabi.rs index 0ff5264c00f0f..d18c963ded02c 100644 --- a/src/librustc_trans/trans/cabi.rs +++ b/src/librustc_trans/trans/cabi.rs @@ -20,6 +20,7 @@ use trans::cabi_arm; use trans::cabi_aarch64; use trans::cabi_powerpc; use trans::cabi_mips; +use trans::cabi_asmjs; use trans::type_::Type; #[derive(Clone, Copy, PartialEq)] @@ -127,6 +128,7 @@ pub fn compute_abi_info(ccx: &CrateContext, }, "mips" => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def), "powerpc" => cabi_powerpc::compute_abi_info(ccx, atys, rty, ret_def), + "asmjs" => cabi_asmjs::compute_abi_info(ccx, atys, rty, ret_def), a => ccx.sess().fatal(&format!("unrecognized arch \"{}\" in target specification", a) ), } diff --git a/src/librustc_trans/trans/cabi_asmjs.rs b/src/librustc_trans/trans/cabi_asmjs.rs new file mode 100644 index 0000000000000..823f333f3317b --- /dev/null +++ b/src/librustc_trans/trans/cabi_asmjs.rs @@ -0,0 +1,22 @@ +// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use trans::cabi::FnType; +use trans::cabi_arm; +use trans::context::CrateContext; +use trans::type_::Type; + +pub fn compute_abi_info(ccx: &CrateContext, + atys: &[Type], + rty: Type, + ret_def: bool) -> FnType { + cabi_arm::compute_abi_info(ccx, atys, rty, ret_def, + cabi_arm::Flavor::General) +} diff --git a/src/librustc_trans/trans/mod.rs b/src/librustc_trans/trans/mod.rs index b102e96af20e2..3c5bd794e6a08 100644 --- a/src/librustc_trans/trans/mod.rs +++ b/src/librustc_trans/trans/mod.rs @@ -28,6 +28,7 @@ mod builder; mod cabi; mod cabi_aarch64; mod cabi_arm; +mod cabi_asmjs; mod cabi_mips; mod cabi_powerpc; mod cabi_x86; diff --git a/src/libstd/dynamic_lib.rs b/src/libstd/dynamic_lib.rs index 62ec23ccb2000..b1e611aa354fd 100644 --- a/src/libstd/dynamic_lib.rs +++ b/src/libstd/dynamic_lib.rs @@ -183,7 +183,8 @@ mod tests { target_os = "dragonfly", target_os = "bitrig", target_os = "netbsd", - target_os = "openbsd"))] + target_os = "openbsd", + target_os = "emscripten"))] mod dl { use prelude::v1::*; diff --git a/src/libstd/env.rs b/src/libstd/env.rs index 760733872ea19..e1fcaa7e55115 100644 --- a/src/libstd/env.rs +++ b/src/libstd/env.rs @@ -832,6 +832,17 @@ mod os { pub const EXE_EXTENSION: &'static str = "pexe"; } +#[cfg(target_os = "emscripten")] +mod os { + pub const FAMILY: &'static str = "unix"; + pub const OS: &'static str = "emscripten"; + pub const DLL_PREFIX: &'static str = "lib"; + pub const DLL_SUFFIX: &'static str = ".so"; + pub const DLL_EXTENSION: &'static str = "so"; + pub const EXE_SUFFIX: &'static str = ".js"; + pub const EXE_EXTENSION: &'static str = "js"; +} + #[cfg(target_arch = "x86")] mod arch { pub const ARCH: &'static str = "x86"; @@ -872,6 +883,11 @@ mod arch { pub const ARCH: &'static str = "le32"; } +#[cfg(target_arch = "asmjs")] +mod arch { + pub const ARCH: &'static str = "asmjs"; +} + #[cfg(test)] mod tests { use prelude::v1::*; diff --git a/src/libstd/os/linux/raw.rs b/src/libstd/os/linux/raw.rs index f44199f311bfa..cd479e92df239 100644 --- a/src/libstd/os/linux/raw.rs +++ b/src/libstd/os/linux/raw.rs @@ -26,7 +26,8 @@ pub use self::arch::{off_t, ino_t, nlink_t, blksize_t, blkcnt_t, stat, time_t}; #[cfg(any(target_arch = "x86", target_arch = "le32", target_arch = "powerpc", - target_arch = "arm"))] + target_arch = "arm", + target_arch = "asmjs"))] mod arch { use super::{dev_t, mode_t}; use os::raw::{c_long, c_short}; diff --git a/src/libstd/os/mod.rs b/src/libstd/os/mod.rs index 84b7d9fbeec93..198283f716ef4 100644 --- a/src/libstd/os/mod.rs +++ b/src/libstd/os/mod.rs @@ -31,4 +31,9 @@ pub use sys::ext as windows; #[cfg(target_os = "netbsd")] pub mod netbsd; #[cfg(target_os = "openbsd")] pub mod openbsd; +// Emscripten is just like linux +#[cfg(target_os = "emscripten")] +#[path = "linux/mod.rs"] +pub mod emscripten; + pub mod raw; diff --git a/src/libstd/sys/common/args.rs b/src/libstd/sys/common/args.rs index 4cfddb036e9c2..694d0d5f1878c 100644 --- a/src/libstd/sys/common/args.rs +++ b/src/libstd/sys/common/args.rs @@ -38,7 +38,8 @@ pub fn clone() -> Option>> { imp::clone() } target_os = "dragonfly", target_os = "bitrig", target_os = "netbsd", - target_os = "openbsd"))] + target_os = "openbsd", + target_os = "emscripten"))] mod imp { use prelude::v1::*; diff --git a/src/libstd/sys/common/libunwind.rs b/src/libstd/sys/common/libunwind.rs index feb05c7b56008..f730cfade36a0 100644 --- a/src/libstd/sys/common/libunwind.rs +++ b/src/libstd/sys/common/libunwind.rs @@ -86,6 +86,10 @@ pub const unwinder_private_data_size: usize = 2; #[cfg(target_arch = "powerpc")] pub const unwinder_private_data_size: usize = 2; +#[cfg(target_arch = "asmjs")] +// FIXME: Copied from arm. Need to confirm. +pub const unwinder_private_data_size: usize = 20; + #[repr(C)] pub struct _Unwind_Exception { pub exception_class: _Unwind_Exception_Class, diff --git a/src/libstd/sys/common/mod.rs b/src/libstd/sys/common/mod.rs index 5062be8cd631c..56628a4c7545e 100644 --- a/src/libstd/sys/common/mod.rs +++ b/src/libstd/sys/common/mod.rs @@ -45,7 +45,7 @@ pub mod unwind; pub mod util; pub mod wtf8; -#[cfg(any(all(unix, not(any(target_os = "macos", target_os = "ios"))), +#[cfg(any(all(unix, not(any(target_os = "macos", target_os = "ios", target_os = "emscripten"))), all(windows, target_env = "gnu")))] pub mod gnu; diff --git a/src/libstd/sys/unix/backtrace/printing/mod.rs b/src/libstd/sys/unix/backtrace/printing/mod.rs index e09832c231e7a..02e53854727f7 100644 --- a/src/libstd/sys/unix/backtrace/printing/mod.rs +++ b/src/libstd/sys/unix/backtrace/printing/mod.rs @@ -10,10 +10,12 @@ pub use self::imp::print; -#[cfg(any(target_os = "macos", target_os = "ios"))] +#[cfg(any(target_os = "macos", target_os = "ios", + target_os = "emscripten"))] #[path = "dladdr.rs"] mod imp; -#[cfg(not(any(target_os = "macos", target_os = "ios")))] +#[cfg(not(any(target_os = "macos", target_os = "ios", + target_os = "emscripten")))] #[path = "gnu.rs"] mod imp; diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs index 68b0c3d6b0ee7..3a935323ff16b 100644 --- a/src/libstd/sys/unix/mod.rs +++ b/src/libstd/sys/unix/mod.rs @@ -26,6 +26,7 @@ use ops::Neg; #[cfg(target_os = "nacl")] pub use os::nacl as platform; #[cfg(target_os = "netbsd")] pub use os::netbsd as platform; #[cfg(target_os = "openbsd")] pub use os::openbsd as platform; +#[cfg(target_os = "emscripten")] pub use os::emscripten as platform; pub mod backtrace; pub mod condvar; diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index c62960d74cb1c..f6bba6172fbf2 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -38,7 +38,8 @@ static ENV_LOCK: StaticMutex = StaticMutex::new(); /// Returns the platform-specific value of errno pub fn errno() -> i32 { extern { - #[cfg_attr(any(target_os = "linux"), link_name = "__errno_location")] + #[cfg_attr(any(target_os = "linux", target_os = "emscripten"), + link_name = "__errno_location")] #[cfg_attr(any(target_os = "bitrig", target_os = "netbsd", target_os = "openbsd", @@ -234,7 +235,7 @@ pub fn current_exe() -> io::Result { } } -#[cfg(any(target_os = "linux", target_os = "android"))] +#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))] pub fn current_exe() -> io::Result { ::fs::read_link("/proc/self/exe") } @@ -359,7 +360,8 @@ pub fn args() -> Args { target_os = "bitrig", target_os = "netbsd", target_os = "openbsd", - target_os = "nacl"))] + target_os = "nacl", + target_os = "emscripten"))] pub fn args() -> Args { use sys_common; let bytes = sys_common::args::clone().unwrap_or(Vec::new()); diff --git a/src/libstd/sys/unix/process.rs b/src/libstd/sys/unix/process.rs index 407fcb0a1b8f1..510546bc0fcda 100644 --- a/src/libstd/sys/unix/process.rs +++ b/src/libstd/sys/unix/process.rs @@ -93,7 +93,7 @@ fn os2c(s: &OsStr) -> CString { pub struct ExitStatus(c_int); #[cfg(any(target_os = "linux", target_os = "android", - target_os = "nacl"))] + target_os = "nacl", target_os = "emscripten"))] mod status_imp { pub fn WIFEXITED(status: i32) -> bool { (status & 0xff) == 0 } pub fn WEXITSTATUS(status: i32) -> i32 { (status >> 8) & 0xff } diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs index 4d715b579c6c5..8f85426b26bf9 100644 --- a/src/libstd/sys/unix/thread.rs +++ b/src/libstd/sys/unix/thread.rs @@ -84,7 +84,9 @@ impl Thread { debug_assert_eq!(ret, 0); } - #[cfg(any(target_os = "linux", target_os = "android"))] + #[cfg(any(target_os = "linux", + target_os = "android", + target_os = "emscripten"))] pub fn set_name(name: &str) { const PR_SET_NAME: libc::c_int = 15; let cname = CString::new(name).unwrap_or_else(|_| { diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index a844bdf1351d9..41dd109de8750 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -1135,14 +1135,16 @@ impl MetricMap { /// elimination. /// /// This function is a no-op, and does not even read from `dummy`. -#[cfg(not(all(target_os = "nacl", target_arch = "le32")))] +#[cfg(not(any(all(target_os = "nacl", target_arch = "le32"), + target_arch = "asmjs")))] pub fn black_box(dummy: T) -> T { // we need to "use" the argument in some way LLVM can't // introspect. unsafe {asm!("" : : "r"(&dummy))} dummy } -#[cfg(all(target_os = "nacl", target_arch = "le32"))] +#[cfg(any(all(target_os = "nacl", target_arch = "le32"), + target_arch = "asmjs"))] #[inline(never)] pub fn black_box(dummy: T) -> T { dummy } diff --git a/src/llvm b/src/llvm index 3564439515985..3acf8b4c4c5a4 160000 --- a/src/llvm +++ b/src/llvm @@ -1 +1 @@ -Subproject commit 3564439515985dc1cc0d77057ed00901635a80ad +Subproject commit 3acf8b4c4c5a4fc9c64f3dc52042f8bd2f42cbf5 From 6f706a54c029d3842521722863d0f929f0675bd9 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 27 Nov 2015 19:44:33 +0000 Subject: [PATCH 2/3] 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. --- .../target/asmjs_unknown_emscripten.rs | 1 + src/librustc_back/target/mod.rs | 5 ++ src/librustc_trans/back/write.rs | 48 +++++++++++++++---- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/librustc_back/target/asmjs_unknown_emscripten.rs b/src/librustc_back/target/asmjs_unknown_emscripten.rs index 4197ae7c661a8..b25640c2791b1 100644 --- a/src/librustc_back/target/asmjs_unknown_emscripten.rs +++ b/src/librustc_back/target/asmjs_unknown_emscripten.rs @@ -22,6 +22,7 @@ pub fn target() -> Target { linker_is_gnu: true, allow_asm: false, archive_format: "gnu".to_string(), + obj_is_bitcode: true, .. Default::default() }; Target { diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs index 67ff13acd58dd..0e5f02bae1c84 100644 --- a/src/librustc_back/target/mod.rs +++ b/src/librustc_back/target/mod.rs @@ -199,6 +199,10 @@ pub struct TargetOptions { /// Flag indicating whether ELF TLS (e.g. #[thread_local]) is available for /// this target. pub has_elf_tls: bool, + // This is mainly for easy compatibility with emscripten. + // If we give emcc .o files that are actually .bc files it + // will 'just work'. + pub obj_is_bitcode: bool, } impl Default for TargetOptions { @@ -245,6 +249,7 @@ impl Default for TargetOptions { exe_allocation_crate: "alloc_system".to_string(), allow_asm: true, has_elf_tls: false, + obj_is_bitcode: false, } } } diff --git a/src/librustc_trans/back/write.rs b/src/librustc_trans/back/write.rs index 67eff1ca19fbc..24246b69e6245 100644 --- a/src/librustc_trans/back/write.rs +++ b/src/librustc_trans/back/write.rs @@ -252,7 +252,6 @@ pub struct ModuleConfig { emit_ir: bool, emit_asm: bool, emit_obj: bool, - // Miscellaneous flags. These are mostly copied from command-line // options. no_verify: bool, @@ -262,7 +261,11 @@ pub struct ModuleConfig { vectorize_loop: bool, vectorize_slp: bool, merge_functions: bool, - inline_threshold: Option + inline_threshold: Option, + // Instead of creating an object file by doing LLVM codegen, just + // make the object file bitcode. Provides easy compatibility with + // emscripten's ecc compiler, when used as the linker. + obj_is_bitcode: bool, } unsafe impl Send for ModuleConfig { } @@ -280,6 +283,7 @@ impl ModuleConfig { emit_ir: false, emit_asm: false, emit_obj: false, + obj_is_bitcode: false, no_verify: false, no_prepopulate_passes: false, @@ -298,6 +302,7 @@ impl ModuleConfig { self.no_builtins = trans.no_builtins; self.time_passes = sess.time_passes(); self.inline_threshold = sess.opts.cg.inline_threshold; + self.obj_is_bitcode = sess.target.target.options.obj_is_bitcode; // Copy what clang does by turning on loop vectorization at O2 and // slp vectorization at O3. Otherwise configure other optimization aspects @@ -524,11 +529,21 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext, f(cpm); } - if config.emit_bc { - let ext = format!("{}.bc", name_extra); - let out = output_names.with_extension(&ext); - let out = path2cstr(&out); - llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr()); + // Change what we write and cleanup based on whether obj files are + // just llvm bitcode. In that case write bitcode, and possibly + // delete the bitcode if it wasn't requisted. Don't generate the + // machine code, instead copy the .o file from the .bc + let write_bc = config.emit_bc || config.obj_is_bitcode; + let rm_bc = !config.emit_bc && config.obj_is_bitcode; + let write_obj = config.emit_obj && !config.obj_is_bitcode; + let copy_bc_to_obj = config.emit_obj && config.obj_is_bitcode; + + let bc_out = output_names.with_extension(&format!("{}.bc", name_extra)); + let obj_out = output_names.with_extension(&format!("{}.o", name_extra)); + + if write_bc { + let bc_out_c = path2cstr(&bc_out); + llvm::LLVMWriteBitcodeToFile(llmod, bc_out_c.as_ptr()); } time(config.time_passes, &format!("codegen passes [{}]", cgcx.worker), || { @@ -562,14 +577,27 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext, } } - if config.emit_obj { - let path = output_names.with_extension(&format!("{}.o", name_extra)); + if write_obj { with_codegen(tm, llmod, config.no_builtins, |cpm| { - write_output_file(cgcx.handler, tm, cpm, llmod, &path, llvm::ObjectFileType); + write_output_file(cgcx.handler, tm, cpm, llmod, &obj_out, llvm::ObjectFileType); }); } }); + if copy_bc_to_obj { + debug!("copying bitcode {:?} to obj {:?}", bc_out, obj_out); + if let Err(e) = fs::copy(&bc_out, &obj_out) { + cgcx.handler.err(&format!("failed to copy bitcode to object file: {}", e)); + } + } + + if rm_bc { + debug!("removing_bitcode {:?}", bc_out); + if let Err(e) = fs::remove_file(&bc_out) { + cgcx.handler.err(&format!("failed to remove bitcode: {}", e)); + } + } + llvm::LLVMDisposeModule(llmod); llvm::LLVMContextDispose(llcx); llvm::LLVMRustDisposeTargetMachine(tm); From b723bad5bc04c9ff7eb1558b5f41b542cd2e661b Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 22 Dec 2015 18:00:49 +0000 Subject: [PATCH 3/3] std: Always use alloc_system in stage0 so snaps work with --disable-jemalloc --- src/libstd/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index e66cc49290adf..9c55d2e4904ee 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -268,6 +268,10 @@ #![feature(vec_push_all)] #![feature(wrapping)] #![feature(zero_one)] +// Snapshots are configured to link jemalloc by default, which makes +// CFG_DISABLE_JEMALLOC break in stage0 because alloc_jemalloc doesn't exist. +// This forces stage0 to always use the system allocator. +#![cfg_attr(stage0, feature(alloc_system))] // Don't link to std. We are std. #![no_std] @@ -285,6 +289,11 @@ unreachable, unimplemented, write, writeln, try)] extern crate core as __core; +// Snapshots are configured to link jemalloc by default, which makes +// CFG_DISABLE_JEMALLOC break in stage0 because alloc_jemalloc doesn't exist. +// This forces stage0 to always use the system allocator. +#[cfg(stage0)] extern crate alloc_system; + #[macro_use] #[macro_reexport(vec, format)] extern crate collections as core_collections;