1.configure生成

再configure最后加上

curDir=$(cd $(dirname $0); pwd);
projectName=`echo "$curDir" | sed -e "s/\/auto$//" -e "s/.*\///"`
echo $projectName;
CmakeListFile=$NGX_OBJS/CmakeLists.txt
cat<<END >$CmakeListFile
cmake_minimum_required(VERSION 3.28)
set(projectName "$projectName")
project(\${projectName} VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 26)
include_directories(
END
ngx_incs=`echo $CORE_INCS $NGX_OBJS $HTTP_INCS $MAIL_INCS $STREAM_INCS\
    | sed -e "s/  *\([^ ][^ ]*\)/\n\1/g" \
          -e "s/\//$ngx_regex_dirsep/g"`
cat << END                                                    >> $CmakeListFile
$ngx_incs
)
set(    SRC
END
cat << END                                                    >> $CmakeListFile
$ngx_modules_c
END
for ngx_src in $CORE_SRCS
do
    ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
    cat << END                                                >> $CmakeListFile
$ngx_src
END
done

if [ $HTTP = YES ]; then
    for ngx_source in $HTTP_SRCS
    do
        ngx_src=`echo $ngx_source | sed -e "s/\//$ngx_regex_dirsep/g"`
            cat << END                                        >> $CmakeListFile
$ngx_src
END
    done
fi

if [ $MAIL = YES ]; then
    for ngx_src in $MAIL_SRCS
    do
        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
        cat << END                                            >> $CmakeListFile
$ngx_src
END
    done
fi

if [ $STREAM = YES ]; then
    for ngx_src in $STREAM_SRCS
    do
        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
        cat << END                                            >> $CmakeListFile
$ngx_src
END
    done
fi

if test -n "$MISC_SRCS"; then
    for ngx_src in $MISC_SRCS
    do
        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
        cat << END                                            >> $CmakeListFile
$ngx_src
END
    done
fi

if test -n "$NGX_ADDON_SRCS"; then
    for ngx_src in $NGX_ADDON_SRCS
    do
        ngx_src=`echo $ngx_src | sed -e "s/\//$ngx_regex_dirsep/g"`
        cat << END                                            >> $CmakeListFile
$ngx_src
END
    done
fi
cat << END                                                    >> $CmakeListFile
)
add_executable(\${projectName} \${SRC})
target_link_libraries(\${projectName}
END
linkLibs=$(echo "$ngx_libs" | sed -e ':a' \
                                   -e '/\\$/N; s/\\\n[[:space:]]*//; ta' \
                                   -e 's/ -l/\n/g')
cat << END                                                    >> $CmakeListFile
$linkLibs
)
END

2.从Makefile读取

nginx configure出来的makefile是人读的,不像cmake生成的那种拼尽全力无法理解的,根据makefile,把那些编译的.c文件添加到cmakelists里就好了

下面是提取.c文件的程序

#include <iostream>
#include <utility>
#include <vector>
#include <queue>
#include <iterator>
#include <sstream>
#include <algorithm>
#include <ranges>
#include <cmath>
#include <set>
#include <numeric>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <bitset>
#include <stack>
#include <list>
#include <numeric>
#include <iomanip>
#include <cassert>
#include <fstream>

//#define int long long
enum class ProgressTypes{
    module,manpage,idle
};
signed main(){
//path to nginx makefile
    std::ifstream ifs(R"(/mnt/e/wsl/nginx/objs/Makefile)",std::ios::in);
    if  (!ifs.is_open()){
        std::cout<<"readFail\n";
        return 2;
    }
    std::vector<std::string> sources;
    std::string buf;
    ProgressTypes progress=ProgressTypes::idle;
    int block=-100;
    while (std::getline(ifs,buf)){
        if (buf.starts_with("modules:")){
            progress=ProgressTypes::module;
        }else if (buf.starts_with("manpage:")){
            progress=ProgressTypes::manpage;
        }
        switch (progress) {
            case ProgressTypes::module:
                if (buf.starts_with("objs/")){
                    block=0;
                }else{
                    block++;
                }
                if (block==1){
                    auto pb=buf.find_first_not_of('\t');
                    sources.emplace_back(buf.substr(pb));
                }
                break;
            case ProgressTypes::manpage:
                break;
            default:
                ;
        }
    }
    ifs.close();
    std::ofstream ofs("./out.txt");
    if (!ofs.is_open()){
        std::cout<<"writeFail\n";
        return 2;
    }
    ofs<<"set(\tSRC";
    for(auto&e:sources){
        ofs<<"\n\t\t${NGINX_SOURCES}/"<<e;
    }
    ofs<<"\n)";
    return 0;
}

下面是完整的cmakelists

cmake_minimum_required(VERSION 3.28.3)
project(nginx01)

set(CMAKE_CXX_STANDARD 26)
#path to nginx sources
set(NGINX_SOURCES "/mnt/e/wsl/nginx")
set(	SRC
        ${NGINX_SOURCES}/objs/ngx_modules.c
        ${NGINX_SOURCES}/src/core/nginx.c
        ${NGINX_SOURCES}/src/core/ngx_log.c
        ${NGINX_SOURCES}/src/core/ngx_palloc.c
        ${NGINX_SOURCES}/src/core/ngx_array.c
        ${NGINX_SOURCES}/src/core/ngx_list.c
        ${NGINX_SOURCES}/src/core/ngx_hash.c
        ${NGINX_SOURCES}/src/core/ngx_buf.c
        ${NGINX_SOURCES}/src/core/ngx_queue.c
        ${NGINX_SOURCES}/src/core/ngx_output_chain.c
        ${NGINX_SOURCES}/src/core/ngx_string.c
        ${NGINX_SOURCES}/src/core/ngx_parse.c
        ${NGINX_SOURCES}/src/core/ngx_parse_time.c
        ${NGINX_SOURCES}/src/core/ngx_inet.c
        ${NGINX_SOURCES}/src/core/ngx_file.c
        ${NGINX_SOURCES}/src/core/ngx_crc32.c
        ${NGINX_SOURCES}/src/core/ngx_murmurhash.c
        ${NGINX_SOURCES}/src/core/ngx_md5.c
        ${NGINX_SOURCES}/src/core/ngx_sha1.c
        ${NGINX_SOURCES}/src/core/ngx_rbtree.c
        ${NGINX_SOURCES}/src/core/ngx_radix_tree.c
        ${NGINX_SOURCES}/src/core/ngx_slab.c
        ${NGINX_SOURCES}/src/core/ngx_times.c
        ${NGINX_SOURCES}/src/core/ngx_shmtx.c
        ${NGINX_SOURCES}/src/core/ngx_connection.c
        ${NGINX_SOURCES}/src/core/ngx_cycle.c
        ${NGINX_SOURCES}/src/core/ngx_spinlock.c
        ${NGINX_SOURCES}/src/core/ngx_rwlock.c
        ${NGINX_SOURCES}/src/core/ngx_cpuinfo.c
        ${NGINX_SOURCES}/src/core/ngx_conf_file.c
        ${NGINX_SOURCES}/src/core/ngx_module.c
        ${NGINX_SOURCES}/src/core/ngx_resolver.c
        ${NGINX_SOURCES}/src/core/ngx_open_file_cache.c
        ${NGINX_SOURCES}/src/core/ngx_crypt.c
        ${NGINX_SOURCES}/src/core/ngx_proxy_protocol.c
        ${NGINX_SOURCES}/src/core/ngx_syslog.c
        ${NGINX_SOURCES}/src/event/ngx_event.c
        ${NGINX_SOURCES}/src/event/ngx_event_timer.c
        ${NGINX_SOURCES}/src/event/ngx_event_posted.c
        ${NGINX_SOURCES}/src/event/ngx_event_accept.c
        ${NGINX_SOURCES}/src/event/ngx_event_udp.c
        ${NGINX_SOURCES}/src/event/ngx_event_connect.c
        ${NGINX_SOURCES}/src/event/ngx_event_pipe.c
        ${NGINX_SOURCES}/src/os/unix/ngx_time.c
        ${NGINX_SOURCES}/src/os/unix/ngx_errno.c
        ${NGINX_SOURCES}/src/os/unix/ngx_alloc.c
        ${NGINX_SOURCES}/src/os/unix/ngx_files.c
        ${NGINX_SOURCES}/src/os/unix/ngx_socket.c
        ${NGINX_SOURCES}/src/os/unix/ngx_recv.c
        ${NGINX_SOURCES}/src/os/unix/ngx_readv_chain.c
        ${NGINX_SOURCES}/src/os/unix/ngx_udp_recv.c
        ${NGINX_SOURCES}/src/os/unix/ngx_send.c
        ${NGINX_SOURCES}/src/os/unix/ngx_writev_chain.c
        ${NGINX_SOURCES}/src/os/unix/ngx_udp_send.c
        ${NGINX_SOURCES}/src/os/unix/ngx_udp_sendmsg_chain.c
        ${NGINX_SOURCES}/src/os/unix/ngx_channel.c
        ${NGINX_SOURCES}/src/os/unix/ngx_shmem.c
        ${NGINX_SOURCES}/src/os/unix/ngx_process.c
        ${NGINX_SOURCES}/src/os/unix/ngx_daemon.c
        ${NGINX_SOURCES}/src/os/unix/ngx_setaffinity.c
        ${NGINX_SOURCES}/src/os/unix/ngx_setproctitle.c
        ${NGINX_SOURCES}/src/os/unix/ngx_posix_init.c
        ${NGINX_SOURCES}/src/os/unix/ngx_user.c
        ${NGINX_SOURCES}/src/os/unix/ngx_dlopen.c
        ${NGINX_SOURCES}/src/os/unix/ngx_process_cycle.c
        ${NGINX_SOURCES}/src/os/unix/ngx_linux_init.c
        ${NGINX_SOURCES}/src/event/modules/ngx_epoll_module.c
        ${NGINX_SOURCES}/src/os/unix/ngx_linux_sendfile_chain.c
        ${NGINX_SOURCES}/src/core/ngx_bpf.c
        ${NGINX_SOURCES}/src/core/ngx_regex.c
        ${NGINX_SOURCES}/src/http/ngx_http.c
        ${NGINX_SOURCES}/src/http/ngx_http_core_module.c
        ${NGINX_SOURCES}/src/http/ngx_http_special_response.c
        ${NGINX_SOURCES}/src/http/ngx_http_request.c
        ${NGINX_SOURCES}/src/http/ngx_http_parse.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_log_module.c
        ${NGINX_SOURCES}/src/http/ngx_http_request_body.c
        ${NGINX_SOURCES}/src/http/ngx_http_variables.c
        ${NGINX_SOURCES}/src/http/ngx_http_script.c
        ${NGINX_SOURCES}/src/http/ngx_http_upstream.c
        ${NGINX_SOURCES}/src/http/ngx_http_upstream_round_robin.c
        ${NGINX_SOURCES}/src/http/ngx_http_file_cache.c
        ${NGINX_SOURCES}/src/http/ngx_http_write_filter_module.c
        ${NGINX_SOURCES}/src/http/ngx_http_header_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_chunked_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_range_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_gzip_filter_module.c
        ${NGINX_SOURCES}/src/http/ngx_http_postpone_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_ssi_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_charset_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_userid_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_headers_filter_module.c
        ${NGINX_SOURCES}/src/http/ngx_http_copy_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_not_modified_filter_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_static_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_autoindex_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_index_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_mirror_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_try_files_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_auth_basic_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_access_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_limit_conn_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_limit_req_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_geo_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_map_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_split_clients_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_referer_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_rewrite_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_proxy_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_fastcgi_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_uwsgi_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_scgi_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_memcached_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_empty_gif_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_browser_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_upstream_hash_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_upstream_ip_hash_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_upstream_least_conn_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_upstream_random_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_upstream_keepalive_module.c
        ${NGINX_SOURCES}/src/http/modules/ngx_http_upstream_zone_module.c
)

add_executable(${PROJECT_NAME} ${SRC})
include_directories(${NGINX_SOURCES}/src/core)
include_directories(${NGINX_SOURCES}/src/event SRC_LIST)
include_directories(${NGINX_SOURCES}/src/os/unix)
include_directories(${NGINX_SOURCES}/src/http)
include_directories(${NGINX_SOURCES}/src/http/modules)
include_directories(${NGINX_SOURCES}/objs)
include_directories(/usr/include)
target_link_libraries(${PROJECT_NAME} pthread crypt pcre crypto z)