OSWikiHK: 請協力 GPLv3 的中文翻譯工作。
重新編譯 dpkg 套件
OSWikiHK,自由中文開源知識庫
當您在使用 Ubuntu/Debian/Xandros 的正式發行版本,但想使用在開發版本中的新加入的軟件或者想使用在開發版本中有關軟件的新版本,又或者想微調某些軟件的編譯設定。當然 backports.org 有不少由 Debian 開發版本 backport 回到正式發行版本的套件,而 Ubuntu 亦有提供 hardy-backports 軟件庫,但如果他們沒有提供你需要的軟件或不符合你的要求,那你就需要重新編譯 dpkg 套件。
目录 |
取得源碼
首先自然需要先取得有關 dpkg 套件的源碼包。和 RPM 不同的套件源碼一般都有三個檔案:
- 源程式碼包 (*.orig.tar.gz) - 這是有關軟件原身的源程式碼包 (例如: mod-cband_0.9.7.5.orig.tar.gz) 。原則上是完全沒有被改動過(除了檔案名稱被改至符合dpkg的規格外),和在開發者網站取得的一模一樣。
- 套件描述 (*.dsc) - 有對有關軟件的簡單描述 (例如: mod-cband_0.9.7.5-1.dsc) 。包括套件名稱、版本、維護者、可編譯平台、編譯時需要的套件和 orig.tar.gz 及 diff.gz 檔的 hash 值。為確保套件沒有被任何人更動過,所有由官方提供的dsc都已用維護者的數碼簽名簽著過。
- 補丁檔 (*.diff.gz) (非必有) - 維護者對改動 (例如: mod-cband_0.9.7.5-1.diff.gz) 。包括加入 control、changelog 等檔案。如果開發者一早已 debian 化整個源程式碼,可能不會有 diff.gz 檔的出現。
使用 apt-get source
取得目標 dpkg 套件源碼的三個檔案,最簡單自然是用 apt-get source 命令下載。首先您需要確定 /etc/apt/sources.list 有沒有「deb-src」一行:
deb http://ftp.hk.debian.org/debian/ etch main contrib non-free deb-src http://ftp.hk.debian.org/debian/ sid main contrib non-free
如果你是想由開發版本中 backport 一個套件回來,自然要使用開發版本的軟件庫,以 Debian 為例,自然是使用 sid。設定好後,在系統管理員身份下用「apt-get update」更新軟件庫資料。然後就可以用「apt-get source 套件名」取得目標 dpkg 套件源碼。這個指令會幫你幫別目標套件真正源碼名稱 (如套件 libapache2-mod-cband 的源碼包是 mod-cband ) ,下載套件源碼的三個檔案,並自動拆開源碼。為方便管理,建議建立目錄 ~/projects/dpkg 並在裡面才進行 apt-get source :
mkdir -p ~/projects/dpkg cd ~/projects/dpkg apt-get source libapache2-mod-cband
完成後,您會在當前目錄找到到套件源碼的三個檔案和以套件源碼名稱和版本編號組成的目錄 (例如mod-cband-0.9.7.5) ,目錄裡面就是套件的源碼。
$ ls mod-cband-0.9.7.5 mod-cband_0.9.7.5-1.diff.gz mod-cband_0.9.7.5-1.dsc mod-cband_0.9.7.5.orig.tar.gz
自行下載
當然您亦可以自動下載 orig.tar.gz、dsc 和 diff.gz 三個檔案。下載後,可以用「dpkg-source -x dsc檔」把原始碼拆開:
$ dpkg-source -x mod-cband_0.9.7.5-1.dsc dpkg-source: extracting mod-cband in mod-cband-0.9.7.5
執行後,您會在當前目錄找到到套件源碼的三個檔案和以套件源碼名稱和版本編號組成的目錄 (例如 mod-cband-0.9.7.5) ,目錄裡面就是套件的源碼。
整理編譯設定
設定版本編號
習慣上您首先要改動版本編號,記錄這次重編的目的。當然您可以略過這個步驟,但有機會做成往後升級的混亂,所以也建議花點時間做做這個簡單的工作。
版本編號和更動說明是記錄在套件源碼下 debian/changelog 中。您可以用文字編輯器直接修改這個檔案,但我們一般會先用命令「debchange -i 更動說明」加入版本更動的框架,例如:
debchange -i Backport to etch
執行後用文字編輯器編輯 debian/changelog,您會見到類似以下內容:
mod-cband (0.9.7.5-2) unstable; urgency=low * Backport to etch -- Fred Random <fred@example.org> Tue, 3 Apr 2007 17:56:19 +0800 mod-cband (0.9.7.5-1) unstable; urgency=low
粗體為 debchange 新加的內容。這表示這個例子中原本你下載的套件版本編號是 0.9.7.5-1,debchange 會順勢把建立編號(build number)加一,變成 0.9.7.5-2 。為讓人一看就知重編後的套是 backport,亦方便以後有更新時可以蓋過個版本,我們會把建立號減一,加上「.1etch1」,令版本編號變成 0.9.7.5-0.1etch1 。etch後的 1 表示這個版本被重編的次數。如果在重編後發覺有問題,需要再新編,請將這個數目加一。改動後的 debian/changelog 會變成如下:
mod-cband (0.9.7.5-0.1etch1) unstable; urgency=low * Backport to etch -- Fred Random <fred@example.org> Tue, 3 Apr 2007 17:56:19 +0800 mod-cband (0.9.7.5-1) unstable; urgency=low
安裝所需開發套件
接著您需要安裝編譯時所需開發套件,這些套件的名單在 debian/control 的的 Build-Depends 一行中:
$ egrep '^Build-Depends:' debian/control Build-Depends: debhelper (>= 4.0.0), autotools-dev, apache2-threaded-dev | apache2-prefork-dev, po-debconf
這顯示你需要安裝debhelper、autotools-dev、apache2-threaded-dev和po-debconf等四個套件才可以重編mod-cband,而debhelper必定要4.0.0或以上,apache2-threaded-dev可以用apache2-prefork-dev取替。
最簡單的方法是在系統管理員身份下打「apt-get build-deb 套件」,這命令自然會幫您下載並安裝有關套件所需開發套件。不過很多情況下不會那麼順利,在 backport 過程中,會遇到在正式發行本中未有所需開發套件,套件名稱改變了或版本不符合等問題,而 apt-get build-deb 遇到些情況會停下來。這時候您需要自行找出在正式發行本中對應的套件 (如 apache2.2-common 對應 apache2-common 或 apache-common) ,自行手動用 apt-get 下載並安裝,和更動debian/control 的需求。最差的情況下,您有機會需要重編幾個開發套件。
整理設定
接著您需要套件的相依性,即檔案 debian/control 中的 Depends 一行中:
$ egrep '^Depends:' debian/control
Depends: ${misc:Depends}, ${shlibs:Depends}, apache2.2-common
這顯示您需要安裝 apache2.2-common 套件才可以安裝 mod-cband。
您需要檢查一下您現時的發行版本是否有 Depends 列出的套件和版本是否符合。例如:
$ apt-cache show apache2.2-common W: Unable to locate package apache2.2-common E: No packages found
這表示您現時的發行版本並沒有 apache2.2-common 這個套件。這時候您需要自行找出在正式發行本中對應的套件 (如 apache2.2-common 對應 apache2-common 或 apache-common) ,例如:
$ apt-cache search apache common apache - versatile, high-performance HTTP server apache-common - support files for all Apache webservers apache-perl - versatile, high-performance HTTP server with Perl support apache-ssl - versatile, high-performance HTTP server with SSL support apache2-common - next generation, scalable, extendable web server axyl-lucene - The Axyl Luceneserver cl-modlisp - Common Lisp interface to the Apache mod-lisp module
apache2.2-common 可能對應 apache2-common 或 apache-common。你需要更動debian/control 的需求,把Depend一行改成:
Depends: ${misc:Depends}, ${shlibs:Depends}, apache2-common
但最差的情況下,您有機會需要重編幾個相依的套件。
重建套件
現在您可以用 debuild 重編套件。因為包封套件時需要製做一個系統管理員的環境,我們需要 fakeroot 的協助。請打「debuild -rfakeroot」:
$ debuild -rfakeroot dpkg-buildpackage: source package is mod-cband dpkg-buildpackage: source version is 0.9.7.5-0.1sarge1 dpkg-buildpackage: source maintainer is Fred Random <fred@example.org> dpkg-buildpackage: host architecture is i386
首先 debuild 會顯示重編的基本資料包括套件名稱、版本編號、維護者和編譯平台等。
接著需要清理上次編譯遺留的檔案,以免干擾這次重編的工作:
fakeroot debian/rules clean dh_testdir dh_testroot rm -f build-stamp rm -f config.* /usr/bin/make clean make[1]: Entering directory `/home/fred/projects/dpkg/mod-cband-0.9.7.5' make[1]: *** No rule to make target `clean'. Stop. make[1]: Leaving directory `/home/fred/projects/dpkg/mod-cband-0.9.7.5' make: [clean] Error 2 (ignored) dh_clean rm -f Makefile
包封現時的源碼和記錄所有你對源碼的更動:
dpkg-source -b mod-cband-0.9.7.5 dpkg-source: warning: unknown information field in input data in package's section of control info file dpkg-source: building mod-cband using existing mod-cband_0.9.7.5.orig.tar.gz dpkg-source: building mod-cband in mod-cband_0.9.7.5-0.1sarge1.diff.gz dpkg-source: building mod-cband in mod-cband_0.9.7.5-0.1sarge1.dsc
正式開始重編:
debian/rules build dh_testdir
配置編譯設定:
CFLAGS="-Wall -g -O2 -Wl,-z,defs" ./configure --prefix=/usr --mandir=\${prefix}/share/man --infodir=\${prefix}/share/info
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking whether make sets $(MAKE)... yes
checking for apr_palloc in -laprutil... no
checking for an ANSI C-conforming const... yes
checking for apxs... no
checking for apxs2... apxs2
checking how to run the C preprocessor... gcc -E
checking for egrep... grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking stdio.h usability... yes
checking stdio.h presence... yes
checking for stdio.h... yes
checking for string.h... (cached) yes
checking assert.h usability... yes
checking assert.h presence... yes
checking for assert.h... yes
checking ctype.h usability... yes
checking ctype.h presence... yes
checking for ctype.h... yes
checking errno.h usability... yes
checking errno.h presence... yes
checking for errno.h... yes
checking math.h usability... yes
checking math.h presence... yes
checking for math.h... yes
checking netinet/in.h usability... yes
checking netinet/in.h presence... yes
checking for netinet/in.h... yes
checking sys/socket.h usability... yes
checking sys/socket.h presence... yes
checking for sys/socket.h... yes
checking arpa/inet.h usability... yes
checking arpa/inet.h presence... yes
checking for arpa/inet.h... yes
configure: creating ./config.status
config.status: creating Makefile
dh_testdir
編譯程式:
/usr/bin/make make[1]: Entering directory `/home/fred/projects/dpkg/mod-cband-0.9.7.5' apxs2 -Wc,-Wall -Wc,-DDST_CLASS=3 -c src/mod_cband.c /usr/bin/libtool --silent --mode=compile gcc -prefer-pic -pipe -I/usr/include/xmltok -I/usr/include/openssl -Wall -O2 -DAP_HAVE_DESIGNATED_INITIALIZER -DLINUX=2 -D_REENTRANT -D_XOPEN_SOURCE=500 -D_BSD_SOURCE -D_SVID_SOURCE -D_GNU_SOURCE -pipe -I/usr/include/xmltok -I/usr/include/openssl -Wall -O2 -pthread -I/usr/include/apache2 -I/usr/include/apr-0 -I/usr/include/apr-0 -I/usr/include -Wall -DDST_CLASS=3 -c -o src/mod_cband.lo src/mod_cband.c && touch src/mod_cband.slo /usr/bin/libtool --silent --mode=link gcc -o src/mod_cband.la -rpath /usr/lib/apache2/modules -module -avoid-version src/mod_cband.lo write "make install" to install module make[1]: Leaving directory `/home/fred/projects/dpkg/mod-cband-0.9.7.5' touch build-stamp
包封套件:
fakeroot debian/rules binary dh_testdir dh_testroot dh_clean -k dh_installdirs install -o root -g root -m644 /home/fred/projects/dpkg/mod-cband-0.9.7.5/src/.libs/mod_cband.so /home/fredo/projects/dpkg/mod-cband-0.9.7.5/debian/libapache2-mod-cband/usr/lib/apache2/modules/ install -m644 debian/libapache2-mod-cband.load /home/fred/projects/dpkg/mod-cband-0.9.7.5/debian/libapache2-mod-cband/etc/apache2/mods-available/cband.load install -m644 debian/libapache2-mod-cband.conf /home/fred/projects/dpkg/mod-cband-0.9.7.5/debian/libapache2-mod-cband/etc/apache2/mods-available/cband.conf dh_testdir dh_testroot dh_installchangelogs Changes dh_installdebconf dh_installdocs dh_installexamples dh_strip dh_compress dh_fixperms dh_installdeb dh_shlibdeps dh_gencontrol dh_md5sums dh_builddeb dpkg-deb: building package `libapache2-mod-cband' in `../libapache2-mod-cband_0.9.7.5-1.1sarge1_i386.deb'. dpkg-genchanges dpkg-genchanges: not including original source code in upload dpkg-buildpackage: binary and diff upload (original source NOT included)
到了現在重編工作已算完成,新套件亦已產生。
不過 debuild 仍會用你的數碼簽名簽署 dsc 檔。如果你沒有安裝 pgp/gpg 或沒有自己的數碼簽名,debuild 會出現錯誤,不過這不會影響編譯出來的套件。你事實上可以略過這個錯誤信息:
Now signing changes and any dsc files... Could not find a signing program (pgp or gpg)! debuild: fatal error at line 788: running debsign failed
現在你的目錄一般會多了最少五個檔案:
$ ls libapache2-mod-cband_0.9.7.5-0.1sarge1_i386.deb mod-cband-0.9.7.5 mod-cband_0.9.7.5-0.1sarge1.diff.gz mod-cband_0.9.7.5-0.1sarge1.dsc mod-cband_0.9.7.5-0.1sarge1_i386.build mod-cband_0.9.7.5-0.1sarge1_i386.changes mod-cband_0.9.7.5-1.diff.gz mod-cband_0.9.7.5-1.dsc mod-cband_0.9.7.5.orig.tar.gz
libapache2-mod-cband_0.9.7.5-0.1sarge1_i386.deb 就是重編出來的套件。
安裝新套件
現在你可以安裝新套件了:
# dpkg -i *.deb
相關頁面
页面分类: Software Development | Dpkg | Debian | Ubuntu

