Skip to content

Commit 1e868ef

Browse files
committed
feat(sys): generate bindings for Stream modules
Detect if Stream or HTTP are enabled in nginx configuration and adjust generated bindings accordingly.
1 parent 1abedbf commit 1e868ef

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",
@@ -336,17 +338,26 @@ pub fn print_cargo_metadata<T: AsRef<Path>>(includes: &[T]) -> Result<(), Box<dy
336338
);
337339

338340
// A quoted list of all recognized features to be passed to rustc-check-cfg.
341+
let values = NGX_CONF_FEATURES.join("\",\"");
342+
println!("cargo::metadata=features_check=\"{}\"", values);
339343
println!(
340-
"cargo::metadata=features_check=\"{}\"",
341-
NGX_CONF_FEATURES.join("\",\"")
344+
"cargo::rustc-check-cfg=cfg(ngx_feature, values(\"{}\"))",
345+
values
342346
);
347+
343348
// A list of features enabled in the nginx build we're using
344349
println!("cargo::metadata=features={}", ngx_features.join(","));
350+
for feature in ngx_features {
351+
println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature);
352+
}
345353

346354
// A quoted list of all recognized operating systems to be passed to rustc-check-cfg.
347-
println!("cargo::metadata=os_check=\"{}\"", NGX_CONF_OS.join("\",\""));
355+
let values = NGX_CONF_OS.join("\",\"");
356+
println!("cargo::metadata=os_check=\"{}\"", values);
357+
println!("cargo::rustc-check-cfg=cfg(ngx_os, values(\"{}\"))", values);
348358
// Current detected operating system
349359
println!("cargo::metadata=os={ngx_os}");
360+
println!("cargo::rustc-cfg=ngx_os=\"{ngx_os}\"");
350361

351362
Ok(())
352363
}
@@ -361,6 +372,22 @@ fn expand_definitions<T: AsRef<Path>>(includes: &[T]) -> Result<Vec<u8>, Box<dyn
361372
#include <ngx_config.h>
362373
#include <ngx_core.h>
363374
375+
/* C23 or Clang/GCC/MSVC >= 15.3 extension */
376+
#if defined(__has_include)
377+
378+
#if __has_include(<ngx_http.h>)
379+
RUST_CONF_HTTP=1
380+
#endif
381+
382+
#if __has_include(<ngx_stream.h>)
383+
RUST_CONF_STREAM=1
384+
#endif
385+
386+
#else
387+
/* fallback */
388+
RUST_CONF_HTTP=1
389+
#endif
390+
364391
RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD
365392
RUST_CONF_NGINX_VERSION=NGINX_VER
366393
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
@@ -4,10 +4,13 @@
44

55
pub mod detail;
66
mod event;
7+
#[cfg(ngx_feature = "http")]
8+
mod http;
79
mod queue;
10+
#[cfg(ngx_feature = "stream")]
11+
mod stream;
812
mod string;
913

10-
use core::mem::offset_of;
1114
use core::ptr;
1215

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

4739
impl ngx_command_t {
4840
/// Creates a new empty [`ngx_command_t`] instance.

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)