Skip to content

Commit ba6e301

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 a1ffd81 commit ba6e301

File tree

5 files changed

+85
-20
lines changed

5 files changed

+85
-20
lines changed

nginx-sys/build/main.rs

Lines changed: 29 additions & 2 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",
@@ -295,14 +297,23 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(includes: &[T]) -> Result<(), Box<dy
295297
);
296298

297299
// A quoted list of all recognized features to be passed to rustc-check-cfg.
298-
println!("cargo::metadata=features_check=\"{}\"", NGX_CONF_FEATURES.join("\",\""));
300+
let values = NGX_CONF_FEATURES.join("\",\"");
301+
println!("cargo::metadata=features_check=\"{}\"", values);
302+
println!("cargo::rustc-check-cfg=cfg(ngx_feature, values(\"{}\"))", values);
303+
299304
// A list of features enabled in the nginx build we're using
300305
println!("cargo::metadata=features={}", ngx_features.join(","));
306+
for feature in ngx_features {
307+
println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature);
308+
}
301309

302310
// A quoted list of all recognized operating systems to be passed to rustc-check-cfg.
303-
println!("cargo::metadata=os_check=\"{}\"", NGX_CONF_OS.join("\",\""));
311+
let values = NGX_CONF_OS.join("\",\"");
312+
println!("cargo::metadata=os_check=\"{}\"", values);
313+
println!("cargo::rustc-check-cfg=cfg(ngx_os, values(\"{}\"))", values);
304314
// Current detected operating system
305315
println!("cargo::metadata=os={ngx_os}");
316+
println!("cargo::rustc-cfg=ngx_os=\"{ngx_os}\"");
306317

307318
Ok(())
308319
}
@@ -317,6 +328,22 @@ fn expand_definitions<T: AsRef<Path>>(includes: &[T]) -> Result<Vec<u8>, Box<dyn
317328
#include <ngx_config.h>
318329
#include <ngx_core.h>
319330
331+
/* C23 or Clang/GCC/MSVC >= 15.3 extension */
332+
#if defined(__has_include)
333+
334+
#if __has_include(<ngx_http.h>)
335+
RUST_CONF_HTTP=1
336+
#endif
337+
338+
#if __has_include(<ngx_stream.h>)
339+
RUST_CONF_STREAM=1
340+
#endif
341+
342+
#else
343+
/* fallback */
344+
RUST_CONF_HTTP=1
345+
#endif
346+
320347
RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD
321348
RUST_CONF_NGINX_VERSION=NGINX_VER
322349
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

@@ -26,22 +29,11 @@ mod bindings {
2629
pub use bindings::*;
2730

2831
pub use event::*;
32+
#[cfg(ngx_feature = "http")]
33+
pub use http::*;
2934
pub use queue::*;
30-
31-
/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct.
32-
///
33-
/// This is used to access the main configuration context for an HTTP module.
34-
pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf);
35-
36-
/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct.
37-
///
38-
/// This is used to access the server configuration context for an HTTP module.
39-
pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf);
40-
41-
/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct.
42-
///
43-
/// This is used to access the location configuration context for an HTTP module.
44-
pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);
35+
#[cfg(ngx_feature = "stream")]
36+
pub use stream::*;
4537

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

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)