[nginx] Dynamic modules.

Maxim Dounin mdounin at mdounin.ru
Thu Feb 4 18:40:42 UTC 2016


details:   http://hg.nginx.org/nginx/rev/85dea406e18f
branches:  
changeset: 6383:85dea406e18f
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Thu Feb 04 20:25:29 2016 +0300
description:
Dynamic modules.

The auto/module script is extended to understand ngx_module_link=DYNAMIC.
When set, it links the module as a shared object rather than statically
into nginx binary.  The module can later be loaded using the "load_module"
directive.

New auto/module parameter ngx_module_order allows to define module loading
order in complex cases.  By default the order is set based on ngx_module_type.

3rd party modules can be compiled dynamically using the --add-dynamic-module
configure option, which will preset ngx_module_link to "DYNAMIC" before
calling the module config script.

Win32 support is rudimentary, and only works when using MinGW gcc (which
is able to handle exports/imports automatically).

In collaboration with Ruslan Ermilov.

diffstat:

 auto/cc/conf          |   22 +++
 auto/cc/msvc          |    6 +
 auto/cc/sunc          |    3 +
 auto/init             |    1 +
 auto/install          |   24 +++
 auto/lib/conf         |    6 +-
 auto/lib/geoip/conf   |    7 +-
 auto/lib/libgd/conf   |    7 +-
 auto/lib/libxslt/conf |   13 +-
 auto/make             |  203 ++++++++++++++++++++++++++++++++-
 auto/module           |   53 ++++++++-
 auto/modules          |  127 ++++++++++++++++++-
 auto/options          |   26 ++++-
 auto/os/darwin        |    3 +
 auto/os/win32         |    5 +
 auto/summary          |    1 +
 src/core/nginx.c      |  109 +++++++++++++++++
 src/core/ngx_cycle.c  |    5 +-
 src/core/ngx_cycle.h  |    2 +
 src/core/ngx_module.c |  308 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/core/ngx_module.h |  239 ++++++++++++++++++++++++++++++++++++++-
 21 files changed, 1136 insertions(+), 34 deletions(-)

diffs (truncated from 1720 to 300 lines):

diff --git a/auto/cc/conf b/auto/cc/conf
--- a/auto/cc/conf
+++ b/auto/cc/conf
@@ -5,12 +5,17 @@
 
 LINK="\$(CC)"
 
+MAIN_LINK=
+MODULE_LINK="-shared"
+
 ngx_include_opt="-I "
 ngx_compile_opt="-c"
+ngx_pic_opt="-fPIC"
 ngx_objout="-o "
 ngx_binout="-o "
 ngx_objext="o"
 ngx_binext=
+ngx_modext=".so"
 
 ngx_long_start=
 ngx_long_end=
@@ -45,6 +50,9 @@ if test -n "$CFLAGS"; then
 
         sunc)
 
+            MAIN_LINK=
+            MODULE_LINK="-G"
+
             case "$NGX_MACHINE" in
 
                 i86pc)
@@ -156,6 +164,20 @@ if [ "$NGX_PLATFORM" != win32 ]; then
     fi
 
 
+    ngx_feature="-Wl,-E switch"
+    ngx_feature_name=
+    ngx_feature_run=no
+    ngx_feature_incs=
+    ngx_feature_path=
+    ngx_feature_libs=-Wl,-E
+    ngx_feature_test=
+    . auto/feature
+
+    if [ $ngx_found = yes ]; then
+        MAIN_LINK="-Wl,-E"
+    fi
+
+
     ngx_feature="gcc builtin atomic operations"
     ngx_feature_name=NGX_HAVE_GCC_ATOMIC
     ngx_feature_run=yes
diff --git a/auto/cc/msvc b/auto/cc/msvc
--- a/auto/cc/msvc
+++ b/auto/cc/msvc
@@ -118,6 +118,12 @@ NGX_RCC="rc -fo$NGX_RES \$(CORE_INCS) $N
 CORE_LINK="$NGX_RES $CORE_LINK"
 
 
+# dynamic modules
+#MAIN_LINK="-link -def:$NGX_OBJS/nginx.def"
+#MODULE_LINK="-LD $NGX_OBJS/nginx.lib"
+
+
+ngx_pic_opt=
 ngx_objout="-Fo"
 ngx_binout="-Fe"
 ngx_objext="obj"
diff --git a/auto/cc/sunc b/auto/cc/sunc
--- a/auto/cc/sunc
+++ b/auto/cc/sunc
@@ -57,6 +57,9 @@ case "$NGX_MACHINE" in
 
 esac
 
+MAIN_LINK=
+MODULE_LINK="-G"
+
 
 # optimizations
 
diff --git a/auto/init b/auto/init
--- a/auto/init
+++ b/auto/init
@@ -5,6 +5,7 @@
 
 NGX_MAKEFILE=$NGX_OBJS/Makefile
 NGX_MODULES_C=$NGX_OBJS/ngx_modules.c
+NGX_MODULES=
 
 NGX_AUTO_HEADERS_H=$NGX_OBJS/ngx_auto_headers.h
 NGX_AUTO_CONFIG_H=$NGX_OBJS/ngx_auto_config.h
diff --git a/auto/install b/auto/install
--- a/auto/install
+++ b/auto/install
@@ -26,6 +26,18 @@ case ".$NGX_SBIN_PATH" in
 esac
 
 
+case ".$NGX_MODULES_PATH" in
+    ./*)
+    ;;
+
+    *)
+        NGX_MODULES_PATH=$NGX_PREFIX/$NGX_MODULES_PATH
+    ;;
+esac
+
+NGX_MODULES_PATH=`dirname $NGX_MODULES_PATH/.`
+
+
 case ".$NGX_CONF_PATH" in
     ./*)
     ;;
@@ -158,12 +170,24 @@ END
 fi
 
 
+if test -n "$NGX_MODULES"; then
+    cat << END                                                >> $NGX_MAKEFILE
+
+	test -d '\$(DESTDIR)$NGX_MODULES_PATH' \
+		|| mkdir -p '\$(DESTDIR)$NGX_MODULES_PATH'
+	cp $NGX_MODULES '\$(DESTDIR)$NGX_MODULES_PATH'
+END
+
+fi
+
+
 # create Makefile
 
 cat << END >> Makefile
 
 build:
 	\$(MAKE) -f $NGX_MAKEFILE
+	\$(MAKE) -f $NGX_MAKEFILE modules
 	\$(MAKE) -f $NGX_MAKEFILE manpage
 
 install:
diff --git a/auto/lib/conf b/auto/lib/conf
--- a/auto/lib/conf
+++ b/auto/lib/conf
@@ -58,11 +58,11 @@ if [ $USE_ZLIB = YES ]; then
     . auto/lib/zlib/conf
 fi
 
-if [ $USE_LIBXSLT = YES ]; then
+if [ $USE_LIBXSLT != NO ]; then
     . auto/lib/libxslt/conf
 fi
 
-if [ $USE_LIBGD = YES ]; then
+if [ $USE_LIBGD != NO ]; then
     . auto/lib/libgd/conf
 fi
 
@@ -70,7 +70,7 @@ if [ $USE_PERL = YES ]; then
     . auto/lib/perl/conf
 fi
 
-if [ $HTTP_GEOIP = YES ]; then
+if [ $USE_GEOIP != NO ]; then
     . auto/lib/geoip/conf
 fi
 
diff --git a/auto/lib/geoip/conf b/auto/lib/geoip/conf
--- a/auto/lib/geoip/conf
+++ b/auto/lib/geoip/conf
@@ -67,7 +67,12 @@ fi
 if [ $ngx_found = yes ]; then
 
     CORE_INCS="$CORE_INCS $ngx_feature_path"
-    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+
+    if [ $USE_GEOIP = YES ]; then
+        CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+    fi
+
+    NGX_LIB_GEOIP=$ngx_feature_libs
 
     if [ $NGX_IPV6 = YES ]; then
         ngx_feature="GeoIP IPv6 support"
diff --git a/auto/lib/libgd/conf b/auto/lib/libgd/conf
--- a/auto/lib/libgd/conf
+++ b/auto/lib/libgd/conf
@@ -67,7 +67,12 @@ fi
 if [ $ngx_found = yes ]; then
 
     CORE_INCS="$CORE_INCS $ngx_feature_path"
-    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+
+    if [ $USE_LIBGD = YES ]; then
+        CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+    fi
+
+    NGX_LIB_LIBGD=$ngx_feature_libs
 
 else
 
diff --git a/auto/lib/libxslt/conf b/auto/lib/libxslt/conf
--- a/auto/lib/libxslt/conf
+++ b/auto/lib/libxslt/conf
@@ -76,7 +76,12 @@ fi
 if [ $ngx_found = yes ]; then
 
     CORE_INCS="$CORE_INCS $ngx_feature_path"
-    CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+
+    if [ $USE_LIBXSLT = YES ]; then
+        CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
+    fi
+
+    NGX_LIB_LIBXSLT=$ngx_feature_libs
 
 else
 
@@ -152,5 +157,9 @@ fi
 
 
 if [ $ngx_found = yes ]; then
-    CORE_LIBS="$CORE_LIBS -lexslt"
+    if [ $USE_LIBXSLT = YES ]; then
+        CORE_LIBS="$CORE_LIBS -lexslt"
+    fi
+
+    NGX_LIB_LIBXSLT="$NGX_LIB_LIBXSLT -lexslt"
 fi
diff --git a/auto/make b/auto/make
--- a/auto/make
+++ b/auto/make
@@ -98,9 +98,11 @@ fi
 
 # the mail dependencies and include paths
 
-if [ $MAIL = YES ]; then
+if [ $MAIL != NO ]; then
 
-    ngx_all_srcs="$ngx_all_srcs $MAIL_SRCS"
+    if [ $MAIL = YES ]; then
+        ngx_all_srcs="$ngx_all_srcs $MAIL_SRCS"
+    fi
 
     ngx_deps=`echo $MAIL_DEPS \
         | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
@@ -124,9 +126,11 @@ fi
 
 # the stream dependencies and include paths
 
-if [ $STREAM = YES ]; then
+if [ $STREAM != NO ]; then
 
-    ngx_all_srcs="$ngx_all_srcs $STREAM_SRCS"
+    if [ $STREAM = YES ]; then
+        ngx_all_srcs="$ngx_all_srcs $STREAM_SRCS"
+    fi
 
     ngx_deps=`echo $STREAM_DEPS \
         | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont\1/g" \
@@ -204,6 +208,7 @@ ngx_objs=`echo $ngx_all_objs $ngx_module
     | sed -e "s/  *\([^ ][^ ]*\)/$ngx_long_regex_cont\1/g" \
           -e "s/\//$ngx_regex_dirsep/g"`
 
+ngx_libs=
 if test -n "$NGX_LD_OPT$CORE_LIBS"; then
     ngx_libs=`echo $NGX_LD_OPT $CORE_LIBS \
         | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_long_regex_cont/"`
@@ -212,13 +217,18 @@ fi
 ngx_link=${CORE_LINK:+`echo $CORE_LINK \
     | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_long_regex_cont/"`}
 
+ngx_main_link=${MAIN_LINK:+`echo $MAIN_LINK \
+    | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_long_regex_cont/"`}
+
 
 cat << END                                                    >> $NGX_MAKEFILE
 
 $NGX_OBJS${ngx_dirsep}nginx${ngx_binext}:	$ngx_deps$ngx_spacer
-	\$(LINK) ${ngx_long_start}${ngx_binout}$NGX_OBJS${ngx_dirsep}nginx$ngx_long_cont$ngx_objs$ngx_libs$ngx_link
+	\$(LINK) ${ngx_long_start}${ngx_binout}$NGX_OBJS${ngx_dirsep}nginx$ngx_long_cont$ngx_objs$ngx_libs$ngx_link$ngx_main_link
 	$ngx_rcc
 ${ngx_long_end}
+
+modules:
 END
 
 
@@ -472,3 +482,186 @@ if test -n "$NGX_PCH"; then
 END
 
 fi
+
+
+# dynamic modules
+
+if test -n "$NGX_PCH"; then
+    ngx_cc="\$(CC) $ngx_compile_opt $ngx_pic_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
+else
+    ngx_cc="\$(CC) $ngx_compile_opt $ngx_pic_opt \$(CFLAGS) \$(ALL_INCS)"
+fi
+



More information about the nginx-devel mailing list