Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghannam committed Feb 8, 2025
1 parent 4cb15cc commit c0bcc0d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 70 deletions.
95 changes: 49 additions & 46 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::env;
use std::error::Error;
use std::path::PathBuf;

use crate::from_source::{compile_scip, download_scip_source, is_from_source_feature_enabled};
use bundled::*;
use crate::from_source::{download_scip_source, is_from_source_feature_enabled, compile_scip};

#[cfg(not(feature = "bundled"))]
pub fn is_bundled_feature_enabled() -> bool {
Expand All @@ -29,7 +29,7 @@ fn _build_from_scip_dir(path: &str) -> bindgen::Builder {
println!("cargo:libdir={}", lib_dir_path);

#[cfg(windows)]
let lib_dir_path = PathBuf::from(&path).join("bin");
let lib_dir_path = PathBuf::from(&path).join("bin");
#[cfg(windows)]
println!("cargo:rustc-link-search={}", lib_dir_path.to_str().unwrap());
} else {
Expand Down Expand Up @@ -94,55 +94,56 @@ fn look_in_scipoptdir_and_conda_env() -> Option<bindgen::Builder> {
}

fn main() -> Result<(), Box<dyn Error>> {
let builder =
if is_bundled_feature_enabled() {
download_scip();
let path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("scip_install");
_build_from_scip_dir(path.to_str().unwrap())
} else if is_from_source_feature_enabled() {
let source_path = download_scip_source();
let build_path = compile_scip(source_path);
_build_from_scip_dir(build_path.to_str().unwrap())
let builder = if is_bundled_feature_enabled() {
download_scip();
let path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("scip_install");
_build_from_scip_dir(path.to_str().unwrap())
} else if is_from_source_feature_enabled() {
let source_path = download_scip_source();
let build_path = compile_scip(source_path);
_build_from_scip_dir(build_path.to_str().unwrap())
} else {
let builder = look_in_scipoptdir_and_conda_env();
if builder.is_some() {
builder.unwrap()
} else {
let builder = look_in_scipoptdir_and_conda_env();
if builder.is_some() {
builder.unwrap()
} else {
println!("cargo:warning=SCIP was not found in SCIPOPTDIR or in Conda environemnt");
println!("cargo:warning=Looking for SCIP in system libraries");

let headers_dir_path = "headers/";
let headers_dir = PathBuf::from(headers_dir_path);
let scip_header_file = PathBuf::from(&headers_dir)
.join("scip")
.join("scip.h")
.to_str()
.unwrap()
.to_owned();
let scipdefplugins_header_file = PathBuf::from(&headers_dir)
.join("scip")
.join("scipdefplugins.h")
.to_str()
.unwrap()
.to_owned();

bindgen::Builder::default()
.header(scip_header_file)
.header(scipdefplugins_header_file)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_arg(format!("-I{}", headers_dir_path))
}
};

println!("cargo:warning=SCIP was not found in SCIPOPTDIR or in Conda environemnt");
println!("cargo:warning=Looking for SCIP in system libraries");

let headers_dir_path = "headers/";
let headers_dir = PathBuf::from(headers_dir_path);
let scip_header_file = PathBuf::from(&headers_dir)
.join("scip")
.join("scip.h")
.to_str()
.unwrap()
.to_owned();
let scipdefplugins_header_file = PathBuf::from(&headers_dir)
.join("scip")
.join("scipdefplugins.h")
.to_str()
.unwrap()
.to_owned();

bindgen::Builder::default()
.header(scip_header_file)
.header(scipdefplugins_header_file)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_arg(format!("-I{}", headers_dir_path))
}
};

#[cfg(windows)] {
#[cfg(windows)]
{
println!("cargo:rustc-link-lib=libscip");
}
#[cfg(not(windows))] {
#[cfg(not(windows))]
{
println!("cargo:rustc-link-lib=scip");
}

#[cfg(feature = "from-source")] {
#[cfg(feature = "from-source")]
{
let target = env::var("TARGET").unwrap();
let apple = target.contains("apple");
let linux = target.contains("linux");
Expand All @@ -153,10 +154,12 @@ fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rustc-link-lib=dylib=stdc++");
}

#[cfg(windows)] {
#[cfg(windows)]
{
println!("cargo:rustc-link-lib=libsoplex");
}
#[cfg(not(windows))] {
#[cfg(not(windows))]
{
println!("cargo:rustc-link-lib=soplex");
}
}
Expand Down
25 changes: 16 additions & 9 deletions download.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::error::Error;
use tempfile::tempdir;
use std::fs::File;
use std::io::Cursor;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::time::Duration;
use tempfile::tempdir;
use zip_extract::extract;


pub fn download_and_extract_zip(url: &str, extract_path: &Path) -> Result<(), Box<dyn Error>> {
// Download the ZIP file
println!("cargo:warning=Downloading from {}", url);
Expand All @@ -27,20 +26,28 @@ pub fn download_and_extract_zip(url: &str, extract_path: &Path) -> Result<(), Bo

println!("cargo:warning=Downloaded to {:?}", zip_path);
println!("cargo:warning=Extracting to {:?}", target_dir);
extract(Cursor::new(
std::fs::read(zip_path).unwrap(),
), &target_dir, false)?;
extract(
Cursor::new(std::fs::read(zip_path).unwrap()),
&target_dir,
false,
)?;

// Check if the extracted content is another zip file
let extracted_files: Vec<_> = std::fs::read_dir(&target_dir)?.collect();
if extracted_files.len() == 1 {
let first_file = extracted_files[0].as_ref().unwrap();
if first_file.path().extension().map_or(false, |ext| ext == "zip") {
if first_file
.path()
.extension()
.map_or(false, |ext| ext == "zip")
{
println!("cargo:warning=Found nested zip file, extracting again");
let nested_zip_path = first_file.path();
extract(Cursor::new(
std::fs::read(&nested_zip_path).unwrap(),
), &(target_dir.join("scip_install")), true)?;
extract(
Cursor::new(std::fs::read(&nested_zip_path).unwrap()),
&(target_dir.join("scip_install")),
true,
)?;
std::fs::remove_file(nested_zip_path)?;
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/create.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::mem::MaybeUninit;
use scip_sys::{SCIPcreate, SCIPfree, SCIPprintVersion};
use std::mem::MaybeUninit;

fn main() {
let mut scip_ptr = MaybeUninit::uninit();
unsafe { SCIPcreate(scip_ptr.as_mut_ptr()) };
let mut scip_ptr = unsafe { scip_ptr.assume_init() };
unsafe {SCIPprintVersion(scip_ptr, std::ptr::null_mut())};
unsafe { SCIPprintVersion(scip_ptr, std::ptr::null_mut()) };
unsafe { SCIPfree(&mut scip_ptr) };
}
}
16 changes: 6 additions & 10 deletions from_source.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;
#[cfg(feature = "from-source")]
use std::env;
#[cfg(feature = "from-source")]
use crate::download::download_and_extract_zip;
#[cfg(feature = "from-source")]
use std::env;
use std::path::PathBuf;

#[cfg(feature = "from-source")]
pub fn is_from_source_feature_enabled() -> bool {
Expand All @@ -14,13 +14,11 @@ pub fn is_from_source_feature_enabled() -> bool {
false
}


#[cfg(not(feature = "from-source"))]
pub fn download_scip_source() -> PathBuf {
unimplemented!("Cannot download SCIP source code without the `from-source` feature")
}


#[cfg(feature = "from-source")]
pub fn download_scip_source() -> PathBuf {
let scip_version = "9.1.1";
Expand All @@ -35,7 +33,6 @@ pub fn download_scip_source() -> PathBuf {
target.join(format!("scipoptsuite-{scip_version}"))
}


#[cfg(feature = "from-source")]
pub fn compile_scip(source_path: PathBuf) -> PathBuf {
let out_dir = env::var("OUT_DIR").unwrap();
Expand All @@ -49,13 +46,12 @@ pub fn compile_scip(source_path: PathBuf) -> PathBuf {
use cmake::Config;
let mut dst = Config::new(source_path);

dst
.define("IPOPT", "OFF")
dst.define("IPOPT", "OFF")
.define("ZIMPL", "OFF")
.define("GMP", "OFF")
.define("READLINE", "OFF")
.define("BOOST", "OFF")
.define("AUTOBUILD","OFF")
.define("AUTOBUILD", "OFF")
.define("PAPILO", "OFF")
.define("SYM", "snauty")
.define("ZLIB", "OFF")
Expand All @@ -66,4 +62,4 @@ pub fn compile_scip(source_path: PathBuf) -> PathBuf {
#[cfg(not(feature = "from-source"))]
pub fn compile_scip(source_path: PathBuf) -> PathBuf {
unimplemented!("Cannot compile SCIP without the `from-source` feature")
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));


#[cfg(test)]
mod tests {
use std::mem::MaybeUninit;
use super::*;
use std::mem::MaybeUninit;

#[test]
fn test_create() {
Expand Down

0 comments on commit c0bcc0d

Please sign in to comment.