From fc1c03c85394b7f39d212f023943564c18f72a24 Mon Sep 17 00:00:00 2001
From: Aleksei Bavshin <a.bavshin@f5.com>
Date: Thu, 17 Jul 2025 09:05:01 -0700
Subject: [PATCH 1/2] fix(auto): rename RUST_TARGET to NGX_RUST_TARGET

This makes it more obvious that the variable is consumed by our script
and not by the Rust toolchain itself.

Also, ensure that NGX_RUST_TARGET passed from the caller has priority
over the detected value.
---
 .github/workflows/sanitizers.yaml |  4 +++-
 examples/auto/rust                | 14 ++++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/sanitizers.yaml b/.github/workflows/sanitizers.yaml
index 93f3807..a0eebd4 100644
--- a/.github/workflows/sanitizers.yaml
+++ b/.github/workflows/sanitizers.yaml
@@ -69,8 +69,10 @@ jobs:
             -fno-omit-frame-pointer
             -fsanitize=address,undefined
           LDFLAGS: -fsanitize=address,undefined
-          RUST_TARGET: x86_64-unknown-linux-gnu
           RUSTFLAGS: -Zsanitizer=address -Zexternal-clangrt
+          # Pass --target to cargo to ensure that sanitizer flags are not
+          # applied to the build scripts and proc-macros.
+          NGX_RUST_TARGET: x86_64-unknown-linux-gnu
           # Extra options passed to cargo rustc
           NGX_RUSTC_OPT: -Zbuild-std
           # Enable unstable features, such as the -Z options above,
diff --git a/examples/auto/rust b/examples/auto/rust
index d64067c..2c50e63 100644
--- a/examples/auto/rust
+++ b/examples/auto/rust
@@ -67,6 +67,12 @@
 #
 # ngx_rust_make_modules
 # ```
+#
+# The following environment variables can affect the generated Makefile:
+#
+# - NGX_CARGO sets the "cargo" binary to use, e.g. NGX_CARGO=cargo-1.82
+# - NGX_RUST_TARGET passes the --target to the Rust compiler if set
+# - NGX_RUSTC_OPT passes additional options to "cargo rustc"
 
 # Prevent duplicate invocation unless it is a newer library version
 if [ "${NGX_RUST_AUTO_VER:-0}" -ge 1 ]; then
@@ -136,7 +142,7 @@ case "$NGX_PLATFORM" in
                 RUST_LIBS="$RUST_LIBS userenv.lib"  # std::env::home_dir
                 RUST_LIBS="$RUST_LIBS dbghelp.lib"  # backtrace symbolization
 
-                RUST_TARGET=$RUST_TARGET_ARCH-pc-windows-msvc
+                NGX_RUST_TARGET=${NGX_RUST_TARGET:-$RUST_TARGET_ARCH-pc-windows-msvc}
             ;;
 
             gcc | clang)
@@ -145,7 +151,7 @@ case "$NGX_PLATFORM" in
                 RUST_LIBS="$RUST_LIBS -luserenv"
                 RUST_LIBS="$RUST_LIBS -ldbghelp"
                 # gnullvm on arm64?
-                RUST_TARGET=$RUST_TARGET_ARCH-pc-windows-gnu
+                NGX_RUST_TARGET=${NGX_RUST_TARGET:-$RUST_TARGET_ARCH-pc-windows-gnu}
             ;;
 
         esac
@@ -220,7 +226,7 @@ ngx_rust_target_path () {
         ngx_rust_obj=examples/$ngx_rust_obj
     fi
 
-    echo "${RUST_TARGET:+$RUST_TARGET/}$ngx_cargo_profile/$ngx_rust_obj"
+    echo "${NGX_RUST_TARGET:+$NGX_RUST_TARGET/}$ngx_cargo_profile/$ngx_rust_obj"
 }
 
 
@@ -304,7 +310,7 @@ $ngx_rust_obj:
 		--manifest-path "$ngx_cargo_manifest" \\
 		--no-default-features \\
 		--profile $ngx_cargo_profile \\
-		${RUST_TARGET:+--target $RUST_TARGET} \\
+		${NGX_RUST_TARGET:+--target $NGX_RUST_TARGET} \\
 		--target-dir $NGX_OBJS/$ngx_addon_id \\
 		--features "$ngx_rust_features" \\
 		$ngx_rustc_module_opt $NGX_RUSTC_OPT

From 93d66d9bb3cd4363982fb1530c03c38561f65a67 Mon Sep 17 00:00:00 2001
From: Aleksei Bavshin <a.bavshin@f5.com>
Date: Fri, 18 Jul 2025 15:09:45 -0700
Subject: [PATCH 2/2] fix(auto): add staticlib dependency to dynamic module
 targets

This allows us to avoid the LINK_DEPS workaround, which resulted in
unintentionally adding dependencies to the nginx binary target.
---
 examples/auto/rust | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/examples/auto/rust b/examples/auto/rust
index 2c50e63..109a2a2 100644
--- a/examples/auto/rust
+++ b/examples/auto/rust
@@ -250,21 +250,30 @@ ngx_rust_module () {
     ngx_module_libs_saved=$ngx_module_libs
     ngx_module_libs="$ngx_rust_obj $ngx_module_libs $RUST_LIBS"
 
-    # Module deps are usually added to the object file targets, but we don't have any
-    LINK_DEPS="$LINK_DEPS $ngx_rust_obj"
-
-    eval ${ngx_addon_id}_RUST_TARGETS=\"\$${ngx_addon_id}_RUST_TARGETS \
-                                        $ngx_rust_target_type:$ngx_rust_target_name\"
-
     if [ -n "$ngx_rust_target_features" ]; then
         eval ${ngx_addon_id}_RUST_FEATURES=\"\$${ngx_addon_id}_RUST_FEATURES \
                                              $ngx_rust_target_features\"
     fi
 
-   . auto/module
+    . auto/module
+
+    ngx_rust_target=$ngx_rust_target_type:$ngx_rust_target_name
+
+    # module deps are usually added to the object file targets, but we don't have any
+
+    if [ "$ngx_module_link" = DYNAMIC ]; then
+        # remember the dynamic module name and generate dependency later
+        ngx_rust_target=$ngx_rust_target:$ngx_module
+    else
+        # add dependency to the binary target
+        LINK_DEPS="$LINK_DEPS $ngx_rust_obj"
+    fi
+
+    eval ${ngx_addon_id}_RUST_TARGETS=\"\$${ngx_addon_id}_RUST_TARGETS \
+                                        $ngx_rust_target\"
 
-   ngx_module_deps=$ngx_module_deps_saved
-   ngx_module_libs=$ngx_module_libs_saved
+    ngx_module_deps=$ngx_module_deps_saved
+    ngx_module_libs=$ngx_module_libs_saved
 }
 
 
@@ -278,8 +287,9 @@ ngx_rust_make_modules () {
     eval ngx_rust_targets="\$${ngx_addon_id}_RUST_TARGETS"
 
     for target in $ngx_rust_targets; do
-        ngx_rust_target_type=${target%%:*}
-        ngx_rust_target_name=${target#*:}
+        IFS=':' read -r ngx_rust_target_type ngx_rust_target_name ngx_rust_module_name <<END
+$target
+END
 
         ngx_rust_make_module
     done
@@ -316,4 +326,14 @@ $ngx_rust_obj:
 		$ngx_rustc_module_opt $NGX_RUSTC_OPT
 
 END
+
+    # Ensure that the "auto"-generated dynamic module target depends on the
+    # static library. Normally this is achieved by attaching ADDON_DEPS to
+    # the module object files, but we don't have any suitable C sources.
+
+    if [ -n "$ngx_rust_module_name" ]; then
+        cat << END                                            >> $NGX_MAKEFILE
+$NGX_OBJS$ngx_dirsep$ngx_rust_module_name$ngx_modext:	$ngx_rust_obj
+END
+    fi
 }
