microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/allocator/mimalloc-sys/build.rs
65lines · 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() { |
| 26 | let mimalloc_vendor_dir = PathBuf::from("mimalloc"); | |
93f01d0dIan Davis2 years ago | 27 | |
| 28 | let mut build = cc::Build::new(); | |
| 29 | | |
91589c3aIan Davis2 years ago | 30 | let include_dir = mimalloc_vendor_dir.join("include"); |
| 31 | let src_dir = mimalloc_vendor_dir.join("src"); | |
| 32 | let static_file = src_dir.join("static.c"); | |
| 33 | | |
| 34 | assert!(include_dir.exists(), "include_dir: {include_dir:?}"); | |
| 35 | assert!(src_dir.exists(), "src_dir: {src_dir:?}"); | |
| 36 | assert!(static_file.exists(), "static_file: {static_file:?}"); | |
| 37 | | |
| 38 | build.include(include_dir); | |
| 39 | build.include(src_dir); | |
| 40 | build.file(static_file); | |
93f01d0dIan Davis2 years ago | 41 | |
| 42 | if build.get_compiler().is_like_msvc() { | |
| 43 | build.static_crt(true); | |
| 44 | } | |
| 45 | // turn off debug mode | |
| 46 | build.define("MI_DEBUG", "0"); | |
| 47 | | |
| 48 | // turning on optimizations doesn't seem to make a difference | |
| 49 | //build.opt_level(3); | |
| 50 | | |
f9675380Ian Davis2 years ago | 51 | // log the command that will be run |
| 52 | build.cargo_debug(true); | |
| 53 | | |
| 54 | // turn off warnings from the mimalloc code | |
| 55 | build.cargo_warnings(false); | |
| 56 | | |
93f01d0dIan Davis2 years ago | 57 | build.compile("mimalloc"); |
| 58 | } | |
| 59 | | |
| 60 | fn get_build_dir() -> Result<PathBuf, Box<dyn Error>> { | |
| 61 | let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; | |
| 62 | let build_dir = PathBuf::from(manifest_dir.as_str()); | |
| 63 | let normalized_build_dir = fs::canonicalize(build_dir)?; | |
| 64 | Ok(normalized_build_dir) | |
| 65 | } |