Skip to content

Commit fb72e11

Browse files
committed
feat(sys): Stream module support
Detect if Stream or HTTP are enabled in nginx configuration and adjust generated bindings accordingly.
1 parent 617c349 commit fb72e11

File tree

5 files changed

+86
-21
lines changed

5 files changed

+86
-21
lines changed

nginx-sys/build/main.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const NGX_CONF_FEATURES: &[&str] = &[
2626
"have_file_aio",
2727
"have_kqueue",
2828
"have_variadic_macros",
29+
"http",
2930
"http_cache",
3031
"http_dav",
3132
"http_gzip",
@@ -40,6 +41,7 @@ const NGX_CONF_FEATURES: &[&str] = &[
4041
"pcre2",
4142
"quic",
4243
"ssl",
44+
"stream",
4345
"stream_ssl",
4446
"stream_upstream_zone",
4547
"threads",
@@ -328,17 +330,26 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(includes: &[T]) -> Result<(), Box<dy
328330
);
329331

330332
// A quoted list of all recognized features to be passed to rustc-check-cfg.
333+
let values = NGX_CONF_FEATURES.join("\",\"");
334+
println!("cargo::metadata=features_check=\"{}\"", values);
331335
println!(
332-
"cargo::metadata=features_check=\"{}\"",
333-
NGX_CONF_FEATURES.join("\",\"")
336+
"cargo::rustc-check-cfg=cfg(ngx_feature, values(\"{}\"))",
337+
values
334338
);
339+
335340
// A list of features enabled in the nginx build we're using
336341
println!("cargo::metadata=features={}", ngx_features.join(","));
342+
for feature in ngx_features {
343+
println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature);
344+
}
337345

338346
// A quoted list of all recognized operating systems to be passed to rustc-check-cfg.
339-
println!("cargo::metadata=os_check=\"{}\"", NGX_CONF_OS.join("\",\""));
347+
let values = NGX_CONF_OS.join("\",\"");
348+
println!("cargo::metadata=os_check=\"{}\"", values);
349+
println!("cargo::rustc-check-cfg=cfg(ngx_os, values(\"{}\"))", values);
340350
// Current detected operating system
341351
println!("cargo::metadata=os={ngx_os}");
352+
println!("cargo::rustc-cfg=ngx_os=\"{ngx_os}\"");
342353

343354
Ok(())
344355
}
@@ -353,6 +364,22 @@ fn expand_definitions<T: AsRef<Path>>(includes: &[T]) -> Result<Vec<u8>, Box<dyn
353364
#include <ngx_config.h>
354365
#include <ngx_core.h>
355366
367+
/* C23 or Clang/GCC/MSVC >= 15.3 extension */
368+
#if defined(__has_include)
369+
370+
#if __has_include(<ngx_http.h>)
371+
RUST_CONF_HTTP=1
372+
#endif
373+
374+
#if __has_include(<ngx_stream.h>)
375+
RUST_CONF_STREAM=1
376+
#endif
377+
378+
#else
379+
/* fallback */
380+
RUST_CONF_HTTP=1
381+
#endif
382+
356383
RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD
357384
RUST_CONF_NGINX_VERSION=NGINX_VER
358385
RUST_CONF_NGINX_VERSION_NUMBER=nginx_version

nginx-sys/build/wrapper.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
#include <ngx_http.h>
2-
#include <ngx_conf_file.h>
31
#include <ngx_config.h>
42
#include <ngx_core.h>
53

4+
/* __has_include was a compiler-specific extension until C23,
5+
* but it's safe to assume that bindgen supports it via libclang.
6+
*/
7+
#if defined(__has_include)
8+
9+
#if __has_include(<ngx_http.h>)
10+
#include <ngx_http.h>
11+
#endif
12+
13+
#if __has_include(<ngx_stream.h>)
14+
#include <ngx_stream.h>
15+
#endif
16+
17+
#else
18+
#include <ngx_http.h>
19+
#endif
20+
621
const char *NGX_RS_MODULE_SIGNATURE = NGX_MODULE_SIGNATURE;
722

823
// `--prefix=` results in not emitting the declaration

nginx-sys/src/http.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use core::mem::offset_of;
2+
3+
use crate::bindings::ngx_http_conf_ctx_t;
4+
5+
/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.
6+
///
7+
/// This is used to access the main configuration context for an HTTP module.
8+
pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf);
9+
10+
/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct.
11+
///
12+
/// This is used to access the server configuration context for an HTTP module.
13+
pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf);
14+
15+
/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct.
16+
///
17+
/// This is used to access the location configuration context for an HTTP module.
18+
pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);

nginx-sys/src/lib.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
#![no_std]
44

55
mod event;
6+
#[cfg(ngx_feature = "http")]
7+
mod http;
68
mod queue;
9+
#[cfg(ngx_feature = "stream")]
10+
mod stream;
711

812
use core::fmt;
9-
use core::mem::offset_of;
1013
use core::ptr::{self, copy_nonoverlapping};
1114
use core::slice;
1215

@@ -25,22 +28,11 @@ mod bindings {
2528
#[doc(no_inline)]
2629
pub use bindings::*;
2730
pub use event::*;
31+
#[cfg(ngx_feature = "http")]
32+
pub use http::*;
2833
pub use queue::*;
29-
30-
/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.
31-
///
32-
/// This is used to access the main configuration context for an HTTP module.
33-
pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf);
34-
35-
/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct.
36-
///
37-
/// This is used to access the server configuration context for an HTTP module.
38-
pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf);
39-
40-
/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct.
41-
///
42-
/// This is used to access the location configuration context for an HTTP module.
43-
pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);
34+
#[cfg(ngx_feature = "stream")]
35+
pub use stream::*;
4436

4537
/// Convert a byte slice to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
4638
///

nginx-sys/src/stream.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use core::mem::offset_of;
2+
3+
use crate::bindings::ngx_stream_conf_ctx_t;
4+
5+
/// The offset of the `main_conf` field in the `ngx_stream_conf_ctx_t` struct.
6+
///
7+
/// This is used to access the main configuration context for a STREAM module.
8+
pub const NGX_STREAM_MAIN_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, main_conf);
9+
10+
/// The offset of the `srv_conf` field in the `ngx_stream_conf_ctx_t` struct.
11+
///
12+
/// This is used to access the server configuration context for a STREAM module.
13+
pub const NGX_STREAM_SRV_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, srv_conf);

0 commit comments

Comments
 (0)