6767#
6868# ngx_rust_make_modules
6969# ```
70+ #
71+ # The following environment variables can affect the generated Makefile:
72+ #
73+ # - NGX_CARGO sets the "cargo" binary to use, e.g. NGX_CARGO=cargo-1.82
74+ # - NGX_RUST_TARGET passes the --target to the Rust compiler if set
75+ # - NGX_RUSTC_OPT passes additional options to "cargo rustc"
7076
7177# Prevent duplicate invocation unless it is a newer library version
7278if [ " ${NGX_RUST_AUTO_VER:- 0} " -ge 1 ]; then
@@ -136,7 +142,7 @@ case "$NGX_PLATFORM" in
136142 RUST_LIBS=" $RUST_LIBS userenv.lib" # std::env::home_dir
137143 RUST_LIBS=" $RUST_LIBS dbghelp.lib" # backtrace symbolization
138144
139- RUST_TARGET= $ RUST_TARGET_ARCH -pc-windows-msvc
145+ NGX_RUST_TARGET= ${NGX_RUST_TARGET :- $ RUST_TARGET_ARCH -pc-windows-msvc}
140146 ;;
141147
142148 gcc | clang)
@@ -145,7 +151,7 @@ case "$NGX_PLATFORM" in
145151 RUST_LIBS=" $RUST_LIBS -luserenv"
146152 RUST_LIBS=" $RUST_LIBS -ldbghelp"
147153 # gnullvm on arm64?
148- RUST_TARGET= $ RUST_TARGET_ARCH -pc-windows-gnu
154+ NGX_RUST_TARGET= ${NGX_RUST_TARGET :- $ RUST_TARGET_ARCH -pc-windows-gnu}
149155 ;;
150156
151157 esac
@@ -220,7 +226,7 @@ ngx_rust_target_path () {
220226 ngx_rust_obj=examples/$ngx_rust_obj
221227 fi
222228
223- echo " ${RUST_TARGET : +$RUST_TARGET / } $ngx_cargo_profile /$ngx_rust_obj "
229+ echo " ${NGX_RUST_TARGET : +$NGX_RUST_TARGET / } $ngx_cargo_profile /$ngx_rust_obj "
224230}
225231
226232
@@ -244,21 +250,30 @@ ngx_rust_module () {
244250 ngx_module_libs_saved=$ngx_module_libs
245251 ngx_module_libs=" $ngx_rust_obj $ngx_module_libs $RUST_LIBS "
246252
247- # Module deps are usually added to the object file targets, but we don't have any
248- LINK_DEPS=" $LINK_DEPS $ngx_rust_obj "
249-
250- eval ${ngx_addon_id} _RUST_TARGETS=\"\$ ${ngx_addon_id} _RUST_TARGETS \
251- $ngx_rust_target_type :$ngx_rust_target_name \"
252-
253253 if [ -n " $ngx_rust_target_features " ]; then
254254 eval ${ngx_addon_id} _RUST_FEATURES=\"\$ ${ngx_addon_id} _RUST_FEATURES \
255255 $ngx_rust_target_features \"
256256 fi
257257
258- . auto/module
258+ . auto/module
259+
260+ ngx_rust_target=$ngx_rust_target_type :$ngx_rust_target_name
261+
262+ # module deps are usually added to the object file targets, but we don't have any
263+
264+ if [ " $ngx_module_link " = DYNAMIC ]; then
265+ # remember the dynamic module name and generate dependency later
266+ ngx_rust_target=$ngx_rust_target :$ngx_module
267+ else
268+ # add dependency to the binary target
269+ LINK_DEPS=" $LINK_DEPS $ngx_rust_obj "
270+ fi
271+
272+ eval ${ngx_addon_id} _RUST_TARGETS=\"\$ ${ngx_addon_id} _RUST_TARGETS \
273+ $ngx_rust_target \"
259274
260- ngx_module_deps=$ngx_module_deps_saved
261- ngx_module_libs=$ngx_module_libs_saved
275+ ngx_module_deps=$ngx_module_deps_saved
276+ ngx_module_libs=$ngx_module_libs_saved
262277}
263278
264279
@@ -272,8 +287,9 @@ ngx_rust_make_modules () {
272287 eval ngx_rust_targets=" \$ ${ngx_addon_id} _RUST_TARGETS"
273288
274289 for target in $ngx_rust_targets ; do
275- ngx_rust_target_type=${target%%:* }
276- ngx_rust_target_name=${target#*: }
290+ IFS=' :' read -r ngx_rust_target_type ngx_rust_target_name ngx_rust_module_name << END
291+ $target
292+ END
277293
278294 ngx_rust_make_module
279295 done
@@ -304,10 +320,20 @@ $ngx_rust_obj:
304320 --manifest-path "$ngx_cargo_manifest " \\
305321 --no-default-features \\
306322 --profile $ngx_cargo_profile \\
307- ${RUST_TARGET : +--target $RUST_TARGET } \\
323+ ${NGX_RUST_TARGET : +--target $NGX_RUST_TARGET } \\
308324 --target-dir $NGX_OBJS /$ngx_addon_id \\
309325 --features "$ngx_rust_features " \\
310326 $ngx_rustc_module_opt $NGX_RUSTC_OPT
311327
312328END
329+
330+ # Ensure that the "auto"-generated dynamic module target depends on the
331+ # static library. Normally this is achieved by attaching ADDON_DEPS to
332+ # the module object files, but we don't have any suitable C sources.
333+
334+ if [ -n " $ngx_rust_module_name " ]; then
335+ cat << END >> $NGX_MAKEFILE
336+ $NGX_OBJS$ngx_dirsep$ngx_rust_module_name$ngx_modext : $ngx_rust_obj
337+ END
338+ fi
313339}
0 commit comments