]> git.lizzy.rs Git - rust.git/blob - src/vendor/gcc/README.md
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[rust.git] / src / vendor / gcc / README.md
1 # gcc-rs
2
3 A library to compile C/C++ code into a Rust library/application.
4
5 [![Build Status](https://travis-ci.org/alexcrichton/gcc-rs.svg?branch=master)](https://travis-ci.org/alexcrichton/gcc-rs)
6 [![Build status](https://ci.appveyor.com/api/projects/status/onu270iw98h81nwv?svg=true)](https://ci.appveyor.com/project/alexcrichton/gcc-rs)
7
8 [Documentation](https://docs.rs/gcc)
9
10 A simple library meant to be used as a build dependency with Cargo packages in
11 order to build a set of C/C++ files into a static archive. Note that while this
12 crate is called "gcc", it actually calls out to the most relevant compile for
13 a platform, for example using `cl` on MSVC. That is, this crate does indeed work
14 on MSVC!
15
16 ## Using gcc-rs
17
18 First, you'll want to both add a build script for your crate (`build.rs`) and
19 also add this crate to your `Cargo.toml` via:
20
21 ```toml
22 [package]
23 # ...
24 build = "build.rs"
25
26 [build-dependencies]
27 gcc = "0.3"
28 ```
29
30 Next up, you'll want to write a build script like so:
31
32 ```rust,no_run
33 // build.rs
34
35 extern crate gcc;
36
37 fn main() {
38     gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
39 }
40 ```
41
42 And that's it! Running `cargo build` should take care of the rest and your Rust
43 application will now have the C files `foo.c` and `bar.c` compiled into it. You
44 can call the functions in Rust by declaring functions in your Rust code like so:
45
46 ```
47 extern {
48     fn foo_function();
49     fn bar_function();
50 }
51
52 pub fn call() {
53     unsafe {
54         foo_function();
55         bar_function();
56     }
57 }
58
59 fn main() {
60     // ...
61 }
62 ```
63
64 ## External configuration via environment variables
65
66 To control the programs and flags used for building, the builder can set a
67 number of different environment variables.
68
69 * `CFLAGS` - a series of space separated flags passed to "gcc". Note that
70              individual flags cannot currently contain spaces, so doing
71              something like: "-L=foo\ bar" is not possible.
72 * `CC` - the actual C compiler used. Note that this is used as an exact
73          executable name, so (for example) no extra flags can be passed inside
74          this variable, and the builder must ensure that there aren't any
75          trailing spaces. This compiler must understand the `-c` flag. For
76          certain `TARGET`s, it also is assumed to know about other flags (most
77          common is `-fPIC`).
78 * `AR` - the `ar` (archiver) executable to use to build the static library.
79
80 Each of these variables can also be supplied with certain prefixes and suffixes,
81 in the following prioritized order:
82
83 1. `<var>_<target>` - for example, `CC_x86_64-unknown-linux-gnu`
84 2. `<var>_<target_with_underscores>` - for example, `CC_x86_64_unknown_linux_gnu`
85 3. `<build-kind>_<var>` - for example, `HOST_CC` or `TARGET_CFLAGS`
86 4. `<var>` - a plain `CC`, `AR` as above.
87
88 If none of these variables exist, gcc-rs uses built-in defaults
89
90 In addition to the the above optional environment variables, `gcc-rs` has some
91 functions with hard requirements on some variables supplied by [cargo's
92 build-script driver][cargo] that it has the `TARGET`, `OUT_DIR`, `OPT_LEVEL`,
93 and `HOST` variables.
94
95 [cargo]: http://doc.crates.io/build-script.html#inputs-to-the-build-script
96
97 ## Optional features
98
99 Currently gcc-rs supports parallel compilation (think `make -jN`) but this
100 feature is turned off by default. To enable gcc-rs to compile C/C++ in parallel,
101 you can change your dependency to:
102
103 ```toml
104 [build-dependencies]
105 gcc = { version = "0.3", features = ["parallel"] }
106 ```
107
108 By default gcc-rs will limit parallelism to `$NUM_JOBS`, or if not present it
109 will limit it to the number of cpus on the machine. If you are using cargo,
110 use `-jN` option of `build`, `test` and `run` commands as `$NUM_JOBS`
111 is supplied by cargo.
112
113 ## Compile-time Requirements
114
115 To work properly this crate needs access to a C compiler when the build script
116 is being run. This crate does not ship a C compiler with it. The compiler
117 required varies per platform, but there are three broad categories:
118
119 * Unix platforms require `cc` to be the C compiler. This can be found by
120   installing gcc/clang on Linux distributions and Xcode on OSX, for example.
121 * Windows platforms targeting MSVC (e.g. your target triple ends in `-msvc`)
122   require `cl.exe` to be available and in `PATH`. This is typically found in
123   standard Visual Studio installations and the `PATH` can be set up by running
124   the appropriate developer tools shell.
125 * Windows platforms targeting MinGW (e.g. your target triple ends in `-gnu`)
126   require `gcc` to be available in `PATH`. We recommend the
127   [MinGW-w64](http://mingw-w64.org) distribution, which is using the
128   [Win-builds](http://win-builds.org) installation system.
129   You may also acquire it via
130   [MSYS2](http://msys2.github.io), as explained [here][msys2-help].  Make sure
131   to install the appropriate architecture corresponding to your installation of
132   rustc. GCC from older [MinGW](http://www.mingw.org) project is compatible
133   only with 32-bit rust compiler.
134
135 [msys2-help]: http://github.com/rust-lang/rust#building-on-windows
136
137 ## C++ support
138
139 `gcc-rs` supports C++ libraries compilation by using the `cpp` method on
140 `Config`:
141
142 ```rust,no_run
143 extern crate gcc;
144
145 fn main() {
146     gcc::Config::new()
147         .cpp(true) // Switch to C++ library compilation.
148         .file("foo.cpp")
149         .compile("libfoo.a");
150 }
151 ```
152
153 When using C++ library compilation switch, the `CXX` and `CXXFLAGS` env
154 variables are used instead of `CC` and `CFLAGS` and the C++ standard library is
155 linked to the crate target.
156
157 ## License
158
159 `gcc-rs` is primarily distributed under the terms of both the MIT license and
160 the Apache License (Version 2.0), with portions covered by various BSD-like
161 licenses.
162
163 See LICENSE-APACHE, and LICENSE-MIT for details.