Skip to content

Commit 8c604dc

Browse files
committed
Auto merge of #30629 - brson:emscripten-upstream, r=alexcrichton
Here's another go at adding emscripten support. This needs to wait again on new [libc definitions](rust-lang/libc#122) landing. To get the libc definitions right I had to add support for i686-unknown-linux-musl, which are very similar to emscripten's, which are derived from arm/musl. This branch additionally removes the makefile dependency on the `EMSCRIPTEN` environment variable by not building the unused compiler-rt. Again, this is not sufficient for actually compiling to asmjs since it needs additional LLVM patches. r? @alexcrichton
2 parents 915fa2a + 7afb56f commit 8c604dc

30 files changed

+319
-32
lines changed

configure

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,12 @@ $ pacman -R cmake && pacman -S mingw-w64-x86_64-cmake
13051305
putvar CFG_DISABLE_JEMALLOC
13061306
;;
13071307

1308+
*-emscripten)
1309+
step_msg "targeting emscripten, disabling jemalloc"
1310+
CFG_DISABLE_JEMALLOC=1
1311+
putvar CFG_DISABLE_JEMALLOC
1312+
;;
1313+
13081314
*)
13091315
;;
13101316
esac

mk/cfg/asmjs-unknown-emscripten.mk

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# asmjs-unknown-emscripten configuration
2+
CC_asmjs-unknown-emscripten=emcc
3+
CXX_asmjs-unknown-emscripten=em++
4+
CPP_asmjs-unknown-emscripten=$(CPP)
5+
AR_asmjs-unknown-emscripten=emar
6+
CFG_LIB_NAME_asmjs-unknown-emscripten=lib$(1).so
7+
CFG_STATIC_LIB_NAME_asmjs-unknown-emscripten=lib$(1).a
8+
CFG_LIB_GLOB_asmjs-unknown-emscripten=lib$(1)-*.so
9+
CFG_LIB_DSYM_GLOB_asmjs-unknown-emscripten=lib$(1)-*.dylib.dSYM
10+
CFG_JEMALLOC_CFLAGS_asmjs-unknown-emscripten := -m32 $(CFLAGS)
11+
CFG_GCCISH_CFLAGS_asmjs-unknown-emscripten := -Wall -Werror -g -fPIC -m32 $(CFLAGS)
12+
CFG_GCCISH_CXXFLAGS_asmjs-unknown-emscripten := -fno-rtti $(CXXFLAGS)
13+
CFG_GCCISH_LINK_FLAGS_asmjs-unknown-emscripten := -shared -fPIC -ldl -pthread -lrt -g -m32
14+
CFG_GCCISH_DEF_FLAG_asmjs-unknown-emscripten := -Wl,--export-dynamic,--dynamic-list=
15+
CFG_LLC_FLAGS_asmjs-unknown-emscripten :=
16+
CFG_INSTALL_NAME_asmjs-unknown-emscripten =
17+
CFG_EXE_SUFFIX_asmjs-unknown-emscripten =
18+
CFG_WINDOWSY_asmjs-unknown-emscripten :=
19+
CFG_UNIXY_asmjs-unknown-emscripten := 1
20+
CFG_LDPATH_asmjs-unknown-emscripten :=
21+
CFG_RUN_asmjs-unknown-emscripten=$(2)
22+
CFG_RUN_TARG_asmjs-unknown-emscripten=$(call CFG_RUN_asmjs-unknown-emscripten,,$(2))
23+
CFG_GNU_TRIPLE_asmjs-unknown-emscripten := asmjs-unknown-emscripten

mk/cfg/i686-unknown-linux-musl.mk

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# i686-unknown-linux-musl configuration
2+
CC_i686-unknown-linux-musl=$(CFG_MUSL_ROOT)/bin/musl-gcc
3+
CXX_i686-unknown-linux-musl=notaprogram
4+
CPP_i686-unknown-linux-musl=$(CFG_MUSL_ROOT)/bin/musl-gcc -E
5+
AR_i686-unknown-linux-musl=$(AR)
6+
CFG_INSTALL_ONLY_RLIB_i686-unknown-linux-musl = 1
7+
CFG_LIB_NAME_i686-unknown-linux-musl=lib$(1).so
8+
CFG_STATIC_LIB_NAME_i686-unknown-linux-musl=lib$(1).a
9+
CFG_LIB_GLOB_i686-unknown-linux-musl=lib$(1)-*.so
10+
CFG_JEMALLOC_CFLAGS_i686-unknown-linux-musl := -m32 -Wl,-melf_i386
11+
CFG_GCCISH_CFLAGS_i686-unknown-linux-musl := -Wall -Werror -g -fPIC -m32 -Wl,-melf_i386
12+
CFG_GCCISH_CXXFLAGS_i686-unknown-linux-musl :=
13+
CFG_GCCISH_LINK_FLAGS_i686-unknown-linux-musl :=
14+
CFG_GCCISH_DEF_FLAG_i686-unknown-linux-musl :=
15+
CFG_LLC_FLAGS_i686-unknown-linux-musl :=
16+
CFG_INSTALL_NAME_i686-unknown-linux-musl =
17+
CFG_EXE_SUFFIX_i686-unknown-linux-musl =
18+
CFG_WINDOWSY_i686-unknown-linux-musl :=
19+
CFG_UNIXY_i686-unknown-linux-musl := 1
20+
CFG_LDPATH_i686-unknown-linux-musl :=
21+
CFG_RUN_i686-unknown-linux-musl=$(2)
22+
CFG_RUN_TARG_i686-unknown-linux-musl=$(call CFG_RUN_i686-unknown-linux-musl,,$(2))
23+
CFG_GNU_TRIPLE_i686-unknown-linux-musl := i686-unknown-linux-musl
24+
CFG_THIRD_PARTY_OBJECTS_i686-unknown-linux-musl := crt1.o crti.o crtn.o
25+
CFG_INSTALLED_OBJECTS_i686-unknown-linux-musl := crt1.o crti.o crtn.o
26+
27+
NATIVE_DEPS_libc_T_i686-unknown-linux-musl += libc.a
28+
NATIVE_DEPS_std_T_i686-unknown-linux-musl += libunwind.a crt1.o crti.o crtn.o

mk/main.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ export CFG_DISABLE_UNSTABLE_FEATURES
361361
export RUSTC_BOOTSTRAP_KEY:=$(CFG_BOOTSTRAP_KEY)
362362
endif
363363
export CFG_BOOTSTRAP_KEY
364+
ifdef CFG_MUSL_ROOT
365+
export CFG_MUSL_ROOT
366+
endif
364367

365368
######################################################################
366369
# Per-stage targets and runner

mk/rt.mk

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ ifeq ($$(findstring freebsd,$(1)),freebsd)
254254
COMPRT_CFLAGS_$(1) += -I/usr/include/c++/v1
255255
endif
256256

257+
ifeq ($$(findstring emscripten,$(1)),emscripten)
258+
259+
# FIXME: emscripten doesn't use compiler-rt and can't build it without
260+
# further hacks
261+
$$(COMPRT_LIB_$(1)):
262+
touch $$@
263+
264+
else
265+
257266
$$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS)
258267
@$$(call E, make: compiler-rt)
259268
$$(Q)$$(MAKE) -C "$(S)src/compiler-rt" \
@@ -266,7 +275,10 @@ $$(COMPRT_LIB_$(1)): $$(COMPRT_DEPS) $$(MKFILE_DEPS)
266275
TargetTriple=$(1) \
267276
triple-builtins
268277
$$(Q)cp $$(COMPRT_BUILD_DIR_$(1))/triple/builtins/libcompiler_rt.a $$@
278+
279+
endif # if emscripten
269280
endif
281+
270282
################################################################################
271283
# libbacktrace
272284
#
@@ -301,6 +313,12 @@ $$(BACKTRACE_LIB_$(1)):
301313
touch $$@
302314
else
303315

316+
ifeq ($$(findstring emscripten,$(1)),emscripten)
317+
# FIXME: libbacktrace doesn't understand the emscripten triple
318+
$$(BACKTRACE_LIB_$(1)):
319+
touch $$@
320+
else
321+
304322
ifdef CFG_ENABLE_FAST_MAKE
305323
BACKTRACE_DEPS := $(S)/.gitmodules
306324
else
@@ -348,6 +366,7 @@ $$(BACKTRACE_LIB_$(1)): $$(BACKTRACE_BUILD_DIR_$(1))/Makefile $$(MKFILE_DEPS)
348366
INCDIR=$(S)src/libbacktrace
349367
$$(Q)cp $$(BACKTRACE_BUILD_DIR_$(1))/.libs/libbacktrace.a $$@
350368

369+
endif # endif for emscripten
351370
endif # endif for msvc
352371
endif # endif for ios
353372
endif # endif for darwin

src/compiletest/runtest.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,12 @@ fn make_lib_name(config: &Config, auxfile: &Path, testfile: &Path) -> PathBuf {
13571357

13581358
fn make_exe_name(config: &Config, testfile: &Path) -> PathBuf {
13591359
let mut f = output_base_name(config, testfile);
1360-
if !env::consts::EXE_SUFFIX.is_empty() {
1360+
// FIXME: This is using the host architecture exe suffix, not target!
1361+
if config.target == "asmjs-unknown-emscripten" {
1362+
let mut fname = f.file_name().unwrap().to_os_string();
1363+
fname.push(".js");
1364+
f.set_file_name(&fname);
1365+
} else if !env::consts::EXE_SUFFIX.is_empty() {
13611366
let mut fname = f.file_name().unwrap().to_os_string();
13621367
fname.push(env::consts::EXE_SUFFIX);
13631368
f.set_file_name(&fname);
@@ -1370,6 +1375,12 @@ fn make_run_args(config: &Config, props: &TestProps, testfile: &Path)
13701375
// If we've got another tool to run under (valgrind),
13711376
// then split apart its command
13721377
let mut args = split_maybe_args(&config.runtool);
1378+
1379+
// If this is emscripten, then run tests under nodejs
1380+
if config.target == "asmjs-unknown-emscripten" {
1381+
args.push("nodejs".to_owned());
1382+
}
1383+
13731384
let exe_file = make_exe_name(config, testfile);
13741385

13751386
// FIXME (#9639): This needs to handle non-utf8 paths

src/compiletest/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const OS_TABLE: &'static [(&'static str, &'static str)] = &[
2626
("win32", "windows"),
2727
("windows", "windows"),
2828
("solaris", "solaris"),
29+
("emscripten", "emscripten"),
2930
];
3031

3132
const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
@@ -44,6 +45,7 @@ const ARCH_TABLE: &'static [(&'static str, &'static str)] = &[
4445
("sparc", "sparc"),
4546
("x86_64", "x86_64"),
4647
("xcore", "xcore"),
48+
("asmjs", "asmjs"),
4749
];
4850

4951
pub fn get_os(triple: &str) -> &'static str {

src/liballoc_system/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ extern crate libc;
3030
target_arch = "arm",
3131
target_arch = "mips",
3232
target_arch = "powerpc",
33-
target_arch = "powerpc64")))]
33+
target_arch = "powerpc64",
34+
target_arch = "asmjs")))]
3435
const MIN_ALIGN: usize = 8;
3536
#[cfg(all(any(target_arch = "x86_64",
3637
target_arch = "aarch64")))]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use super::{Target, TargetOptions};
12+
13+
pub fn target() -> Target {
14+
let opts = TargetOptions {
15+
linker: "emcc".to_string(),
16+
ar: "emar".to_string(),
17+
18+
dynamic_linking: false,
19+
executables: true,
20+
exe_suffix: ".js".to_string(),
21+
no_compiler_rt: true,
22+
linker_is_gnu: true,
23+
allow_asm: false,
24+
archive_format: "gnu".to_string(),
25+
obj_is_bitcode: true,
26+
.. Default::default()
27+
};
28+
Target {
29+
llvm_target: "asmjs-unknown-emscripten".to_string(),
30+
target_endian: "little".to_string(),
31+
target_pointer_width: "32".to_string(),
32+
target_os: "emscripten".to_string(),
33+
target_env: "".to_string(),
34+
target_vendor: "unknown".to_string(),
35+
arch: "asmjs".to_string(),
36+
options: opts,
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// See x86_64_unknown_linux_musl for explanation of arguments
12+
13+
use target::Target;
14+
15+
pub fn target() -> Target {
16+
let mut base = super::linux_base::opts();
17+
base.cpu = "pentium4".to_string();
18+
base.pre_link_args.push("-m32".to_string());
19+
base.pre_link_args.push("-Wl,-melf_i386".to_string());
20+
21+
base.pre_link_args.push("-nostdlib".to_string());
22+
base.pre_link_args.push("-static".to_string());
23+
base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string());
24+
25+
base.pre_link_args.push("-Wl,-(".to_string());
26+
base.post_link_args.push("-Wl,-)".to_string());
27+
28+
base.pre_link_objects_exe.push("crt1.o".to_string());
29+
base.pre_link_objects_exe.push("crti.o".to_string());
30+
base.post_link_objects.push("crtn.o".to_string());
31+
32+
base.dynamic_linking = false;
33+
base.has_rpath = false;
34+
base.position_independent_executables = false;
35+
36+
Target {
37+
llvm_target: "i686-unknown-linux-musl".to_string(),
38+
target_endian: "little".to_string(),
39+
target_pointer_width: "32".to_string(),
40+
arch: "x86".to_string(),
41+
target_os: "linux".to_string(),
42+
target_env: "musl".to_string(),
43+
target_vendor: "unknown".to_string(),
44+
options: base,
45+
}
46+
}

src/librustc_back/target/mod.rs

Lines changed: 8 additions & 1 deletion
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
}
@@ -426,6 +431,7 @@ impl Target {
426431
armv7_unknown_linux_gnueabihf,
427432
aarch64_unknown_linux_gnu,
428433
x86_64_unknown_linux_musl,
434+
i686_unknown_linux_musl,
429435
mips_unknown_linux_musl,
430436
mipsel_unknown_linux_musl,
431437

@@ -461,7 +467,8 @@ impl Target {
461467
x86_64_pc_windows_msvc,
462468
i686_pc_windows_msvc,
463469

464-
le32_unknown_nacl
470+
le32_unknown_nacl,
471+
asmjs_unknown_emscripten
465472
);
466473

467474

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)