Skip to content

Commit 3d67a1a

Browse files
alexcrichtoneddyb
authored andcommitted
rustc: Add a new crate type, cdylib
This commit is an implementation of [RFC 1510] which adds a new crate type, `cdylib`, to the compiler. This new crate type differs from the existing `dylib` crate type in a few key ways: * No metadata is present in the final artifact * Symbol visibility rules are the same as executables, that is only reachable `extern` functions are visible symbols * LTO is allowed * All libraries are always linked statically This commit is relatively simple by just plubming the compiler with another crate type which takes different branches here and there. The only major change is an implementation of the `Linker::export_symbols` function on Unix which now actually does something. This helps restrict the public symbols from a cdylib on Unix. With this PR a "hello world" `cdylib` is 7.2K while the same `dylib` is 2.4MB, which is some nice size savings! [RFC 1510]: rust-lang/rfcs#1510 Closes rust-lang#33132
1 parent b5a5be7 commit 3d67a1a

File tree

15 files changed

+253
-108
lines changed

15 files changed

+253
-108
lines changed

src/librustc/middle/dependency_format.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ fn calculate_type(sess: &session::Session,
115115
// got long ago), so don't bother with anything.
116116
config::CrateTypeRlib => return Vec::new(),
117117

118-
// Staticlibs must have all static dependencies. If any fail to be
119-
// found, we generate some nice pretty errors.
120-
config::CrateTypeStaticlib => {
118+
// Staticlibs and cdylibs must have all static dependencies. If any fail
119+
// to be found, we generate some nice pretty errors.
120+
config::CrateTypeStaticlib |
121+
config::CrateTypeCdylib => {
121122
match attempt_static(sess) {
122123
Some(v) => return v,
123124
None => {}

src/librustc/middle/reachable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
145145
// Creates a new reachability computation context.
146146
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ReachableContext<'a, 'tcx> {
147147
let any_library = tcx.sess.crate_types.borrow().iter().any(|ty| {
148-
*ty != config::CrateTypeExecutable
148+
*ty == config::CrateTypeRlib || *ty == config::CrateTypeDylib
149149
});
150150
ReachableContext {
151151
tcx: tcx,

src/librustc/middle/weak_lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn verify(sess: &Session, items: &lang_items::LanguageItems) {
7070
let needs_check = sess.crate_types.borrow().iter().any(|kind| {
7171
match *kind {
7272
config::CrateTypeDylib |
73+
config::CrateTypeCdylib |
7374
config::CrateTypeExecutable |
7475
config::CrateTypeStaticlib => true,
7576
config::CrateTypeRlib => false,

src/librustc/session/config.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ pub enum CrateType {
300300
CrateTypeDylib,
301301
CrateTypeRlib,
302302
CrateTypeStaticlib,
303+
CrateTypeCdylib,
303304
}
304305

305306
#[derive(Clone)]
@@ -1326,6 +1327,7 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
13261327
"rlib" => CrateTypeRlib,
13271328
"staticlib" => CrateTypeStaticlib,
13281329
"dylib" => CrateTypeDylib,
1330+
"cdylib" => CrateTypeCdylib,
13291331
"bin" => CrateTypeExecutable,
13301332
_ => {
13311333
return Err(format!("unknown crate type: `{}`",
@@ -1413,7 +1415,8 @@ impl fmt::Display for CrateType {
14131415
CrateTypeExecutable => "bin".fmt(f),
14141416
CrateTypeDylib => "dylib".fmt(f),
14151417
CrateTypeRlib => "rlib".fmt(f),
1416-
CrateTypeStaticlib => "staticlib".fmt(f)
1418+
CrateTypeStaticlib => "staticlib".fmt(f),
1419+
CrateTypeCdylib => "cdylib".fmt(f),
14171420
}
14181421
}
14191422
}

src/librustc_driver/driver.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,9 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<c
11751175
Some(ref n) if *n == "dylib" => {
11761176
Some(config::CrateTypeDylib)
11771177
}
1178+
Some(ref n) if *n == "cdylib" => {
1179+
Some(config::CrateTypeCdylib)
1180+
}
11781181
Some(ref n) if *n == "lib" => {
11791182
Some(config::default_lib_output())
11801183
}

src/librustc_metadata/creader.rs

+1
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,7 @@ impl<'a> CrateReader<'a> {
747747
match *ct {
748748
config::CrateTypeExecutable => need_exe_alloc = true,
749749
config::CrateTypeDylib |
750+
config::CrateTypeCdylib |
750751
config::CrateTypeStaticlib => need_lib_alloc = true,
751752
config::CrateTypeRlib => {}
752753
}

src/librustc_trans/back/link.rs

+42-38
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ pub fn invalid_output_for_target(sess: &Session,
228228
crate_type: config::CrateType) -> bool {
229229
match (sess.target.target.options.dynamic_linking,
230230
sess.target.target.options.executables, crate_type) {
231+
(false, _, config::CrateTypeCdylib) |
231232
(false, _, config::CrateTypeDylib) => true,
232233
(_, false, config::CrateTypeExecutable) => true,
233234
_ => false
@@ -250,6 +251,7 @@ pub fn filename_for_input(sess: &Session,
250251
config::CrateTypeRlib => {
251252
outputs.out_directory.join(&format!("lib{}.rlib", libname))
252253
}
254+
config::CrateTypeCdylib |
253255
config::CrateTypeDylib => {
254256
let (prefix, suffix) = (&sess.target.target.options.dll_prefix,
255257
&sess.target.target.options.dll_suffix);
@@ -278,9 +280,10 @@ pub fn each_linked_rlib(sess: &Session,
278280
f: &mut FnMut(ast::CrateNum, &Path)) {
279281
let crates = sess.cstore.used_crates(LinkagePreference::RequireStatic).into_iter();
280282
let fmts = sess.dependency_formats.borrow();
281-
let fmts = fmts.get(&config::CrateTypeExecutable).or_else(|| {
282-
fmts.get(&config::CrateTypeStaticlib)
283-
}).unwrap_or_else(|| {
283+
let fmts = fmts.get(&config::CrateTypeExecutable)
284+
.or_else(|| fmts.get(&config::CrateTypeStaticlib))
285+
.or_else(|| fmts.get(&config::CrateTypeCdylib));
286+
let fmts = fmts.unwrap_or_else(|| {
284287
bug!("could not find formats for rlibs")
285288
});
286289
for (cnum, path) in crates {
@@ -335,13 +338,9 @@ fn link_binary_output(sess: &Session,
335338
config::CrateTypeStaticlib => {
336339
link_staticlib(sess, &objects, &out_filename, tmpdir.path());
337340
}
338-
config::CrateTypeExecutable => {
339-
link_natively(sess, false, &objects, &out_filename, trans, outputs,
340-
tmpdir.path());
341-
}
342-
config::CrateTypeDylib => {
343-
link_natively(sess, true, &objects, &out_filename, trans, outputs,
344-
tmpdir.path());
341+
_ => {
342+
link_natively(sess, crate_type, &objects, &out_filename, trans,
343+
outputs, tmpdir.path());
345344
}
346345
}
347346

@@ -609,13 +608,14 @@ fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path,
609608
//
610609
// This will invoke the system linker/cc to create the resulting file. This
611610
// links to all upstream files as well.
612-
fn link_natively(sess: &Session, dylib: bool,
613-
objects: &[PathBuf], out_filename: &Path,
611+
fn link_natively(sess: &Session,
612+
crate_type: config::CrateType,
613+
objects: &[PathBuf],
614+
out_filename: &Path,
614615
trans: &CrateTranslation,
615616
outputs: &OutputFilenames,
616617
tmpdir: &Path) {
617-
info!("preparing dylib? ({}) from {:?} to {:?}", dylib, objects,
618-
out_filename);
618+
info!("preparing {:?} from {:?} to {:?}", crate_type, objects, out_filename);
619619

620620
// The invocations of cc share some flags across platforms
621621
let (pname, mut cmd) = get_linker(sess);
@@ -624,18 +624,18 @@ fn link_natively(sess: &Session, dylib: bool,
624624
let root = sess.target_filesearch(PathKind::Native).get_lib_path();
625625
cmd.args(&sess.target.target.options.pre_link_args);
626626

627-
let pre_link_objects = if dylib {
628-
&sess.target.target.options.pre_link_objects_dll
629-
} else {
627+
let pre_link_objects = if crate_type == config::CrateTypeExecutable {
630628
&sess.target.target.options.pre_link_objects_exe
629+
} else {
630+
&sess.target.target.options.pre_link_objects_dll
631631
};
632632
for obj in pre_link_objects {
633633
cmd.arg(root.join(obj));
634634
}
635635

636636
{
637637
let mut linker = trans.linker_info.to_linker(&mut cmd, &sess);
638-
link_args(&mut *linker, sess, dylib, tmpdir,
638+
link_args(&mut *linker, sess, crate_type, tmpdir,
639639
objects, out_filename, outputs);
640640
if !sess.target.target.options.no_compiler_rt {
641641
linker.link_staticlib("compiler-rt");
@@ -701,7 +701,7 @@ fn link_natively(sess: &Session, dylib: bool,
701701

702702
fn link_args(cmd: &mut Linker,
703703
sess: &Session,
704-
dylib: bool,
704+
crate_type: config::CrateType,
705705
tmpdir: &Path,
706706
objects: &[PathBuf],
707707
out_filename: &Path,
@@ -722,26 +722,28 @@ fn link_args(cmd: &mut Linker,
722722

723723
// If we're building a dynamic library then some platforms need to make sure
724724
// that all symbols are exported correctly from the dynamic library.
725-
if dylib {
726-
cmd.export_symbols(tmpdir);
725+
if crate_type != config::CrateTypeExecutable {
726+
cmd.export_symbols(tmpdir, crate_type);
727727
}
728728

729729
// When linking a dynamic library, we put the metadata into a section of the
730730
// executable. This metadata is in a separate object file from the main
731731
// object file, so we link that in here.
732-
if dylib {
732+
if crate_type == config::CrateTypeDylib {
733733
cmd.add_object(&outputs.with_extension("metadata.o"));
734734
}
735735

736736
// Try to strip as much out of the generated object by removing unused
737737
// sections if possible. See more comments in linker.rs
738738
if !sess.opts.cg.link_dead_code {
739-
cmd.gc_sections(dylib);
739+
let keep_metadata = crate_type == config::CrateTypeDylib;
740+
cmd.gc_sections(keep_metadata);
740741
}
741742

742743
let used_link_args = sess.cstore.used_link_args();
743744

744-
if !dylib && t.options.position_independent_executables {
745+
if crate_type == config::CrateTypeExecutable &&
746+
t.options.position_independent_executables {
745747
let empty_vec = Vec::new();
746748
let empty_str = String::new();
747749
let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec);
@@ -796,12 +798,12 @@ fn link_args(cmd: &mut Linker,
796798
// in this DAG so far because they're only dylibs and dylibs can only depend
797799
// on other dylibs (e.g. other native deps).
798800
add_local_native_libraries(cmd, sess);
799-
add_upstream_rust_crates(cmd, sess, dylib, tmpdir);
801+
add_upstream_rust_crates(cmd, sess, crate_type, tmpdir);
800802
add_upstream_native_libraries(cmd, sess);
801803

802804
// # Telling the linker what we're doing
803805

804-
if dylib {
806+
if crate_type != config::CrateTypeExecutable {
805807
cmd.build_dylib(out_filename);
806808
}
807809

@@ -899,8 +901,10 @@ fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
899901
// Rust crates are not considered at all when creating an rlib output. All
900902
// dependencies will be linked when producing the final output (instead of
901903
// the intermediate rlib version)
902-
fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
903-
dylib: bool, tmpdir: &Path) {
904+
fn add_upstream_rust_crates(cmd: &mut Linker,
905+
sess: &Session,
906+
crate_type: config::CrateType,
907+
tmpdir: &Path) {
904908
// All of the heavy lifting has previously been accomplished by the
905909
// dependency_format module of the compiler. This is just crawling the
906910
// output of that module, adding crates as necessary.
@@ -910,11 +914,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
910914
// involves just passing the right -l flag.
911915

912916
let formats = sess.dependency_formats.borrow();
913-
let data = if dylib {
914-
formats.get(&config::CrateTypeDylib).unwrap()
915-
} else {
916-
formats.get(&config::CrateTypeExecutable).unwrap()
917-
};
917+
let data = formats.get(&crate_type).unwrap();
918918

919919
// Invoke get_used_crates to ensure that we get a topological sorting of
920920
// crates.
@@ -929,7 +929,8 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
929929
Linkage::NotLinked |
930930
Linkage::IncludedFromDylib => {}
931931
Linkage::Static => {
932-
add_static_crate(cmd, sess, tmpdir, dylib, &src.rlib.unwrap().0)
932+
add_static_crate(cmd, sess, tmpdir, crate_type,
933+
&src.rlib.unwrap().0)
933934
}
934935
Linkage::Dynamic => {
935936
add_dynamic_crate(cmd, sess, &src.dylib.unwrap().0)
@@ -974,9 +975,12 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
974975
// (aka we're making an executable), we can just pass the rlib blindly to
975976
// the linker (fast) because it's fine if it's not actually included as
976977
// we're at the end of the dependency chain.
977-
fn add_static_crate(cmd: &mut Linker, sess: &Session, tmpdir: &Path,
978-
dylib: bool, cratepath: &Path) {
979-
if !sess.lto() && !dylib {
978+
fn add_static_crate(cmd: &mut Linker,
979+
sess: &Session,
980+
tmpdir: &Path,
981+
crate_type: config::CrateType,
982+
cratepath: &Path) {
983+
if !sess.lto() && crate_type != config::CrateTypeDylib {
980984
cmd.link_rlib(&fix_windows_verbatim_for_gcc(cratepath));
981985
return
982986
}
@@ -1012,7 +1016,7 @@ fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
10121016

10131017
if any_objects {
10141018
archive.build();
1015-
if dylib {
1019+
if crate_type == config::CrateTypeDylib {
10161020
cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst));
10171021
} else {
10181022
cmd.link_rlib(&fix_windows_verbatim_for_gcc(&dst));

0 commit comments

Comments
 (0)