Browse Source

Merge pull request #212 from AndyGauge/lib_name

Changed compile to accept name of library and updated examples
cmd
Alex Crichton 8 years ago
committed by GitHub
parent
commit
8b993f4537
  1. 7
      README.md
  2. 29
      src/lib.rs

7
README.md

@ -35,13 +35,14 @@ Next up, you'll want to write a build script like so:
extern crate gcc; extern crate gcc;
fn main() { 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 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 application will now have the C files `foo.c` and `bar.c` compiled into a file
can call the functions in Rust by declaring functions in your Rust code like so: named libfoo.a. You can call the functions in Rust by declaring functions in
your Rust code like so:
``` ```
extern { extern {

29
src/lib.rs

@ -27,7 +27,7 @@
//! extern crate gcc; //! extern crate gcc;
//! //!
//! fn main() { //! fn main() {
//! gcc::compile_library("libfoo.a", &["src/foo.c"]); //! gcc::compile_library("foo", &["src/foo.c"]);
//! } //! }
//! ``` //! ```
//! //!
@ -41,7 +41,7 @@
//! .file("src/foo.c") //! .file("src/foo.c")
//! .define("FOO", Some("bar")) //! .define("FOO", Some("bar"))
//! .include("src") //! .include("src")
//! .compile("libfoo.a"); //! .compile("foo");
//! } //! }
//! ``` //! ```
@ -171,7 +171,7 @@ impl ToolFamily {
/// # Example /// # Example
/// ///
/// ```no_run /// ```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]) { pub fn compile_library(output: &str, files: &[&str]) {
let mut c = Config::new(); let mut c = Config::new();
@ -226,7 +226,7 @@ impl Config {
/// .file("src/foo.c") /// .file("src/foo.c")
/// .include(library_path) /// .include(library_path)
/// .include("src") /// .include("src")
/// .compile("libfoo.a"); /// .compile("foo");
/// ``` /// ```
pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Config { pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Config {
self.include_directories.push(dir.as_ref().to_path_buf()); self.include_directories.push(dir.as_ref().to_path_buf());
@ -242,7 +242,7 @@ impl Config {
/// .file("src/foo.c") /// .file("src/foo.c")
/// .define("FOO", Some("BAR")) /// .define("FOO", Some("BAR"))
/// .define("BAZ", None) /// .define("BAZ", None)
/// .compile("libfoo.a"); /// .compile("foo");
/// ``` /// ```
pub fn define(&mut self, var: &str, val: Option<&str>) -> &mut Config { pub fn define(&mut self, var: &str, val: Option<&str>) -> &mut Config {
self.definitions.push((var.to_string(), val.map(|s| s.to_string()))); self.definitions.push((var.to_string(), val.map(|s| s.to_string())));
@ -263,7 +263,7 @@ impl Config {
/// gcc::Config::new() /// gcc::Config::new()
/// .file("src/foo.c") /// .file("src/foo.c")
/// .flag("-ffunction-sections") /// .flag("-ffunction-sections")
/// .compile("libfoo.a"); /// .compile("foo");
/// ``` /// ```
pub fn flag(&mut self, flag: &str) -> &mut Config { pub fn flag(&mut self, flag: &str) -> &mut Config {
self.flags.push(flag.to_string()); self.flags.push(flag.to_string());
@ -301,7 +301,7 @@ impl Config {
/// .file("src/foo.c") /// .file("src/foo.c")
/// .shared_flag(true) /// .shared_flag(true)
/// .static_flag(true) /// .static_flag(true)
/// .compile("libfoo.so"); /// .compile("foo");
/// ``` /// ```
pub fn static_flag(&mut self, static_flag: bool) -> &mut Config { pub fn static_flag(&mut self, static_flag: bool) -> &mut Config {
self.static_flag = Some(static_flag); self.static_flag = Some(static_flag);
@ -386,7 +386,7 @@ impl Config {
/// gcc::Config::new() /// gcc::Config::new()
/// .file("src/foo.c") /// .file("src/foo.c")
/// .target("aarch64-linux-android") /// .target("aarch64-linux-android")
/// .compile("libfoo.so"); /// .compile("foo");
/// ``` /// ```
pub fn target(&mut self, target: &str) -> &mut Config { pub fn target(&mut self, target: &str) -> &mut Config {
self.target = Some(target.to_string()); self.target = Some(target.to_string());
@ -404,7 +404,7 @@ impl Config {
/// gcc::Config::new() /// gcc::Config::new()
/// .file("src/foo.c") /// .file("src/foo.c")
/// .host("arm-linux-gnueabihf") /// .host("arm-linux-gnueabihf")
/// .compile("libfoo.so"); /// .compile("foo");
/// ``` /// ```
pub fn host(&mut self, host: &str) -> &mut Config { pub fn host(&mut self, host: &str) -> &mut Config {
self.host = Some(host.to_string()); self.host = Some(host.to_string());
@ -505,11 +505,14 @@ impl Config {
/// Run the compiler, generating the file `output` /// 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) { pub fn compile(&self, output: &str) {
assert!(output.starts_with("lib")); let name_start = if output.starts_with("lib") { 3 } else { 0 };
assert!(output.ends_with(".a")); let name_end = if output.ends_with(".a") { output.len() - 2 } else { output.len() };
let lib_name = &output[3..output.len() - 2]; let lib_name = &output[name_start..name_end];
let dst = self.get_out_dir(); let dst = self.get_out_dir();
let mut objects = Vec::new(); let mut objects = Vec::new();

Loading…
Cancel
Save