今天稍微研究了一下交叉编译RISC-V上的Nginx。事实上, Nginx本身对于交叉编译的支持并不好, 为了进行交叉编译, 需要魔改一些脚本。这里主要参考这篇文章。
但是, 与上述文章不同, 我修改的是configure中编译测试程序使用的编译选项, 并为其增加了--static选项。这样编译出来的测试程序即可借助qemu-riscv64-static在本地顺利执行, 使得脚本的其他部分正常执行。
diff --git a/auto/feature b/auto/feature
index 3561f59..4a439a9 100644
--- a/auto/feature
+++ b/auto/feature
@@ -39,7 +39,7 @@ int main(void) {
END
-ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS $ngx_feature_inc_path \
+ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS --static $ngx_feature_inc_path \
-o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_TEST_LD_OPT $ngx_feature_libs"
ngx_feature_inc_path=
diff --git a/auto/types/sizeof b/auto/types/sizeof
index 480d8cf..fd7cdd2 100644
--- a/auto/types/sizeof
+++ b/auto/types/sizeof
@@ -33,7 +33,7 @@ int main(void) {
END
-ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS \
+ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS --static \
-o $NGX_AUTOTEST $NGX_AUTOTEST.c $NGX_LD_OPT $ngx_feature_libs"
eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
然后, 运行如下命令即可编译:
./configure --prefix=install-nginx --with-cc=`which riscv64-unknown-linux-gnu-gcc` --with-cpp=`which riscv64-unknown-linux-gnu-gcc` --without-http_rewrite_module --without-http_gzip_module make
此外, 下载pcre或zlib库后, 还可以将--without-http_rewrite_module或--without-http_gzip_module换成相应的选项来进行编译。

发表评论