Skip to content

Commit

Permalink
Force release crt flag for msvc.
Browse files Browse the repository at this point in the history
  • Loading branch information
alisomay committed Nov 28, 2024
1 parent 195adea commit 24b8360
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn main() {
let pthread_root = project_dir.join("pthreads");

let compiler = target_info.compiler.unwrap();

let (pthread_lib_root, pthread_lib_path, pthread_lib_name, pthread_include): (
PathBuf,
PathBuf,
Expand All @@ -77,26 +78,28 @@ fn main() {
) = match &*target_info.arch {
"x86_64" => match compiler.as_str() {
"msvc" => {
let lib_root = pthread_root
let base_dir = pthread_root
.join("msvc")
.join("pthreads_x64-windows-static")
.join("lib");
.join("pthreads_x64-windows-static");

let (lib_dir, lib_name) = if cfg!(debug_assertions) {
(base_dir.join("debug").join("lib"), "pthreadVC3d")
} else {
(base_dir.join("lib"), "pthreadVC3")
};

(
lib_root.clone(),
lib_root.join("pthreadVC3.lib"),
"pthreadVC3",
pthread_root
.join("msvc")
.join("pthreads_x64-windows-static")
.join("include"),
lib_dir.clone(),
lib_dir.join(format!("{}.lib", lib_name)),
lib_name,
base_dir.join("include"),
)
}
"gnu" => {
let lib_root = pthread_root.join("gnu/x64/lib");
(
lib_root.clone(),
lib_root.join("libpthreadGC2.a"),
// Re-visit this
"pthreadGC2",
pthread_root.join("gnu").join("include"),
)
Expand All @@ -105,26 +108,28 @@ fn main() {
},
"aarch64" => match compiler.as_str() {
"msvc" => {
let lib_root = pthread_root
let base_dir = pthread_root
.join("msvc")
.join("pthreads_arm64-windows-static")
.join("lib");
.join("pthreads_arm64-windows-static");

let (lib_dir, lib_name) = if cfg!(debug_assertions) {
(base_dir.join("debug").join("lib"), "pthreadVC3d")
} else {
(base_dir.join("lib"), "pthreadVC3")
};

(
lib_root.clone(),
lib_root.join("pthreadVC3.lib"),
"pthreadVC3",
pthread_root
.join("msvc")
.join("pthreads_arm64-windows-static")
.join("include"),
lib_dir.clone(),
lib_dir.join(format!("{}.lib", lib_name)),
lib_name,
base_dir.join("include"),
)
}
"gnu" => {
let lib_root = pthread_root.join("gnu/aarch64/lib");
(
lib_root.clone(),
lib_root.join("libpthreadGC2.a"),
// Re-visit this
"pthreadGC2",
pthread_root.join("gnu").join("include"),
)
Expand All @@ -135,9 +140,19 @@ fn main() {
};

let mut lib_destination = Config::new("libpd");

if compiler.as_str() == "msvc" {
lib_destination.define("LIBPD_CRT_LINKAGE", "dynamic");
// This is a complicated one..
// So the pthreads library is built with the /MD flag for CRT.
// One would have expected that the d suffixed prebuilt libraries would be built with /MDd flag.
// Unfortunately this is not the case, it is not easy to rebuild the pthreads library with /MDd flag.
// This is why we are forcing libpd to use the /MD flag even in debug mode.
// Because mismatched CRT flags can cause linking runtime errors.
// Another thing is that rust, if not stated otherwise, dynamic CRT flags and it is better to stay in that domain
// since forcing /MT or /MTd flags can cause other problems.
lib_destination.define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreadedDLL");
}

lib_destination
.define("PD_LOCALE", PD_LOCALE)
.define("PD_MULTI", PD_MULTI)
Expand All @@ -155,7 +170,6 @@ fn main() {
.very_verbose(true);

let lib_destination = lib_destination.build();

let library_root = lib_destination.join("build/libs");

// Look for pthread
Expand Down

0 comments on commit 24b8360

Please sign in to comment.