2018-06-13 14:17 by 轩脉刃, 阅读, 评论, 收藏, 编辑

nginx的编译过程,第一步是configure。我们使用 –help可以看到configure的很多配置。

configure的过程做的事情其实就是检测环境,然后根据环境生成Makefile,包含各种参数的c头文件等(ngx_auto_config.h/ ngx_auto_headers.h)。这个c头文件包含了所有根据当前环境配置的参数。

这里的Makefile文件指的是根目录下的Makefile, 这个是后面make命令的入口

objs文件夹里面有这几个文件:

configure过程中不仅仅是使用简单的shell命令来检测环境,还会生成一些简单的c程序,并且用编译器编译执行,获取输出来获取环境参数。只是这些c程序和编译后的文件在获取参数完成之后会删除,所以我们实际看不到这个文件的存在。

这些shell命令,c程序的一些输出和错误信息都记录在这个autoconf.err中了,实际上这个文件没有什么用。

这个obj/Makefile才是本质的构建程序和模块的过程。所有需要加载的模块和一些设置都在这边了。

我们可以看一下,它使用的编译器是cc, cc编译器在linux上就是gcc。 gcc是一个各种不同语言的编译器。gcc代表 the GNU Compiler Collection。 比如你的代码后缀是.c, 它会调用c的编译器还有linker去链接c的库。

这个就是configure出来的一个重要成果,本机环境的一些配置,比如int多少位之类的。

这个是configure出来的,判断一些header是否存在。

这个文件就告诉我们了我们这次编译nginx到底有多少个模块。

其中的一些模块是很重要的,但是我们也可以自主选择。

  1. have=NGX_SBIN_PATH value="\"$NGX_SBIN_PATH\"" . auto/define

auto/define的定义:

  1. # Copyright (C) Igor Sysoev
  2. # Copyright (C) Nginx, Inc.
  3. cat << END >> $NGX_AUTO_CONFIG_H
  4. #ifndef $have
  5. #define $have $value
  6. #endif
  7. END

结合起来的意思就是,如果我在参数里面设置了NGX_SBIN_PATH(有参数可以设置),那么我就在ngx_auto_config.h中增加这个宏定义:

  1. #ifndef NGX_SBIN_PATH
  2. #define NGX_SBIN_PATH "sbin/nginx"
  3. #endif
  1. cat << END >> $NGX_AUTO_CONFIG_H
  2. #ifndef $have
  3. #define $have 1
  4. #endif
  5. END

意思其实就是auto/define的翻版,只是如果这里的value是1

比如

  1. have=NGX_HAVE_EPOLL . auto/have

等同

  1. #ifndef NGX_HAVE_EPOLL
  2. #define NGX_HAVE_EPOLL 1
  3. #endif
  1. ngx_feature="GeoIP IPv6 support"
  2. ngx_feature_name="NGX_HAVE_GEOIP_V6"
  3. ngx_feature_run=no
  4. ngx_feature_incs="#include <stdio.h>
  5. #include <GeoIP.h>"
  6. #ngx_feature_path=
  7. #ngx_feature_libs=
  8. ngx_feature_test="printf(\"%d\", GEOIP_CITY_EDITION_REV0_V6);"
  9. . auto/feature

上面这个是个feature的模版,它的目的是为了检查当前的环境是否包含这个feature, 比如上面的例子就是判断这个机器环境是否支持GEO IPV6。

还可以看看下面这个例子:

  1. ngx_feature="GeoIP library in /opt/local/"
  2. ngx_feature_path="/opt/local/include"
  3. if [ $NGX_RPATH = YES ]; then
  4. ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -lGeoIP"
  5. else
  6. ngx_feature_libs="-L/opt/local/lib -lGeoIP"
  7. fi
  8. . auto/feature

feature的代码就不贴出来了, 具体做了下面几个事情:

  • 控制台先输出checking for
  • 在auto_config.err中输出checking for
  • 检查变量$ngx_feature_name是否有设置
  • 如果设置了ngx_feature_path,就设置ngx_feature_inc_path
  • 根据$ngx_feature_test生成测试c文件
  • 编译生成的测试c文件
  • 执行测试c文件并捕获输出到auto_config.err
  • 如果设置了ngx_feature_run, 就把输出设置为对应的$ngx_feature_name的值
  • 没有找到的情况,在auto_config.err中体现,并且在控制台显示 not found
  • 删除测试c文件及编译出来的东西
  1. ngx_include="inttypes.h"; . auto/include

生成一个简单的c文件:

  1. cat << END > $NGX_AUTOTEST.c
  2. $NGX_INCLUDE_SYS_PARAM_H
  3. #include <$ngx_include>
  4. int main(void) {
  5. return 0;
  6. }
  7. END

判断检测环境是否有这个头文件。

  1. ngx_module_type=HTTP
  2. if :; then
  3. ngx_module_name="ngx_http_module \
  4. ngx_http_core_module \
  5. ngx_http_log_module \
  6. ngx_http_upstream_module"
  7. ngx_module_incs="src/http src/http/modules"
  8. ngx_module_deps="src/http/ngx_http.h \
  9. src/http/ngx_http_request.h \
  10. src/http/ngx_http_config.h \
  11. src/http/ngx_http_core_module.h \
  12. src/http/ngx_http_cache.h \
  13. src/http/ngx_http_variables.h \
  14. src/http/ngx_http_script.h \
  15. src/http/ngx_http_upstream.h \
  16. src/http/ngx_http_upstream_round_robin.h"
  17. ngx_module_srcs="src/http/ngx_http.c \
  18. src/http/ngx_http_core_module.c \
  19. src/http/ngx_http_special_response.c \
  20. src/http/ngx_http_request.c \
  21. src/http/ngx_http_parse.c \
  22. src/http/modules/ngx_http_log_module.c \
  23. src/http/ngx_http_request_body.c \
  24. src/http/ngx_http_variables.c \
  25. src/http/ngx_http_script.c \
  26. src/http/ngx_http_upstream.c \
  27. src/http/ngx_http_upstream_round_robin.c"
  28. ngx_module_libs=
  29. ngx_module_link=YES
  30. . auto/module

这个auto/module会对每一个模块设置一些模块需要的变量,比如模块的源码地址,模块依赖的lib库等。

  1. checking for OS
  2. + Linux 3.10.0-514.16.1.el7.x86_64 x86_64
  3. checking for C compiler ... found
  4. + using GNU C compiler
  5. + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
  6. checking for gcc -pipe switch ... found
  7. checking for -Wl,-E switch ... found
  8. checking for gcc builtin atomic operations ... found
  9. checking for C99 variadic macros ... found
  10. checking for gcc variadic macros ... found
  11. checking for gcc builtin 64 bit byteswap ... found
  12. checking for unistd.h ... found
  13. checking for inttypes.h ... found
  14. checking for limits.h ... found
  15. checking for sys/filio.h ... not found
  16. checking for sys/param.h ... found
  17. checking for sys/mount.h ... found
  18. checking for sys/statvfs.h ... found
  19. checking for crypt.h ... found
  20. checking for Linux specific features
  21. checking for epoll ... found
  22. checking for EPOLLRDHUP ... found
  23. checking for EPOLLEXCLUSIVE ... not found
  24. checking for O_PATH ... not found
  25. checking for sendfile() ... found
  26. checking for sendfile64() ... found
  27. checking for sys/prctl.h ... found
  28. checking for prctl(PR_SET_DUMPABLE) ... found
  29. checking for prctl(PR_SET_KEEPCAPS) ... found
  30. checking for capabilities ... found
  31. checking for crypt_r() ... found
  32. checking for sys/vfs.h ... found
  33. checking for nobody group ... found
  34. checking for poll() ... found
  35. checking for /dev/poll ... not found
  36. checking for kqueue ... not found
  37. checking for crypt() ... not found
  38. checking for crypt() in libcrypt ... found
  39. checking for F_READAHEAD ... not found
  40. checking for posix_fadvise() ... found
  41. checking for O_DIRECT ... found
  42. checking for F_NOCACHE ... not found
  43. checking for directio() ... not found
  44. checking for statfs() ... found
  45. checking for statvfs() ... found
  46. checking for dlopen() ... not found
  47. checking for dlopen() in libdl ... found
  48. checking for sched_yield() ... found
  49. checking for sched_setaffinity() ... found
  50. checking for SO_SETFIB ... not found
  51. checking for SO_REUSEPORT ... found
  52. checking for SO_ACCEPTFILTER ... not found
  53. checking for SO_BINDANY ... not found
  54. checking for IP_TRANSPARENT ... found
  55. checking for IP_BINDANY ... not found
  56. checking for IP_BIND_ADDRESS_NO_PORT ... not found
  57. checking for IP_RECVDSTADDR ... not found
  58. checking for IP_SENDSRCADDR ... not found
  59. checking for IP_PKTINFO ... found
  60. checking for IPV6_RECVPKTINFO ... found
  61. checking for TCP_DEFER_ACCEPT ... found
  62. checking for TCP_KEEPIDLE ... found
  63. checking for TCP_FASTOPEN ... not found
  64. checking for TCP_INFO ... found
  65. checking for accept4() ... found
  66. checking for eventfd() ... found
  67. checking for int size ... 4 bytes
  68. checking for long size ... 8 bytes
  69. checking for long long size ... 8 bytes
  70. checking for void * size ... 8 bytes
  71. checking for uint32_t ... found
  72. checking for uint64_t ... found
  73. checking for sig_atomic_t ... found
  74. checking for sig_atomic_t size ... 4 bytes
  75. checking for socklen_t ... found
  76. checking for in_addr_t ... found
  77. checking for in_port_t ... found
  78. checking for rlim_t ... found
  79. checking for uintptr_t ... uintptr_t found
  80. checking for system byte ordering ... little endian
  81. checking for size_t size ... 8 bytes
  82. checking for off_t size ... 8 bytes
  83. checking for time_t size ... 8 bytes
  84. checking for AF_INET6 ... found
  85. checking for setproctitle() ... not found
  86. checking for pread() ... found
  87. checking for pwrite() ... found
  88. checking for pwritev() ... found
  89. checking for sys_nerr ... found
  90. checking for localtime_r() ... found
  91. checking for clock_gettime(CLOCK_MONOTONIC) ... not found
  92. checking for clock_gettime(CLOCK_MONOTONIC) in librt ... found
  93. checking for posix_memalign() ... found
  94. checking for memalign() ... found
  95. checking for mmap(MAP_ANON|MAP_SHARED) ... found
  96. checking for mmap("/dev/zero", MAP_SHARED) ... found
  97. checking for System V shared memory ... found
  98. checking for POSIX semaphores ... not found
  99. checking for POSIX semaphores in libpthread ... found
  100. checking for struct msghdr.msg_control ... found
  101. checking for ioctl(FIONBIO) ... found
  102. checking for struct tm.tm_gmtoff ... found
  103. checking for struct dirent.d_namlen ... not found
  104. checking for struct dirent.d_type ... found
  105. checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
  106. checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
  107. checking for openat(), fstatat() ... found
  108. checking for getaddrinfo() ... found
  109. checking for zlib library ... found
  110. creating objs/Makefile
  111. Configuration summary
  112. + PCRE library is not used
  113. + OpenSSL library is not used
  114. + using system zlib library
  115. nginx path prefix: "/usr/local/nginx"
  116. nginx binary file: "/usr/local/nginx/sbin/nginx"
  117. nginx modules path: "/usr/local/nginx/modules"
  118. nginx configuration prefix: "/usr/local/nginx/conf"
  119. nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  120. nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  121. nginx error log file: "/usr/local/nginx/logs/error.log"
  122. nginx http access log file: "/usr/local/nginx/logs/access.log"
  123. nginx http client request body temporary files: "client_body_temp"
  124. nginx http proxy temporary files: "proxy_temp"
  125. nginx http fastcgi temporary files: "fastcgi_temp"
  126. nginx http uwsgi temporary files: "uwsgi_temp"
  127. nginx http scgi temporary files: "scgi_temp

我们其实可以对着 xmind 的流程和 configure 这个程序来看就很清晰了

https://github.com/its-tech/nginx-1.14.0-research/blob/master/configure

再加上前面几个configure语法就能看懂了。

https://blog.csdn.net/fzy0201/article/details/17683883

版权声明:本文为yjf512原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yjf512/p/9177562.html