forked from rust-skia/rust-skia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
206 lines (178 loc) · 6.22 KB
/
build.rs
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
use std::{fs, io};
use build_support::{
binaries_config,
cargo::{self, Target},
features, skia, skia_bindgen,
};
mod build_support;
fn main() -> Result<(), io::Error> {
// since 0.25.0
if cfg!(feature = "shaper") {
cargo::warning("The feature 'shaper' has been removed. To use the SkShaper bindings, enable the feature 'textlayout'.");
}
if env::is_docs_rs_build() {
println!("DETECTED DOCS_RS BUILD");
return fake_bindings();
}
let skia_debug = env::is_skia_debug();
let features = features::Features::default();
let binaries_config =
binaries_config::BinariesConfiguration::from_features(&features, skia_debug);
//
// skip attempting to download?
//
if let Some(source_dir) = env::source_dir() {
if let Some(search_path) = env::skia_lib_search_path() {
println!("STARTING BIND AGAINST SYSTEM SKIA");
binaries_config.import(&search_path, false).unwrap();
let definitions = skia_bindgen::definitions::from_env();
generate_bindings(
&features,
definitions,
&binaries_config,
&source_dir,
cargo::target(),
None,
);
} else {
println!("STARTING OFFLINE BUILD");
let final_build_configuration = build_from_source(
features.clone(),
&binaries_config,
&source_dir,
skia_debug,
true,
);
let definitions = skia_bindgen::definitions::from_ninja_features(
&features,
final_build_configuration.use_system_libraries,
&binaries_config.output_directory,
);
generate_bindings(
&features,
definitions,
&binaries_config,
&source_dir,
final_build_configuration.target,
final_build_configuration
.sysroot
.as_ref()
.map(AsRef::as_ref),
);
}
} else {
//
// is the download of prebuilt binaries possible?
//
#[allow(unused_variables)]
let build_skia = true;
#[cfg(feature = "binary-cache")]
let build_skia = build_support::binary_cache::try_prepare_download(&binaries_config);
//
// full build?
//
if build_skia {
println!("STARTING A FULL BUILD");
println!("HOST: {}", cargo::host());
let source_dir = std::env::current_dir().unwrap().join("skia");
let final_build_configuration = build_from_source(
features.clone(),
&binaries_config,
&source_dir,
skia_debug,
false,
);
let definitions = skia_bindgen::definitions::from_ninja_features(
&features,
final_build_configuration.use_system_libraries,
&binaries_config.output_directory,
);
generate_bindings(
&features,
definitions,
&binaries_config,
&source_dir,
final_build_configuration.target,
final_build_configuration
.sysroot
.as_ref()
.map(AsRef::as_ref),
);
}
};
binaries_config.commit_to_cargo();
#[cfg(feature = "binary-cache")]
if let Some(staging_directory) = build_support::binary_cache::should_export() {
build_support::binary_cache::publish(&binaries_config, &staging_directory);
}
Ok(())
}
fn build_from_source(
features: features::Features,
binaries_config: &binaries_config::BinariesConfiguration,
skia_source_dir: &std::path::Path,
skia_debug: bool,
offline: bool,
) -> skia::FinalBuildConfiguration {
let build_config = skia::BuildConfiguration::from_features(features, skia_debug);
let final_configuration = skia::FinalBuildConfiguration::from_build_configuration(
&build_config,
skia::env::use_system_libraries(),
skia_source_dir,
);
skia::build(
&final_configuration,
binaries_config,
skia::env::ninja_command(),
skia::env::gn_command(),
offline,
);
final_configuration
}
fn generate_bindings(
features: &features::Features,
definitions: Vec<skia_bindgen::Definition>,
binaries_config: &binaries_config::BinariesConfiguration,
skia_source_dir: &std::path::Path,
target: Target,
sysroot: Option<&str>,
) {
// Emit the ninja definitions, to help debug build consistency.
skia_bindgen::definitions::save_definitions(&definitions, &binaries_config.output_directory)
.expect("failed to write Skia defines");
let bindings_config = skia_bindgen::Configuration::new(features, definitions, skia_source_dir);
skia_bindgen::generate_bindings(
&bindings_config,
&binaries_config.output_directory,
target,
sysroot,
);
}
/// On docs.rs, rustdoc runs inside a container with no networking, so copy a pre-generated
/// `bindings.rs` file.
fn fake_bindings() -> Result<(), io::Error> {
println!("COPYING bindings_docs.rs to OUT_DIR/skia/bindings.rs");
let bindings_target = cargo::output_directory()
.join(binaries_config::SKIA_OUTPUT_DIR)
.join("bindings.rs");
fs::copy("bindings_docs.rs", bindings_target).map(|_| ())
}
/// Environment variables used by this build script.
mod env {
use crate::build_support::cargo;
use std::path::PathBuf;
/// The path to the Skia source directory.
pub fn source_dir() -> Option<PathBuf> {
cargo::env_var("SKIA_SOURCE_DIR").map(PathBuf::from)
}
/// The path to where a pre-built Skia library can be found.
pub fn skia_lib_search_path() -> Option<PathBuf> {
cargo::env_var("SKIA_LIBRARY_SEARCH_PATH").map(PathBuf::from)
}
pub fn is_skia_debug() -> bool {
matches!(cargo::env_var("SKIA_DEBUG"), Some(v) if v != "0")
}
pub fn is_docs_rs_build() -> bool {
matches!(cargo::env_var("DOCS_RS"), Some(v) if v != "0")
}
}