海思平台交叉编译nginx

常怀感恩之心
        ——鲁迅

0、预置条件

这里已假定你已经成功安装了海思平台的交叉编译环境,并能成功编译出海思平台kernel。

小技巧:
可以通过“在shell输入‘arm-’并使用Tab按键来看是否有补全命令列表”来快速确认。


本资料将以arm-hisiv300-linux平台为例,其他平台可以参考此文做相应修改

1、源码准备

下载nginx源码

官网下载nginx源码:点我下载,本文档编写将基于次新稳定版本是1.16.1.

下载nginx-rtmp模块[可选]

git clone https://github.com/arut/nginx-rtmp-module.git

如果速度慢,也可以直接: 点我下载

下载zlib源码

官网下载zlib源码:点我下载,本文档编写将基于最新稳定版本是1.2.11。

下载openssl源码

官网下载openssl源码:点我下载,本文档编写时最新稳定版本是1.1.1系列。

下载完成,将源码解压到同级目录,参考:
.
├── nginx-1.16.1
├── nginx-rtmp-module-1.2.1
├── openssl-1.1.1c
└── zlib-1.2.11

2、编译配置

打开shell至nginx-1.16.1路径下:

./configure --prefix=/opt/nginx \
--with-zlib=/home/alex/workspace/zlib-1.2.11 \
--with-cc=arm-hisiv300-linux-gcc \
--user=root \
--group=root \
--without-http_rewrite_module \
--without-http_fastcgi_module \
--without-http_charset_module \
--without-http_ssi_module \
--without-http_userid_module \
--add-module=/home/alex/workspace/nginx-rtmp-module-1.2.1 \
--without-http_auth_basic_module \
--without-http_geo_module \
--without-http_map_module \
--without-http_split_clients_module \
--without-http_referer_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--without-http_memcached_module \
--without-http_memcached_module \
--without-http_limit_conn_module \
--without-http_limit_req_module \
--without-http_empty_gif_module \
--without-http_browser_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--without-http_upstream_zone_module \
--with-ipv6 \
--with-openssl=/home/alex/workspace/openssl-1.1.1c

注意将"/home/alex/workspace"替换为实际目录,按照参考都在同级目录也可以直接用".."替换,如"--with-openssl=../openssl-1.1.1c"。
如果没有报错,则可以进入下一步。

3、 执行编译

由于nginx对交叉编译支持的不好,需要如下修改以使openssl和zlib正确被交叉环境编译。

修改openssl模块:

编辑auto/lib/openssl/make文件,将54行:

&& ./config --prefix=$ngx_prefix no-shared no-threads $OPENSSL_OPT \\

改为:

&& ./config --prefix=$ngx_prefix no-shared no-asm no-async no-threads --cross-compile-prefix=arm-hisiv300-linux- $OPENSSL_OPT \\
&& sed -i 's/-m64/ /g' Makefile \\

修改zlib模块:

编辑configure文件,在32行添加:

CHOST="arm-hisiv300-linux"

修改完成,最后执行编译:

make

4、 执行make install

sudo make install

完成安装后,你可以在/opt/发现nginx安装目录,一般包含如下内容:
.
├── conf
├── html
├── logs
└── sbin

5、踩过的坑

踩坑一:

问题内容:

checking for C compiler ... found but is not working
./configure: error: C compiler arm-hisiv400-linux-gcc is not found

原因分析:
configure首先会编译一个小测试程序,通过测试其运行结果来判断编译器是否能正常工作,由于交叉编译器所编译出的程序是无法在编译主机上运行的,故而产生此错误。
解决办法:
方法1:
编辑auto/cc/name文件,将21行的“exit 1”注释掉。
方法2:
编辑auto/cc/name文件,将10行的“ngx_feature_run=yes”改为“ngx_feature_run=no”:

ngx_feature_run=no
踩坑二:

问题内容:

checking for int size ...objs/autotest: 1: objs/autotest: Syntax error: word unexpected (expecting ")")
  bytes
./configure: error: can not detect int size

原因分析:
因为cc编译后的程序无法本地执行导致。
解决办法:
编辑auto/types/sizeof文件,将15行的“ngx_size=”改为“ngx_size=4”,

ngx_size=4

将39行的eval "ngx_test >>NGX_AUTOCONF_ERR 2>&1"注释掉:

#eval "$ngx_test >> $NGX_AUTOCONF_ERR 2>&1"
踩坑三:

问题内容:

src/os/unix/ngx_errno.c:58:11: error: ‘NGX_SYS_NERR’ undeclared (first use in this function)
     len = NGX_SYS_NERR * sizeof(ngx_str_t);
           ^
objs/Makefile:708: recipe for target 'objs/src/os/unix/ngx_errno.o' failed

原因分析:
因为交叉编译程序无法本地运行,导致NGX_SYS_NERR宏没有赋值。
解决办法:
编辑objs/ngx_auto_config.h文件,在:

#ifndef NGX_COMPILER
#define NGX_COMPILER  "gcc 4.8.3 20131202 (prerelease) (Hisilicon_v300) "
#endif

行后添加:

#ifndef NGX_SYS_NERR
#define NGX_SYS_NERR  132
#endif
踩坑四:

问题内容:

objs/src/core/ngx_cycle.o: In function 'ngx_init_cycle':
/home/src/nginx-1.14.0/src/core/ngx_cycle.c:476: undefined reference to 'ngx_shm_alloc'
/home/src/nginx-1.14.0/src/core/ngx_cycle.c:685: undefined reference to 'ngx_shm_free'

原因分析:
因为交叉编译程序无法本地运行,导致NGX_HAVE_SYSVSHM宏没有赋值。
解决办法:
编辑objs/ngx_auto_config.h文件,在:

#ifndef NGX_COMPILER
#define NGX_COMPILER  "gcc 4.8.3 20131202 (prerelease) (Hisilicon_v300) "
#endif

行后添加:

#ifndef NGX_HAVE_SYSVSHM
#define NGX_HAVE_SYSVSHM 1
#endif

参考文章:

1、 Nginx mips交叉编译总结
2、 交叉编译Hi3536上面使用的nginx
3、 海思Rtmp之Nginx和ffmpeg的移植
4、 嵌入式hi3516-hi3518海思平台移植nginx-1.12.2

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容