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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use log::info;
pub use ritual_common as common;
use ritual_common::cpp_build_config::{CppBuildConfig, CppBuildPaths, CppLibraryType};
use ritual_common::cpp_lib_builder::{BuildType, CMakeConfigData, CppLibBuilder};
use ritual_common::errors::{bail, err_msg, FancyUnwrap, Result, ResultExt};
use ritual_common::file_utils::{create_file, file_to_string, load_json, path_to_str};
use ritual_common::target::{current_target, LibraryTarget, OS};
use ritual_common::{env_var_names, BuildScriptData};
use std::env;
use std::io::Write;
use std::path::PathBuf;
use std::process;
#[derive(Debug)]
pub struct Config {
cpp_build_paths: CppBuildPaths,
build_script_data: BuildScriptData,
current_cpp_library_version: Option<String>,
}
fn manifest_dir() -> Result<PathBuf> {
let dir =
env::var("CARGO_MANIFEST_DIR").with_context(|_| "CARGO_MANIFEST_DIR env var is missing")?;
Ok(PathBuf::from(dir))
}
fn out_dir() -> Result<PathBuf> {
let dir = env::var("OUT_DIR").with_context(|_| "OUT_DIR env var is missing")?;
Ok(PathBuf::from(dir))
}
fn build_script_data() -> Result<BuildScriptData> {
load_json(manifest_dir()?.join("build_script_data.json"))
}
impl Config {
pub fn new() -> Result<Self> {
Ok(Config {
build_script_data: build_script_data()?,
cpp_build_paths: CppBuildPaths::default(),
current_cpp_library_version: None,
})
}
pub fn set_current_cpp_library_version(&mut self, version: Option<String>) {
self.current_cpp_library_version = version;
}
pub fn known_targets(&self) -> &[LibraryTarget] {
&self.build_script_data.known_targets
}
pub fn cpp_build_config(&self) -> &CppBuildConfig {
&self.build_script_data.cpp_build_config
}
pub fn cpp_build_config_mut(&mut self) -> &mut CppBuildConfig {
&mut self.build_script_data.cpp_build_config
}
pub fn set_cpp_build_config(&mut self, config: CppBuildConfig) {
self.build_script_data.cpp_build_config = config;
}
pub fn cpp_build_paths(&self) -> &CppBuildPaths {
&self.cpp_build_paths
}
pub fn cpp_build_paths_mut(&mut self) -> &mut CppBuildPaths {
&mut self.cpp_build_paths
}
pub fn set_cpp_build_paths(&mut self, config: CppBuildPaths) {
self.cpp_build_paths = config;
}
pub fn try_run(mut self) -> Result<()> {
let current_target = LibraryTarget {
target: current_target(),
cpp_library_version: self.current_cpp_library_version.clone(),
};
let is_known_target = self
.build_script_data
.known_targets
.contains(¤t_target);
if !is_known_target {
println!("Current target is unknown: {}", current_target.short_text());
println!("Known targets:");
for target in &self.build_script_data.known_targets {
println!("* {}", target.short_text());
}
}
self.cpp_build_paths.apply_env();
let cpp_build_config_data = self
.build_script_data
.cpp_build_config
.eval(¤t_target.target)?;
let out_dir = out_dir()?;
let c_lib_install_dir = out_dir.join("c_lib_install");
let manifest_dir = manifest_dir()?;
let profile = env::var("PROFILE").with_context(|_| "PROFILE env var is missing")?;
info!("Building C++ wrapper library");
let library_type = cpp_build_config_data
.library_type()
.ok_or_else(|| err_msg("library type (shared or static) is not set"))?;
let cmake_config = CMakeConfigData {
cpp_build_config_data: &cpp_build_config_data,
cpp_build_paths: &self.cpp_build_paths,
library_type: Some(library_type),
cpp_library_version: self.current_cpp_library_version.clone(),
};
let cmake_vars = cmake_config.cmake_vars()?.into_iter().collect();
CppLibBuilder {
cmake_source_dir: manifest_dir.join("c_lib"),
build_dir: out_dir.join("c_lib_build"),
install_dir: Some(c_lib_install_dir.clone()),
num_jobs: env::var("NUM_JOBS").ok().and_then(|x| x.parse().ok()),
cmake_vars,
build_type: match profile.as_str() {
"debug" => BuildType::Debug,
"release" => BuildType::Release,
_ => bail!("unknown value of PROFILE env var: {}", profile),
},
capture_output: false,
skip_cmake: false,
skip_cmake_after_first_run: false,
}
.run()?;
{
info!("Generating ffi.rs file");
let mut ffi_file = create_file(out_dir.join("ffi.rs"))?;
if cpp_build_config_data.library_type() == Some(CppLibraryType::Shared) {
writeln!(
ffi_file,
"#[link(name = \"{}\")]",
&self.build_script_data.cpp_wrapper_lib_name
)?;
} else {
writeln!(
ffi_file,
"#[link(name = \"{}\", kind = \"static\")]",
&self.build_script_data.cpp_wrapper_lib_name
)?;
}
write!(
ffi_file,
"{}",
file_to_string(manifest_dir.join("src").join("ffi.in.rs"))?
)?;
}
for name in cpp_build_config_data.linked_libs() {
println!("cargo:rustc-link-lib={}", name);
}
if crate::common::target::current_os() == OS::MacOS {
println!("cargo:rustc-link-lib=c++");
} else if crate::common::target::current_env() != crate::common::target::Env::Msvc {
println!("cargo:rustc-link-lib=stdc++");
}
for name in cpp_build_config_data.linked_frameworks() {
println!("cargo:rustc-link-lib=framework={}", name);
}
for path in self.cpp_build_paths.lib_paths() {
println!("cargo:rustc-link-search=native={}", path_to_str(path)?);
}
for path in self.cpp_build_paths.framework_paths() {
println!("cargo:rustc-link-search=framework={}", path_to_str(path)?);
}
println!(
"cargo:rustc-link-search=native={}",
path_to_str(&c_lib_install_dir)?
);
if let Some(version) = self.current_cpp_library_version {
println!("cargo:rustc-cfg=cpp_lib_version={:?}", version);
}
for var in &[
env_var_names::LIBRARY_PATH,
env_var_names::FRAMEWORK_PATH,
env_var_names::INCLUDE_PATH,
] {
println!("cargo:rerun-if-env-changed={}", var);
}
info!("ritual build script finished.");
Ok(())
}
pub fn run(self) -> ! {
self.try_run().fancy_unwrap();
process::exit(0)
}
}
pub fn try_run() -> Result<()> {
Config::new()?.try_run()
}
pub fn run() -> ! {
let config = Config::new().fancy_unwrap();
config.run()
}