Skip to content

output target json files #32847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub enum PrintRequest {
CrateName,
Cfg,
TargetList,
TargetSpec,
}

pub enum Input {
Expand Down Expand Up @@ -905,7 +906,7 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
"[asm|llvm-bc|llvm-ir|obj|link|dep-info]"),
opt::multi_s("", "print", "Comma separated list of compiler information to \
print on stdout",
"[crate-name|file-names|sysroot|cfg|target-list]"),
"[crate-name|file-names|sysroot|cfg|target-list|target-spec]"),
opt::flagmulti_s("g", "", "Equivalent to -C debuginfo=2"),
opt::flagmulti_s("O", "", "Equivalent to -C opt-level=2"),
opt::opt_s("o", "", "Write output to <filename>", "FILENAME"),
Expand Down Expand Up @@ -1189,6 +1190,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
"sysroot" => PrintRequest::Sysroot,
"cfg" => PrintRequest::Cfg,
"target-list" => PrintRequest::TargetList,
"target-spec" => PrintRequest::TargetSpec,
req => {
early_error(error_format, &format!("unknown print request `{}`", req))
}
Expand Down
77 changes: 76 additions & 1 deletion src/librustc_back/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
//! settings, though `target-feature` and `link-args` will *add* to the list
//! specified by the target, rather than replace.

use serialize::json::Json;
use serialize::json::{Json, ToJson};
use std::collections::BTreeMap;
use std::default::Default;
use std::io::prelude::*;
use syntax::abi::Abi;
Expand Down Expand Up @@ -504,6 +505,80 @@ impl Target {
}
}

impl ToJson for Target {
fn to_json(&self) -> Json {
let mut d = BTreeMap::new();
let default: TargetOptions = Default::default();

macro_rules! target_val {
($attr:ident) => ( {
let name = (stringify!($attr)).replace("_", "-");
d.insert(name.to_string(), self.$attr.to_json());
} );
($attr:ident, $key_name:expr) => ( {
let name = $key_name;
d.insert(name.to_string(), self.$attr.to_json());
} );
}

macro_rules! target_option_val {
($attr:ident) => ( {
let name = (stringify!($attr)).replace("_", "-");
if default.$attr != self.options.$attr {
d.insert(name.to_string(), self.options.$attr.to_json());
}
} );
($attr:ident, $key_name:expr) => ( {
let name = $key_name;
if default.$attr != self.options.$attr {
d.insert(name.to_string(), self.options.$attr.to_json());
}
} );
}

target_val!(llvm_target);
target_val!(target_endian);
target_val!(target_pointer_width);
target_val!(arch);
target_val!(target_os, "os");
target_val!(target_env, "env");
target_val!(target_vendor, "vendor");

target_option_val!(cpu);
target_option_val!(ar);
target_option_val!(linker);
target_option_val!(relocation_model);
target_option_val!(code_model);
target_option_val!(dll_prefix);
target_option_val!(dll_suffix);
target_option_val!(exe_suffix);
target_option_val!(staticlib_prefix);
target_option_val!(staticlib_suffix);
target_option_val!(features);
target_option_val!(data_layout);
target_option_val!(dynamic_linking);
target_option_val!(executables);
target_option_val!(disable_redzone);
target_option_val!(eliminate_frame_pointer);
target_option_val!(function_sections);
target_option_val!(target_family);
target_option_val!(is_like_osx);
target_option_val!(is_like_windows);
target_option_val!(is_like_msvc);
target_option_val!(linker_is_gnu);
target_option_val!(has_rpath);
target_option_val!(no_compiler_rt);
target_option_val!(no_default_libraries);
target_option_val!(pre_link_args);
target_option_val!(post_link_args);
target_option_val!(archive_format);
target_option_val!(allow_asm);
target_option_val!(custom_unwind_resume);

Json::Object(d)
}
}

fn maybe_jemalloc() -> String {
if cfg!(feature = "jemalloc") {
"alloc_jemalloc".to_string()
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ use rustc_metadata::loader;
use rustc_metadata::cstore::CStore;
use rustc::util::common::time;

use serialize::json::ToJson;

use std::cmp::max;
use std::cmp::Ordering::Equal;
use std::default::Default;
Expand Down Expand Up @@ -560,6 +562,7 @@ impl RustcDefaultCalls {
println!("{}", targets.join("\n"));
},
PrintRequest::Sysroot => println!("{}", sess.sysroot().display()),
PrintRequest::TargetSpec => println!("{}", sess.target.target.to_json().pretty()),
PrintRequest::FileNames |
PrintRequest::CrateName => {
let input = match input {
Expand Down
1 change: 1 addition & 0 deletions src/test/run-make/target-specs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ all:
$(RUSTC) foo.rs --target=my-incomplete-platform.json 2>&1 | grep 'Field llvm-target'
RUST_TARGET_PATH=. $(RUSTC) foo.rs --target=my-awesome-platform --crate-type=lib --emit=asm
RUST_TARGET_PATH=. $(RUSTC) foo.rs --target=x86_64-unknown-linux-gnu --crate-type=lib --emit=asm
$(RUSTC) --target=my-awesome-platform.json --print target-spec > $(TMPDIR)/test-platform.json && $(RUSTC) --target=$(TMPDIR)/test-platform.json --print target-spec | diff -q $(TMPDIR)/test-platform.json -