Skip to content

Commit 6e7968b

Browse files
committed
Redesign output flags for rustc
This commit removes the -c, --emit-llvm, -s, --rlib, --dylib, --staticlib, --lib, and --bin flags from rustc, adding the following flags: * --emit=[asm,ir,bc,obj,link] * --crate-type=[dylib,rlib,staticlib,bin,lib] The -o option has also been redefined to be used for *all* flavors of outputs. This means that we no longer ignore it for libraries. The --out-dir remains the same as before. The new logic for files that rustc emits is as follows: 1. Output types are dictated by the --emit flag. The default value is --emit=link, and this option can be passed multiple times and have all options stacked on one another. 2. Crate types are dictated by the --crate-type flag and the #[crate_type] attribute. The flags can be passed many times and stack with the crate attribute. 3. If the -o flag is specified, and only one output type is specified, the output will be emitted at this location. If more than one output type is specified, then the filename of -o is ignored, and all output goes in the directory that -o specifies. The -o option always ignores the --out-dir option. 4. If the --out-dir flag is specified, all output goes in this directory. 5. If -o and --out-dir are both not present, all output goes in the current directory of the process. 6. When multiple output types are specified, the filestem of all output is the same as the name of the CrateId (derived from a crate attribute or from the filestem of the crate file). Closes #7791 Closes #11056 Closes #11667
1 parent f039d10 commit 6e7968b

File tree

30 files changed

+417
-358
lines changed

30 files changed

+417
-358
lines changed

man/rustc.1

+8-16
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,21 @@ This program is a compiler for the Rust language, available at
1212
.SH OPTIONS
1313

1414
.TP
15-
\fB\-\-bin\fR
16-
Compile an executable crate (default)
17-
.TP
18-
\fB\-c\fR
19-
Compile and assemble, but do not link
15+
\fB\-\-crate-type=[bin|lib|dylib|rlib|staticlib]\fR
16+
Configure the flavor of rust crate that is generated (default `bin`)
2017
.TP
2118
\fB\-\-cfg\fR SPEC
2219
Configure the compilation environment
2320
.TP
24-
\fB\-\-emit\-llvm\fR
25-
Produce an LLVM bitcode file
21+
\fB\-\-emit=[asm,ir,bc,obj,link]\fR
22+
Configure the output that rustc will produce
2623
.TP
2724
\fB\-h\fR, \fB\-\-help\fR
2825
Display this message
2926
.TP
3027
\fB\-L\fR PATH
3128
Add a directory to the library search path
3229
.TP
33-
\fB\-\-lib\fR
34-
Compile a library crate
35-
.TP
3630
\fB\-\-linker\fR LINKER
3731
Program to use for linking instead of the default
3832
.TP
@@ -49,7 +43,7 @@ Run all passes except translation; no output
4943
Equivalent to \fI\-\-opt\-level=2\fR
5044
.TP
5145
\fB\-o\fR FILENAME
52-
Write output to <filename>
46+
Write output to <filename>. Ignored if more than one --emit is specified.
5347
.TP
5448
\fB\-\-opt\-level\fR LEVEL
5549
Optimize with possible levels 0-3
@@ -60,7 +54,8 @@ the default passes for the optimization level. A value of 'list'
6054
will list the available passes.
6155
.TP
6256
\fB\-\-out\-dir\fR DIR
63-
Write output to compiler-chosen filename in <dir>
57+
Write output to compiler-chosen filename in <dir>. Ignored if -o is specified.
58+
(default the current directory)
6459
.TP
6560
\fB\-\-parse\-only\fR
6661
Parse only; do not compile, assemble, or link
@@ -71,9 +66,6 @@ Pretty-print the input instead of compiling; valid types are: normal
7166
expanded, with type annotations), or identified (fully parenthesized,
7267
AST nodes and blocks with IDs)
7368
.TP
74-
\fB\-S\fR
75-
Compile only; do not assemble or link
76-
.TP
7769
\fB\-\-save\-temps\fR
7870
Write intermediate files (.bc, .opt.bc, .o) in addition to normal output
7971
.TP
@@ -120,7 +112,7 @@ To build an executable from a source file with a main function:
120112
$ rustc -o hello hello.rs
121113

122114
To build a library from a source file:
123-
$ rustc --lib hello-lib.rs
115+
$ rustc --crate-type=lib hello-lib.rs
124116

125117
To build either with a crate (.rs) file:
126118
$ rustc hello.rs

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ $(foreach crate,$(CRATES),$(eval $(call RUST_CRATE,$(crate))))
102102
#
103103
# $(1) is the crate to generate variables for
104104
define RUST_TOOL
105-
TOOL_INPUTS_$(1) := $$(wildcard $$(addprefix $(S)$$(dir $$(TOOL_SOURCE_$(1))), \
105+
TOOL_INPUTS_$(1) := $$(wildcard $$(addprefix $$(dir $$(TOOL_SOURCE_$(1))), \
106106
*.rs */*.rs */*/*.rs */*/*/*.rs))
107107
endef
108108

mk/tests.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ $$(TLIB2_T_$(2)_H_$(3))/$$(FT_LIB): \
835835
tmp/$$(FT).rc \
836836
$$(SREQ2_T_$(2)_H_$(3))
837837
@$$(call E, compile_and_link: $$@)
838-
$$(STAGE2_T_$(2)_H_$(3)) --lib -o $$@ $$< \
838+
$$(STAGE2_T_$(2)_H_$(3)) --crate-type=dylib --out-dir $$(@D) $$< \
839839
-L "$$(RT_OUTPUT_DIR_$(2))"
840840

841841
$(3)/test/$$(FT_DRIVER)-$(2)$$(X_$(2)): \

src/compiletest/runtest.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ actual:\n\
245245
};
246246
// FIXME (#9639): This needs to handle non-utf8 paths
247247
let mut args = ~[~"-",
248-
~"--no-trans", ~"--lib",
248+
~"--no-trans", ~"--crate-type=lib",
249249
~"--target=" + target,
250250
~"-L", config.build_base.as_str().unwrap().to_owned(),
251251
~"-L",
@@ -659,7 +659,7 @@ fn compile_test_(config: &config, props: &TestProps,
659659
// FIXME (#9639): This needs to handle non-utf8 paths
660660
let link_args = ~[~"-L", aux_dir.as_str().unwrap().to_owned()];
661661
let args = make_compile_args(config, props, link_args + extra_args,
662-
make_exe_name, testfile);
662+
|a, b| ThisFile(make_exe_name(a, b)), testfile);
663663
compose_and_run_compiler(config, props, testfile, args, None)
664664
}
665665

@@ -702,8 +702,12 @@ fn compose_and_run_compiler(
702702
let abs_ab = config.aux_base.join(rel_ab.as_slice());
703703
let aux_props = load_props(&abs_ab);
704704
let aux_args =
705-
make_compile_args(config, &aux_props, ~[~"--dylib"] + extra_link_args,
706-
|a,b| make_lib_name(a, b, testfile), &abs_ab);
705+
make_compile_args(config, &aux_props, ~[~"--crate-type=dylib"]
706+
+ extra_link_args,
707+
|a,b| {
708+
let f = make_lib_name(a, b, testfile);
709+
ThisDirectory(f.dir_path())
710+
}, &abs_ab);
707711
let auxres = compose_and_run(config, &abs_ab, aux_args, ~[],
708712
config.compile_lib_path, None);
709713
if !auxres.status.success() {
@@ -741,10 +745,15 @@ fn compose_and_run(config: &config, testfile: &Path,
741745
prog, args, procenv, input);
742746
}
743747

748+
enum TargetLocation {
749+
ThisFile(Path),
750+
ThisDirectory(Path),
751+
}
752+
744753
fn make_compile_args(config: &config,
745754
props: &TestProps,
746755
extras: ~[~str],
747-
xform: |&config, &Path| -> Path,
756+
xform: |&config, &Path| -> TargetLocation,
748757
testfile: &Path)
749758
-> ProcArgs {
750759
let xform_file = xform(config, testfile);
@@ -755,10 +764,14 @@ fn make_compile_args(config: &config,
755764
};
756765
// FIXME (#9639): This needs to handle non-utf8 paths
757766
let mut args = ~[testfile.as_str().unwrap().to_owned(),
758-
~"-o", xform_file.as_str().unwrap().to_owned(),
759767
~"-L", config.build_base.as_str().unwrap().to_owned(),
760768
~"--target=" + target]
761769
+ extras;
770+
let path = match xform_file {
771+
ThisFile(path) => { args.push(~"-o"); path }
772+
ThisDirectory(path) => { args.push(~"--out-dir"); path }
773+
};
774+
args.push(path.as_str().unwrap().to_owned());
762775
args.push_all_move(split_maybe_args(&config.rustcflags));
763776
args.push_all_move(split_maybe_args(&props.compile_flags));
764777
return ProcArgs {prog: config.rustc_path.as_str().unwrap().to_owned(), args: args};
@@ -1043,10 +1056,10 @@ fn compile_test_and_save_bitcode(config: &config, props: &TestProps,
10431056
let aux_dir = aux_output_dir_name(config, testfile);
10441057
// FIXME (#9639): This needs to handle non-utf8 paths
10451058
let link_args = ~[~"-L", aux_dir.as_str().unwrap().to_owned()];
1046-
let llvm_args = ~[~"-c", ~"--lib", ~"--save-temps"];
1059+
let llvm_args = ~[~"--emit=obj", ~"--crate-type=lib", ~"--save-temps"];
10471060
let args = make_compile_args(config, props,
10481061
link_args + llvm_args,
1049-
make_o_name, testfile);
1062+
|a, b| ThisFile(make_o_name(a, b)), testfile);
10501063
compose_and_run_compiler(config, props, testfile, args, None)
10511064
}
10521065

src/doc/rust.md

+35-35
Original file line numberDiff line numberDiff line change
@@ -3678,43 +3678,43 @@ found in the [ffi tutorial][ffi].
36783678
In one session of compilation, the compiler can generate multiple artifacts
36793679
through the usage of command line flags and the `crate_type` attribute.
36803680

3681-
* `--bin`, `#[crate_type = "bin"]` - A runnable executable will be produced.
3682-
This requires that there is a `main` function in the crate which will be run
3683-
when the program begins executing. This will link in all Rust and native
3684-
dependencies, producing a distributable binary.
3685-
3686-
* `--lib`, `#[crate_type = "lib"]` - A Rust library will be produced. This is
3687-
an ambiguous concept as to what exactly is produced because a library can
3688-
manifest itself in several forms. The purpose of this generic `lib` option is
3689-
to generate the "compiler recommended" style of library. The output library
3690-
will always be usable by rustc, but the actual type of library may change
3691-
from time-to-time. The remaining output types are all different flavors of
3692-
libraries, and the `lib` type can be seen as an alias for one of them (but
3693-
the actual one is compiler-defined).
3694-
3695-
* `--dylib`, `#[crate_type = "dylib"]` - A dynamic Rust library will be
3696-
produced. This is different from the `lib` output type in that this forces
3681+
* `--crate-type=bin`, `#[crate_type = "bin"]` - A runnable executable will be
3682+
produced. This requires that there is a `main` function in the crate which
3683+
will be run when the program begins executing. This will link in all Rust and
3684+
native dependencies, producing a distributable binary.
3685+
3686+
* `--crate-type=lib`, `#[crate_type = "lib"]` - A Rust library will be produced.
3687+
This is an ambiguous concept as to what exactly is produced because a library
3688+
can manifest itself in several forms. The purpose of this generic `lib` option
3689+
is to generate the "compiler recommended" style of library. The output library
3690+
will always be usable by rustc, but the actual type of library may change from
3691+
time-to-time. The remaining output types are all different flavors of
3692+
libraries, and the `lib` type can be seen as an alias for one of them (but the
3693+
actual one is compiler-defined).
3694+
3695+
* `--crate-type=dylib`, `#[crate_type = "dylib"]` - A dynamic Rust library will
3696+
be produced. This is different from the `lib` output type in that this forces
36973697
dynamic library generation. The resulting dynamic library can be used as a
36983698
dependency for other libraries and/or executables. This output type will
36993699
create `*.so` files on linux, `*.dylib` files on osx, and `*.dll` files on
37003700
windows.
37013701

3702-
* `--staticlib`, `#[crate_type = "staticlib"]` - A static system library will
3703-
be produced. This is different from other library outputs in that the Rust
3704-
compiler will never attempt to link to `staticlib` outputs. The purpose of
3705-
this output type is to create a static library containing all of the local
3706-
crate's code along with all upstream dependencies. The static library is
3707-
actually a `*.a` archive on linux and osx and a `*.lib` file on windows. This
3708-
format is recommended for use in situtations such as linking Rust code into an
3709-
existing non-Rust application because it will not have dynamic dependencies on
3710-
other Rust code.
3711-
3712-
* `--rlib`, `#[crate_type = "rlib"]` - A "Rust library" file will be produced.
3713-
This is used as an intermediate artifact and can be thought of as a "static
3714-
Rust library". These `rlib` files, unlike `staticlib` files, are interpreted
3715-
by the Rust compiler in future linkage. This essentially means that `rustc`
3716-
will look for metadata in `rlib` files like it looks for metadata in dynamic
3717-
libraries. This form of output is used to produce statically linked
3702+
* `--crate-type=staticlib`, `#[crate_type = "staticlib"]` - A static system
3703+
library will be produced. This is different from other library outputs in that
3704+
the Rust compiler will never attempt to link to `staticlib` outputs. The
3705+
purpose of this output type is to create a static library containing all of
3706+
the local crate's code along with all upstream dependencies. The static
3707+
library is actually a `*.a` archive on linux and osx and a `*.lib` file on
3708+
windows. This format is recommended for use in situtations such as linking
3709+
Rust code into an existing non-Rust application because it will not have
3710+
dynamic dependencies on other Rust code.
3711+
3712+
* `--crate-type=rlib`, `#[crate_type = "rlib"]` - A "Rust library" file will be
3713+
produced. This is used as an intermediate artifact and can be thought of as a
3714+
"static Rust library". These `rlib` files, unlike `staticlib` files, are
3715+
interpreted by the Rust compiler in future linkage. This essentially means
3716+
that `rustc` will look for metadata in `rlib` files like it looks for metadata
3717+
in dynamic libraries. This form of output is used to produce statically linked
37183718
executables as well as `staticlib` outputs.
37193719

37203720
Note that these outputs are stackable in the sense that if multiple are
@@ -3769,9 +3769,9 @@ dependencies will be used:
37693769
then the compiler will force all dependencies to be dynamic and will generate
37703770
errors if dynamic versions could not be found.
37713771

3772-
In general, `--bin` or `--lib` should be sufficient for all compilation needs,
3773-
and the other options are just available if more fine-grained control is desired
3774-
over the output format of a Rust crate.
3772+
In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for
3773+
all compilation needs, and the other options are just available if more
3774+
fine-grained control is desired over the output format of a Rust crate.
37753775

37763776
### Logging system
37773777

src/doc/tutorial.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3162,8 +3162,8 @@ fn main() { println!("hello {}", world::explore()); }
31623162
Now compile and run like this (adjust to your platform if necessary):
31633163

31643164
~~~~ {.notrust}
3165-
> rustc --lib world.rs # compiles libworld-<HASH>-0.42.so
3166-
> rustc main.rs -L . # compiles main
3165+
> rustc --crate-type=lib world.rs # compiles libworld-<HASH>-0.42.so
3166+
> rustc main.rs -L . # compiles main
31673167
> ./main
31683168
"hello world"
31693169
~~~~

src/etc/zsh/_rust

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ typeset -A opt_args
77
_rustc_opts_switches=(
88
--android-cross-path'[The path to the Android NDK]'
99
--ar'[Program to use for managing archives instead of the default.]'
10-
--bin'[Compile an executable crate (default)]'
1110
-c'[Compile and assemble, but do not link]'
1211
--cfg'[Configure the compilation environment]'
1312
--crate-id'[Output the crate id and exit]'
1413
--crate-file-name'[Output the file(s) that would be written if compilation continued and exit]'
1514
--crate-name'[Output the crate name and exit]'
1615
--dep-info'[Output dependency info to <filename> after compiling]'
17-
--dylib'[Compile a dynamic library crate]'
18-
--emit-llvm'[Produce an LLVM bitcode file]'
16+
--crate-type'[Specify the type of crate to crate]'
1917
{-h,--help}'[Display this message]'
2018
-L'[Add a directory to the library search path]'
21-
--lib'[Compile a library crate]'
2219
--linker'[Program to use for linking instead of the default.]'
2320
--link-args'[FLAGS is a space-separated list of flags passed to the linker]'
2421
--llvm-args'[A list of arguments to pass to llvm, comma separated]'
@@ -33,10 +30,7 @@ _rustc_opts_switches=(
3330
--parse-only'[Parse only; do not compile, assemble, or link]'
3431
--passes'[Comma or space separated list of pass names to use]'
3532
--pretty'[Pretty-print the input instead of compiling]'
36-
--rlib'[Compile a rust library crate as an rlib file]'
37-
-S'[Compile only; do not assemble or link]'
3833
--save-temps'[Write intermediate files (.bc, .opt.bc, .o) in addition to normal output]'
39-
--staticlib'[Compile a static library crate]'
4034
--sysroot'[Override the system root]'
4135
--test'[Build a test harness]'
4236
--target'[Target triple cpu-manufacturer-kernel\[-os\] to compile]'

0 commit comments

Comments
 (0)