Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed compile to accept name of library and updated examples #212

Merged
merged 2 commits into from Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -35,13 +35,14 @@ Next up, you'll want to write a build script like so:
extern crate gcc;

fn main() {
gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
gcc::compile_library("foo", &["foo.c", "bar.c"]);
}
```

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 it. You
can call the functions in Rust by declaring functions in your Rust code like so:
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 {
Expand Down
31 changes: 17 additions & 14 deletions src/lib.rs
Expand Up @@ -24,7 +24,7 @@
//! extern crate gcc;
//!
//! fn main() {
//! gcc::compile_library("libfoo.a", &["src/foo.c"]);
//! gcc::compile_library("foo", &["src/foo.c"]);
//! }
//! ```
//!
Expand All @@ -38,7 +38,7 @@
//! .file("src/foo.c")
//! .define("FOO", Some("bar"))
//! .include("src")
//! .compile("libfoo.a");
//! .compile("foo");
//! }
//! ```

Expand Down Expand Up @@ -168,7 +168,7 @@ impl ToolFamily {
/// # Example
///
/// ```no_run
/// gcc::compile_library("libfoo.a", &["foo.c", "bar.c"]);
/// gcc::compile_library("foo", &["foo.c", "bar.c"]);
/// ```
pub fn compile_library(output: &str, files: &[&str]) {
let mut c = Config::new();
Expand Down Expand Up @@ -221,7 +221,7 @@ impl Config {
/// .file("src/foo.c")
/// .include(library_path)
/// .include("src")
/// .compile("libfoo.a");
/// .compile("foo");
/// ```
pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Config {
self.include_directories.push(dir.as_ref().to_path_buf());
Expand All @@ -237,7 +237,7 @@ impl Config {
/// .file("src/foo.c")
/// .define("FOO", Some("BAR"))
/// .define("BAZ", None)
/// .compile("libfoo.a");
/// .compile("foo");
/// ```
pub fn define(&mut self, var: &str, val: Option<&str>) -> &mut Config {
self.definitions.push((var.to_string(), val.map(|s| s.to_string())));
Expand All @@ -258,7 +258,7 @@ impl Config {
/// gcc::Config::new()
/// .file("src/foo.c")
/// .flag("-ffunction-sections")
/// .compile("libfoo.a");
/// .compile("foo");
/// ```
pub fn flag(&mut self, flag: &str) -> &mut Config {
self.flags.push(flag.to_string());
Expand Down Expand Up @@ -296,7 +296,7 @@ impl Config {
/// .file("src/foo.c")
/// .shared_flag(true)
/// .static_flag(true)
/// .compile("libfoo.so");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you review this line. I'm really not sure if this is in scope. IDK how .so passes that assert!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah yeah this is fine!

/// .compile("foo");
/// ```
pub fn static_flag(&mut self, static_flag: bool) -> &mut Config {
self.static_flag = Some(static_flag);
Expand Down Expand Up @@ -381,7 +381,7 @@ impl Config {
/// gcc::Config::new()
/// .file("src/foo.c")
/// .target("aarch64-linux-android")
/// .compile("libfoo.so");
/// .compile("foo");
/// ```
pub fn target(&mut self, target: &str) -> &mut Config {
self.target = Some(target.to_string());
Expand All @@ -392,14 +392,14 @@ impl Config {
///
/// This option is automatically scraped from the `HOST` environment
/// variable by build scripts, so it's not required to call this function.
///
///
/// # Example
///
/// ```no_run
/// gcc::Config::new()
/// .file("src/foo.c")
/// .host("arm-linux-gnueabihf")
/// .compile("libfoo.so");
/// .compile("foo");
/// ```
pub fn host(&mut self, host: &str) -> &mut Config {
self.host = Some(host.to_string());
Expand Down Expand Up @@ -500,11 +500,14 @@ impl Config {

/// Run the compiler, generating the file `output`
///
/// The name `output` must begin with `lib` and end with `.a`
/// The name `output` should be the name of the library. For backwards compatibility,
/// the `output` may start with `lib` and end with `.a`. The Rust compilier will create
/// the assembly with the lib prefix and .a extension. MSVC will create a file without prefix,
/// ending with `.lib`.
pub fn compile(&self, output: &str) {
assert!(output.starts_with("lib"));
assert!(output.ends_with(".a"));
let lib_name = &output[3..output.len() - 2];
let name_start = if output.starts_with("lib") { 3 } else { 0 };
let name_end = if output.ends_with(".a") { output.len() - 2 } else { output.len() };
let lib_name = &output[name_start..name_end];
let dst = self.get_out_dir();

let mut objects = Vec::new();
Expand Down