libapr在powerpc64-linux上的交叉編譯

aprapache公司的一個支持跨平臺編程的開源庫,有很好的內存管理機制,支持LinuxAIXWindows等多操做系統。代碼由C語言編寫。
近期接到一個需求,因爲咱們的客戶在powerpc機器上安裝了Redhat,致使咱們的程序沒法運行,所以想到了交叉編譯。
而咱們的程序由於使用了apr庫,因此在所不免地,使用交叉編譯的方式編譯apr也就成爲了避免可避免的工做。node

環境搭建

我使用的apr版本爲1.6.2,編譯環境爲Ubuntu18.04GLIBC版本爲2.27。經過拉起docker容器的方式搭建編譯環境。linux

docker run -itd --name power-linux --restart unless-stopped -w /var/apr-1.6.2 -v `pwd`:/var/apr-1.6.2 ubuntu:18.04

以上命令會在宿主機上拉起一個Ubuntu18.04container,並將apr的代碼映射在/var/apr-1.6.2目錄下。docker

而後進入容器:apache

docker exec -it power-linux bash

因爲這是一個全新的Ubuntu環境,咱們須要作一些基礎的配置:編程

apt update
apt -y install vim 
apt -y install openssh*

編譯工具鏈安裝

交叉編譯主要使用gcc-powerpc64-linux-gnu這個編譯器,因此首先咱們要下載該編譯器:ubuntu

apt -y install gcc-powerpc64-linux-gnu

同時,咱們還須要安裝一些其餘編譯工具:vim

apt -y install libtool automake autoconf
apt -y install make
apt -y install gcc

須要說明一下,前面已經安裝了gcc-powerpc64-linux-gnu,爲何這裏還要安裝gccgcc是爲了編譯在當前x86環境能夠運行的可執行程序用的,在後面會用到。bash

有一點要注意的是,gcc-powerpc64-linux-gnu默認的是大端編譯器,若是power機器是小端的話,須要使用小端編譯器,小端編譯器爲gcc-powerpc64le-linux-gnuless

編譯

因爲apr是經過autoconf的方式管理Makefile的,因此首先須要使用configure命令生成當前平臺適用的Makefile,命令以下:ssh

./configure --disable-dso --prefix=/usr --host=powerpc-linux CC=/usr/bin/powerpc64-linux-gnu-gcc  ac_cv_file__dev_zero=yes ac_cv_func_setpgrp_void=yes  apr_cv_process_shared_works=yes apr_cv_mutex_robust_shared=no apr_cv_tcp_nodelay_with_cork=yes ac_cv_sizeof_struct_iovec=16 apr_cv_mutex_recursive=yes

執行完以上命令,Makefile已經生成,此時,若是咱們執行make,會報錯:

/bin/bash: tools/gen_test_char: cannot execute binary file: Exec format error
Makefile:143: recipe for target 'include/private/apr_escape_test_char.h' failed
make[1]: *** [include/private/apr_escape_test_char.h] Error 126
make[1]: Leaving directory '/root/apr-1.6.2'
/root/apr-1.6.2/build/apr_rules.mk:118: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1

上面這個錯誤是因爲執行gen_test_char的時候發現執行不了形成的,由於gen_test_char是用powerpc64-linux-gun-gcc編譯出來的,在當前環境固然執行不了,咱們手動用gcc編譯一下就能夠了。
進入tools目錄,執行:

cd tools
gcc gen_test_char.c -I"(where ever apr is)/apr/include" -o gen_test_char

修改完成後,退回上一級目錄,繼續make,仍然報錯:

./include/apr_want.h:94:8: error: redefinition of 'struct iovec'
 struct iovec
        ^~~~~
In file included from /usr/powerpc64-linux-gnu/include/sys/socket.h:26:0,
                 from ./include/apr.h:168,
                 from ./include/apr_escape.h:22,
                 from encoding/apr_escape.c:28:
/usr/powerpc64-linux-gnu/include/bits/types/struct_iovec.h:26:8: note: originally defined here
 struct iovec
        ^~~~~
/root/apr-1.6.2/build/apr_rules.mk:206: recipe for target 'encoding/apr_escape.lo' failed
make[1]: *** [encoding/apr_escape.lo] Error 1
make[1]: Leaving directory '/root/apr-1.6.2'

咱們須要修改include/apr_want.h文件:

vi include/apr_want.h

按照下面的方式修改:

原內容:

#ifndef APR_IOVEC_DEFINED
#define APR_IOVEC_DEFINED
struct iovec
{
  void *iov_base;
  size_t iov_len;
};  
#endif /* !APR_IOVEC_DEFINED */

修改成:

#if  0
struct iovec
{
  void *iov_base;
  size_t iov_len;
};  
#endif /* !APR_IOVEC_DEFINED */

再次make,成功,而後執行make install安裝:

make
make install

咱們進入到/usr/lib目錄下,能夠看到libapr的動態庫已經生成了,經過file命令去查看,能夠看到確實是powerpc的庫文件:

root@61ca5c46b8ef:/usr/lib# file libapr-1.so.0.6.2
libapr-1.so.0.6.2: ELF 64-bit MSB shared object, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, BuildID[sha1]=1f3dd3d15dbd80d5568544b1a81fae9d6d47cef4, with debug_info, not stripped

至此,aprpowerpc64-linux的交叉編譯成功。

相關文章
相關標籤/搜索