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

Remove copy process on building lightgbm-sys #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 8 additions & 36 deletions lightgbm-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,41 @@ extern crate cmake;

use cmake::Config;
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::path::PathBuf;

fn main() {
let target = env::var("TARGET").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let lgbm_root = Path::new(&out_dir).join("lightgbm");

// copy source code
if !lgbm_root.exists() {
let status = if target.contains("windows") {
Command::new("cmd")
.args(&[
"/C",
"echo D | xcopy /S /Y lightgbm",
lgbm_root.to_str().unwrap(),
])
.status()
} else {
Command::new("cp")
.args(&["-r", "lightgbm", lgbm_root.to_str().unwrap()])
.status()
};
if let Some(err) = status.err() {
panic!(
"Failed to copy ./lightgbm to {}: {}",
lgbm_root.display(),
err
);
}
}

// CMake
let dst = Config::new(&lgbm_root)
let dst = Config::new("lightgbm")
.profile("Release")
.uses_cxx11()
.define("BUILD_STATIC_LIB", "ON")
.build();

// bindgen build
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_args(&["-x", "c++", "-std=c++11"])
.clang_arg(format!("-I{}", lgbm_root.join("include").display()))
.clang_arg(format!("-I{}", out_path.join("include").display()))
.generate()
.expect("Unable to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings.");

// link to appropriate C++ lib
if target.contains("apple") {
if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=c++");
println!("cargo:rustc-link-lib=dylib=omp");
} else if target.contains("linux") {
} else if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=stdc++");
println!("cargo:rustc-link-lib=dylib=gomp");
}

println!("cargo:rustc-link-search={}", out_path.join("lib").display());
println!("cargo:rustc-link-search=native={}", dst.display());
if target.contains("windows") {

if cfg!(target_os = "windows") {
println!("cargo:rustc-link-lib=static=lib_lightgbm");
} else {
println!("cargo:rustc-link-lib=static=_lightgbm");
Expand Down