OpenWRT(MIPS)交叉编译OpenSSL

引言

因为很多嵌入式模块需要 libssl 和 libcrypto 库,所以编写此文。本文是使用mips编译器编译OpenSSL,并安装在/usr/local/mips,编译安装好后以后可以直接引用。

1、下载和配置

下载和解压:

cd /opt/
wget https://www.openssl.org/source/openssl-1.0.2l.tar.gz
tar zxf openssl-1.0.2l.tar.gz 
cd openssl-1.0.2l

2、配置

这里的配置会分为两种方法。

方法一

configure

配置成不要有ASM代码:

./configure no-asm shared --prefix=/usr/local/mips

OR

./config no-asm shared --prefix=/usr/local/mips

--prefix为指定make install安装路径。

修改Makefile

修改CCARRANLIBNM等路径,指向MISP编译器:

假设 toolchain 路径为:/opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-gcc

# 1、修改CC
CC= /opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-gcc

CFLAG= -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -DL_ENDIAN -O3 -Wall 
DEPFLAG= -DOPENSSL_NO_EC_NISTP_64_GCC_128 -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MD2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -DOPENSSL_NO_SCTP -DOPENSSL_NO_STORE -DOPENSSL_NO_UNIT_TEST

PEX_LIBS= 
EX_LIBS= -ldl
EXE_EXT= 
ARFLAGS= 

# 2、修改AR
AR= /opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-ar $(ARFLAGS) r
# 3、修改RANLIB
RANLIB= /opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-ranlib
# 4、修改NM
NM= /opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-nm


PERL= /usr/bin/perl
TAR= tar
TARFLAGS= --no-recursion --record-size=10240
MAKEDEPPROG= gcc
LIBDIR=lib

# 注释下面这一行
#SHARED_LDFLAGS=-m64

修改完成保存。

如果是mips交叉编译,后仍需要将此 Makefile 文件中的-m64后缀全部去掉。在VIM环境下可以使用/-m64回车查找,然后按下n查找下一个,全文应该只有两处!

方法二

这个方法我觉得比较通用,应该不止适用于编译OpenSSL,但是无论怎样最后最好都检查一下Makefile

首先配置环境变量:

$ export PATH=/opt/openwrt-toolchain/mips/bin:$PATH
$ echo $PATH
/opt/openwrt-toolchain/mips/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/lzq/.local/bin:/home/lzq/bin

配置编译器,这样执行config时候就会换为这些工具了:

export CC=/opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-gcc
export CPP=/opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-g++
export AR=/opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-ar
export LD=/opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-ld
export RANLIB=/opt/openwrt-toolchain/mips/bin/mips-openwrt-linux-ranlib

因为是使用export导入环境变量,所以关闭会话窗口后就失效了,所以不用考虑修改回来。设置好环境变量后可以输入$CC -v检查一下。

之后同方法一的config配置一样

./config no-asm shared --prefix=/usr/local/mips

配置好之后还要修改Makefile,这一次只需要查找全文删掉-m64关键字即可。

3、编译

注意:编译的时候可能还需要设置STAGING_DIR变量。

export STAGING_DIR=/opt/openwrt-toolchain/mips/

然后可以执行make

make

打印以下信息即表示编译成功:

...
making all in tools...
make[1]: Entering directory `/opt/openssl-1.0.2d/tools'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/opt/openssl-1.0.2d/tools'

然后可以在根目录下看到动态库libcrypto.solibcrypto.so.1.0.0libssl.solibssl.so.1.0. 和静态库libcrypto.alibssl.a

安装:

sudo make install

最后打印信息如下表示安装成功:

cp libcrypto.pc /usr/local/mips/lib/pkgconfig
chmod 644 /usr/local/mips/lib/pkgconfig/libcrypto.pc
cp libssl.pc /usr/local/mips/lib/pkgconfig
chmod 644 /usr/local/mips/lib/pkgconfig/libssl.pc
cp openssl.pc /usr/local/mips/lib/pkgconfig
chmod 644 /usr/local/mips/lib/pkgconfig/openssl.pc

4、引用

以后需要引用,修改如下配置即可

libssl 和 libcrypto 库路径:/usr/local/mips/lib/

$ ls /usr/local/mips/lib/
engines      libcrypto.so        libssl.a   libssl.so.1.0.0
libcrypto.a  libcrypto.so.1.0.0  libssl.so  pkgconfig

头文件路径:/usr/local/mips/include/

$ ls /usr/local/mips/include/openssl/
aes.h       cmac.h      dso.h     hmac.h      obj_mac.h      rand.h       ssl23.h      ui.h
asn1.h      cms.h       dtls1.h   idea.h      ocsp.h         rc2.h        ssl2.h       whrlpool.h
asn1_mac.h  comp.h      ebcdic.h  krb5_asn.h  opensslconf.h  rc4.h        ssl3.h       x509.h
asn1t.h     conf_api.h  ecdh.h    kssl.h      opensslv.h     ripemd.h     ssl.h        x509v3.h
bio.h       conf.h      ecdsa.h   lhash.h     ossl_typ.h     rsa.h        stack.h      x509_vfy.h
blowfish.h  crypto.h    ec.h      md4.h       pem2.h         safestack.h  symhacks.h
bn.h        des.h       engine.h  md5.h       pem.h          seed.h       tls1.h
buffer.h    des_old.h   e_os2.h   mdc2.h      pkcs12.h       sha.h        ts.h
camellia.h  dh.h        err.h     modes.h     pkcs7.h        srp.h        txt_db.h
cast.h      dsa.h       evp.h     objects.h   pqueue.h       srtp.h       ui_compat.h

猜你喜欢

转载自blog.csdn.net/u012163234/article/details/79791146
今日推荐