Skip to content

Commit

Permalink
Auto merge of #38401 - redox-os:redox_cross, r=brson
Browse files Browse the repository at this point in the history
Redox Cross Compilation

I will admit - there are things here that I wish I did not have to do. This completes the ability to create a cross compiler from the rust repository for `x86_64-unknown-redox`. I will document this PR with inline comments explaining some things.

[View this gist to see how a cross compiler is built](https://gist.github.com/jackpot51/6680ad973986e84d69c79854249f2b7e)

Prior discussion of a smaller change is here: #38366
  • Loading branch information
bors committed Dec 23, 2016
2 parents 82611a0 + 4dcb867 commit 99913c5
Show file tree
Hide file tree
Showing 39 changed files with 1,439 additions and 42 deletions.
1 change: 1 addition & 0 deletions mk/cfg/x86_64-unknown-redox.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# rustbuild-only target
3 changes: 2 additions & 1 deletion src/liballoc_jemalloc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ fn main() {
// targets, which means we have to build the alloc_jemalloc crate
// for targets like emscripten, even if we don't use it.
if target.contains("rumprun") || target.contains("bitrig") || target.contains("openbsd") ||
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") {
target.contains("msvc") || target.contains("emscripten") || target.contains("fuchsia") ||
target.contains("redox") {
println!("cargo:rustc-cfg=dummy_jemalloc");
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/liballoc_system/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
issue = "27783")]
#![feature(allocator)]
#![feature(staged_api)]
#![cfg_attr(unix, feature(libc))]
#![cfg_attr(any(unix, target_os = "redox"), feature(libc))]

// The minimum alignment guaranteed by the architecture. This value is used to
// add fast paths for low alignment values. In practice, the alignment is a
Expand Down Expand Up @@ -71,7 +71,7 @@ pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
imp::usable_size(size, align)
}

#[cfg(unix)]
#[cfg(any(unix, target_os = "redox"))]
mod imp {
extern crate libc;

Expand All @@ -87,7 +87,7 @@ mod imp {
}
}

#[cfg(target_os = "android")]
#[cfg(any(target_os = "android", target_os = "redox"))]
unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
// On android we currently target API level 9 which unfortunately
// doesn't have the `posix_memalign` API used below. Instead we use
Expand All @@ -109,7 +109,7 @@ mod imp {
libc::memalign(align as libc::size_t, size as libc::size_t) as *mut u8
}

#[cfg(not(target_os = "android"))]
#[cfg(not(any(target_os = "android", target_os = "redox")))]
unsafe fn aligned_malloc(size: usize, align: usize) -> *mut u8 {
let mut out = ptr::null_mut();
let ret = libc::posix_memalign(&mut out, align as libc::size_t, size as libc::size_t);
Expand Down
2 changes: 1 addition & 1 deletion src/libcompiler_builtins/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn main() {
"atomic_thread_fence.c"]);
}

if !target.contains("windows") {
if !target.contains("redox") && !target.contains("windows") {
sources.extend(&["emutls.c"]);
}

Expand Down
18 changes: 6 additions & 12 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,26 +945,20 @@ pub fn default_configuration(sess: &Session) -> ast::CrateConfig {
let vendor = &sess.target.target.target_vendor;
let max_atomic_width = sess.target.target.max_atomic_width();

let fam = if let Some(ref fam) = sess.target.target.options.target_family {
Symbol::intern(fam)
} else if sess.target.target.options.is_like_windows {
Symbol::intern("windows")
} else {
Symbol::intern("unix")
};

let mut ret = HashSet::new();
// Target bindings.
ret.insert((Symbol::intern("target_os"), Some(Symbol::intern(os))));
ret.insert((Symbol::intern("target_family"), Some(fam)));
if let Some(ref fam) = sess.target.target.options.target_family {
ret.insert((Symbol::intern("target_family"), Some(Symbol::intern(fam))));
if fam == "windows" || fam == "unix" {
ret.insert((Symbol::intern(fam), None));
}
}
ret.insert((Symbol::intern("target_arch"), Some(Symbol::intern(arch))));
ret.insert((Symbol::intern("target_endian"), Some(Symbol::intern(end))));
ret.insert((Symbol::intern("target_pointer_width"), Some(Symbol::intern(wordsz))));
ret.insert((Symbol::intern("target_env"), Some(Symbol::intern(env))));
ret.insert((Symbol::intern("target_vendor"), Some(Symbol::intern(vendor))));
if fam == "windows" || fam == "unix" {
ret.insert((fam, None));
}
if sess.target.target.options.has_elf_tls {
ret.insert((Symbol::intern("target_thread_local"), None));
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/apple_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn opts() -> TargetOptions {
function_sections: false,
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
is_like_osx: true,
has_rpath: true,
dll_prefix: "lib".to_string(),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/bitrig_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
position_independent_executables: true,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/dragonfly_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
pre_link_args: vec![
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/freebsd_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
pre_link_args: vec![
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/fuchsia_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
pre_link_args: vec![
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/haiku_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub fn opts() -> TargetOptions {
dynamic_linking: true,
executables: true,
has_rpath: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
.. Default::default()
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/linux_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
pre_link_args: vec![
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_back/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod windows_base;
mod windows_msvc_base;
mod thumb_base;
mod fuchsia_base;
mod redox_base;

pub type TargetResult = Result<Target, String>;

Expand Down Expand Up @@ -184,6 +185,8 @@ supported_targets! {
("aarch64-unknown-fuchsia", aarch64_unknown_fuchsia),
("x86_64-unknown-fuchsia", x86_64_unknown_fuchsia),

("x86_64-unknown-redox", x86_64_unknown_redox),

("i386-apple-ios", i386_apple_ios),
("x86_64-apple-ios", x86_64_apple_ios),
("aarch64-apple-ios", aarch64_apple_ios),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/netbsd_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
pre_link_args: vec![
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/openbsd_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn opts() -> TargetOptions {
TargetOptions {
dynamic_linking: true,
executables: true,
target_family: Some("unix".to_string()),
linker_is_gnu: true,
has_rpath: true,
is_like_openbsd: true,
Expand Down
50 changes: 50 additions & 0 deletions src/librustc_back/target/redox_base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2014 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.

use PanicStrategy;
use target::TargetOptions;
use std::default::Default;

pub fn opts() -> TargetOptions {
TargetOptions {
pre_link_args: vec![
// We want to be able to strip as much executable code as possible
// from the linker command line, and this flag indicates to the
// linker that it can avoid linking in dynamic libraries that don't
// actually satisfy any symbols up to that point (as with many other
// resolutions the linker does). This option only applies to all
// following libraries so we're sure to pass it as one of the first
// arguments.
"-Wl,--as-needed".to_string(),

// Always enable NX protection when it is available
"-Wl,-z,noexecstack".to_string(),

// Static link
"-static".to_string()
],
late_link_args: vec![
"-lc".to_string(),
"-lm".to_string()
],
executables: true,
relocation_model: "static".to_string(),
disable_redzone: true,
eliminate_frame_pointer: false,
target_family: None,
linker_is_gnu: true,
no_default_libraries: true,
lib_allocation_crate: "alloc_system".to_string(),
exe_allocation_crate: "alloc_system".to_string(),
has_elf_tls: true,
panic_strategy: PanicStrategy::Abort,
.. Default::default()
}
}
1 change: 1 addition & 0 deletions src/librustc_back/target/solaris_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn opts() -> TargetOptions {
dynamic_linking: true,
executables: true,
has_rpath: true,
target_family: Some("unix".to_string()),
is_like_solaris: true,
exe_allocation_crate: super::maybe_jemalloc(),

Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/windows_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn opts() -> TargetOptions {
staticlib_prefix: "".to_string(),
staticlib_suffix: ".lib".to_string(),
no_default_libraries: true,
target_family: Some("windows".to_string()),
is_like_windows: true,
allows_weak_linkage: false,
pre_link_args: vec![
Expand Down
1 change: 1 addition & 0 deletions src/librustc_back/target/windows_msvc_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub fn opts() -> TargetOptions {
exe_suffix: ".exe".to_string(),
staticlib_prefix: "".to_string(),
staticlib_suffix: ".lib".to_string(),
target_family: Some("windows".to_string()),
is_like_windows: true,
is_like_msvc: true,
pre_link_args: vec![
Expand Down
30 changes: 30 additions & 0 deletions src/librustc_back/target/x86_64_unknown_redox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2016 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.

use target::{Target, TargetResult};

pub fn target() -> TargetResult {
let mut base = super::redox_base::opts();
base.cpu = "x86-64".to_string();
base.max_atomic_width = Some(64);
base.pre_link_args.push("-m64".to_string());

Ok(Target {
llvm_target: "x86_64-unknown-redox".to_string(),
target_endian: "little".to_string(),
target_pointer_width: "64".to_string(),
data_layout: "e-m:e-i64:64-f80:128-n8:16:32:64-S128".to_string(),
arch: "x86_64".to_string(),
target_os: "redox".to_string(),
target_env: "".to_string(),
target_vendor: "unknown".to_string(),
options: base,
})
}
2 changes: 1 addition & 1 deletion src/libstd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() {
let target = env::var("TARGET").expect("TARGET was not set");
let host = env::var("HOST").expect("HOST was not set");
if cfg!(feature = "backtrace") && !target.contains("apple") && !target.contains("msvc") &&
!target.contains("emscripten") && !target.contains("fuchsia") {
!target.contains("emscripten") && !target.contains("fuchsia") && !target.contains("redox") {
build_libbacktrace(&host, &target);
}

Expand Down
35 changes: 20 additions & 15 deletions src/libstd/sys/redox/ext/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#![stable(feature = "rust1", since = "1.0.0")]

use fs;
use net;
use sys;
use sys_common::{AsInner, FromInner, IntoInner};
use sys_common::{self, AsInner, FromInner, IntoInner};

/// Raw file descriptors.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -89,58 +90,62 @@ impl IntoRawFd for fs::File {
}
}

/*
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::TcpStream {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_inner().fd().raw()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::TcpListener {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_inner().fd().raw()
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl AsRawFd for net::UdpSocket {
fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() }
fn as_raw_fd(&self) -> RawFd {
self.as_inner().as_inner().fd().raw()
}
}

#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
let socket = sys::net::Socket::from_inner(fd);
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(socket))
let file = sys::fs::File::from_inner(fd);
net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(file))
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::TcpListener {
unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
let socket = sys::net::Socket::from_inner(fd);
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(socket))
let file = sys::fs::File::from_inner(fd);
net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(file))
}
}
#[stable(feature = "from_raw_os", since = "1.1.0")]
impl FromRawFd for net::UdpSocket {
unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
let socket = sys::net::Socket::from_inner(fd);
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(socket))
let file = sys::fs::File::from_inner(fd);
net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(file))
}
}

#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::TcpStream {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_inner().into_fd().into_raw()
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::TcpListener {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_inner().into_fd().into_raw()
}
}
#[stable(feature = "into_raw_os", since = "1.4.0")]
impl IntoRawFd for net::UdpSocket {
fn into_raw_fd(self) -> RawFd {
self.into_inner().into_socket().into_inner()
self.into_inner().into_inner().into_fd().into_raw()
}
}
*/
Loading

0 comments on commit 99913c5

Please sign in to comment.