6 dpkg-cross

岩松信洋      
___________________________________________________________________________________________________________________________

6.1 はじめに

今 回 、 dpkg-cross について調べてみました。

6.1.1 クロス開発ってなんですか

dpkg-cross についてお話する前にクロス開発というものを理解する必要があります。 i386 などの高速な CPU を扱っていると、 ソフトウェアをコンパイルするマシンと実行するマシンは一緒だったりしますが、 非 力 な CPU ARM とか SupserH など) でコンパイルしようとすると非常に時間がかかります 。 ま た 、 プ ロ グ ラ ム を 動 か し た い が 、 対 象 の マ シ ン に は 開 発 環 境 が な い と い う と き も あ ります。 このような時にコンパイルは高速な CPU で行うと非常に速くコンパイルできたり 、 別 の マ シ ン で コ ン パ イ ル し た ソ フ ト ウ ェア を 対 象 の マ シ ン に 転 送 し 、 動 か す こ と が でき ます。

こ のように、 クロス開発とはプログラムを開発する環境と実行する環境が異なる開発方法をクロス開発 と言います。

6.1.2 dpkg-cross で何をするの?

dpkg-cross はクロス環境用パッケージを生成するときに使用します。 クロスで開発するときには対象アーキ テ ク チ ャの バ イ ナ リ パ ッケ ー ジ が 必 要 に な り ま す 。

例えば、 i386 上で PowerPC で動く libncurses を使ったアプリケーションをクロスコンパイルしたいとき に は libncurses5_*.*.*_powerpc.deb libncurses5-dev_*.*.*_powerpc.deb が必要になります。 これら の パ ッケ ー ジ は PowerPC 用なので通常 i386 上ではインストールできません。 PowerPC用 の パ ッケ ー ジ を 持 って た と し て も 、 ク ロ ス コ ン パ イ ル 用 に デ ィレ ク ト リ を 作 成 し て そ こ にヘッダファイルをコピーして...... と面倒くさいです。 そこで dpkg-cross の登場になりま す 。

6.2 やってみる

6.2.1 インストール

# apt-get install dpkg-cross

以上。

6.2.2 設定

使うアーキテクチャによって設定を行う必要があります。 設定ファイルは /etc/dpkg-cross/cross-compile です。 以下が内容です。

#  
# /etc/dpkg-cross/cross-compile: configuration for dpkg-cross & Co.  
#  
 
# default architecture for dpkg-cross (to avoid always typing the -a option  
# if you do cross installations only for one architecture)  
#default_arch = m68k  
 
#  
# general section: paths of cross compiling environment  
#  
# you can set the following variables here:  
#  crossprefix: prefix for cross compiling binaries; default: $(ARCH)-linux-  
#  crossbase  : base prefix for the following; default: /usr  
#  crossdir   : base directory for architecture; default:  
#               $(CROSSBASE)/$(ARCH)-linux  
#  crossbin   : dir for binaries; default: $(CROSSDIR)/bin  
#  crosslib   : dir for libraries; default: $(CROSSDIR)/lib  
#  crossinc   : dir for headers; default: $(CROSSDIR)/include  
#  crossinfo  : dir dpkg-cross’ package info files; default:  
#               \$(CROSSLIB)/dpkg-cross-info  
#  maintainer : maintainer name to pass to original dpkg-buildpackage  
#               in -m option. If not set at all, don’t pass a -m, thus  
#               dpkg-buildpackage will use the name from the changelog  
#               file. If set to the special string CURRENTUSER,  
#               dpkg-buildpackage will use the name from the  
#               changelog, too, but signing the .changes will be done  
#               as the current user (default key).  
#  removedeps : comma-separated list of package names that should be removed  
#               from depends/conflicts/etc fields  
#  keepdeps   : comma-separated list of package names thet should be kept  
#               in depends/conflicts/etc fields as is, without adding  
#               -arch-cross.  
#  
# Usually, you need only set crossbase, or maybe also crossdir  
#  
crossbase = /usr  
 
# A crossroot definition is for the complete-Debian-system-mounted-somewhere  
# approach, mainly used for Hurd.  
#crossroot-hurd-i386 = /gnu  
 
#  
# This setting for maintainer is usually right:  
#  
maintainer = CURRENTUSER  
#  
# This list is far from being complete ...  
# Please send additions to Nikita Youshchenko <yoush@cs.msu.su>  
#  
removedeps = gcc, binutils, gpm, cpp, debianutils, xfree86-common, libpam-runtime,  
xlibs-data, debconf  
 
#:w  
 
# per-package sections: additional environment variables to set  
#  
# Please send additions to Nikita Youshchenko <yoush@cs.msu.su>  
 
package e2fsprogs:  
    unset LD  
 
# package gs-aladdin:  
# # must be a native gcc  
#   CCAUX = gcc  
#  
# -------------------------------  
# This should fit EmDebian needs:  
#  
# mode emdebian:  
# package all:  
#   scope environment:  
#       emdebian = true  
#   scope makeflags:  
#       CROSSPREFIX = $(crossprefix)  
#       EXTRA_CFLAGS = ...  
#       LIBC = ...  
#       CONFIG = ...  

これらの中で重要なのは

です。 あとは特に気にしなくてよいと思います。

6.2.3 パッケージを作ってみる

クロス開発用のパッケージを作成するには dpkg-cross コマンドを使います。

$ ls  
libncurses5_5.4-4_powerpc.deb  
libncurses5-dev_5.4-4_powerpc.deb  
$ dpkg-cross -b -apowerpc libncurses5_5.4-4_powerpc.deb  
$ dpkg-cross -b -apowerpc libncurses5-dev_5.4-4_powerpc.deb

-a でアーキテクチャを指定します。 -b build を行うという意味です。 行うと以下のような名前のパッ ケージが作成されます。

$ ls  
libncurses5_5.4-4_powerpc.deb  
libncurses5-dev_5.4-4_powerpc.deb  
libncurses5-powerpc-cross_5.4-4_all.deb  
libncurses5-dev-powerpc-cross_5.4-4_all.deb

6.2.4 インストールしてソフトウェアを作ってみる

最初はクロスコンパイル用のパッケージを入れないままコンパイルしてみます。

iwamatsu@soki:~/dev/study/src$ ls  
sample.c  
iwamatsu@soki:~/dev/study/src$ powerpc-linux-gcc -o sample sample.c -lncurses  
sample.c:10:20: curses.h: そのようなファ イルやディ レクトリはありません  
sample.c: In function ‘main’:  
sample.c:24: error: ‘WINDOW’ undeclared (first use in this function)  
sample.c:24: error: (Each undeclared identifier is reported only once  
sample.c:24: error: for each function it appears in.)  
sample.c:24: error: ‘w_pRootwin’ undeclared (first use in this function)  
sample.c:24: error: ‘w_pWin’ undeclared (first use in this function)

/usr/power-pc/include curses.h がないのでエラーになっています。 クロスコンパイル用のパッケー ジをインストールして再度コンパイルしてみます。

iwamatsu@soki:~/dev/study/src$ sudo dpkg -i libncurses5-powerpc-cross_5.4-4_all.deb  
iwamatsu@soki:~/dev/study/src$ sudo dpkg -i libncurses5-dev-powerpc-cross_5.4-4_all.deb  
iwamatsu@soki:~/dev/study/src$ powerpc-linux-gcc -o sample sample.c -lncurses  
iwamatsu@soki:~/dev/study/src$ ls  
sample.c  
sample  
iwamatsu@soki:~/dev/study/src$ file sample  
sample: ELF 32-bit MSB executable, PowerPC or cisco 4500, version 1 (SYSV),  
for GNU/Linux 2.2.0, dynamically linked (uses shared libs), not stripped

PowerPC 用のプログラムとしてコンパイルができました。 PowerPC なマシンに持っていって動作確認し てみて、 正常に動作すれば OK です。

6.3 おわりに

今回 dpkg-cross について説明してみました。 debian Redhat 系とくらべてクロス開発環境の構築方法 が容易だなと感じました。 Redhat だとクロス開発環境用に再コンパイルしなおすという方 法しかないようです(私が調べた限りでは) 。 やっぱ Debian はすばらしいなぁと思いま した。

また、 今回の事前課題が「さわってみた/さわってみたい Debian のこんなアーキテクチャ移植版」 でも ありますし、 x86 以外のアーキテクチャ(最近なら PowerPC?) を触ってみていろいろコンパイルしてみる のもいいかもしれません。

東 京 エ リ ア Debian 勉強会 2005 _________________________________________________________

PIC