Skip to content

Commit 475f788

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 526f2bf commit 475f788

File tree

1 file changed

+76
-1
lines changed
  • src/librustc_back/target

1 file changed

+76
-1
lines changed

src/librustc_back/target/mod.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
//! settings, though `target-feature` and `link-args` will *add* to the list
4646
//! specified by the target, rather than replace.
4747
48-
use serialize::json::Json;
48+
use serialize::json::{Json, ToJson};
49+
use std::collections::BTreeMap;
4950
use std::default::Default;
5051
use std::io::prelude::*;
5152
use syntax::abi::Abi;
@@ -504,6 +505,80 @@ impl Target {
504505
}
505506
}
506507

508+
impl ToJson for Target {
509+
fn to_json(&self) -> Json {
510+
let mut d = BTreeMap::new();
511+
let default: TargetOptions = Default::default();
512+
513+
macro_rules! target_val {
514+
($attr:ident) => ( {
515+
let name = (stringify!($attr)).replace("_", "-");
516+
d.insert(name.to_string(), self.$attr.to_json());
517+
} );
518+
($attr:ident, $key_name:expr) => ( {
519+
let name = $key_name;
520+
d.insert(name.to_string(), self.$attr.to_json());
521+
} );
522+
}
523+
524+
macro_rules! target_option_val {
525+
($attr:ident) => ( {
526+
let name = (stringify!($attr)).replace("_", "-");
527+
if default.$attr != self.options.$attr {
528+
d.insert(name.to_string(), self.options.$attr.to_json());
529+
}
530+
} );
531+
($attr:ident, $key_name:expr) => ( {
532+
let name = $key_name;
533+
if default.$attr != self.options.$attr {
534+
d.insert(name.to_string(), self.options.$attr.to_json());
535+
}
536+
} );
537+
}
538+
539+
target_val!(llvm_target);
540+
target_val!(target_endian);
541+
target_val!(target_pointer_width);
542+
target_val!(arch);
543+
target_val!(target_os, "os");
544+
target_val!(target_env, "env");
545+
target_val!(target_vendor, "vendor");
546+
547+
target_option_val!(cpu);
548+
target_option_val!(ar);
549+
target_option_val!(linker);
550+
target_option_val!(relocation_model);
551+
target_option_val!(code_model);
552+
target_option_val!(dll_prefix);
553+
target_option_val!(dll_suffix);
554+
target_option_val!(exe_suffix);
555+
target_option_val!(staticlib_prefix);
556+
target_option_val!(staticlib_suffix);
557+
target_option_val!(features);
558+
target_option_val!(data_layout);
559+
target_option_val!(dynamic_linking);
560+
target_option_val!(executables);
561+
target_option_val!(disable_redzone);
562+
target_option_val!(eliminate_frame_pointer);
563+
target_option_val!(function_sections);
564+
target_option_val!(target_family);
565+
target_option_val!(is_like_osx);
566+
target_option_val!(is_like_windows);
567+
target_option_val!(is_like_msvc);
568+
target_option_val!(linker_is_gnu);
569+
target_option_val!(has_rpath);
570+
target_option_val!(no_compiler_rt);
571+
target_option_val!(no_default_libraries);
572+
target_option_val!(pre_link_args);
573+
target_option_val!(post_link_args);
574+
target_option_val!(archive_format);
575+
target_option_val!(allow_asm);
576+
target_option_val!(custom_unwind_resume);
577+
578+
Json::Object(d)
579+
}
580+
}
581+
507582
fn maybe_jemalloc() -> String {
508583
if cfg!(feature = "jemalloc") {
509584
"alloc_jemalloc".to_string()

0 commit comments

Comments
 (0)