Skip to content

Commit f7dc512

Browse files
committed
feat: example of Nginx buildsystem integration
1 parent 4e6520f commit f7dc512

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed

examples/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ crate-type = ["cdylib"]
4242
tokio = { version = "1.33.0", features = ["full"] }
4343

4444
[features]
45+
default = ["export-modules"]
46+
# Generate `ngx_modules` table with module exports
47+
export-modules = []
4548
linux = []

examples/async.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ static ngx_http_async_module_ctx: ngx_http_module_t = ngx_http_module_t {
7878
merge_loc_conf: Some(Module::merge_loc_conf),
7979
};
8080

81+
#[cfg(feature = "export-modules")]
8182
ngx_modules!(ngx_http_async_module);
8283

8384
#[no_mangle]
85+
#[used]
8486
pub static mut ngx_http_async_module: ngx_module_t = ngx_module_t {
8587
ctx_index: ngx_uint_t::max_value(),
8688
index: ngx_uint_t::max_value(),

examples/awssig.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ static ngx_http_awssigv4_module_ctx: ngx_http_module_t = ngx_http_module_t {
9797
merge_loc_conf: Some(Module::merge_loc_conf),
9898
};
9999

100+
#[cfg(feature = "export-modules")]
100101
ngx_modules!(ngx_http_awssigv4_module);
101102

102103
#[no_mangle]
104+
#[used]
103105
pub static mut ngx_http_awssigv4_module: ngx_module_t = ngx_module_t {
104106
ctx_index: ngx_uint_t::max_value(),
105107
index: ngx_uint_t::max_value(),

examples/config

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
ngx_addon_name=ngx_rust_examples
2+
ngx_cargo_profile=ngx-module
3+
4+
if [ $HTTP = YES ]; then
5+
ngx_module_type=HTTP
6+
7+
if :; then
8+
ngx_module_name=ngx_http_async_module
9+
ngx_module_lib=async
10+
11+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
12+
ngx_module_deps=$ngx_module_lib
13+
ngx_module_libs="$ngx_module_lib -lm"
14+
15+
# Module deps are usually added to the object file targets, but we don't have any
16+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
17+
18+
. auto/module
19+
fi
20+
21+
if :; then
22+
ngx_module_name=ngx_http_awssigv4_module
23+
ngx_module_lib=awssig
24+
25+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
26+
ngx_module_deps=$ngx_module_lib
27+
ngx_module_libs=$ngx_module_lib
28+
29+
# Module deps are usually added to the object file targets, but we don't have any
30+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
31+
32+
. auto/module
33+
fi
34+
35+
if :; then
36+
ngx_module_name=ngx_http_curl_module
37+
ngx_module_lib=curl
38+
39+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
40+
ngx_module_deps=$ngx_module_lib
41+
ngx_module_libs=$ngx_module_lib
42+
43+
# Module deps are usually added to the object file targets, but we don't have any
44+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
45+
46+
. auto/module
47+
fi
48+
49+
case "$NGX_PLATFORM" in
50+
Linux:*)
51+
ngx_module_name=ngx_http_orig_dst_module
52+
ngx_module_lib=httporigdst
53+
54+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
55+
ngx_module_deps=$ngx_module_lib
56+
ngx_module_libs=$ngx_module_lib
57+
58+
# Module deps are usually added to the object file targets, but we don't have any
59+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
60+
61+
. auto/module
62+
;;
63+
esac
64+
65+
if :; then
66+
ngx_module_name=ngx_http_upstream_custom_module
67+
ngx_module_lib=upstream
68+
69+
ngx_module_lib=$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_module_lib.a
70+
ngx_module_deps=$ngx_module_lib
71+
ngx_module_libs=$ngx_module_lib
72+
73+
# Module deps are usually added to the object file targets, but we don't have any
74+
LINK_DEPS="$LINK_DEPS $ngx_module_lib"
75+
76+
. auto/module
77+
fi
78+
fi
79+
80+
# Write a cargo config with the $ngx_cargo_profile definition (optional)
81+
82+
if [ "$NGX_DEBUG" = YES ]; then
83+
NGX_CARGO_PROFILE_BASE=dev
84+
else
85+
NGX_CARGO_PROFILE_BASE=release
86+
fi
87+
88+
mkdir -p "$NGX_OBJS/.cargo"
89+
cat > "$NGX_OBJS/.cargo/config.toml" << END
90+
91+
[profile.$ngx_cargo_profile]
92+
inherits = "$NGX_CARGO_PROFILE_BASE"
93+
94+
END

examples/config.make

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ngx_addon_name=ngx_rust_examples
2+
ngx_cargo_profile=ngx-module
3+
ngx_cargo_manifest=$(realpath $ngx_addon_dir/Cargo.toml)
4+
ngx_cargo_features=
5+
ngx_rust_examples="async awssig curl upstream"
6+
7+
case "$NGX_PLATFORM" in
8+
Linux:*)
9+
ngx_cargo_features="$ngx_cargo_features linux"
10+
ngx_rust_examples="$ngx_rust_examples httporigdst"
11+
;;
12+
esac
13+
14+
for ngx_rust_example in $ngx_rust_examples
15+
do
16+
17+
cat << END >> $NGX_MAKEFILE
18+
19+
# Always call cargo instead of tracking the source modifications
20+
.PHONY: $NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_rust_example.a
21+
22+
$NGX_OBJS/$ngx_addon_name/$ngx_cargo_profile/examples/lib$ngx_rust_example.a:
23+
cd $NGX_OBJS && \\
24+
NGX_OBJS="\$\$PWD" cargo rustc \\
25+
--crate-type staticlib \\
26+
--example "$ngx_rust_example" \\
27+
--no-default-features \\
28+
--features "$ngx_cargo_features" \\
29+
--profile $ngx_cargo_profile \\
30+
--target-dir $ngx_addon_name \\
31+
--manifest-path $ngx_cargo_manifest
32+
33+
END
34+
35+
done

examples/curl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ static ngx_http_curl_module_ctx: ngx_http_module_t = ngx_http_module_t {
6161
merge_loc_conf: Some(Module::merge_loc_conf),
6262
};
6363

64+
#[cfg(feature = "export-modules")]
6465
ngx_modules!(ngx_http_curl_module);
6566

6667
#[no_mangle]
68+
#[used]
6769
pub static mut ngx_http_curl_module: ngx_module_t = ngx_module_t {
6870
ctx_index: ngx_uint_t::max_value(),
6971
index: ngx_uint_t::max_value(),

examples/httporigdst.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ static ngx_http_orig_dst_module_ctx: ngx_http_module_t = ngx_http_module_t {
8686
merge_loc_conf: Some(Module::merge_loc_conf),
8787
};
8888

89+
#[cfg(feature = "export-modules")]
8990
ngx_modules!(ngx_http_orig_dst_module);
9091

9192
#[no_mangle]
93+
#[used]
9294
pub static mut ngx_http_orig_dst_module: ngx_module_t = ngx_module_t {
9395
ctx_index: ngx_uint_t::max_value(),
9496
index: ngx_uint_t::max_value(),

examples/upstream.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ static mut ngx_http_upstream_custom_commands: [ngx_command_t; 2] = [
105105
ngx_null_command!(),
106106
];
107107

108+
#[cfg(feature = "export-modules")]
108109
ngx_modules!(ngx_http_upstream_custom_module);
109110

110111
#[no_mangle]
112+
#[used]
111113
pub static mut ngx_http_upstream_custom_module: ngx_module_t = ngx_module_t {
112114
ctx_index: ngx_uint_t::max_value(),
113115
index: ngx_uint_t::max_value(),

0 commit comments

Comments
 (0)