Skip to content

Commit

Permalink
Add Support for MSVC Makefiles
Browse files Browse the repository at this point in the history
If we are building with the MSVC toolchain then use the existing NMAKE files instead of Cmake.

This is initial work on #35
  • Loading branch information
iwillspeak committed Mar 18, 2017
1 parent 72dc706 commit cc035dd
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions onig_sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
extern crate pkg_config;

#[cfg(not(target_env="msvc"))]
extern crate cmake;

use std::env;

fn compile_with_cmake() {
use cmake::Config;
fn rustc_link_type(static_link: bool) -> &'static str {
if static_link {
"static"
} else {
"dylib"
}
}

let static_link = env::var("CARGO_FEATURE_STATIC_ONIG").is_ok();

#[cfg(not(target_env="msvc"))]
fn compile(static_link: bool) {
use cmake::Config;

// Builds the project in the directory located in `oniguruma`, installing it
// into $OUT_DIR
Expand All @@ -19,21 +29,44 @@ fn compile_with_cmake() {
}
.build();

let link_type = if static_link {
"static"
} else {
"dylib"
};

println!("cargo:rustc-link-search=native={}",
dst.join("build").display());
println!("cargo:rustc-link-lib={}=onig", link_type);
println!("cargo:rustc-link-lib={}=onig", rustc_link_type(static_link));
}

#[cfg(target_env="msvc")]
pub fn compile(static_link: bool) {
use std::process::Command;

// TODO: does this need to be dynamically looked up?
let onig_dir = env::current_dir().unwrap().join("oniguruma");
let build_dir = onig_dir.join("src");
let lib_name = if static_link { "onig_s" } else { "onig" };

let bitness = if cfg!(target_pointer_width="64") { "64" } else { "32" };

let r = Command::new("cmd")
.args(&["/c", &format!("make_win{}.bat", bitness)])
.current_dir(&onig_dir)
.output()
.expect("error running build");

if !r.status.success() {
let err = String::from_utf8_lossy(&r.stderr);
let out = String::from_utf8_lossy(&r.stdout);
panic!("Build error:\nSTDERR:{}\nSTDOUT:{}", err, out);
}

println!("cargo:rustc-link-search=native={}", build_dir.display());
println!("cargo:rustc-link-lib={}={}", rustc_link_type(static_link), lib_name);
}

pub fn main() {
if let Ok(_) = pkg_config::find_library("oniguruma") {
return;
}

let static_link = env::var("CARGO_FEATURE_STATIC_ONIG").is_ok();

compile_with_cmake();
compile(static_link);
}

0 comments on commit cc035dd

Please sign in to comment.