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

Relax lib name requirement; fix cross-compilation #15

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,27 @@ impl Build {

/// Run the compiler, generating the file output
///
/// The name output should be the name of the library
/// including platform-specific prefix and file extension,
/// e.g. `"libfoo.a"`
pub fn compile(&mut self, output: &str) {
#[cfg(not(target_env = "msvc"))]
assert!(output.starts_with("lib"));

#[cfg(not(target_env = "msvc"))]
assert!(output.ends_with(".a"));
/// The name output should be the base name of the library,
/// without file extension, and without "lib" prefix.
///
/// The output file will have target-specific name,
/// such as `lib*.a` (non-MSVC) or `*.lib` (MSVC).
pub fn compile(&mut self, lib_name: &str) {
// Trim name for backwards comatibility
let lib_name = if lib_name.starts_with("lib") && lib_name.ends_with(".a") {
&lib_name[3..lib_name.len() - 2]
} else {
lib_name.trim_right_matches(".lib")
};

#[cfg(target_env = "msvc")]
assert!(output.ends_with(".lib"));
let target = self.get_target();
let output = if target.ends_with("-msvc") {
format!("{}.lib", lib_name)
} else {
format!("lib{}.a", lib_name)
};

let dst = &self.get_out_dir();

let objects = self.compile_objects();
self.archive(&dst, &output, &objects[..]);

Expand All @@ -209,8 +215,7 @@ impl Build {
///
/// The files can be linked in a separate step, e.g. passed to `cc`
pub fn compile_objects(&mut self) -> Vec<PathBuf> {
let target = self.target.clone()
.unwrap_or_else(|| env::var("TARGET").expect("TARGET must be set"));
let target = self.get_target();

let nasm = self.find_nasm();
let args = self.get_args(&target);
Expand Down Expand Up @@ -270,6 +275,11 @@ impl Build {
.unwrap_or_else(|| PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set")))
}

fn get_target(&self) -> String {
self.target.clone()
.unwrap_or_else(|| env::var("TARGET").expect("TARGET must be set"))
}

fn find_nasm(&mut self) -> PathBuf {
match self.nasm.clone() {
Some(path) => path,
Expand Down