4
1

More than 1 year has passed since last update.

RaspberryPi 用のクロスツールチェインを Ubuntu 上にビルド

Last updated at Posted at 2021-04-10

「 Building a cross toolchain for Raspberry Pi 3 Model B」
https://www.nayab.xyz/rpi3b-elinux/embedded-linux-rpi3-010-toolchain.html

に沿って作業してみる。

環境

  • 母艦 Ubuntu Linux 20.04 LTS AMD64 日本語版 ( also Windows WSL2 )
  • ターゲット Raspberry Pi 3 ( also Raspberry Pi 4 )

事前のインストール

作業していたら出てきたエラー

  • コマンド 'git' が見つかりません。

  • ./bootstrap: 行 821: autoreconf: コマンドが見つかりません

  • configure: error: missing required tool: flex

  • configure: error: missing required tool: makeinfo

  • configure: error: missing required tool: help2man

  • configure: error: Required tool not found: GNU awk

  • checking for GNU libtool >= 2.4... no configure: error: Required tool not found: libtool

  • configure: error: curses library not found

  • /bin/bash: yacc: コマンドが見つかりません

のために、以下のようにインストールしています。


$ sudo apt install build-essential git autoconf flex texinfo help2man gawk libtool-bin libncurses-dev bison make autoconf automake libtool 

ここで、 libtool-bin についてメモ。


$ sudo apt install libtool
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
libtool はすでに最新バージョン (2.4.6-14) です。
アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 292 個。

としても、


checking for GNU libtool >= 2.4... no
configure: error: Required tool not found: libtool

となりました。
これは libtool-bin ではなく libtool だけだとこうなるようです。

作業場所作成

今回は、RaspberryPi3をターゲットにするために rpi3 というフォルダにします。RaspberryPi4などをターゲットにする場合は rpi4 などにするのがいいでしょう。


$ mkdir ~/rpi3
$ cd ~/rpi3

ダウンロード


$ git clone https://github.com/crosstool-ng/crosstool-ng.git
$ cd crosstool-ng/

checkout

記事詩筆時には 75d7525a として checkout しました(気がしますが、記録が不完全で自信がありませんトホホ)

$ git checkout 75d7525a

なお、ブート環境の開発などを行う場合は以下を選択しました。(上記の75d7525a だと、RaspberryPi4 に対応できていないようだとのことです。)

$ git checkout crosstool-ng-1.25.0 -b 1.25.0

crosstool-ng をインストール

crosstool-ng はツールチェインを生成する専用コマンドです。

$ ./bootstrap
$ ./configure --enable-local
$ make -j`nproc`

ビルドツールを設定

ターゲットが RaspberryPi3 を使うので、以下のようにしました。


$ ./ct-ng aarch64-rpi3-linux-gnu

ターゲットが RaspberyPi4 の場合は以下を選んでみます。


$ ./ct-ng aarch64-rpi4-linux-gnu

その後


$ ./ct-ng menuconfig

とします。
画面が以下のように出るので


 .config - crosstool-NG 1.24.0.299_6729a76 Configuration
 ──────────────────────────────────────────────────────────────────────────────
  ┌───────────── crosstool-NG 1.24.0.299_6729a76 Configuration ─────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty │  
  │  submenus ----).  Highlighted letters are hotkeys.  Pressing <Y>        │  
  │  includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to │  
  │  exit, <?> for Help, </> for Search.  Legend: [*] built-in  [ ]         │  
  │ ┌─────────────────────────────────────────────────────────────────────┐ │  
  │ │        Paths and misc options  --->                                 │ │  
  │ │        Target options  --->                                         │ │  
  │ │        Toolchain options  --->                                      │ │  
  │ │        Operating System  --->                                       │ │  
  │ │        Binary utilities  --->                                       │ │  
  │ │        C-library  --->                                              │ │  
  │ │        C compiler  --->                                             │ │  
  │ │        Debug facilities  --->                                       │ │  
  │ │        Companion libraries  --->                                    │ │  
  │ │        Companion tools  --->                                        │ │  
  │ └─────────────────────────────────────────────────────────────────────┘ │  
  ├─────────────────────────────────────────────────────────────────────────┤  
  │        <Select>    < Exit >    < Help >    < Save >    < Load >         │  
  └─────────────────────────────────────────────────────────────────────────┘  
  • Path and misc options で Maximum log level to see: (EXTRA) を DEBUG に
  • Operating System で Version of linux を確認、実験環境では 5.10.7 でした。
  • Debug facilities では gdb にチェックが付いているのを消して、全てなしにしました。

なお、RaspberryPiのKernelをビルドする際に Operating Syatem で 5.10.7 だと問題があり、以下のように結構古いバージョンにしました。

   ┌────────────────────── Version of linux ───────────────────────┐
   │  Use the arrow keys to navigate this window or press the      │  
   │  hotkey of the item you wish to select followed by the <SPACE │  
   │  BAR>. Press <?> for additional information about this        │  
   │ ┌───────────────────────────^(-)────────────────────────────┐ │  
   │ │                       ( ) 5.1.21                          │ │  
   │ │                       ( ) 5.0.19                          │ │  
   │ │                       (X) 4.20.17                         │ │  
   │ │                       ( ) 4.19.229                        │ │  
   │ │                       ( ) 4.18.20                         │ │  
   │ │                       ( ) 4.17.19                         │ │  
   │ └───────────────────────────v(+)────────────────────────────┘ │  
   ├───────────────────────────────────────────────────────────────┤  
   │                    <Select>      < Help >                     │  
   └───────────────────────────────────────────────────────────────┘  
                                                          
                                                                                              

なお、2022/10/15 に行った低レベル勉強会では RaspberryPi4をターゲットにした場合は以下のようなサジェスチョンを頂いています。

設定が終わったら、 Save して、 .config を選び、Exit

ツールチェインをビルド


$ ./ct-ng build

とするとビルドが始まります。

2022/10/29追記

[DEBUG]    ==> Return status 8
[DEBUG]    Not at this location: "https://www.zlib.net//zlib-1.2.12.tar.gz"
[ERROR]    zlib: download failed
[ERROR]  

というエラーが出ました。
あらかじめ /rpi3/crosstool-ng/.build/tarballsに、 $ として、ダウンロードしておきます。

無限の時が流れて


.
.
.
[ALL  ]    '/home/nanbuwks/rpi3/crosstool-ng/licenses.d/by-sa/legalcode_files/deeds.css' -> '/home/nanbuwks/x-tools/aarch64-rpi3-linux-gnu/share/licenses/crosstool-ng/licenses.d/by-sa/legalcode_files/deeds.css'
[ALL  ]    '/home/nanbuwks/rpi3/crosstool-ng/licenses.d/by-sa/legalcode_files/logo_code.gif' -> '/home/nanbuwks/x-tools/aarch64-rpi3-linux-gnu/share/licenses/crosstool-ng/licenses.d/by-sa/legalcode_files/logo_code.gif'
[DEBUG]    ==> Return status 0
[INFO ]  Finalizing the toolchain's directory: done in 9.37s (at 383:32)
[INFO ]  Build completed at 20210411.070001
[INFO ]  (elapsed: 383:31.51)
[INFO ]  Finishing installation (may take a few seconds)..

ビルドが終わりました。
~/x-tools/ に生成されています。


$ ls ~/x-tools/
aarch64-rpi3-linux-gnu

パスを通しておきましょう


$ echo 'export PATH=$PATH:$HOME/x-tools/aarch64-rpi3-linux-gnu/bin/' >> ~/.bashrc
$ source ~/.bashrc

テスト


$ cd ~/rpi3
$ vim dummy.c

として、以下の C ソースファイルを作ります。


int main(void)
{
	return 0;
}

以下のようにしてクロスビルドします。


$ aarch64-rpi3-linux-gnu-gcc dummy.c -o dummy

バイナリができたようですようです。

$ ls
crosstool-ng  dummy  dummy.c

ファイルを検査すると ARM aarch64 となっていて、うまくいっているようです。


$ file dummy
dummy: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 5.10.7, with debug_info, not stripped

できました。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1