Skip to content

Commit c7b6ed2

Browse files
committed
librustc_back: add ToJson trait to Target
Target's can already be built up from JSON files as well as built into librustc_back so this adds the ability to convert any Target back into JSON. Signed-off-by: Doug Goldstein <[email protected]>
1 parent 9c83fa4 commit c7b6ed2

File tree

1 file changed

+96
-2
lines changed
  • src/librustc_back/target

1 file changed

+96
-2
lines changed

src/librustc_back/target/mod.rs

+96-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
//! the target's settings, though `target-feature` and `link-args` will *add*
4545
//! to the list specified by the target, rather than replace.
4646
47-
use serialize::json::Json;
47+
use serialize::json::{Json, ToJson};
48+
use std::collections::BTreeMap;
4849
use std::default::Default;
4950
use std::io::prelude::*;
5051
use syntax::abi::Abi;
@@ -364,7 +365,12 @@ impl Target {
364365

365366
/// Load a target descriptor from a JSON object.
366367
pub fn from_json(obj: Json) -> Target {
367-
// this is 1. ugly, 2. error prone.
368+
// While ugly, this code must remain this way to retain
369+
// compatibility with existing JSON fields and the internal
370+
// expected naming of the Target and TargetOptions structs.
371+
// To ensure compatibility is retained, the built-in targets
372+
// are round-tripped through this code to catch cases where
373+
// the JSON parser is not updated to match the structs.
368374

369375
let get_req_field = |name: &str| {
370376
match obj.find(name)
@@ -535,6 +541,94 @@ impl Target {
535541
}
536542
}
537543

544+
impl ToJson for Target {
545+
fn to_json(&self) -> Json {
546+
let mut d = BTreeMap::new();
547+
let default: TargetOptions = Default::default();
548+
549+
macro_rules! target_val {
550+
($attr:ident) => ( {
551+
let name = (stringify!($attr)).replace("_", "-");
552+
d.insert(name.to_string(), self.$attr.to_json());
553+
} );
554+
($attr:ident, $key_name:expr) => ( {
555+
let name = $key_name;
556+
d.insert(name.to_string(), self.$attr.to_json());
557+
} );
558+
}
559+
560+
macro_rules! target_option_val {
561+
($attr:ident) => ( {
562+
let name = (stringify!($attr)).replace("_", "-");
563+
if default.$attr != self.options.$attr {
564+
d.insert(name.to_string(), self.options.$attr.to_json());
565+
}
566+
} );
567+
($attr:ident, $key_name:expr) => ( {
568+
let name = $key_name;
569+
if default.$attr != self.options.$attr {
570+
d.insert(name.to_string(), self.options.$attr.to_json());
571+
}
572+
} );
573+
}
574+
575+
target_val!(llvm_target);
576+
target_val!(target_endian);
577+
target_val!(target_pointer_width);
578+
target_val!(arch);
579+
target_val!(target_os, "os");
580+
target_val!(target_env, "env");
581+
target_val!(target_vendor, "vendor");
582+
target_val!(arch);
583+
target_val!(data_layout);
584+
585+
target_option_val!(is_builtin);
586+
target_option_val!(linker);
587+
target_option_val!(ar);
588+
target_option_val!(pre_link_args);
589+
target_option_val!(pre_link_objects_exe);
590+
target_option_val!(pre_link_objects_dll);
591+
target_option_val!(late_link_args);
592+
target_option_val!(post_link_objects);
593+
target_option_val!(post_link_args);
594+
target_option_val!(cpu);
595+
target_option_val!(features);
596+
target_option_val!(dynamic_linking);
597+
target_option_val!(executables);
598+
target_option_val!(relocation_model);
599+
target_option_val!(code_model);
600+
target_option_val!(disable_redzone);
601+
target_option_val!(eliminate_frame_pointer);
602+
target_option_val!(function_sections);
603+
target_option_val!(dll_prefix);
604+
target_option_val!(dll_suffix);
605+
target_option_val!(exe_suffix);
606+
target_option_val!(staticlib_prefix);
607+
target_option_val!(staticlib_suffix);
608+
target_option_val!(target_family);
609+
target_option_val!(is_like_osx);
610+
target_option_val!(is_like_solaris);
611+
target_option_val!(is_like_windows);
612+
target_option_val!(is_like_msvc);
613+
target_option_val!(is_like_android);
614+
target_option_val!(linker_is_gnu);
615+
target_option_val!(has_rpath);
616+
target_option_val!(no_compiler_rt);
617+
target_option_val!(no_default_libraries);
618+
target_option_val!(position_independent_executables);
619+
target_option_val!(archive_format);
620+
target_option_val!(allow_asm);
621+
target_option_val!(custom_unwind_resume);
622+
target_option_val!(lib_allocation_crate);
623+
target_option_val!(exe_allocation_crate);
624+
target_option_val!(has_elf_tls);
625+
target_option_val!(obj_is_bitcode);
626+
target_option_val!(max_atomic_width);
627+
628+
Json::Object(d)
629+
}
630+
}
631+
538632
fn maybe_jemalloc() -> String {
539633
if cfg!(feature = "jemalloc") {
540634
"alloc_jemalloc".to_string()

0 commit comments

Comments
 (0)