浅谈第三方库移植的一般性解决思路

本文介绍了将第三方库移植到ARM平台的一般性思路,以移植squashfs-tools为例,包括获取源码、阅读官方教程、编译与安装等步骤。在遇到问题时,强调了查阅官方文档的重要性,并分享了如何处理编译器选择和安装路径等细节。此外,还提及了如何支持额外的压缩选项。

浅谈移植第三方库至ARM的一般性解决思路

一、简介

1.1 前言

  由于涉及新机型开发,近期的主要工作就是要将平时用到的第三方库,移植到arm64上。在持续了整整一周后,总算是完成了相关工作。好记性不如烂笔头,本篇文章通过一个具体的示例,来介绍移植第三方库的一般性思路,并简单介绍一下自己遇到的一些主要问题。

二、处理过程

  本文是在踩了无数坑后的一篇总结性文章,因此本部分主要介绍自认为正确的一套处理思路。

2.1 阅读官方教程,进行编译和安装工作

  在我看来,对于一个稳定,成熟的开源库,势必拥有一套相对完备的安装说明,想要少走弯路,最好的建议就是先阅读一下官方的ReadMe文档。算是自己的一些臭毛病吧,在进行移植工作前,我习惯是确保工程在Build主机上编译通过,以确保官方提供的流程是可靠的。
  众所周知,所谓的移植过程不过就是需要执行正确的编译以及构建工作:

代码变成可执行文件,叫做编译(compile);先编译这个,还是先编译那个(即编译的安排),叫做构建(build)。—《Make 命令教程》

  借助于make编译工具,开发人员只需要键入make,make insall就可以完成全部的编译以及安装工作。而具体的构建规则一般都写在名为Makefile文件里边(当然,我们也可以通过make-f选项,来指定具体的规则文件)。

三、实例

  本节,将介绍如何将制作squashfs文件系统所要用到的工具集,移植到指定arm平台。

3.1 开发环境

3.1.1 Build环境

  系统版本:

Linux version 4.4.0-187-generic (buildd@lgw01-amd64-035) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ) #217-Ubuntu SMP Tue Jul 21 04:18:15 UTC 2020

  编译器版本:

Target: x86_64-linux-gnu
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 
3.1.1 Host环境

  系统版本:

Linux version 4.9.118 (virtual-machine) (gcc version 6.5.0 (Buildroot 2019.05.2) ) #1 SMP Mon Apr 12 19:22:33 CST 2021

  编译器版本:

Target: arm-ca9-linux-gnueabihf
gcc version 6.5.0 (Buildroot 2019.05.2)   

:关于build与host的区别,将会在后文介绍

3.2 编译squashfs_tool

3.2.1 获取源码

  首先需要我们可以直接从官方网址获取待移植开源库的源码: squashfs-tools

git clone https://github.com/plougher/squashfs-tools.git

  针对较活跃的开源库,其可能一直处于开发过程中,因此难免存在一些未经测试的新特性。倘若需要将其应用到生产中,建议选择一个稳定的发布版本,此次实例中,我们选取的是最新的发布分支:

[~/Github/opensource/cross_compile/squashfs-tools 18:08 #121]$ git tag 
3.1
3.2
3.2-r2
4.4
4.4-git.1
[~/Github/opensource/cross_compile/squashfs-tools 18:08 #122]$ git checkout 4.4-git.1 
Note: checking out '4.4-git.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at ddfb69d... Update version strings for tagged "point" release
[~/Github/opensource/cross_compile/squashfs-tools 18:08 #123]$ git log
commit ddfb69d50971710502c9818a1fd1be42497e9808
Author: Phillip Lougher <phillip@squashfs.org.uk>
Date:   Fri Oct 30 11:13:56 2020 +0000

    Update version strings for tagged "point" release
    
    Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>

3.2.2 获取帮助信息

  “谜底就在谜面上”,正如前边提到的,对于一个相对成熟的开源库而言,一般都会有较为详细的帮助文档,squashfs-tools亦是如此:
图1:squashfs-tools

  约定俗成的,在仓库的根目录下,一般都会存在readme,对于小型项目而言,其中一般会记录项目介绍,特性,编译,安装方法等信息。而对于较复杂项目,则会选择在其中介绍一个梗概。而对于详细的编译,安装方法的介绍,则会另开篇幅。对于squashfs-tools而言,对应的就是INSATLL

			INSTALLING SQUASHFS

1. Kernel support
-----------------

This release is for 2.6.29 and newer kernels.  Kernel patching is not necessary.
...
2. Building squashfs tools
--------------------------

The squashfs-tools directory contains the source code for the Mksquashfs and
Unsquashfs programs.  These can be compiled by typing make (or sudo make
install to install in /usr/local/bin) within that directory.
...
2.1 Compressors supported

By default the Makefile is configured to build Mksquashfs and Unsquashfs
with GZIP suppport.  Read the Makefile in squashfs-tools for instructions on
building LZO, LZ4, XZ and ZSTD compression support.

2.2 Extended attribute support

By default the Makefile is configured to build Mksquashfs and Unsquashfs
with extended attribute support.  Read the Makefile in squashfs-tools for
instructions on how to disable extended attribute support, if not supported
by your distribution/C library, or if it is not needed.
...

  如果我们仔细阅读该内容,不难发现,我们只需要在squashfs-tools子目录下,执行make操作,就可以获得我们需要的可执行文件Mksquashfs,以及Unsquashfs;对于安装而言,直接执行make install即可。

  • 关于安装路径
      默认的安装路径为/usr/local/bin,通过在makefile中修改INSTALL_DIR即可达到目的
  • 关于编译器选择
      默认的编译为gcc,根据实际情况,我们在makefile中,将CC变量修改为arm-ca9-linux-gnueabihf-gcc即可

五、常见问题解答

  • 如何支持额外的压缩选项
      在INSTALL文档的2.1小节:

2.1 Compressors supported
By default the Makefile is configured to build Mksquashfs and Unsquashfs
with GZIP suppport. Read the Makefile in squashfs-tools for instructions on
building LZO, LZ4, XZ and ZSTD compression support.
  Makefile用来配置带有Gzip属性Mksquashfs,Unsquashfs的固件。阅读squashfs-tools中的Makefile,以获取带有LZO,LZ4,XZ和ZSTD压缩支持的说明。

  以上,再次印证了谜底就在谜面上这句话。

六、参考与链接

  1. 《Make 命令教程》:http://www.ruanyifeng.com/blog/2015/02/make.html
  2. squashfs-tools:https://github.com/plougher/squashfs-tools

五、文档信息

作者: Litost_Cheng

发表日期:2021年04月14日
更多内容:

  1. Litost_Cheng的博客
  2. Litost_Cheng的Github
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值