|
| 1 | +# nginx-sys |
| 2 | + |
| 3 | +The `nginx-sys` crate provides low-level bindings for the nginx C API, allowing |
| 4 | +Rust applications to interact with nginx servers and modules. |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +Add `nginx-sys` as a dependency in your `Cargo.toml`: |
| 9 | + |
| 10 | +```toml |
| 11 | +[dependencies] |
| 12 | +nginx-sys = "0.5.0" |
| 13 | +``` |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- `vendored`: Enables the build scripts to download and build a copy of nginx |
| 18 | + source and link against it. |
| 19 | + This feature is enabled by default. |
| 20 | + |
| 21 | +## Output variables |
| 22 | + |
| 23 | +Following metadata variables are passed to the build scripts of any **direct** |
| 24 | +dependents of the package. |
| 25 | + |
| 26 | +Check the [using another sys crate] and the [links manifest key] sections of the |
| 27 | +Cargo Book for more details about passing metadata between packages. |
| 28 | + |
| 29 | +### `DEP_NGINX_FEATURES` |
| 30 | + |
| 31 | +nginx has various configuration options that may affect the availability of |
| 32 | +functions, constants and structure fields. This is not something that can be |
| 33 | +detected at runtime, as an attempt to use a symbol unavailable in the bindings |
| 34 | +would result in compilation error. |
| 35 | + |
| 36 | +`DEP_NGINX_FEATURES_CHECK` contains the full list of feature flags supported |
| 37 | +by `nginx-sys`, i.e. everything that can be used for feature checks. |
| 38 | +The most common use for this variable is to specify [`cargo::rustc-check-cfg`]. |
| 39 | + |
| 40 | +`DEP_NGINX_FEATURES` contains the list of features enabled in the version of |
| 41 | +nginx being built against. |
| 42 | + |
| 43 | +An example of a build script with these variables: |
| 44 | +```rust |
| 45 | +// Specify acceptable values for `ngx_feature`. |
| 46 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES_CHECK"); |
| 47 | +println!( |
| 48 | + "cargo::rustc-check-cfg=cfg(ngx_feature, values({}))", |
| 49 | + std::env::var("DEP_NGINX_FEATURES_CHECK").unwrap_or("any()".to_string()) |
| 50 | +); |
| 51 | +// Read feature flags detected by nginx-sys and pass to the compiler. |
| 52 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES"); |
| 53 | +if let Ok(features) = std::env::var("DEP_NGINX_FEATURES") { |
| 54 | + for feature in features.split(',').map(str::trim) { |
| 55 | + println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature); |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +And an usage example: |
| 61 | +```rust |
| 62 | +#[cfg(ngx_feature = "debug")] |
| 63 | +println!("this nginx binary was built with debug logging enabled"); |
| 64 | +``` |
| 65 | + |
| 66 | +### `DEP_NGINX_OS` |
| 67 | + |
| 68 | +Version, as detected by the nginx configuration script. |
| 69 | + |
| 70 | +`DEP_NGINX_OS_CHECK` contains the full list of supported values, and |
| 71 | +`DEP_NGINX_OS` the currently detected one. |
| 72 | + |
| 73 | +Usage examples: |
| 74 | +```rust |
| 75 | +// Specify acceptable values for `ngx_os` |
| 76 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_OS_CHECK"); |
| 77 | +println!( |
| 78 | + "cargo::rustc-check-cfg=cfg(ngx_os, values({}))", |
| 79 | + std::env::var("DEP_NGINX_OS_CHECK").unwrap_or("any()".to_string()) |
| 80 | +); |
| 81 | +// Read operating system detected by nginx-sys and pass to the compiler. |
| 82 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_OS"); |
| 83 | +if let Ok(os) = std::env::var("DEP_NGINX_OS") { |
| 84 | + println!("cargo::rustc-cfg=ngx_os=\"{}\"", os); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +```rust |
| 89 | +#[cfg(ngx_os = "freebsd")] |
| 90 | +println!("this nginx binary was built on FreeBSD"); |
| 91 | +``` |
| 92 | + |
| 93 | +### Version and build information |
| 94 | + |
| 95 | +- `DEP_NGINX_VERSION_NUMBER`: |
| 96 | + a numeric representation with 3 digits for each component: `1026002` |
| 97 | +- `DEP_NGINX_VERSION`: |
| 98 | + human-readable string in a product/version format: `nginx/1.26.2` |
| 99 | +- `DEP_NGINX_BUILD`: |
| 100 | + version string with the optional build name (`--build=`) included: |
| 101 | + `nginx/1.25.5 (nginx-plus-r32)` |
| 102 | + |
| 103 | +Usage example: |
| 104 | +```rust |
| 105 | +println!("cargo::rustc-check-cfg=cfg(nginx1_27_0)"); |
| 106 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_VERSION_NUMBER"); |
| 107 | +if let Ok(version) = std::env::var("DEP_NGINX_VERSION_NUMBER") { |
| 108 | + let version: u64 = version.parse().unwrap(); |
| 109 | + |
| 110 | + if version >= 1_027_000 { |
| 111 | + println!("cargo::rustc-cfg=nginx1_27_0"); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Examples |
| 117 | + |
| 118 | +### Get nginx Version |
| 119 | + |
| 120 | +This example demonstrates how to retrieve the version of the nginx server. |
| 121 | + |
| 122 | +```rust,no_run |
| 123 | +use nginx_sys::nginx_version; |
| 124 | +
|
| 125 | +println!("nginx version: {}", nginx_version); |
| 126 | +``` |
| 127 | +[using another sys crate]: https://doc.rust-lang.org/nightly/cargo/reference/build-script-examples.html#using-another-sys-crate |
| 128 | +[links manifest key]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#the-links-manifest-key |
| 129 | +[`cargo::rustc-check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-check-cfg |
0 commit comments