microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
allocator/mimalloc-sys/build.rs
84lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use std::boxed::Box; |
| 5 | use std::env; |
| 6 | use std::error::Error; |
| 7 | use std::fs; |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | |
| 10 | use cmake::Config; |
| 11 | |
| 12 | // 1.8.2 |
| 13 | //static ALLOCATOR_MIMALLOC_TAG: &str = "b66e3214d8a104669c2ec05ae91ebc26a8f5ab78"; |
| 14 | // 2.1.2 |
| 15 | static ALLOCATOR_MIMALLOC_TAG: &str = "43ce4bd7fd34bcc730c1c7471c99995597415488"; |
| 16 | |
| 17 | fn main() -> Result<(), Box<dyn Error>> { |
| 18 | let dst = download_mimalloc()?; |
| 19 | compile_mimalloc(&dst); |
| 20 | println!("cargo:rerun-if-changed=build.rs"); |
| 21 | println!("cargo:rerun-if-changed=CMakeLists.txt"); |
| 22 | Ok(()) |
| 23 | } |
| 24 | |
| 25 | // Compile mimalloc source code and link it to the crate. |
| 26 | // The cc crate is used to compile the source code into a static library. |
| 27 | // The cmake crate is used to download the source code and stage it in the build directory. |
| 28 | // We don't use the cmake crate to compile the source code because the mimalloc build system |
| 29 | // loads extra libraries, changes the name and path around, and does other things that are |
| 30 | // difficult to handle. The cc crate is much simpler and more predictable. |
| 31 | fn compile_mimalloc(dst: &Path) { |
| 32 | let src_dir = dst |
| 33 | .join("build") |
| 34 | .join("mimalloc-prefix") |
| 35 | .join("src") |
| 36 | .join("mimalloc"); |
| 37 | |
| 38 | let mut build = cc::Build::new(); |
| 39 | |
| 40 | build.include(src_dir.join("include")); |
| 41 | build.include(src_dir.join("src")); |
| 42 | build.file(src_dir.join("src/static.c")); |
| 43 | |
| 44 | if build.get_compiler().is_like_msvc() { |
| 45 | build.cpp(true); |
| 46 | build.static_crt(true); |
| 47 | } |
| 48 | // turn off debug mode |
| 49 | build.define("MI_DEBUG", "0"); |
| 50 | |
| 51 | // turning on optimizations doesn't seem to make a difference |
| 52 | //build.opt_level(3); |
| 53 | |
| 54 | build.compile("mimalloc"); |
| 55 | |
| 56 | println!( |
| 57 | "cargo:rustc-link-search=native={}", |
| 58 | dst.join("lib").display() |
| 59 | ); |
| 60 | println!("cargo:rustc-link-lib=static=mimalloc"); |
| 61 | } |
| 62 | |
| 63 | // Use cmake to download mimalloc source code and stage |
| 64 | // it in the build directory. |
| 65 | fn download_mimalloc() -> Result<PathBuf, Box<dyn Error>> { |
| 66 | let build_dir = get_build_dir()?; |
| 67 | let mut config = Config::new(build_dir); |
| 68 | |
| 69 | config |
| 70 | .no_build_target(true) |
| 71 | .env("ALLOCATOR_MIMALLOC_TAG", ALLOCATOR_MIMALLOC_TAG) |
| 72 | .very_verbose(true); |
| 73 | |
| 74 | let dst = config.build(); |
| 75 | |
| 76 | Ok(dst) |
| 77 | } |
| 78 | |
| 79 | fn get_build_dir() -> Result<PathBuf, Box<dyn Error>> { |
| 80 | let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; |
| 81 | let build_dir = PathBuf::from(manifest_dir.as_str()); |
| 82 | let normalized_build_dir = fs::canonicalize(build_dir)?; |
| 83 | Ok(normalized_build_dir) |
| 84 | } |
| 85 | |