SH3クロス開発環境構築(GCCビルドエラー未解決)

SH3のマイコンボード「T-SH7706LSR」のクロス開発環境を
Debian上に作ろうと思い、GNU BinutilsGCCの最新版をビルドしていました。


結果として、まだGCC-4.6.4のビルドに成功していないのですが、
いろいろとトラブルシューティングを行ったので、ここまで至ってわかった

をそれぞれメモしておこうと思います。

成功経路(GCC-4.6.4ビルドまで)

セルフ・コンパイラの確認

のセルフコンパイル環境が整っているか確認します。


確認と、無ければインストールの意味で、以下のコマンドを実行しました。

$ sudo apt-get install binutils
$ sudo apt-get install gcc
$ sudo apt-get install make
GNU Binutils(バージョン2.23.2)のビルド・インストール

http://core.ring.gr.jp/pub/GNU/
ここから、

binutils-2.23.2.tar.gz

をダウンロードします。


展開し、ビルドします。

$ tar zxf binutils-2.23.2.tar.gz
$ cd binutils-2.23.2/
$ ./configure --target=sh3-elf --disable-nls
$ make
$ sudo make install


binutilsのビルドとインストールには特に問題はありませんでした。

GCCビルドの準備

の3つをビルド・インストールします。


GMP
http://gmplib.org/
ここから、

gmp-5.1.1.tar.lz

をダウンロードします。


lzipコマンドをインストールします。

$ sudo apt-get install lzip


展開し、ビルド・インストールします。

$ tar xf gmp-5.1.1.tar.lz --lzip
$ cd gmp-5.1.1/
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig

※ ldconfigが重要でした


【MPFR】
http://www.mpfr.org/
ここから、

mpfr-3.1.2.tar.gz

をダウンロードします。


展開し、ビルド・インストールします。

$ tar zxf mpfr-3.1.2.tar.gz
$ cd mpfr-3.1.2/
$ ./configure --with-gmp-lib=/usr/local/lib --with-gmp-include=/usr/local/include
$ make
$ make check
$ sudo make install
$ sudo ldconfig

※ 「--with-gmp-lib」には、「libgmp.so.10.1.1」などのライブラリファイルがあるディレクトリパスを、
  「--with-gmp-include」には、「gmp.h」があるディレクトリパスを指定します。


【MPC】
http://www.multiprecision.org/
ここから、

mpc-1.0.1.tar.gz

をダウンロードします。


同じく、展開、ビルド、インストールを行います。

$ tar zxf mpc-1.0.1.tar.gz
$ cd mpc-1.0.1/
$ ./configure
$ make
$ make check
$ sudo make install
$ sudo ldconfig
GCC(バージョン4.6.4)のビルド・インストール(挫折中)

http://core.ring.gr.jp/pub/GNU/
ここから、

gcc-4.6.4.tar.bz2

をダウンロードします。


展開・ビルド・エラー

$ tar jxf gcc-4.6.4.tar.bz2
$ cd gcc-4.6.4/
$ mkdir sh3-elf
$ cd sh3-elf/
$ ../configure --target=sh3-elf --disable-nls --disable-threads --disable-shared --enable-languages=c

・・・

make[3]: ディレクトリ `/mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/sh3-elf/libssp' に入ります
/bin/bash ./libtool --tag=CC   --mode=compile /mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/./gcc/xgcc -B/mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/./gcc/ -B/usr/local/sh3-elf/bin/ -B/usr/local/sh3-elf/lib/ -isystem /usr/local/sh3-elf/include -isystem /usr/local/sh3-elf/sys-include    -DHAVE_CONFIG_H -I. -I../../../libssp    -Wall -g -O2 -MT ssp.lo -MD -MP -MF .deps/ssp.Tpo -c -o ssp.lo ../../../libssp/ssp.c
libtool: compile:  /mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/./gcc/xgcc -B/mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/./gcc/ -B/usr/local/sh3-elf/bin/ -B/usr/local/sh3-elf/lib/ -isystem /usr/local/sh3-elf/include -isystem /usr/local/sh3-elf/sys-include -DHAVE_CONFIG_H -I. -I../../../libssp -Wall -g -O2 -MT ssp.lo -MD -MP -MF .deps/ssp.Tpo -c ../../../libssp/ssp.c -o ssp.o
../../../libssp/ssp.c: In function '__guard_setup':
../../../libssp/ssp.c:78:3: warning: implicit declaration of function 'open' [-Wimplicit-function-declaration]
../../../libssp/ssp.c:78:30: error: 'O_RDONLY' undeclared (first use in this function)
../../../libssp/ssp.c:78:30: note: each undeclared identifier is reported only once for each function it appears in
../../../libssp/ssp.c:81:7: error: unknown type name 'ssize_t'
../../../libssp/ssp.c:81:7: warning: implicit declaration of function 'read' [-Wimplicit-function-declaration]
../../../libssp/ssp.c:83:7: warning: implicit declaration of function 'close' [-Wimplicit-function-declaration]
../../../libssp/ssp.c: At top level:
../../../libssp/ssp.c:97:25: error: unknown type name 'size_t'
../../../libssp/ssp.c: In function '__stack_chk_fail':
../../../libssp/ssp.c:169:3: warning: implicit declaration of function 'fail' [-Wimplicit-function-declaration]
../../../libssp/ssp.c:169:3: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
../../../libssp/ssp.c:169:14: warning: incompatible implicit declaration of built-in function 'strlen' [enabled by default]
../../../libssp/ssp.c: In function '__chk_fail':
../../../libssp/ssp.c:176:14: warning: incompatible implicit declaration of built-in function 'strlen' [enabled by default]
make[3]: *** [ssp.lo] エラー 1
make[3]: ディレクトリ `/mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/sh3-elf/libssp' から出ます
make[2]: *** [all] エラー 2
make[2]: ディレクトリ `/mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf/sh3-elf/libssp' から出ます
make[1]: *** [all-target-libssp] エラー 2
make[1]: ディレクトリ `/mnt/storage/work_embOS12/stp01/gcc-4.6.4/sh3-elf' から出ます
make: *** [all] エラー 2


まず、一つ目のエラー

../../../libssp/ssp.c:78:30: error: 'O_RDONLY' undeclared (first use in this function)

に関しては、エラーメッセージでGoogle検索をかけてみると

のように、バグの報告はされていても、まだ解決はされていないように見えます。
(私が探しきれていない可能性も大きいですが)


エラーメッセージを素直に受け取って、「O_RDONLY」が定義されている場所を探してみると、

$ cd gcc-4.6.4/
$ find . -name '*.h' -exec grep -Hn 'O_RDONLY' {} \; | grep define
./gcc/system.h:316:# define O_RDONLY 0

のように、「./gcc/system.h」のみヒットし、これを「ssp.c」からインクルードすればよさそうです。


しかし、

$ find . -name 'system.h'

をしてみると分かる通り、「system.h」自体は様々なディレクトリに複数あり、
構造の理解もなしに、無闇にインクルードさせてよいものか分かりませんでした。


もう少し調べてみて、分からなければ
これより古いバージョンのGCCで試してみようと思います。

トラブルシューティング

GCCのconfigure時に「Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+.」のエラー

エラーメッセージの通り、

をインストールします。
その方法は上述の通りです。


参考:

MPFRのconfigure時に「'gmp.h' and 'libgmp' seems to have different versions or...」の警告

configure: WARNING: 'gmp.h' and 'libgmp' seems to have different versions or
configure: WARNING: we cannot run a program linked with GMP (if you cannot
configure: WARNING: see the version numbers above). A cause may be different
configure: WARNING: GMP versions with different ABI's.
configure: WARNING: However since we can't use 'libtool' inside the configure,
configure: WARNING: we can't be sure. See 'config.log' for details.

このような警告は、configure時にgmpのライブラリとインクルードファイルのパスを指定すれば解決するようです。

$ ./configure --with-gmp-lib=/usr/local/lib --with-gmp-include=/usr/local/include

のように指定します。


参考:

MPFRのビルド時に「error while loading shared libraries: libgmp.so.10: cannot open shared object file: No such file」

インストールしたばかりだとライブラリが参照できないようです。

$ sudo ldconfig

で解決しました。


参考:

GCCのビルド時に「../.././gcc/libgcc.mvars: そのようなファイルやディレクトリはありません」

configureやmakeを行う場所は、展開した「gcc-X.X.X」直下だとダメなようです。


以下のように、一階層下から実行すると回避できました。

$ cd gcc-4.6.4/
$ mkdir sh3-elf
$ cd sh3-elf/
$ ../configure --target=sh3-elf --disable-nls --disable-threads --disable-shared --enable-languages=c


参考:

備考

http://mes.sourceforge.jp/mes26/
こちらに、ビルド済みのgccbinutilsが配布されていますので、
それを使えばそもそも問題は無いです。


今回は、自前でビルドしてみたいなと思い、やってみました。


# あと、今、参考にしている書籍の関係で、
# binutilsgccを「--target=sh3-elf」でビルドしたかったのも理由の一つです。
#
# 配布されているビルド済みの「sh3-linux-gcc」は、
# 「-v」のオプションで表示させると、「sh3-linux」でビルドされているようです。
#
# といっても、targetオプションの「XXX-YYY-ZZZ」の意味をよく理解していないので、
# なんともいえないですが。