-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
115 lines (99 loc) · 3.69 KB
/
build.rs
File metadata and controls
115 lines (99 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-changed=wrapper/wrapper.cpp");
println!("cargo:rerun-if-changed=wrapper/wrapper.hpp");
let mut dst = cmake::Config::new("cg3");
let cg3_sysroot = std::env::var("CG3_SYSROOT").ok();
let mut includes = if cfg!(windows) && cg3_sysroot.is_none() {
let lib = vcpkg::Config::new().find_package("icu").unwrap();
lib.include_paths
} else {
vec![]
};
if let Some(sysroot) = cg3_sysroot.as_ref() {
let cg3_sysroot_path = PathBuf::from(sysroot);
let pkgconfig_path = cg3_sysroot_path.join("lib").join("pkgconfig");
dst.env("PKG_CONFIG_PATH", &pkgconfig_path);
dst.define("CMAKE_PREFIX_PATH", &cg3_sysroot_path);
// Bypass FindICU by setting ICU variables directly
let lib_dir = cg3_sysroot_path.join("lib");
dst.define("ICU_INCLUDE_DIRS", cg3_sysroot_path.join("include"));
dst.define(
"ICU_LIBRARIES",
format!(
"{};{};{};{}",
lib_dir.join("libicuuc.a").display(),
lib_dir.join("libicui18n.a").display(),
lib_dir.join("libicuio.a").display(),
lib_dir.join("libicudata.a").display()
),
);
includes.push(PathBuf::from(sysroot).join("include"));
}
#[cfg(windows)]
let dst = dst
.define("WIN32", "ON")
.define("MSVC", "ON")
.define(
"CMAKE_CXX_FLAGS",
"/Dcg3_EXPORTS /DWIN32 /D_WIN32 /D_WINDOWS /W3 /GR /EHsc /O2",
)
.define("BUILD_SHARED_LIBS", "OFF")
.build();
#[cfg(unix)]
let dst = {
let dst = dst.define("BUILD_SHARED_LIBS", "OFF");
dst.define("CMAKE_POSITION_INDEPENDENT_CODE", "ON");
dst.define("CMAKE_C_COMPILER", "clang");
dst.define("CMAKE_CXX_COMPILER", "clang++");
let mut cflags: Vec<String> = includes
.iter()
.map(|x| format!("-I{}", x.display()))
.collect();
cflags.push("-fPIC".to_string());
cflags.push("-flto=thin".to_string());
dst.define("CMAKE_CXX_FLAGS", cflags.join(" "));
dst.define("CMAKE_C_FLAGS", cflags.join(" "));
dst.build()
};
let is_shared = cfg!(windows) && std::env::var("VCPKGRS_DYNAMIC").is_ok();
let mut build = cc::Build::new();
#[cfg(unix)]
{
build.compiler("clang++");
build.flag("-fPIC");
build.flag("-flto=thin");
}
build
.file("wrapper/wrapper.cpp")
.includes(includes)
.include(dst.join("include"))
.include(dst.join("include").join("cg3"))
.include(&dst)
.static_crt(!is_shared)
.cpp(true)
.flag(if cfg!(windows) {
"/std:c++14"
} else {
"-std=c++20"
});
build.compile("cg3_wrapper");
// Link directives must come AFTER cc::compile() to ensure correct link order:
// cg3_wrapper (from cc) -> cg3 -> ICU
println!("cargo:rustc-link-search=native={}/lib", dst.display());
if let Some(sysroot) = cg3_sysroot.as_ref() {
println!("cargo:rustc-link-search=native={}/lib", sysroot);
}
println!("cargo:rustc-link-lib=static:+whole-archive=cg3");
if cfg!(unix) {
println!("cargo:rustc-link-lib=static=icuuc");
println!("cargo:rustc-link-lib=static=icuio");
println!("cargo:rustc-link-lib=static=icudata");
println!("cargo:rustc-link-lib=static=icui18n");
} else if cfg!(windows) {
println!("cargo:rustc-link-lib=icudt");
println!("cargo:rustc-link-lib=icuin");
println!("cargo:rustc-link-lib=icudata");
println!("cargo:rustc-link-lib=icui18n");
}
}