[メモ]ラズパイで使用する bazelのビルド (on Tinkerboard)

# 概要

- 参考: [Build Bazel](https://github.com/samjabrahams/tensorflow-on-raspberry-pi/blob/master/GUIDE.md#3-build-bazel)
- ソースコード: https://github.com/bazelbuild/bazel.git
- 使うだけなら、

    ```bash:コピペ
# 25MB制限なので、分割してある.
wget https://github.com/mt08xx/files/raw/master/bazel_0.8.0-20171127.1_armhf.deb.a{a,b,c,d}
cat bazel_0.8.0-20171127.1_armhf.deb.a[a-d] > bazel_0.8.0-20171127.1_armhf.deb
rm bazel_0.8.0-20171127.1_armhf.deb.a*
sudo apt install ./bazel_0.8.0-20171127.1_armhf.deb
```



# 環境
- ビルド対象: `bazel-0.8.0`

- ビルド環境: Raspbian lite on <font color='blue'>**Tinkerboard**</font>
    - Raspbian: `2017-09-07-raspbian-stretch-lite.zip`
    - https://qiita.com/mt08/items/5aee9d3f0dc10a1c1c5a

- 実行環境: Raspbian on <font color='#C72C48'>**Raspberry Pi 3**</font>
    - Raspbian: `2017-09-07-raspbian-stretch.zip`

# ビルド手順

1. 必要なパッケージをインストール

    ```bash:他にも必要かも...
sudo apt install openjdk-8-jdk
sudo apt install pkg-config zip g++ zlib1g-dev unzip
```

2. ソース取得・解凍

    ```bash
cd ~
wget https://github.com/bazelbuild/bazel/releases/download/0.8.0/bazel-0.8.0-dist.zip
unzip -d bazel bazel-0.8.0-dist.zip
```

3. コード修正

	1. `scripts/bootstrap/compile.sh` : `-J-Xmx500M` を追加する

		```bash:scripts/bootstrap/compile.sh
		run "${JAVAC}" -classpath "${classpath}" -sourcepath "${sourcepath}" \
			  -d "${output}/classes" -source "$JAVA_VERSION" -target "$JAVA_VERSION" \
			  -encoding UTF-8 "@${paramfile}" -J-Xmx500M
		```

	2. `tools/cpp/lib_cc_configure.bzl` : `_get_cpu_value`関数の最初のほうに、`return "arm"`を挿入


4. ビルド開始:<br> `time sudo ./compile`
5. 確認

    ```shell-session
pi@raspberrypi:~/bazel $ ls -l output/bazel 
-rwxr-xr-x 1 root root 91024013 Nov 27 18:31 output/bazel
pi@raspberrypi:~/bazel $ 
```


5. debパッケージ作成

	1. パッケージ名:(`bazel`) バージョン(`0.8.0-20171127.1`)を決める.
	
	2. フォルダ作成

		```bash
		PACKAGE_NAME=bazel_0.8.0-20171127.1_armhf
		mkdir -p ~/${PACKAGE_NAME}{/usr/local,DEBIAN}
		cd ~/${PACKAGE_NAME}
		```



	3. `DEBIAN/control`ファイル作成

		```txt:DEBIAN/control例(適当です)
		Package: bazel
		Priority: extra
		Section: universe/devel
		Maintainer: mt08 <mt08xx@users.noreply.github.com>
		Architecture: armhf
		Version: 0.8.0-20171127.1
		Homepage: https://bazel.build/
		Description: Bazel is an open-source build and test tool similar to
		 Make, Maven, and Gradle. It uses a human-readable, high-level build
		 language. Bazel supports projects in multiple languages and builds
		 outputs for multiple platforms. Bazel supports large codebases across
		 multiple repositories, and large numbers of users.
		```



	4. ` ~/${PACKAGE_NAME}/usr/local` 以下に、ファイルを入れる。

		```bash:今回はファイル一つ.ほんとは、ドキュメントとかも入れないといけないと思う。
		mkdir  ~/${PACKAGE_NAME}/usr/local/bin
		cp ~/bazel/output/bazel ~/${PACKAGE_NAME}/usr/local/bin
		```


	5. debパッケージビルド

		```bash
		cd ~
		sudo chown -R root:staff ${PACKAGE_NAME}
		sudo dpkg-deb --build ${PACKAGE_NAME}
		```

	6 確認
	
		```shell-session
		pi@raspberrypi:~ $ ls -l ./bazel*.deb
		-rw-r--r-- 1 root root 88820222 Nov 27 18:56 ./bazel_0.8.0-20171127.1_armhf.deb
		pi@raspberrypi:~ $ 
		```

7. 別のマシンにもっていって、インストール確認

	```shell-session:実行例
	pi@raspberrypi:~ $ uname -a
	Linux raspberrypi 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l GNU/Linux
	pi@raspberrypi:~ $ sudo apt install ./bazel_0.8.0-20171127.1_armhf.deb
	Reading package lists... Done
	Building dependency tree       
	Reading state information... Done
	Note, selecting 'bazel' instead of './bazel_0.8.0-20171127.1_armhf.deb'
	The following NEW packages will be installed:
	  bazel
	0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
	Need to get 0 B/88.8 MB of archives.
	After this operation, 0 B of additional disk space will be used.
	Get:1 /home/pi/bazel_0.8.0-20171127.1_armhf.deb bazel armhf 0.8.0-20171127.1 [88.8 MB]
	Selecting previously unselected package bazel.
	(Reading database ... 105718 files and directories currently installed.)
	Preparing to unpack .../bazel_0.8.0-20171127.1_armhf.deb ...
	Unpacking bazel (0.8.0-20171127.1) ...
	Setting up bazel (0.8.0-20171127.1) ...
	pi@raspberrypi:~ $ bazel
	Extracting Bazel installation...
												   [bazel release 0.8.0- (@non-git)]
	Usage: bazel <command> <options> ...

	Available commands:
	  analyze-profile     Analyzes build profile data.
	  build               Builds the specified targets.
	  canonicalize-flags  Canonicalizes a list of bazel options.
	  clean               Removes output files and optionally stops the server.
	  coverage            Generates code coverage report for specified test targets.
	  dump                Dumps the internal state of the bazel server process.
	  fetch               Fetches external repositories that are prerequisites to the targets.
	  help                Prints help for commands, or the index.
	  info                Displays runtime info about the bazel server.
	  license             Prints the license of this software.
	  mobile-install      Installs targets to mobile devices.
	  print_action        Prints the command line args for compiling a file.
	  query               Executes a dependency graph query.
	  run                 Runs the specified target.
	  shutdown            Stops the bazel server.
	  test                Builds and runs the specified test targets.
	  version             Prints version information for bazel.

	Getting more help:
	  bazel help <command>
					   Prints help and options for <command>.
	  bazel help startup_options
					   Options for the JVM hosting bazel.
	  bazel help target-syntax
					   Explains the syntax for specifying targets.
	  bazel help info-keys
					   Displays a list of keys used by the info command.
	pi@raspberrypi:~ $ sudo apt remove -y bazel
	Reading package lists... Done
	Building dependency tree       
	Reading state information... Done
	The following packages will be REMOVED:
	  bazel
	0 upgraded, 0 newly installed, 1 to remove and 1 not upgraded.
	After this operation, 0 B of additional disk space will be used.
	(Reading database ... 105719 files and directories currently installed.)
	Removing bazel (0.8.0-20171127.1) ...
	pi@raspberrypi:~ $ bazel
	-bash: /usr/local/bin/bazel: No such file or directory
	pi@raspberrypi:~ $ 
	```

# TODO
- 今回、時間計測を記録し忘れた..次回..