Skip to content

Commit d368a28

Browse files
committed
feat: expose DEP_NGINX_ vars to the high-level bindings
The example build script converts `DEP_NGINX_FEATURES`, `DEP_NGINX_OS` and `DEP_NGINX_VERSION_NUMBER` received from `nginx-sys` to the `cfg` values for conditional compilation. See tests for usage examples.
1 parent e3bb1b4 commit d368a28

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// Example buildscript for an nginx module.
2+
///
3+
/// Due to the limitations of cargo[1], this buildscript _requires_ adding `nginx-sys` to the
4+
/// direct dependencies of your crate.
5+
///
6+
/// [1]: https://github.com/rust-lang/cargo/issues/3544
7+
fn main() {
8+
// Generate `ngx_os` and `ngx_feature` cfg values
9+
10+
// Specify acceptable values for `ngx_feature`
11+
println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES_CHECK");
12+
println!(
13+
"cargo::rustc-check-cfg=cfg(ngx_feature, values({}))",
14+
std::env::var("DEP_NGINX_FEATURES_CHECK").unwrap_or("any()".to_string())
15+
);
16+
// Read feature flags detected by nginx-sys and pass to the compiler.
17+
println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES");
18+
if let Ok(features) = std::env::var("DEP_NGINX_FEATURES") {
19+
for feature in features.split(',').map(str::trim) {
20+
println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature);
21+
}
22+
}
23+
24+
// Specify acceptable values for `ngx_os`
25+
println!("cargo::rerun-if-env-changed=DEP_NGINX_OS_CHECK");
26+
println!(
27+
"cargo::rustc-check-cfg=cfg(ngx_os, values({}))",
28+
std::env::var("DEP_NGINX_OS_CHECK").unwrap_or("any()".to_string())
29+
);
30+
// Read operating system detected by nginx-sys and pass to the compiler.
31+
println!("cargo::rerun-if-env-changed=DEP_NGINX_OS");
32+
if let Ok(os) = std::env::var("DEP_NGINX_OS") {
33+
println!("cargo::rustc-cfg=ngx_os=\"{}\"", os);
34+
}
35+
36+
// Generate cfg values for version checks
37+
// println!("cargo::rustc-check-cfg=cfg(nginx1_27_0)");
38+
// println!("cargo::rerun-if-env-changed=DEP_NGINX_VERSION_NUMBER");
39+
// if let Ok(version) = std::env::var("DEP_NGINX_VERSION_NUMBER") {
40+
// let version: u64 = version.parse().unwrap();
41+
//
42+
// if version >= 1_027_000 {
43+
// println!("cargo::rustc-cfg=nginx1_27_0");
44+
// }
45+
// }
46+
}

examples/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ homepage.workspace = true
88
repository.workspace = true
99
rust-version.workspace = true
1010

11-
[dev-dependencies]
11+
build = "../build.rs"
12+
13+
[dependencies]
14+
nginx-sys = { path = "../nginx-sys/", default-features = false }
1215
ngx = { path = "../", default-features = false }
16+
17+
[dev-dependencies]
1318
aws-sign-v4 = "0.3.0"
1419
chrono = "0.4.23"
1520
http = "1.1.0"
@@ -42,8 +47,6 @@ name = "async"
4247
path = "async.rs"
4348
crate-type = ["cdylib"]
4449

45-
[dependencies]
46-
4750
[features]
4851
default = ["export-modules", "ngx/vendored"]
4952
# Generate `ngx_modules` table with module exports

tests/conditional-compilation.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use ngx::ffi;
2+
3+
#[test]
4+
fn test_os_symbols() {
5+
#[cfg(ngx_os = "freebsd")]
6+
assert_eq!(ffi::NGX_FREEBSD, 1);
7+
8+
#[cfg(ngx_os = "linux")]
9+
assert_eq!(ffi::NGX_LINUX, 1);
10+
11+
#[cfg(ngx_os = "darwin")]
12+
assert_eq!(ffi::NGX_DARWIN, 1);
13+
}
14+
15+
#[test]
16+
fn test_feature_symbols() {
17+
let ev: ffi::ngx_event_t = unsafe { std::mem::zeroed() };
18+
19+
assert_eq!(ev.available, 0);
20+
21+
#[cfg(ngx_feature = "have_kqueue")]
22+
assert_eq!(ev.kq_errno, 0);
23+
}

0 commit comments

Comments
 (0)