Blame third_party/rust/gcc/README.md

Packit f0b94e
# gcc-rs
Packit f0b94e
Packit f0b94e
A library to compile C/C++ code into a Rust library/application.
Packit f0b94e
Packit f0b94e
[![Build Status](https://travis-ci.org/alexcrichton/gcc-rs.svg?branch=master)](https://travis-ci.org/alexcrichton/gcc-rs)
Packit f0b94e
[![Build status](https://ci.appveyor.com/api/projects/status/onu270iw98h81nwv?svg=true)](https://ci.appveyor.com/project/alexcrichton/gcc-rs)
Packit f0b94e
Packit f0b94e
[Documentation](https://docs.rs/gcc)
Packit f0b94e
Packit f0b94e
A simple library meant to be used as a build dependency with Cargo packages in
Packit f0b94e
order to build a set of C/C++ files into a static archive. Note that while this
Packit f0b94e
crate is called "gcc", it actually calls out to the most relevant compile for
Packit f0b94e
a platform, for example using `cl` on MSVC. That is, this crate does indeed work
Packit f0b94e
on MSVC!
Packit f0b94e
Packit f0b94e
## Using gcc-rs
Packit f0b94e
Packit f0b94e
First, you'll want to both add a build script for your crate (`build.rs`) and
Packit f0b94e
also add this crate to your `Cargo.toml` via:
Packit f0b94e
Packit f0b94e
```toml
Packit f0b94e
[package]
Packit f0b94e
# ...
Packit f0b94e
build = "build.rs"
Packit f0b94e
Packit f0b94e
[build-dependencies]
Packit f0b94e
gcc = "0.3"
Packit f0b94e
```
Packit f0b94e
Packit f0b94e
Next up, you'll want to write a build script like so:
Packit f0b94e
Packit f0b94e
```rust,no_run
Packit f0b94e
// build.rs
Packit f0b94e
Packit f0b94e
extern crate gcc;
Packit f0b94e
Packit f0b94e
fn main() {
Packit f0b94e
    gcc::Build::new()
Packit f0b94e
        .file("foo.c")
Packit f0b94e
        .file("bar.c")
Packit f0b94e
        .compile("foo");
Packit f0b94e
}
Packit f0b94e
```
Packit f0b94e
Packit f0b94e
And that's it! Running `cargo build` should take care of the rest and your Rust
Packit f0b94e
application will now have the C files `foo.c` and `bar.c` compiled into a file
Packit f0b94e
named libfoo.a. You can call the functions in Rust by declaring functions in
Packit f0b94e
your Rust code like so:
Packit f0b94e
Packit f0b94e
```
Packit f0b94e
extern {
Packit f0b94e
    fn foo_function();
Packit f0b94e
    fn bar_function();
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
pub fn call() {
Packit f0b94e
    unsafe {
Packit f0b94e
        foo_function();
Packit f0b94e
        bar_function();
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
fn main() {
Packit f0b94e
    // ...
Packit f0b94e
}
Packit f0b94e
```
Packit f0b94e
Packit f0b94e
## External configuration via environment variables
Packit f0b94e
Packit f0b94e
To control the programs and flags used for building, the builder can set a
Packit f0b94e
number of different environment variables.
Packit f0b94e
Packit f0b94e
* `CFLAGS` - a series of space separated flags passed to "gcc". Note that
Packit f0b94e
             individual flags cannot currently contain spaces, so doing
Packit f0b94e
             something like: "-L=foo\ bar" is not possible.
Packit f0b94e
* `CC` - the actual C compiler used. Note that this is used as an exact
Packit f0b94e
         executable name, so (for example) no extra flags can be passed inside
Packit f0b94e
         this variable, and the builder must ensure that there aren't any
Packit f0b94e
         trailing spaces. This compiler must understand the `-c` flag. For
Packit f0b94e
         certain `TARGET`s, it also is assumed to know about other flags (most
Packit f0b94e
         common is `-fPIC`).
Packit f0b94e
* `AR` - the `ar` (archiver) executable to use to build the static library.
Packit f0b94e
Packit f0b94e
Each of these variables can also be supplied with certain prefixes and suffixes,
Packit f0b94e
in the following prioritized order:
Packit f0b94e
Packit f0b94e
1. `_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
Packit f0b94e
2. `_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
Packit f0b94e
3. `<build-kind>_` - for example, `HOST_CC` or `TARGET_CFLAGS`
Packit f0b94e
4. `` - a plain `CC`, `AR` as above.
Packit f0b94e
Packit f0b94e
If none of these variables exist, gcc-rs uses built-in defaults
Packit f0b94e
Packit f0b94e
In addition to the the above optional environment variables, `gcc-rs` has some
Packit f0b94e
functions with hard requirements on some variables supplied by [cargo's
Packit f0b94e
build-script driver][cargo] that it has the `TARGET`, `OUT_DIR`, `OPT_LEVEL`,
Packit f0b94e
and `HOST` variables.
Packit f0b94e
Packit f0b94e
[cargo]: http://doc.crates.io/build-script.html#inputs-to-the-build-script
Packit f0b94e
Packit f0b94e
## Optional features
Packit f0b94e
Packit f0b94e
Currently gcc-rs supports parallel compilation (think `make -jN`) but this
Packit f0b94e
feature is turned off by default. To enable gcc-rs to compile C/C++ in parallel,
Packit f0b94e
you can change your dependency to:
Packit f0b94e
Packit f0b94e
```toml
Packit f0b94e
[build-dependencies]
Packit f0b94e
gcc = { version = "0.3", features = ["parallel"] }
Packit f0b94e
```
Packit f0b94e
Packit f0b94e
By default gcc-rs will limit parallelism to `$NUM_JOBS`, or if not present it
Packit f0b94e
will limit it to the number of cpus on the machine. If you are using cargo,
Packit f0b94e
use `-jN` option of `build`, `test` and `run` commands as `$NUM_JOBS`
Packit f0b94e
is supplied by cargo.
Packit f0b94e
Packit f0b94e
## Compile-time Requirements
Packit f0b94e
Packit f0b94e
To work properly this crate needs access to a C compiler when the build script
Packit f0b94e
is being run. This crate does not ship a C compiler with it. The compiler
Packit f0b94e
required varies per platform, but there are three broad categories:
Packit f0b94e
Packit f0b94e
* Unix platforms require `cc` to be the C compiler. This can be found by
Packit f0b94e
  installing gcc/clang on Linux distributions and Xcode on OSX, for example.
Packit f0b94e
* Windows platforms targeting MSVC (e.g. your target triple ends in `-msvc`)
Packit f0b94e
  require `cl.exe` to be available and in `PATH`. This is typically found in
Packit f0b94e
  standard Visual Studio installations and the `PATH` can be set up by running
Packit f0b94e
  the appropriate developer tools shell.
Packit f0b94e
* Windows platforms targeting MinGW (e.g. your target triple ends in `-gnu`)
Packit f0b94e
  require `gcc` to be available in `PATH`. We recommend the
Packit f0b94e
  [MinGW-w64](http://mingw-w64.org) distribution, which is using the
Packit f0b94e
  [Win-builds](http://win-builds.org) installation system.
Packit f0b94e
  You may also acquire it via
Packit f0b94e
  [MSYS2](http://msys2.github.io), as explained [here][msys2-help].  Make sure
Packit f0b94e
  to install the appropriate architecture corresponding to your installation of
Packit f0b94e
  rustc. GCC from older [MinGW](http://www.mingw.org) project is compatible
Packit f0b94e
  only with 32-bit rust compiler.
Packit f0b94e
Packit f0b94e
[msys2-help]: http://github.com/rust-lang/rust#building-on-windows
Packit f0b94e
Packit f0b94e
## C++ support
Packit f0b94e
Packit f0b94e
`gcc-rs` supports C++ libraries compilation by using the `cpp` method on
Packit f0b94e
`Build`:
Packit f0b94e
Packit f0b94e
```rust,no_run
Packit f0b94e
extern crate gcc;
Packit f0b94e
Packit f0b94e
fn main() {
Packit f0b94e
    gcc::Build::new()
Packit f0b94e
        .cpp(true) // Switch to C++ library compilation.
Packit f0b94e
        .file("foo.cpp")
Packit f0b94e
        .compile("libfoo.a");
Packit f0b94e
}
Packit f0b94e
```
Packit f0b94e
Packit f0b94e
When using C++ library compilation switch, the `CXX` and `CXXFLAGS` env
Packit f0b94e
variables are used instead of `CC` and `CFLAGS` and the C++ standard library is
Packit f0b94e
linked to the crate target.
Packit f0b94e
Packit f0b94e
## License
Packit f0b94e
Packit f0b94e
`gcc-rs` is primarily distributed under the terms of both the MIT license and
Packit f0b94e
the Apache License (Version 2.0), with portions covered by various BSD-like
Packit f0b94e
licenses.
Packit f0b94e
Packit f0b94e
See LICENSE-APACHE, and LICENSE-MIT for details.