为了研究nginx源码,笔者想编译调试一下nginx源码,由于nginx是使用configure脚本方式生成Makefile来编译的,MinGW中理论上也是可以的,但是直接在MinGW中编译nginx源码会出现找不到PCRE库以及zlib库的问题:


实际上MinGW中是有安装这两个库的:


在Linux下如果安装了这两个库,在运行auto/configure时,是完全没问题的,能够正确找到这两个库:

笔者经过研究,发现在查找PCRE库以及zlib库的脚本中都只对非win32平台进行了检测,win32平台没做检测。可以参见目前最新稳定版本nginx 1.26的源码,PCRE配置以及zlib配置。
只需要把条件判定去掉即可。
1. PCRE配置

2. zlib配置

3. OpenSSL配置

3. libxslt配置

75行处添加:
if [ $ngx_found = no ]; then
# MinGW64
ngx_feature="libxslt in /mingw64"
ngx_feature_path="/mingw64/include/libxml2"
ngx_feature_libs="-L/opt/local/lib -lxml2 -lxslt"
. auto/feature
fi
168行处添加:
if [ $ngx_found = no ]; then
# MinGW64
ngx_feature="libexslt in /mingw64"
ngx_feature_path="/mingw64/include/libxml2"
ngx_feature_libs="-lexslt"
. auto/feature
fi
再运行:
auto/configure --prefix= --with-cc=clang

如果要启用https,需要添加参数:--with-http_ssl_module,如果要调试添加参数:--with-debug。

生成好Makefile之后,就可以编译了:
make -j8
可以使用VSCode进行调试,以下是笔者的launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "(lldb) 启动",
"program": "${workspaceFolder}/objs/nginx.exe",
"args": [],
"cwd": "${workspaceFolder}",
"env": {
"PATH": "G:\\msys64\\mingw64\\bin"
}
},
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/objs/nginx.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "G:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"windows": {
"environment": [
{
"name": "PATH",
"value": "G:\\msys64\\mingw64\\bin"
}
]
}
}
]
}
需要注意的是,由于nginx默认是使用的多进程模式,如果直接调试,将无法调试工作进程的情况,为此需要在调试时改为单进程的运行方式,修改nginx.c:1143为:
ngx_conf_init_value(ccf->daemon, 0)
ngx_conf_init_value(ccf->master, 0);
重新编译后,即可使用GDB或者LLDB调试了:

目前虽然可以调试了,但是看代码,是没有intellisense的,也就不能正常代码跳转。所以最好是改为能够使用CMake构建系统,这样就可以使用clangd分析代码,进行代码跳转了。笔者根据源码中的脚本,写了一份可以在MinGW以及ubuntu 22.04中使用的CMakeLists.txt。
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(nginx)
set(ShareModules
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module
--with-http_geoip_module
--with-http_dav_module
--with-http_stub_status_module
--with-http_image_filter_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_ssl_module
)
if(MINGW)
execute_process(COMMAND sh auto/configure
--prefix=
--with-cc=clang
--with-debug
${ShareModules}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
else()
execute_process(COMMAND auto/configure
--with-cc=clang
--with-debug
${ShareModules}
--with-http_degradation_module
--with-threads
--with-file-aio
--with-http_v3_module
--with-google_perftools_module
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
if(WIN32)
if(MSVC)
message(FATAL_ERROR "WIN32下不支持使用MSVC编译,仅支持MinGW或者Cygwin环境下使用gcc或者clang编译器编译")
endif()
endif()
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/objs/ngx_auto_config.h)
message(STATUS "不存在ngx_auto_config.h,需要先执行`auto/configure`脚本")
endif()
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/objs/ngx_auto_headers.h)
message(STATUS "ngx_auto_headers.h,需要先执行`auto/configure`脚本")
endif()
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/objs/ngx_modules.c)
message(STATUS "不存在ngx_modules.c,需要先执行`auto/configure`脚本")
endif()
aux_source_directory(src/core SRC_LIST)
aux_source_directory(src/event SRC_LIST)
aux_source_directory(src/http SRC_LIST)
aux_source_directory(src/http/v2 SRC_LIST)
aux_source_directory(src/http/modules SRC_LIST)
aux_source_directory(src/mail SRC_LIST)
aux_source_directory(src/misc SRC_LIST)
aux_source_directory(src/stream SRC_LIST)
if(WIN32)
aux_source_directory(src/os/win32 SRC_LIST)
else()
aux_source_directory(src/os/unix SRC_LIST)
endif()
list(APPEND SRC_LIST objs/ngx_modules.c)
if(WIN32)
list(APPEND SRC_LIST src/event/modules/ngx_win32_poll_module.c)
list(APPEND SRC_LIST src/event/modules/ngx_iocp_module.c)
list(APPEND SRC_LIST src/event/modules/ngx_win32_select_module.c)
list(REMOVE_ITEM SRC_LIST src/core/ngx_bpf.c)
# 不支持Windows
list(REMOVE_ITEM SRC_LIST src/core/ngx_thread_pool.c)
# 代码有错误
list(REMOVE_ITEM SRC_LIST src/os/win32/ngx_service.c)
# 代码有错误
list(REMOVE_ITEM SRC_LIST src/event/ngx_event_connectex.c)
#MinGW没sbrk函数
list(REMOVE_ITEM SRC_LIST src/http/modules/ngx_http_degradation_module.c)
elseif(LINUX)
aux_source_directory(src/http/v3 SRC_LIST)
aux_source_directory(src/event/quic SRC_LIST)
list(APPEND SRC_LIST src/event/modules/ngx_epoll_module.c)
list(REMOVE_ITEM SRC_LIST src/event/ngx_event_connectex.c)
list(REMOVE_ITEM SRC_LIST src/event/ngx_event_acceptex.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_darwin_init.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_darwin_sendfile_chain.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_freebsd_init.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_freebsd_sendfile_chain.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_solaris_init.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_solaris_sendfilev_chain.c)
list(REMOVE_ITEM SRC_LIST src/os/unix/ngx_file_aio_read.c)
endif()
add_executable(${PROJECT_NAME} ${SRC_LIST})
include_directories(
src/core
src/event
src/event/modules
src/http
src/http/modules
src/http/v2
src/http/v3
src/event/quic
src/mail
src/stream
objs
)
if(WIN32)
include_directories(src/os/win32)
else()
include_directories(src/os/unix)
endif()
find_package(LibXml2 REQUIRED)
find_package(LibXslt REQUIRED)
target_include_directories(${PROJECT_NAME} PUBLIC ${LIBXML2_INCLUDE_DIRS} ${LIBXSLT_INCLUDE_DIRS})
if(WIN32)
target_link_libraries(${PROJECT_NAME} PUBLIC advapi32 ws2_32)
else()
target_link_libraries(${PROJECT_NAME} PUBLIC crypt profiler)
endif()
target_link_libraries(${PROJECT_NAME} PUBLIC pcre crypto z pcre2-8 ssl gd GeoIP ${LIBXML2_LIBRARIES} ${LIBXSLT_LIBRARIES} ${LIBXSLT_EXSLT_LIBRARIES})
MinGW需要安装的一些库(不一定全,需要自行安装一些库):
pacman -S mingw-w64-x86_64-geoip mingw-w64-x86_64-libgd
ubuntu也需要安装的一些库(不一定全,需要自行安装一些库):
sudo apt install libxslt1-dev libxml2-dev libgeoip-dev libgd-dev libgoogle-perftools-dev
当然,也可以使用辅助工具bear或者compiledb来生成compile_commands.json:
bear
ubuntu下使用sudo apt install bear安装bear工具。
在bear 3.0之前使用
bear make
在3.0之后使用
bear --make
就可以生成compile_commands.json
compiledb
compiledb是一个使用python开发的工具,所以需要使用
pip install compiledb
来安装。
直接使用:
compiledb make
就可以生成compile_commands.json
bear目前在MinGW中还没有安装包,貌似也暂时不支持MinGW,所以需要在Linux下使用。而compiledb是只要有python环境即可。
如果对你有帮助,欢迎点赞收藏!

1944

被折叠的 条评论
为什么被折叠?



