Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
168 lines (156 loc) · 6.34 KB
/
Copy pathbuild.rs
File metadata and controls
168 lines (156 loc) · 6.34 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
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
use std::path::{Path, PathBuf};
/// Forward `nginx-sys` build metadata into this crate.
///
/// `nginx-sys` must be a direct dependency so Cargo exports `DEP_NGINX_*`.
/// See https://github.com/rust-lang/cargo/issues/3544 and the ngx-rust build.rs.
fn main() {
println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES_CHECK");
println!(
"cargo::rustc-check-cfg=cfg(ngx_feature, values({}))",
std::env::var("DEP_NGINX_FEATURES_CHECK").unwrap_or_else(|_| "any()".to_string())
);
println!("cargo::rustc-check-cfg=cfg(ngx_features, values(\"rustc-test\"))");
println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES");
if let Ok(features) = std::env::var("DEP_NGINX_FEATURES") {
for feature in features.split(',').map(str::trim) {
println!("cargo::rustc-cfg=ngx_feature=\"{feature}\"");
}
}
println!("cargo::rerun-if-env-changed=DEP_NGINX_OS_CHECK");
println!(
"cargo::rustc-check-cfg=cfg(ngx_os, values({}))",
std::env::var("DEP_NGINX_OS_CHECK").unwrap_or_else(|_| "any()".to_string())
);
println!("cargo::rerun-if-env-changed=DEP_NGINX_OS");
if let Ok(os) = std::env::var("DEP_NGINX_OS") {
println!("cargo::rustc-cfg=ngx_os=\"{os}\"");
}
println!("cargo::rerun-if-env-changed=DEP_NGINX_VERSION_NUMBER");
if let Ok(version) = std::env::var("DEP_NGINX_VERSION_NUMBER") {
let version: u64 = version.parse().unwrap();
println!("cargo::rustc-check-cfg=cfg(nginx1_21_1)");
println!("cargo::rustc-check-cfg=cfg(nginx1_25_1)");
if version >= 1_021_001 {
println!("cargo::rustc-cfg=nginx1_21_1");
}
if version >= 1_025_001 {
println!("cargo::rustc-cfg=nginx1_25_1");
}
}
println!("cargo::rerun-if-env-changed=DEP_NGINX_BUILD_DIR");
if let Ok(build_dir) = std::env::var("DEP_NGINX_BUILD_DIR") {
println!("cargo::rustc-env=DEP_NGINX_BUILD_DIR={build_dir}");
}
println!("cargo::rerun-if-env-changed=NGINX_SOURCE_DIR");
println!("cargo::rerun-if-env-changed=NGINX_BUILD_DIR");
println!("cargo::rerun-if-env-changed=NGX_RUSTSCRIPT_INTEGRATION_HOOKS");
println!("cargo::rustc-check-cfg=cfg(ngx_rustscript_integration_hooks)");
// Only the current compile-time opt-in enables hooks. The fingerprint is
// always emitted so a leftover build-script stdout cannot keep the cfg on
// after NGX_RUSTSCRIPT_INTEGRATION_HOOKS is unset or set to anything else.
let hooks_enabled = std::env::var("NGX_RUSTSCRIPT_INTEGRATION_HOOKS").as_deref() == Ok("1");
println!(
"cargo::rustc-env=NGX_RUSTSCRIPT_HOOKS_FINGERPRINT={}",
if hooks_enabled { "1" } else { "0" }
);
if hooks_enabled {
println!("cargo::rustc-cfg=ngx_rustscript_integration_hooks");
}
compile_request_shim();
if cfg!(target_os = "macos") {
println!("cargo::rustc-link-arg=-undefined");
println!("cargo::rustc-link-arg=dynamic_lookup");
}
}
fn compile_request_shim() {
println!("cargo::rerun-if-changed=src/nginx_request_shim.c");
println!("cargo::rerun-if-env-changed=DEP_NGINX_INCLUDE");
println!("cargo::rerun-if-env-changed=DEP_NGINX_CFLAGS");
let dependency_include = std::env::var_os("DEP_NGINX_INCLUDE").unwrap_or_else(|| {
panic!("DEP_NGINX_INCLUDE is required to compile src/nginx_request_shim.c")
});
let includes = selected_nginx_include_paths(&dependency_include);
let encoded_includes = std::env::join_paths(&includes)
.expect("selected NGINX include paths must be representable as DEP_NGINX_INCLUDE");
println!(
"cargo::rustc-env=DEP_NGINX_INCLUDE={}",
encoded_includes.to_string_lossy()
);
if let Ok(cflags) = std::env::var("DEP_NGINX_CFLAGS") {
println!("cargo::rustc-env=DEP_NGINX_CFLAGS={cflags}");
} else {
println!("cargo::rustc-env=DEP_NGINX_CFLAGS=");
}
let mut build = cc::Build::new();
build
.file("src/nginx_request_shim.c")
.warnings(true)
.define("NGX_RUSTSCRIPT_NO_C_PUBLIC_ALIAS", None);
for path in &includes {
build.include(path);
}
if let Ok(cflags) = std::env::var("DEP_NGINX_CFLAGS") {
for flag in cflags.split_whitespace() {
build.flag(flag);
}
}
build.compile("ngx_rustscript_request_shim");
}
fn selected_nginx_include_paths(dependency_include: &std::ffi::OsStr) -> Vec<PathBuf> {
let source = std::env::var_os("NGINX_SOURCE_DIR").map(PathBuf::from);
let build = std::env::var_os("NGINX_BUILD_DIR").map(PathBuf::from);
match (source, build) {
(Some(source), Some(build)) => {
let makefile = build.join("Makefile");
println!("cargo::rerun-if-changed={}", makefile.display());
parse_makefile_includes(&makefile)
.into_iter()
.map(|path| {
if path.is_absolute() {
path
} else {
source.join(path)
}
})
.collect()
}
_ => std::env::split_paths(dependency_include).collect(),
}
}
fn parse_makefile_includes(makefile: &Path) -> Vec<PathBuf> {
let contents = std::fs::read_to_string(makefile).unwrap_or_else(|error| {
panic!(
"unable to read NGINX makefile {}: {error}",
makefile.display()
)
});
let mut includes = Vec::new();
let mut logical = String::new();
for raw in contents.lines() {
if let Some(part) = raw.strip_suffix('\\') {
logical.push_str(part);
continue;
}
logical.push_str(raw);
if let Some(fragment) = logical.strip_prefix("ALL_INCS") {
let mut words = fragment.split_whitespace();
while let Some(word) = words.next() {
if let Some(include) = word.strip_prefix("-I") {
let include = if include.is_empty() {
words.next().expect("-I argument in NGINX Makefile")
} else {
include
};
includes.push(PathBuf::from(include));
}
}
}
logical.clear();
}
assert!(
!includes.is_empty(),
"NGINX makefile {} did not contain ALL_INCS include paths",
makefile.display()
);
includes
}