Skip to content

Commit cba53f0

Browse files
committed
Allow writing metadata without llvm
1 parent a6a7dac commit cba53f0

File tree

10 files changed

+95
-14
lines changed

10 files changed

+95
-14
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,6 @@ version.texi
103103
.cargo
104104
!src/vendor/**
105105
/src/target/
106+
107+
no_llvm_build
108+

src/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bootstrap/compile.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ impl Step for Std {
104104

105105
let out_dir = build.cargo_out(compiler, Mode::Libstd, target);
106106
build.clear_if_dirty(&out_dir, &builder.rustc(compiler));
107-
let mut cargo = builder.cargo(compiler, Mode::Libstd, target, "build");
107+
let mut cargo = if compiler.stage == 0 {
108+
builder.cargo(compiler, Mode::Libstd, target, "build")
109+
}else{
110+
builder.cargo(compiler, Mode::Libstd, target, "check")
111+
};
108112
std_cargo(build, &compiler, target, &mut cargo);
109113
run_cargo(build,
110114
&mut cargo,
@@ -161,6 +165,7 @@ pub fn std_cargo(build: &Build,
161165
// missing
162166
// We also only build the runtimes when --enable-sanitizers (or its
163167
// config.toml equivalent) is used
168+
//cargo.env("RUST_FLAGS", "-Zno-trans");
164169
cargo.env("LLVM_CONFIG", build.llvm_config(target));
165170
}
166171

src/librustc_driver/driver.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ pub fn compile_input(sess: &Session,
7777
addl_plugins: Option<Vec<String>>,
7878
control: &CompileController) -> CompileResult {
7979
use rustc_trans::back::write::OngoingCrateTranslation;
80+
use rustc::session::config::CrateType;
81+
8082
macro_rules! controller_entry_point {
8183
($point: ident, $tsess: expr, $make_state: expr, $phase_result: expr) => {{
8284
let state = &mut $make_state;
@@ -94,7 +96,6 @@ pub fn compile_input(sess: &Session,
9496
}
9597

9698
if cfg!(not(feature="llvm")) {
97-
use rustc::session::config::CrateType;
9899
if !sess.opts.debugging_opts.no_trans && sess.opts.output_types.should_trans() {
99100
sess.err("LLVM is not supported by this rustc. Please use -Z no-trans to compile")
100101
}

src/librustc_driver/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#![feature(rustc_diagnostic_macros)]
2626
#![feature(set_stdio)]
2727

28+
#[cfg(not(feature="llvm"))]
29+
extern crate ar;
2830
extern crate arena;
2931
extern crate getopts;
3032
extern crate graphviz;
@@ -157,7 +159,6 @@ pub use rustc_trans::LlvmMetadataLoader as MetadataLoader;
157159

158160
#[cfg(not(feature="llvm"))]
159161
mod no_llvm_metadata_loader {
160-
extern crate ar;
161162
extern crate owning_ref;
162163

163164
use rustc::middle::cstore::MetadataLoader as MetadataLoaderTrait;
@@ -166,7 +167,7 @@ mod no_llvm_metadata_loader {
166167
use std::fs::File;
167168
use std::path::Path;
168169

169-
use self::ar::Archive;
170+
use ar::Archive;
170171
use self::owning_ref::{OwningRef, ErasedBoxRef};
171172

172173
pub struct NoLLvmMetadataLoader;

src/librustc_trans/back/link.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,8 @@ pub const RLIB_BYTECODE_OBJECT_V1_DATA_OFFSET: usize =
8989
RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET + 8;
9090

9191
pub use self::rustc_trans_utils::link::{find_crate_name, filename_for_input,
92-
default_output_for_target, invalid_output_for_target};
93-
94-
pub fn build_link_meta(crate_hash: Fingerprint) -> LinkMeta {
95-
let r = LinkMeta {
96-
crate_hash: Svh::new(crate_hash.to_smaller_hash()),
97-
};
98-
info!("{:?}", r);
99-
return r;
100-
}
92+
default_output_for_target, invalid_output_for_target,
93+
build_link_meta};
10194

10295
// The third parameter is for env vars, used on windows to set up the
10396
// path for MSVC to find its DLLs, and gcc to find its bundled

src/librustc_trans/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ pub fn find_exported_symbols(tcx: TyCtxt) -> NodeSet {
938938
pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
939939
rx: mpsc::Receiver<Box<Any + Send>>)
940940
-> OngoingCrateTranslation {
941+
use back::link::rustc_trans_utils::find_exported_symbols;
942+
941943
check_for_rustc_errors_attr(tcx);
942944

943945

src/librustc_trans_utils/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ crate-type = ["dylib"]
1010
test = false
1111

1212
[dependencies]
13+
log = "0.3"
1314
rustc = { path = "../librustc" }
15+
rustc_incremental = { path = "../librustc_incremental" }
1416
syntax = { path = "../libsyntax" }
1517
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_trans_utils/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,68 @@
2929

3030
#![cfg_attr(stage0, feature(const_fn))]
3131

32+
#[macro_use]
33+
extern crate log;
3234
extern crate rustc;
35+
extern crate rustc_incremental;
3336
extern crate syntax;
3437
extern crate syntax_pos;
3538

39+
use rustc::ty::TyCtxt;
40+
use rustc::hir;
41+
use rustc::hir::map as hir_map;
42+
use rustc::util::nodemap::NodeSet;
43+
44+
use syntax::attr;
45+
3646
pub mod link;
47+
48+
/// The context provided lists a set of reachable ids as calculated by
49+
/// middle::reachable, but this contains far more ids and symbols than we're
50+
/// actually exposing from the object file. This function will filter the set in
51+
/// the context to the set of ids which correspond to symbols that are exposed
52+
/// from the object file being generated.
53+
///
54+
/// This list is later used by linkers to determine the set of symbols needed to
55+
/// be exposed from a dynamic library and it's also encoded into the metadata.
56+
pub fn find_exported_symbols(tcx: TyCtxt, reachable: &NodeSet) -> NodeSet {
57+
reachable.iter().cloned().filter(|&id| {
58+
// Next, we want to ignore some FFI functions that are not exposed from
59+
// this crate. Reachable FFI functions can be lumped into two
60+
// categories:
61+
//
62+
// 1. Those that are included statically via a static library
63+
// 2. Those included otherwise (e.g. dynamically or via a framework)
64+
//
65+
// Although our LLVM module is not literally emitting code for the
66+
// statically included symbols, it's an export of our library which
67+
// needs to be passed on to the linker and encoded in the metadata.
68+
//
69+
// As a result, if this id is an FFI item (foreign item) then we only
70+
// let it through if it's included statically.
71+
match tcx.hir.get(id) {
72+
hir_map::NodeForeignItem(..) => {
73+
let def_id = tcx.hir.local_def_id(id);
74+
tcx.sess.cstore.is_statically_included_foreign_item(def_id)
75+
}
76+
77+
// Only consider nodes that actually have exported symbols.
78+
hir_map::NodeItem(&hir::Item {
79+
node: hir::ItemStatic(..), .. }) |
80+
hir_map::NodeItem(&hir::Item {
81+
node: hir::ItemFn(..), .. }) |
82+
hir_map::NodeImplItem(&hir::ImplItem {
83+
node: hir::ImplItemKind::Method(..), .. }) => {
84+
let def_id = tcx.hir.local_def_id(id);
85+
let generics = tcx.generics_of(def_id);
86+
let attributes = tcx.get_attrs(def_id);
87+
(generics.parent_types == 0 && generics.types.is_empty()) &&
88+
// Functions marked with #[inline] are only ever translated
89+
// with "internal" linkage and are never exported.
90+
!attr::requests_inline(&attributes)
91+
}
92+
93+
_ => false
94+
}
95+
}).collect()
96+
}

src/librustc_trans_utils/link.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@
1010

1111
use rustc::session::config::{self, OutputFilenames, Input, OutputType};
1212
use rustc::session::Session;
13-
use rustc::middle::cstore;
13+
use rustc::middle::cstore::{self, LinkMeta};
14+
use rustc::dep_graph::{DepKind, DepNode};
15+
use rustc::hir::svh::Svh;
16+
use rustc_incremental::IncrementalHashesMap;
1417
use std::path::PathBuf;
1518
use syntax::ast;
1619
use syntax_pos::Span;
1720

21+
pub fn build_link_meta(incremental_hashes_map: &IncrementalHashesMap) -> LinkMeta {
22+
let krate_dep_node = &DepNode::new_no_params(DepKind::Krate);
23+
let r = LinkMeta {
24+
crate_hash: Svh::new(incremental_hashes_map[krate_dep_node].to_smaller_hash()),
25+
};
26+
info!("{:?}", r);
27+
return r;
28+
}
29+
1830
pub fn find_crate_name(sess: Option<&Session>,
1931
attrs: &[ast::Attribute],
2032
input: &Input) -> String {

0 commit comments

Comments
 (0)