Skip to content

Commit

Permalink
auto merge of #12020 : alexcrichton/rust/output-flags, r=brson
Browse files Browse the repository at this point in the history
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 directory of
   the crate file.
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
  • Loading branch information
bors committed Feb 6, 2014
2 parents 680925e + 6e7968b commit c13a929
Show file tree
Hide file tree
Showing 30 changed files with 417 additions and 358 deletions.
24 changes: 8 additions & 16 deletions man/rustc.1
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,21 @@ This program is a compiler for the Rust language, available at
.SH OPTIONS

.TP
\fB\-\-bin\fR
Compile an executable crate (default)
.TP
\fB\-c\fR
Compile and assemble, but do not link
\fB\-\-crate-type=[bin|lib|dylib|rlib|staticlib]\fR
Configure the flavor of rust crate that is generated (default `bin`)
.TP
\fB\-\-cfg\fR SPEC
Configure the compilation environment
.TP
\fB\-\-emit\-llvm\fR
Produce an LLVM bitcode file
\fB\-\-emit=[asm,ir,bc,obj,link]\fR
Configure the output that rustc will produce
.TP
\fB\-h\fR, \fB\-\-help\fR
Display this message
.TP
\fB\-L\fR PATH
Add a directory to the library search path
.TP
\fB\-\-lib\fR
Compile a library crate
.TP
\fB\-\-linker\fR LINKER
Program to use for linking instead of the default
.TP
Expand All @@ -49,7 +43,7 @@ Run all passes except translation; no output
Equivalent to \fI\-\-opt\-level=2\fR
.TP
\fB\-o\fR FILENAME
Write output to <filename>
Write output to <filename>. Ignored if more than one --emit is specified.
.TP
\fB\-\-opt\-level\fR LEVEL
Optimize with possible levels 0-3
Expand All @@ -60,7 +54,8 @@ the default passes for the optimization level. A value of 'list'
will list the available passes.
.TP
\fB\-\-out\-dir\fR DIR
Write output to compiler-chosen filename in <dir>
Write output to compiler-chosen filename in <dir>. Ignored if -o is specified.
(default the current directory)
.TP
\fB\-\-parse\-only\fR
Parse only; do not compile, assemble, or link
Expand All @@ -71,9 +66,6 @@ Pretty-print the input instead of compiling; valid types are: normal
expanded, with type annotations), or identified (fully parenthesized,
AST nodes and blocks with IDs)
.TP
\fB\-S\fR
Compile only; do not assemble or link
.TP
\fB\-\-save\-temps\fR
Write intermediate files (.bc, .opt.bc, .o) in addition to normal output
.TP
Expand Down Expand Up @@ -120,7 +112,7 @@ To build an executable from a source file with a main function:
$ rustc -o hello hello.rs

To build a library from a source file:
$ rustc --lib hello-lib.rs
$ rustc --crate-type=lib hello-lib.rs

To build either with a crate (.rs) file:
$ rustc hello.rs
Expand Down
2 changes: 1 addition & 1 deletion mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ $(foreach crate,$(CRATES),$(eval $(call RUST_CRATE,$(crate))))
#
# $(1) is the crate to generate variables for
define RUST_TOOL
TOOL_INPUTS_$(1) := $$(wildcard $$(addprefix $(S)$$(dir $$(TOOL_SOURCE_$(1))), \
TOOL_INPUTS_$(1) := $$(wildcard $$(addprefix $$(dir $$(TOOL_SOURCE_$(1))), \
*.rs */*.rs */*/*.rs */*/*/*.rs))
endef

Expand Down
2 changes: 1 addition & 1 deletion mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ $$(TLIB2_T_$(2)_H_$(3))/$$(FT_LIB): \
tmp/$$(FT).rc \
$$(SREQ2_T_$(2)_H_$(3))
@$$(call E, compile_and_link: $$@)
$$(STAGE2_T_$(2)_H_$(3)) --lib -o $$@ $$< \
$$(STAGE2_T_$(2)_H_$(3)) --crate-type=dylib --out-dir $$(@D) $$< \
-L "$$(RT_OUTPUT_DIR_$(2))"

$(3)/test/$$(FT_DRIVER)-$(2)$$(X_$(2)): \
Expand Down
29 changes: 21 additions & 8 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ actual:\n\
};
// FIXME (#9639): This needs to handle non-utf8 paths
let mut args = ~[~"-",
~"--no-trans", ~"--lib",
~"--no-trans", ~"--crate-type=lib",
~"--target=" + target,
~"-L", config.build_base.as_str().unwrap().to_owned(),
~"-L",
Expand Down Expand Up @@ -659,7 +659,7 @@ fn compile_test_(config: &config, props: &TestProps,
// FIXME (#9639): This needs to handle non-utf8 paths
let link_args = ~[~"-L", aux_dir.as_str().unwrap().to_owned()];
let args = make_compile_args(config, props, link_args + extra_args,
make_exe_name, testfile);
|a, b| ThisFile(make_exe_name(a, b)), testfile);
compose_and_run_compiler(config, props, testfile, args, None)
}

Expand Down Expand Up @@ -702,8 +702,12 @@ fn compose_and_run_compiler(
let abs_ab = config.aux_base.join(rel_ab.as_slice());
let aux_props = load_props(&abs_ab);
let aux_args =
make_compile_args(config, &aux_props, ~[~"--dylib"] + extra_link_args,
|a,b| make_lib_name(a, b, testfile), &abs_ab);
make_compile_args(config, &aux_props, ~[~"--crate-type=dylib"]
+ extra_link_args,
|a,b| {
let f = make_lib_name(a, b, testfile);
ThisDirectory(f.dir_path())
}, &abs_ab);
let auxres = compose_and_run(config, &abs_ab, aux_args, ~[],
config.compile_lib_path, None);
if !auxres.status.success() {
Expand Down Expand Up @@ -741,10 +745,15 @@ fn compose_and_run(config: &config, testfile: &Path,
prog, args, procenv, input);
}

enum TargetLocation {
ThisFile(Path),
ThisDirectory(Path),
}

fn make_compile_args(config: &config,
props: &TestProps,
extras: ~[~str],
xform: |&config, &Path| -> Path,
xform: |&config, &Path| -> TargetLocation,
testfile: &Path)
-> ProcArgs {
let xform_file = xform(config, testfile);
Expand All @@ -755,10 +764,14 @@ fn make_compile_args(config: &config,
};
// FIXME (#9639): This needs to handle non-utf8 paths
let mut args = ~[testfile.as_str().unwrap().to_owned(),
~"-o", xform_file.as_str().unwrap().to_owned(),
~"-L", config.build_base.as_str().unwrap().to_owned(),
~"--target=" + target]
+ extras;
let path = match xform_file {
ThisFile(path) => { args.push(~"-o"); path }
ThisDirectory(path) => { args.push(~"--out-dir"); path }
};
args.push(path.as_str().unwrap().to_owned());
args.push_all_move(split_maybe_args(&config.rustcflags));
args.push_all_move(split_maybe_args(&props.compile_flags));
return ProcArgs {prog: config.rustc_path.as_str().unwrap().to_owned(), args: args};
Expand Down Expand Up @@ -1043,10 +1056,10 @@ fn compile_test_and_save_bitcode(config: &config, props: &TestProps,
let aux_dir = aux_output_dir_name(config, testfile);
// FIXME (#9639): This needs to handle non-utf8 paths
let link_args = ~[~"-L", aux_dir.as_str().unwrap().to_owned()];
let llvm_args = ~[~"-c", ~"--lib", ~"--save-temps"];
let llvm_args = ~[~"--emit=obj", ~"--crate-type=lib", ~"--save-temps"];
let args = make_compile_args(config, props,
link_args + llvm_args,
make_o_name, testfile);
|a, b| ThisFile(make_o_name(a, b)), testfile);
compose_and_run_compiler(config, props, testfile, args, None)
}

Expand Down
70 changes: 35 additions & 35 deletions src/doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -3678,43 +3678,43 @@ found in the [ffi tutorial][ffi].
In one session of compilation, the compiler can generate multiple artifacts
through the usage of command line flags and the `crate_type` attribute.

* `--bin`, `#[crate_type = "bin"]` - A runnable executable will be produced.
This requires that there is a `main` function in the crate which will be run
when the program begins executing. This will link in all Rust and native
dependencies, producing a distributable binary.

* `--lib`, `#[crate_type = "lib"]` - A Rust library will be produced. This is
an ambiguous concept as to what exactly is produced because a library can
manifest itself in several forms. The purpose of this generic `lib` option is
to generate the "compiler recommended" style of library. The output library
will always be usable by rustc, but the actual type of library may change
from time-to-time. The remaining output types are all different flavors of
libraries, and the `lib` type can be seen as an alias for one of them (but
the actual one is compiler-defined).

* `--dylib`, `#[crate_type = "dylib"]` - A dynamic Rust library will be
produced. This is different from the `lib` output type in that this forces
* `--crate-type=bin`, `#[crate_type = "bin"]` - A runnable executable will be
produced. This requires that there is a `main` function in the crate which
will be run when the program begins executing. This will link in all Rust and
native dependencies, producing a distributable binary.

* `--crate-type=lib`, `#[crate_type = "lib"]` - A Rust library will be produced.
This is an ambiguous concept as to what exactly is produced because a library
can manifest itself in several forms. The purpose of this generic `lib` option
is to generate the "compiler recommended" style of library. The output library
will always be usable by rustc, but the actual type of library may change from
time-to-time. The remaining output types are all different flavors of
libraries, and the `lib` type can be seen as an alias for one of them (but the
actual one is compiler-defined).

* `--crate-type=dylib`, `#[crate_type = "dylib"]` - A dynamic Rust library will
be produced. This is different from the `lib` output type in that this forces
dynamic library generation. The resulting dynamic library can be used as a
dependency for other libraries and/or executables. This output type will
create `*.so` files on linux, `*.dylib` files on osx, and `*.dll` files on
windows.

* `--staticlib`, `#[crate_type = "staticlib"]` - A static system library will
be produced. This is different from other library outputs in that the Rust
compiler will never attempt to link to `staticlib` outputs. The purpose of
this output type is to create a static library containing all of the local
crate's code along with all upstream dependencies. The static library is
actually a `*.a` archive on linux and osx and a `*.lib` file on windows. This
format is recommended for use in situtations such as linking Rust code into an
existing non-Rust application because it will not have dynamic dependencies on
other Rust code.

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

* `--crate-type=rlib`, `#[crate_type = "rlib"]` - A "Rust library" file will be
produced. This is used as an intermediate artifact and can be thought of as a
"static Rust library". These `rlib` files, unlike `staticlib` files, are
interpreted by the Rust compiler in future linkage. This essentially means
that `rustc` will look for metadata in `rlib` files like it looks for metadata
in dynamic libraries. This form of output is used to produce statically linked
executables as well as `staticlib` outputs.

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

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

### Logging system

Expand Down
4 changes: 2 additions & 2 deletions src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -3162,8 +3162,8 @@ fn main() { println!("hello {}", world::explore()); }
Now compile and run like this (adjust to your platform if necessary):

~~~~ {.notrust}
> rustc --lib world.rs # compiles libworld-<HASH>-0.42.so
> rustc main.rs -L . # compiles main
> rustc --crate-type=lib world.rs # compiles libworld-<HASH>-0.42.so
> rustc main.rs -L . # compiles main
> ./main
"hello world"
~~~~
Expand Down
8 changes: 1 addition & 7 deletions src/etc/zsh/_rust
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ typeset -A opt_args
_rustc_opts_switches=(
--android-cross-path'[The path to the Android NDK]'
--ar'[Program to use for managing archives instead of the default.]'
--bin'[Compile an executable crate (default)]'
-c'[Compile and assemble, but do not link]'
--cfg'[Configure the compilation environment]'
--crate-id'[Output the crate id and exit]'
--crate-file-name'[Output the file(s) that would be written if compilation continued and exit]'
--crate-name'[Output the crate name and exit]'
--dep-info'[Output dependency info to <filename> after compiling]'
--dylib'[Compile a dynamic library crate]'
--emit-llvm'[Produce an LLVM bitcode file]'
--crate-type'[Specify the type of crate to crate]'
{-h,--help}'[Display this message]'
-L'[Add a directory to the library search path]'
--lib'[Compile a library crate]'
--linker'[Program to use for linking instead of the default.]'
--link-args'[FLAGS is a space-separated list of flags passed to the linker]'
--llvm-args'[A list of arguments to pass to llvm, comma separated]'
Expand All @@ -33,10 +30,7 @@ _rustc_opts_switches=(
--parse-only'[Parse only; do not compile, assemble, or link]'
--passes'[Comma or space separated list of pass names to use]'
--pretty'[Pretty-print the input instead of compiling]'
--rlib'[Compile a rust library crate as an rlib file]'
-S'[Compile only; do not assemble or link]'
--save-temps'[Write intermediate files (.bc, .opt.bc, .o) in addition to normal output]'
--staticlib'[Compile a static library crate]'
--sysroot'[Override the system root]'
--test'[Build a test harness]'
--target'[Target triple cpu-manufacturer-kernel\[-os\] to compile]'
Expand Down
Loading

0 comments on commit c13a929

Please sign in to comment.