microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/qdk_package

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/allocator/mimalloc-sys/build.rs

82lines · modecode

1// 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;
8use std::path::PathBuf;
9
10fn main() -> Result<(), Box<dyn Error>> {
11 compile_mimalloc();
12 let build_dir = get_build_dir()?;
13 println!(
14 "cargo:rerun-if-changed={}",
15 build_dir.join("mimalloc").display()
16 );
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.
25fn compile_mimalloc() {
26 let target = env::var("TARGET").expect("TARGET variable should exist");
27 let mimalloc_vendor_dir = PathBuf::from("mimalloc");
28
29 let mut build = cc::Build::new();
30
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
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 );
46
47 build.include(include_dir);
48 build.include(src_dir);
49 build.file(static_file);
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
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
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
74 build.compile("mimalloc");
75}
76
77fn 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}