microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.22.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/allocator/mimalloc-sys/build.rs

82lines · modeblame

93f01d0dIan Davis2 years ago1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use std::boxed::Box;
5use std::env;
6use std::error::Error;
7use std::fs;
91589c3aIan Davis2 years ago8use std::path::PathBuf;
93f01d0dIan Davis2 years ago9
10fn main() -> Result<(), Box<dyn Error>> {
91589c3aIan Davis2 years ago11compile_mimalloc();
12let build_dir = get_build_dir()?;
13println!(
14"cargo:rerun-if-changed={}",
15build_dir.join("mimalloc").display()
16);
93f01d0dIan Davis2 years ago17Ok(())
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 ago25fn compile_mimalloc() {
221780beorpuente-MS12 months ago26let target = env::var("TARGET").expect("TARGET variable should exist");
91589c3aIan Davis2 years ago27let mimalloc_vendor_dir = PathBuf::from("mimalloc");
93f01d0dIan Davis2 years ago28
29let mut build = cc::Build::new();
30
91589c3aIan Davis2 years ago31let include_dir = mimalloc_vendor_dir.join("include");
32let src_dir = mimalloc_vendor_dir.join("src");
33let static_file = src_dir.join("static.c");
34
e938e457orpuente-MS1 years ago35assert!(
36include_dir.exists(),
37"include_dir: {}",
38include_dir.display()
39);
40assert!(src_dir.exists(), "src_dir: {}", src_dir.display());
41assert!(
42static_file.exists(),
43"static_file: {}",
44static_file.display()
45);
91589c3aIan Davis2 years ago46
47build.include(include_dir);
48build.include(src_dir);
49build.file(static_file);
93f01d0dIan Davis2 years ago50
51if build.get_compiler().is_like_msvc() {
52build.static_crt(true);
53}
54// turn off debug mode
55build.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 ago60// log the command that will be run
61build.cargo_debug(true);
62
63// turn off warnings from the mimalloc code
64build.cargo_warnings(false);
65
221780beorpuente-MS12 months ago66// 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>
70if target.contains("windows") {
71println!("cargo:rustc-link-lib=advapi32");
72}
73
93f01d0dIan Davis2 years ago74build.compile("mimalloc");
75}
76
77fn get_build_dir() -> Result<PathBuf, Box<dyn Error>> {
78let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
79let build_dir = PathBuf::from(manifest_dir.as_str());
80let normalized_build_dir = fs::canonicalize(build_dir)?;
81Ok(normalized_build_dir)
82}