about summary refs log tree commit diff
path: root/src/librustc_binaryen/lib.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2017-08-26 18:30:12 -0700
committerAlex Crichton <alex@alexcrichton.com>2018-03-03 20:21:35 -0800
commitd69b24805b5071e5ec9e62d2777a761723644144 (patch)
treef4b8f9d6205f7eb73ddc124457d2bc03f364f292 /src/librustc_binaryen/lib.rs
parent0be38e1ce3dcfe6402b3bd9617f74857fe7fa8c3 (diff)
downloadrust-d69b24805b5071e5ec9e62d2777a761723644144.tar.gz
rust-d69b24805b5071e5ec9e62d2777a761723644144.zip
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.

Moving to LLD brings with it a number of benefits for wasm code:

* LLD is itself an actual linker, so there's no need to compile all wasm code
  with LTO any more. As a result builds should be *much* speedier as LTO is no
  longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
  This, I believe at least, is intended to be the main supported linker for
  native code and wasm moving forward. Picking up support early on should help
  ensure that we can help LLD identify bugs and otherwise prove that it works
  great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
  and LLD (from what I can tell at least), so it's in general much better to be
  on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
  will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
  means a postprocessor is no longer needed to show off Rust's "small wasm
  binary size".

LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!

LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.

Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.

Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.

[gc]: https://reviews.llvm.org/D42511
Diffstat (limited to 'src/librustc_binaryen/lib.rs')
-rw-r--r--src/librustc_binaryen/lib.rs172
1 files changed, 0 insertions, 172 deletions
diff --git a/src/librustc_binaryen/lib.rs b/src/librustc_binaryen/lib.rs
deleted file mode 100644
index 36174e11ba0..00000000000
--- a/src/librustc_binaryen/lib.rs
+++ /dev/null
@@ -1,172 +0,0 @@
-// Copyright 2017 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 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-//! Rustc bindings to the binaryen project.
-//!
-//! This crate is a small shim around the binaryen project which provides us the
-//! ability to take LLVM's output and generate a wasm module. Specifically this
-//! only supports one operation, creating a module from LLVM's assembly format
-//! and then serializing that module to a wasm module.
-
-extern crate libc;
-
-use std::slice;
-use std::ffi::{CString, CStr};
-
-/// In-memory representation of a serialized wasm module.
-pub struct Module {
-    ptr: *mut BinaryenRustModule,
-}
-
-impl Module {
-    /// Creates a new wasm module from the LLVM-assembly provided (in a C string
-    /// format).
-    ///
-    /// The actual module creation can be tweaked through the various options in
-    /// `ModuleOptions` as well. Any errors are just returned as a bland string.
-    pub fn new(assembly: &CStr, opts: &ModuleOptions) -> Result<Module, String> {
-        unsafe {
-            let ptr = BinaryenRustModuleCreate(opts.ptr, assembly.as_ptr());
-            if ptr.is_null() {
-                Err(format!("failed to create binaryen module"))
-            } else {
-                Ok(Module { ptr })
-            }
-        }
-    }
-
-    /// Returns the data of the serialized wasm module. This is a `foo.wasm`
-    /// file contents.
-    pub fn data(&self) -> &[u8] {
-        unsafe {
-            let ptr = BinaryenRustModulePtr(self.ptr);
-            let len = BinaryenRustModuleLen(self.ptr);
-            slice::from_raw_parts(ptr, len)
-        }
-    }
-
-    /// Returns the data of the source map JSON.
-    pub fn source_map(&self) -> &[u8] {
-        unsafe {
-            let ptr = BinaryenRustModuleSourceMapPtr(self.ptr);
-            let len = BinaryenRustModuleSourceMapLen(self.ptr);
-            slice::from_raw_parts(ptr, len)
-        }
-    }
-}
-
-impl Drop for Module {
-    fn drop(&mut self) {
-        unsafe {
-            BinaryenRustModuleFree(self.ptr);
-        }
-    }
-}
-
-pub struct ModuleOptions {
-    ptr: *mut BinaryenRustModuleOptions,
-}
-
-impl ModuleOptions {
-    pub fn new() -> ModuleOptions {
-        unsafe {
-            let ptr = BinaryenRustModuleOptionsCreate();
-            ModuleOptions { ptr }
-        }
-    }
-
-    /// Turns on or off debug info.
-    ///
-    /// From what I can tell this just creates a "names" section of the wasm
-    /// module which contains a table of the original function names.
-    pub fn debuginfo(&mut self, debug: bool) -> &mut Self {
-        unsafe {
-            BinaryenRustModuleOptionsSetDebugInfo(self.ptr, debug);
-        }
-        self
-    }
-
-    /// Configures a `start` function for the module, to be executed when it's
-    /// loaded.
-    pub fn start(&mut self, func: &str) -> &mut Self {
-        let func = CString::new(func).unwrap();
-        unsafe {
-            BinaryenRustModuleOptionsSetStart(self.ptr, func.as_ptr());
-        }
-        self
-    }
-
-    /// Configures a `sourceMappingURL` custom section value for the module.
-    pub fn source_map_url(&mut self, url: &str) -> &mut Self {
-        let url = CString::new(url).unwrap();
-        unsafe {
-            BinaryenRustModuleOptionsSetSourceMapUrl(self.ptr, url.as_ptr());
-        }
-        self
-    }
-
-    /// Configures how much stack is initially allocated for the module. 1MB is
-    /// probably good enough for now.
-    pub fn stack(&mut self, amt: u64) -> &mut Self {
-        unsafe {
-            BinaryenRustModuleOptionsSetStackAllocation(self.ptr, amt);
-        }
-        self
-    }
-
-    /// Flags whether the initial memory should be imported or exported. So far
-    /// we export it by default.
-    pub fn import_memory(&mut self, import: bool) -> &mut Self {
-        unsafe {
-            BinaryenRustModuleOptionsSetImportMemory(self.ptr, import);
-        }
-        self
-    }
-}
-
-impl Drop for ModuleOptions {
-    fn drop(&mut self) {
-        unsafe {
-            BinaryenRustModuleOptionsFree(self.ptr);
-        }
-    }
-}
-
-enum BinaryenRustModule {}
-enum BinaryenRustModuleOptions {}
-
-extern {
-    fn BinaryenRustModuleCreate(opts: *const BinaryenRustModuleOptions,
-                                assembly: *const libc::c_char)
-        -> *mut BinaryenRustModule;
-    fn BinaryenRustModulePtr(module: *const BinaryenRustModule) -> *const u8;
-    fn BinaryenRustModuleLen(module: *const BinaryenRustModule) -> usize;
-    fn BinaryenRustModuleSourceMapPtr(module: *const BinaryenRustModule) -> *const u8;
-    fn BinaryenRustModuleSourceMapLen(module: *const BinaryenRustModule) -> usize;
-    fn BinaryenRustModuleFree(module: *mut BinaryenRustModule);
-
-    fn BinaryenRustModuleOptionsCreate()
-        -> *mut BinaryenRustModuleOptions;
-    fn BinaryenRustModuleOptionsSetDebugInfo(module: *mut BinaryenRustModuleOptions,
-                                             debuginfo: bool);
-    fn BinaryenRustModuleOptionsSetStart(module: *mut BinaryenRustModuleOptions,
-                                         start: *const libc::c_char);
-    fn BinaryenRustModuleOptionsSetSourceMapUrl(module: *mut BinaryenRustModuleOptions,
-                                                sourceMapUrl: *const libc::c_char);
-    fn BinaryenRustModuleOptionsSetStackAllocation(
-        module: *mut BinaryenRustModuleOptions,
-        stack: u64,
-    );
-    fn BinaryenRustModuleOptionsSetImportMemory(
-        module: *mut BinaryenRustModuleOptions,
-        import: bool,
-    );
-    fn BinaryenRustModuleOptionsFree(module: *mut BinaryenRustModuleOptions);
-}