From 309a2ff3751a92643e4f49e29159828d33b6f74e Mon Sep 17 00:00:00 2001 From: Aleksei Bavshin Date: Wed, 16 Apr 2025 13:47:45 -0700 Subject: [PATCH] feat(sys): generate bindings for Stream modules Detect if Stream or HTTP are enabled in nginx configuration and adjust generated bindings accordingly. --- nginx-sys/build/main.rs | 33 ++++++++++++++++++++++++++++++--- nginx-sys/build/wrapper.h | 19 +++++++++++++++++-- nginx-sys/src/http.rs | 18 ++++++++++++++++++ nginx-sys/src/lib.rs | 24 ++++++++---------------- nginx-sys/src/stream.rs | 13 +++++++++++++ 5 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 nginx-sys/src/http.rs create mode 100644 nginx-sys/src/stream.rs diff --git a/nginx-sys/build/main.rs b/nginx-sys/build/main.rs index 6028f318..22208dd7 100644 --- a/nginx-sys/build/main.rs +++ b/nginx-sys/build/main.rs @@ -26,6 +26,7 @@ const NGX_CONF_FEATURES: &[&str] = &[ "have_file_aio", "have_kqueue", "have_variadic_macros", + "http", "http_cache", "http_dav", "http_gzip", @@ -40,6 +41,7 @@ const NGX_CONF_FEATURES: &[&str] = &[ "pcre2", "quic", "ssl", + "stream", "stream_ssl", "stream_upstream_zone", "threads", @@ -336,17 +338,26 @@ pub fn print_cargo_metadata>(includes: &[T]) -> Result<(), Box>(includes: &[T]) -> Result, Box #include +/* C23 or Clang/GCC/MSVC >= 15.3 extension */ +#if defined(__has_include) + +#if __has_include() +RUST_CONF_HTTP=1 +#endif + +#if __has_include() +RUST_CONF_STREAM=1 +#endif + +#else +/* fallback */ +RUST_CONF_HTTP=1 +#endif + RUST_CONF_NGINX_BUILD=NGINX_VER_BUILD RUST_CONF_NGINX_VERSION=NGINX_VER RUST_CONF_NGINX_VERSION_NUMBER=nginx_version diff --git a/nginx-sys/build/wrapper.h b/nginx-sys/build/wrapper.h index daaf38de..c333744f 100644 --- a/nginx-sys/build/wrapper.h +++ b/nginx-sys/build/wrapper.h @@ -1,8 +1,23 @@ -#include -#include #include #include +/* __has_include was a compiler-specific extension until C23, + * but it's safe to assume that bindgen supports it via libclang. + */ +#if defined(__has_include) + +#if __has_include() +#include +#endif + +#if __has_include() +#include +#endif + +#else +#include +#endif + const char *NGX_RS_MODULE_SIGNATURE = NGX_MODULE_SIGNATURE; // `--prefix=` results in not emitting the declaration diff --git a/nginx-sys/src/http.rs b/nginx-sys/src/http.rs new file mode 100644 index 00000000..435cc794 --- /dev/null +++ b/nginx-sys/src/http.rs @@ -0,0 +1,18 @@ +use core::mem::offset_of; + +use crate::bindings::ngx_http_conf_ctx_t; + +/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct. +/// +/// This is used to access the main configuration context for an HTTP module. +pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf); + +/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct. +/// +/// This is used to access the server configuration context for an HTTP module. +pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf); + +/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct. +/// +/// This is used to access the location configuration context for an HTTP module. +pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf); diff --git a/nginx-sys/src/lib.rs b/nginx-sys/src/lib.rs index 9ed9aa36..b6d01822 100644 --- a/nginx-sys/src/lib.rs +++ b/nginx-sys/src/lib.rs @@ -4,10 +4,13 @@ pub mod detail; mod event; +#[cfg(ngx_feature = "http")] +mod http; mod queue; +#[cfg(ngx_feature = "stream")] +mod stream; mod string; -use core::mem::offset_of; use core::ptr; #[doc(hidden)] @@ -27,22 +30,11 @@ mod bindings { #[doc(no_inline)] pub use bindings::*; pub use event::*; +#[cfg(ngx_feature = "http")] +pub use http::*; pub use queue::*; - -/// The offset of the `main_conf` field in the `ngx_http_conf_ctx_t` struct. -/// -/// This is used to access the main configuration context for an HTTP module. -pub const NGX_HTTP_MAIN_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, main_conf); - -/// The offset of the `srv_conf` field in the `ngx_http_conf_ctx_t` struct. -/// -/// This is used to access the server configuration context for an HTTP module. -pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_conf); - -/// The offset of the `loc_conf` field in the `ngx_http_conf_ctx_t` struct. -/// -/// This is used to access the location configuration context for an HTTP module. -pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf); +#[cfg(ngx_feature = "stream")] +pub use stream::*; impl ngx_command_t { /// Creates a new empty [`ngx_command_t`] instance. diff --git a/nginx-sys/src/stream.rs b/nginx-sys/src/stream.rs new file mode 100644 index 00000000..6288fd4e --- /dev/null +++ b/nginx-sys/src/stream.rs @@ -0,0 +1,13 @@ +use core::mem::offset_of; + +use crate::bindings::ngx_stream_conf_ctx_t; + +/// The offset of the `main_conf` field in the `ngx_stream_conf_ctx_t` struct. +/// +/// This is used to access the main configuration context for a STREAM module. +pub const NGX_STREAM_MAIN_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, main_conf); + +/// The offset of the `srv_conf` field in the `ngx_stream_conf_ctx_t` struct. +/// +/// This is used to access the server configuration context for a STREAM module. +pub const NGX_STREAM_SRV_CONF_OFFSET: usize = offset_of!(ngx_stream_conf_ctx_t, srv_conf);