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