Skip to content

Commit 12dc0ba

Browse files
authored
Merge branch 'master' into master
2 parents f1e8ae1 + 7b6f4a0 commit 12dc0ba

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ Next up, you'll want to write a build script like so:
3535
extern crate gcc;
3636
3737
fn main() {
38-
gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
38+
gcc::Config::new()
39+
.files(["foo.c", "bar.c"])
40+
.compile("foo");
3941
}
4042
```
4143

4244
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+
application will now have the C files `foo.c` and `bar.c` compiled into a file
46+
named libfoo.a. You can call the functions in Rust by declaring functions in
47+
your Rust code like so:
4548

4649
```
4750
extern {

src/lib.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,16 @@
1010
//!
1111
//! The purpose of this crate is to provide the utility functions necessary to
1212
//! compile C code into a static archive which is then linked into a Rust crate.
13-
//! The top-level `compile_library` function serves as a convenience and more
14-
//! advanced configuration is available through the `Config` builder.
13+
//! Configuration is available through the `Config` builder.
1514
//!
1615
//! This crate will automatically detect situations such as cross compilation or
1716
//! other environment variables set by Cargo and will build code appropriately.
1817
//!
19-
//! # Examples
20-
//!
21-
//! Use the default configuration:
18+
//! [`Config`]: struct.Config.html
2219
//!
23-
//! ```no_run
24-
//! extern crate gcc;
25-
//!
26-
//! fn main() {
27-
//! gcc::compile_library("libfoo.a", &["src/foo.c"]);
28-
//! }
29-
//! ```
20+
//! # Examples
3021
//!
31-
//! Use more advanced configuration:
22+
//! Use the `Config` builder to compile `src/foo.c`:
3223
//!
3324
//! ```no_run
3425
//! extern crate gcc;
@@ -38,7 +29,7 @@
3829
//! .file("src/foo.c")
3930
//! .define("FOO", Some("bar"))
4031
//! .include("src")
41-
//! .compile("libfoo.a");
32+
//! .compile("foo");
4233
//! }
4334
//! ```
4435
@@ -168,8 +159,10 @@ impl ToolFamily {
168159
/// # Example
169160
///
170161
/// ```no_run
171-
/// gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
162+
/// gcc::compile_library("foo", &["foo.c", "bar.c"]);
172163
/// ```
164+
#[deprecated]
165+
#[doc(hidden)]
173166
pub fn compile_library(output: &str, files: &[&str]) {
174167
let mut c = Config::new();
175168
for f in files.iter() {
@@ -181,7 +174,9 @@ pub fn compile_library(output: &str, files: &[&str]) {
181174
impl Config {
182175
/// Construct a new instance of a blank set of configuration.
183176
///
184-
/// This builder is finished with the `compile` function.
177+
/// This builder is finished with the [`compile`] function.
178+
///
179+
/// [`compile`]: struct.Config.html#method.compile
185180
pub fn new() -> Config {
186181
Config {
187182
include_directories: Vec::new(),
@@ -221,7 +216,7 @@ impl Config {
221216
/// .file("src/foo.c")
222217
/// .include(library_path)
223218
/// .include("src")
224-
/// .compile("libfoo.a");
219+
/// .compile("foo");
225220
/// ```
226221
pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Config {
227222
self.include_directories.push(dir.as_ref().to_path_buf());
@@ -237,7 +232,7 @@ impl Config {
237232
/// .file("src/foo.c")
238233
/// .define("FOO", Some("BAR"))
239234
/// .define("BAZ", None)
240-
/// .compile("libfoo.a");
235+
/// .compile("foo");
241236
/// ```
242237
pub fn define(&mut self, var: &str, val: Option<&str>) -> &mut Config {
243238
self.definitions.push((var.to_string(), val.map(|s| s.to_string())));
@@ -258,7 +253,7 @@ impl Config {
258253
/// gcc::Config::new()
259254
/// .file("src/foo.c")
260255
/// .flag("-ffunction-sections")
261-
/// .compile("libfoo.a");
256+
/// .compile("foo");
262257
/// ```
263258
pub fn flag(&mut self, flag: &str) -> &mut Config {
264259
self.flags.push(flag.to_string());
@@ -296,7 +291,7 @@ impl Config {
296291
/// .file("src/foo.c")
297292
/// .shared_flag(true)
298293
/// .static_flag(true)
299-
/// .compile("libfoo.so");
294+
/// .compile("foo");
300295
/// ```
301296
pub fn static_flag(&mut self, static_flag: bool) -> &mut Config {
302297
self.static_flag = Some(static_flag);
@@ -381,7 +376,7 @@ impl Config {
381376
/// gcc::Config::new()
382377
/// .file("src/foo.c")
383378
/// .target("aarch64-linux-android")
384-
/// .compile("libfoo.so");
379+
/// .compile("foo");
385380
/// ```
386381
pub fn target(&mut self, target: &str) -> &mut Config {
387382
self.target = Some(target.to_string());
@@ -392,14 +387,14 @@ impl Config {
392387
///
393388
/// This option is automatically scraped from the `HOST` environment
394389
/// variable by build scripts, so it's not required to call this function.
395-
///
390+
///
396391
/// # Example
397392
///
398393
/// ```no_run
399394
/// gcc::Config::new()
400395
/// .file("src/foo.c")
401396
/// .host("arm-linux-gnueabihf")
402-
/// .compile("libfoo.so");
397+
/// .compile("foo");
403398
/// ```
404399
pub fn host(&mut self, host: &str) -> &mut Config {
405400
self.host = Some(host.to_string());
@@ -500,17 +495,20 @@ impl Config {
500495

501496
/// Run the compiler, generating the file `output`
502497
///
503-
/// The name `output` must begin with `lib` and end with `.a`
498+
/// The name `output` should be the name of the library. For backwards compatibility,
499+
/// the `output` may start with `lib` and end with `.a`. The Rust compilier will create
500+
/// the assembly with the lib prefix and .a extension. MSVC will create a file without prefix,
501+
/// ending with `.lib`.
504502
///
505503
/// # Panics
506504
///
507505
/// Panics if `output` is not formatted correctly or if one of the underlying
508506
/// compiler commands fails. It can also panic if it fails reading file names
509507
/// or creating directories.
510508
pub fn compile(&self, output: &str) {
511-
assert!(output.starts_with("lib"));
512-
assert!(output.ends_with(".a"));
513-
let lib_name = &output[3..output.len() - 2];
509+
let name_start = if output.starts_with("lib") { 3 } else { 0 };
510+
let name_end = if output.ends_with(".a") { output.len() - 2 } else { output.len() };
511+
let lib_name = &output[name_start..name_end];
514512
let dst = self.get_out_dir();
515513

516514
let mut objects = Vec::new();

0 commit comments

Comments
 (0)