Hello!

I'm sure many developers have faced issues with manual dependency handling
in the nginx build system. I've tried several workarounds which were
semi-acceptable for personal use, but definitely not something that you
would offer other people to use.

Then I thought that it would be easy to add objs/Makefile -> CMakeList.txt
converted, because makefile is strictly-formatted, but what if we add a
primitive CMakeLists.txt generator to auto/make?

I've made a quick and dirty prototype and it was quite successful (for *nix
environment as it relies on sed-ing compiler/linker flags).

I'm attaching a small patch of that proof-of-concept attempt (based on v
1.17.10). And I just want to ask if the community is interested in having
some kind of cmake support. The advantages for developers are:
- automatic dependency handling
- easier integration with clang-based tools (cmake can generate
compile_commands.json on its own rather that using tools such as
https://github.com/rizsotto/Bear)
- cmake can target not only makefiles (for example Visual Studio, if anyone
uses it)
- it's easier to use C++ with cmake as it inherently has an ability to use
different set of compiler flags for C and C++ compilers

-- 
Regards,
Dmitry
diff --git a/auto/make b/auto/make
index 34c40cdd5..fc319db1e 100644
--- a/auto/make
+++ b/auto/make
@@ -18,6 +18,7 @@ ngx_objs_dir=$NGX_OBJS$ngx_regex_dirsep
 ngx_use_pch=`echo $NGX_USE_PCH | sed -e "s/\//$ngx_regex_dirsep/g"`
 
 
+cmake_CFLAGS="$CFLAGS"
 cat << END                                                     > $NGX_MAKEFILE
 
 CC =	$CC
@@ -37,6 +38,8 @@ fi
 
 # ALL_INCS, required by the addons and by OpenWatcom C precompiled headers
 
+cmake_ngx_incs="$CORE_INCS $NGX_OBJS $HTTP_INCS $MAIL_INCS $STREAM_INCS"
+
 ngx_incs=`echo $CORE_INCS $NGX_OBJS $HTTP_INCS $MAIL_INCS $STREAM_INCS\
     | sed -e "s/  *\([^ ][^ ]*\)/$ngx_regex_cont$ngx_include_opt\1/g" \
           -e "s/\//$ngx_regex_dirsep/g"`
@@ -222,6 +225,7 @@ ngx_main_link=${MAIN_LINK:+`echo $MAIN_LINK \
     | sed -e "s/\//$ngx_regex_dirsep/g" -e "s/^/$ngx_long_regex_cont/"`}
 
 
+cmake_LDOPTS="$ngx_libs$ngx_link$ngx_main_link"
 cat << END                                                    >> $NGX_MAKEFILE
 
 build:	binary modules manpage
@@ -255,6 +259,7 @@ END
 
 # the core sources
 
+cmake_SRCS="$CORE_SRCS"
 for ngx_src in $CORE_SRCS
 do
     ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
@@ -286,6 +291,7 @@ if [ $HTTP = YES ]; then
         ngx_perl_cc="$ngx_perl_cc \$(CORE_INCS) \$(HTTP_INCS)"
     fi
 
+    cmake_SRCS="$cmake_SRCS $HTTP_SRCS"
     for ngx_source in $HTTP_SRCS
     do
         ngx_src=`echo $ngx_source | sed -e "s/\//$ngx_regex_dirsep/g"`
@@ -328,6 +334,7 @@ if [ $MAIL = YES ]; then
         ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS) \$(MAIL_INCS)"
     fi
 
+      cmake_SRCS="$cmake_SRCS $mail_SRCS"
     for ngx_src in $MAIL_SRCS
     do
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
@@ -358,6 +365,7 @@ if [ $STREAM = YES ]; then
         ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) \$(CORE_INCS) \$(STREAM_INCS)"
     fi
 
+    cmake_SRCS="$cmake_SRCS $STREAM_SRCS"
     for ngx_src in $STREAM_SRCS
     do
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
@@ -384,6 +392,7 @@ if test -n "$MISC_SRCS"; then
 
     ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
 
+    cmake_SRCS="$cmake_SRCS $MISC_SRCS"
     for ngx_src in $MISC_SRCS
     do
         ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
@@ -410,6 +419,7 @@ if test -n "$NGX_ADDON_SRCS"; then
 
     ngx_cc="\$(CC) $ngx_compile_opt \$(CFLAGS) $ngx_use_pch \$(ALL_INCS)"
 
+    cmake_SRCS="$cmake_SRCS $NGX_ADDON_SRCS"
     for ngx_src in $NGX_ADDON_SRCS
     do
         ngx_obj="addon/`basename \`dirname $ngx_src\``"
@@ -670,3 +680,42 @@ END
         fi
     done
 done
+
+
+############################################
+# Quick'n'dirty CMakeListst.txt generation #
+############################################
+cat << EOF > CMakeLists.txt
+cmake_minimum_required (VERSION 2.8)
+project (nginx)
+
+find_program(CCACHE_FOUND ccache)
+if(CCACHE_FOUND)
+  set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
+endif(CCACHE_FOUND)
+
+
+include_directories($cmake_ngx_incs $(echo $cmake_CFLAGS | tr ' ' '\n' | grep -v '^[[:space:]]*$' | sed -ne 's/^-I//p'))
+link_directories($(echo $cmake_LDOPTS | tr ' ' '\n' | sed -nre 's/-L[[:space:]]*([^[:space:]]+)/\1/p'))
+set (CMAKE_C_FLAGS "\${CMAKE_C_FLAGS} $(echo $CFLAGS | sed -e 's/"/\\"/g' -e 's/-O[0-9]*//' -e 's/-I[^[:space:]]*//' -e 's/-Werror[^[:space:]]*//')")
+set (SRCS
+  $ngx_modules_c
+EOF
+
+# This enforces shell expansion on patterns that occur in sources list
+echo $cmake_SRCS | while read s; do
+	echo $s
+done >> CMakeLists.txt
+
+cat << EOF >> CMakeLists.txt
+)
+add_executable (nginx
+	\${SRCS}
+)
+
+target_link_libraries(nginx
+  $(echo $cmake_LDOPTS | sed -e 's/\\//g' | tr ' ' '\n' | sed -ne 's@^[^-].*\.a@\${CMAKE_SOURCE_DIR}/\0@p' -e 's/^-l//p' )
+)
+EOF
+
+set | egrep ngx_http_stub_status_module.c > /tmp/env
_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-le...@nginx.org

Reply via email to