最近工作中需要針對freescale的iMX5和iMX6平台做一些arm平台下的系統核心軟體包的整合,比如將glib、dbus、pkg-config、libxml2、pixman、gdb、libpng、gcc、gstreamer、ffmpeg、bluez等軟體原始碼針對arm平台打包成二進位的rpm包以及二進位原始碼包.src.rpm。
一、為什麼要將原始碼打包成rpm格式的二進位包。
1、由於不同的電腦體系架構,比如arm和X86上的二進位是不一樣的。我試過將一個簡單的hello world的C語言的程式,當使用ubuntu12.04內建的gcc編譯產生hello可執行程式,在arm的板子上跑不起來;使用scratchbox環境下的arm-linux-gcc交叉編譯器編譯產生基於arm的可執行程式hello在arm板子上能跑。所以在Linux下我們有時需要自己下載原始碼進行編譯,比如說Qt4.8.4的原始碼,我曾經下載過qt-everywhere-opensource-src-4.8.4.tar.gz原始碼,使用自己2G的記憶體、T5900的處理器編譯安裝Qt4.8.4需要兩三個小時。但是如果針對自己的機器體繫結構(比如一般的PC是i386體系)將QT原始碼打包成rpm,再使用rpm工具安裝,應該快多了,這就是rpm打包的好處吧。
2、Linux作業系統發行商通常會針對自己的發行版本將原始碼打包成二進位的rpm,方便開發人員或者軟體使用者安裝,這樣就不用直接使用原始碼安裝了。當然reahat系列提供了yum安裝或者使用者介面的安裝方式,ubuntu也提供了對應的apt-get和ubuntu軟體中心的軟體安裝方式。
3、在嵌入式中,需要針對自己板子所在的平台,將第三方原始碼或者自己寫的代碼打包成rpm二進位rpm包,發布給客戶使用。
二、如何將.tar.gz、.tar.xz等格式的原始碼打包成rpm包。
1、首先從BLFS網站http://www.linuxfromscratch.org/blfs/view/svn/index.html或者http://sourceforge.net/等開源網站下載原始碼。
2、然後編寫spec檔案,這個是很重要的一個環節。使用autotools(automake+autoconf)或者cmake產生原始碼對應的makefile檔案,再根據makefile編譯、安裝,打包成rpm。關於Spec檔案的編寫,可參照以下這幾篇文章:
spec檔案詳解:http://blog.csdn.net/hncomputer/article/details/7162339
spec檔案具體編寫:http://cpbest.blog.163.com/blog/static/412415192009917477295/
RPM包rpmbuild SPEC檔案深度說明:http://hlee.iteye.com/blog/343499
spec檔案指南:http://zh.opensuse.org/index.php?title=openSUSE:Specfile_guidelines&variant=zh
RPM打包技術與典型SPEC檔案分析:http://www.cnblogs.com/cnland/archive/2013/02/08/2909301.html
3、配置好rpm打包的目錄結構,一般在rpmbuild目錄下有BUILD、SOURCES、RPMS、SRPMS、SPECS這幾個目錄。
4、打rpm包。
以從http://www.linuxfromscratch.org/blfs/view/svn/general/libpng.html下載的libpng-1.6.2為例,針對i386環境打包,編寫好對應的libpng.spec檔案後,再使用rmpbuild -ba libpng.spec執行第2步中編寫的spec檔案打包,假如在spec檔案中的%package -n 描述有libpng-tools、libpng-runtime、libpng-devel三個包,那麼執行完spec檔案中的內容後可以在RPMS目錄下看到對應的libpng-tools-1.6.2-1.i386.rpm、libpng-runtime-1.6.2-1.i386.rpm、libpng-devel-1.6.2-1.i386.rpm以及在SRPMS下產生libpng.src.rpm二進位源碼包。
可以從https://review.tizen.org/git/網站上找到一些常用軟體的spec檔案,如libpng對應的spec檔案網址為:https://review.tizen.org/git/?p=external/libpng.git;a=tree;f=packaging;hb=refs/heads/tizen_2.1
tizen官網給出的libpng.spec內容如下:
#sbs-git:slp/unmodified/libpng libpng 1.2.46 fcaa793c53a17a30625312c0e4e6de51383f2debName: libpngSummary: A library of functions for manipulating PNG image format filesVersion: 1.2.50Release: 1Group: System/LibrariesLicense: zlibURL: http://www.libpng.org/pub/png/Source0: ftp://ftp.simplesystems.org/pub/png/src/libpng-%{version}.tar.bz2Requires(post): /sbin/ldconfigRequires(postun): /sbin/ldconfigBuildRequires: zlib-devel%descriptionThe libpng package contains a library of functions for creating andmanipulating PNG (Portable Network Graphics) image format files. PNGis a bit-mapped graphics format similar to the GIF format. PNG wascreated to replace the GIF format, since GIF uses a patented datacompression algorithm.Libpng should be installed if you need to manipulate PNG format imagefiles.%package develSummary: Development tools for programs to manipulate PNG image format filesGroup: Development/LibrariesRequires: %{name} = %{version}-%{release}Requires: libpng = %{version}-%{release}Requires: zlib-devel%description develThe libpng-devel package contains header files and documentation necessaryfor developing programs using the PNG (Portable Network Graphics) library.%prep%setup -q -n %{name}-%{version}%build%configure --disable-staticmake %{?jobs:-j%jobs}%installrm -rf %{buildroot}%make_install rm -rf $RPM_BUILD_ROOT/usr/share/manmkdir -p %{buildroot}/usr/share/licensecp LICENSE %{buildroot}/usr/share/license/%{name}%post -p /sbin/ldconfig%postun -p /sbin/ldconfig%files/usr/share/license/%{name}%manifest libpng.manifest%{_libdir}/libpng*.so.*%files devel%{_bindir}/*%{_includedir}/*%{_libdir}/libpng*.so%{_libdir}/pkgconfig/*
當然,我們應該根據自己的需求加以修改,然後就可以打包、發布自己的軟體了。
5、最後可以使用rpm -ivh libpng-1.6.2-1.i386.rpm在自己的X86機器上安裝對應的rpm包。