microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/allocator/mimalloc-sys/build.rs
82lines · modeblame
93f01d0dIan Davis2 years ago | 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; | |
91589c3aIan Davis2 years ago | 8 | use std::path::PathBuf; |
93f01d0dIan Davis2 years ago | 9 | |
| 10 | fn main() -> Result<(), Box<dyn Error>> { | |
91589c3aIan Davis2 years ago | 11 | compile_mimalloc(); |
| 12 | let build_dir = get_build_dir()?; | |
| 13 | println!( | |
| 14 | "cargo:rerun-if-changed={}", | |
| 15 | build_dir.join("mimalloc").display() | |
| 16 | ); | |
93f01d0dIan Davis2 years ago | 17 | Ok(()) |
| 18 | } | |
| 19 | | |
| 20 | // Compile mimalloc source code and link it to the crate. | |
| 21 | // The cc crate is used to compile the source code into a static library. | |
| 22 | // We don't use the cmake crate to compile the source code because the mimalloc build system | |
| 23 | // loads extra libraries, changes the name and path around, and does other things that are | |
| 24 | // difficult to handle. The cc crate is much simpler and more predictable. | |
91589c3aIan Davis2 years ago | 25 | fn compile_mimalloc() { |
221780beorpuente-MS12 months ago | 26 | let target = env::var("TARGET").expect("TARGET variable should exist"); |
91589c3aIan Davis2 years ago | 27 | let mimalloc_vendor_dir = PathBuf::from("mimalloc"); |
93f01d0dIan Davis2 years ago | 28 | |
| 29 | let mut build = cc::Build::new(); | |
| 30 | | |
91589c3aIan Davis2 years ago | 31 | let include_dir = mimalloc_vendor_dir.join("include"); |
| 32 | let src_dir = mimalloc_vendor_dir.join("src"); | |
| 33 | let static_file = src_dir.join("static.c"); | |
| 34 | | |
e938e457orpuente-MS1 years ago | 35 | assert!( |
| 36 | include_dir.exists(), | |
| 37 | "include_dir: {}", | |
| 38 | include_dir.display() | |
| 39 | ); | |
| 40 | assert!(src_dir.exists(), "src_dir: {}", src_dir.display()); | |
| 41 | assert!( | |
| 42 | static_file.exists(), | |
| 43 | "static_file: {}", | |
| 44 | static_file.display() | |
| 45 | ); | |
91589c3aIan Davis2 years ago | 46 | |
| 47 | build.include(include_dir); | |
| 48 | build.include(src_dir); | |
| 49 | build.file(static_file); | |
93f01d0dIan Davis2 years ago | 50 | |
| 51 | if build.get_compiler().is_like_msvc() { | |
| 52 | build.static_crt(true); | |
| 53 | } | |
| 54 | // turn off debug mode | |
| 55 | build.define("MI_DEBUG", "0"); | |
| 56 | | |
| 57 | // turning on optimizations doesn't seem to make a difference | |
| 58 | //build.opt_level(3); | |
| 59 | | |
f9675380Ian Davis2 years ago | 60 | // log the command that will be run |
| 61 | build.cargo_debug(true); | |
| 62 | | |
| 63 | // turn off warnings from the mimalloc code | |
| 64 | build.cargo_warnings(false); | |
| 65 | | |
221780beorpuente-MS12 months ago | 66 | // Starting on rust 1.87, the Std doesn't link advapi32 on windows |
| 67 | // anymore. So, C libraries that depended on it, like mimalloc, now | |
| 68 | // need to link it manually. | |
| 69 | // Link to the Rust PR: <https://github.com/rust-lang/rust/pull/138233> | |
| 70 | if target.contains("windows") { | |
| 71 | println!("cargo:rustc-link-lib=advapi32"); | |
| 72 | } | |
| 73 | | |
93f01d0dIan Davis2 years ago | 74 | build.compile("mimalloc"); |
| 75 | } | |
| 76 | | |
| 77 | fn get_build_dir() -> Result<PathBuf, Box<dyn Error>> { | |
| 78 | let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; | |
| 79 | let build_dir = PathBuf::from(manifest_dir.as_str()); | |
| 80 | let normalized_build_dir = fs::canonicalize(build_dir)?; | |
| 81 | Ok(normalized_build_dir) | |
| 82 | } |