ASUS TinkerBoard: カーネル セルフビルド/Building Kernel Locally on Tinker Board

## 概要/Overview
- Tinkerboardで、カーネルをセルフビルド
- Building Kernel Locally on Tinker Board
- Small patch: to avoid warning of '-Wmisleading-indentation' with GCC6<br>(disabled by '-Wno-misleading-indentation')

## 環境/Environment
- ASUS TinkerBoard
-  TinkerOS_Debian V1.8 (`20170417-tinker-board-linaro-stretch-alip-v1.8.zip`)
- Kernel source code: https://github.com/TinkerBoard/debian_kernel

	```shell-session
	linaro@linaro-alip:~/debian_kernel$ git log -n 1
	commit 4431f98d9cd1695c7d28a02ff77f1dad20d4a56b
	Author: andy_chi <andy_chi@asus.com>
	Date:   Mon Apr 17 12:00:22 2017 +0800

		Etherent: disable EEE LED mode
		
		Change-Id: I44f1ead2138f716c4af092b0daf7b1d1141c7f0c
	linaro@linaro-alip:~/debian_kernel$ 

	# Kernel version (TinkerOS v1.8)
	linaro@linaro-alip:~$ uname -a
	Linux linaro-alip 4.4.16-00006-g4431f98-dirty #1 SMP Mon Apr 17 17:27:25 CST 2017 armv7l GNU/Linux
	```
## 手順/Steps
1.　Burn TinkerOS_Debian V1.8 to microSD, and boot
2.　パッケージ更新

	```bash:Update_packages(Optional??)
	sudo apt update && sudo apt upgrade -y && sudo reboot
	```
3.　必要なパッケージインストール

	```bash:Install_packages_for_kernel_build
	sudo apt update && sudo apt install -y git-core build-essential bc libssl-dev device-tree-compiler
	```
4.　ソース取得

	```bash:Kernel_source_code
	git clone --depth 1 -b linux4.4-rk3288 https://github.com/TinkerBoard/debian_kernel.git
	# Move to source code directory
	cd debian_kernel
	```
5.　Patch (GCC6: avoiding warning of `'-Wmisleading-indentation'`)

	```bash:Copy_And_Paste
	cat <<EOF | patch -p1
	diff --git a/drivers/media/i2c/soc_camera/rockchip/Makefile b/drivers/media/i2c/soc_camera/rockchip/Makefile
	index 7933bcd1..2486999a 100755
	--- a/drivers/media/i2c/soc_camera/rockchip/Makefile
	+++ b/drivers/media/i2c/soc_camera/rockchip/Makefile
	@@ -1,2 +1,3 @@
	 obj-\$(CONFIG_VIDEO_OV8858) += ov_camera_module.o rk_camera_module.o ov8858_v4l2-i2c-subdev.o
	 obj-\$(CONFIG_VIDEO_IMX219) += imx_camera_module.o rk_camera_module.o imx219_v4l2-i2c-subdev.o
	+ccflags-y += -Wno-misleading-indentation # GCC6
	diff --git a/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile b/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile
	index 40715655..bd33c700 100644
	--- a/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile
	+++ b/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile
	@@ -16,6 +16,7 @@ EXTRA_CFLAGS += -Wno-unused
	 #EXTRA_CFLAGS += -Wno-uninitialized
	 #EXTRA_CFLAGS += -Wno-error=date-time  # Fix compile error on gcc 4.9 and later
	
	+EXTRA_CFLAGS += -Wno-misleading-indentation # GCC6
	 EXTRA_CFLAGS += -I\$(src)/include
	 EXTRA_CFLAGS += -I\$(src)/hal/phydm
	EOF
	```
5.　Edit `Makefile` for `EXTRAVERSION` (Optional)

	```Makefile:Makefile(Example)
	VERSION = 4
	PATCHLEVEL = 4
	SUBLEVEL = 16
	EXTRAVERSION = mt08      # <--- example
	NAME = Blurry Fish Butt
	
	...
	...
	...
	```
6.　ビルド

	```bash:Build
	# .config
	make ARCH=arm miniarm-rk3288_defconfig

	# zImage
	time make ARCH=arm -j6 zImage 2>&1 | tee 1.log
#real    25m12.322s
#user    78m3.870s
#sys    4m43.920s

	# modules
	time make ARCH=arm -j6 modules 2>&1 | tee 2.log
#real	1m35.393s
#user	4m26.670s
#sys	0m24.790s

	# dtbs
	time make ARCH=arm dtbs 2>&1 | tee 3.log
#real	0m8.204s
#user	0m3.110s
#sys	0m1.100s
	```
6.　インストール

	```bash:Install
	# Modules
	sudo make ARCH=arm modules_install

	# Backup: old kernel & dtb
	sudo mv -v /boot/zImage{,.bak}
	sudo mv -v /boot/rk3288-miniarm.dtb{,.bak}

	# Copy: new kernel & dtb
	sudo cp -v arch/arm/boot/{zImage,dts/rk3288-miniarm.dtb} /boot

	```
7.　`sudo reboot`
8.　:-)

    ```shell-session
linaro@linaro-alip:~$ uname -a
Linux linaro-alip 4.4.16mt08-dirty #1 SMP Tue Jun 13 17:47:01 UTC 2017 armv7l GNU/Linux
linaro@linaro-alip:~$ 
```


## その他
- diff

	```diff:diff
	diff --git a/drivers/media/i2c/soc_camera/rockchip/Makefile b/drivers/media/i2c/soc_camera/rockchip/Makefile
	index 7933bcd1..2486999a 100755
	--- a/drivers/media/i2c/soc_camera/rockchip/Makefile
	+++ b/drivers/media/i2c/soc_camera/rockchip/Makefile
	@@ -1,2 +1,3 @@
	 obj-$(CONFIG_VIDEO_OV8858) += ov_camera_module.o rk_camera_module.o ov8858_v4l2-i2c-subdev.o
	 obj-$(CONFIG_VIDEO_IMX219) += imx_camera_module.o rk_camera_module.o imx219_v4l2-i2c-subdev.o
	+ccflags-y += -Wno-misleading-indentation # GCC6
	diff --git a/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile b/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile
	index 40715655..bd33c700 100644
	--- a/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile
	+++ b/drivers/net/wireless/rockchip_wlan/rtl8723bs/Makefile
	@@ -16,6 +16,7 @@ EXTRA_CFLAGS += -Wno-unused
	 #EXTRA_CFLAGS += -Wno-uninitialized
	 #EXTRA_CFLAGS += -Wno-error=date-time  # Fix compile error on gcc 4.9 and later
	
	+EXTRA_CFLAGS += -Wno-misleading-indentation # GCC6
	 EXTRA_CFLAGS += -I$(src)/include
	 EXTRA_CFLAGS += -I$(src)/hal/phydm
	```

- gcc version

	```shell-session
	#
	# gcc: included in TinkerOS v1.8
	linaro@linaro-alip:~$ gcc -v
	Using built-in specs.
	COLLECT_GCC=gcc
	COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/6/lto-wrapper
	Target: arm-linux-gnueabihf
	Configured with: ../src/configure -v --with-pkgversion='Debian 6.3.0-12' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=arm-linux-gnueabihf- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
	Thread model: posix
	gcc version 6.3.0 20170406 (Debian 6.3.0-12) 

	#
	# After apt upgrade (on Jun-13-2017)
	linaro@linaro-alip:~$ arm-linux-gnueabihf-gcc -v
	Using built-in specs.
	COLLECT_GCC=arm-linux-gnueabihf-gcc
	COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/6/lto-wrapper
	Target: arm-linux-gnueabihf
	Configured with: ../src/configure -v --with-pkgversion='Debian 6.3.0-18' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=arm-linux-gnueabihf- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-sjlj-exceptions --with-arch=armv7-a --with-fpu=vfpv3-d16 --with-float=hard --with-mode=thumb --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
	Thread model: posix
	gcc version 6.3.0 20170516 (Debian 6.3.0-18) 
	```

- `Vagrantfile` (For Vagrant + Virtualbox)

	```rb
	# Vagrantfile
	# mt08

	VB_NAME="Build-TinkerBoard-debian_kernel"
	VM_MEMORY=8192
	VM_CORES=8

	Vagrant.configure("2") do |config|
		config.vm.box = "ubuntu/xenial64"
		config.ssh.username = 'ubuntu'
		#config.vm.box = "bento/ubuntu-16.04"

		config.vm.provider "virtualbox" do |vb|
			vb.gui    = false 
			vb.name   = VB_NAME
			vb.memory = VM_MEMORY
			vb.cpus   = VM_CORES
		end

		config.vm.provision "shell", inline: <<-SHELL
			echo 'Acquire::http::Proxy "http://linux64:3142";' | tee /etc/apt/apt.conf.d/02proxy

			apt-get update
			apt-get upgrade -y
			apt-get install -y git build-essential bc libssl-dev python gcc-arm-linux-gnueabihf device-tree-compiler
		SHELL

		config.vm.provision "shell", privileged: false, inline: <<-SHELL
			cd ${HOME}
			git clone --depth 1 -b linux4.4-rk3288 https://github.com/TinkerBoard/debian_kernel.git
			cd debian_kernel
			
			# Build Kernel
			make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- miniarm-rk3288_defconfig
			bash -c "time make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage  -j10" 2>&1 | tee 1.log
			bash -c "time make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- modules -j10" 2>&1 | tee 2.log
			bash -c "time make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- dtbs        " 2>&1 | tee 3.log

			# Copy Objects
			cd ${HOME}/debian_kernel
			DIR_INSTALL=rk3288-miniarm-$(date +%s)
			mkdir -p ${DIR_INSTALL}
			make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=/tmp/${DIR_INSTALL}  modules_install
			cp -v arch/arm/boot/zImage arch/arm/boot/dts/rk3288-miniarm.dtb /tmp/${DIR_INSTALL}
			tar zcvf /vagrant/${DIR_INSTALL}.tgz /tmp/${DIR_INSTALL}
			rm -rf /tmp/${DIR_INSTALL}
		SHELL
	end
	```

	```shell-session:vagrant-up
	mt08@windows-pc MINGW64 /c/Vagrant/TB-Dev
	$ vagrant destroy -f
	==> default: Destroying VM and associated drives...

	mt08@windows-pc MINGW64 /c/Vagrant/TB-Dev
	$ time vagrant up
	Bringing machine 'default' up with 'virtualbox' provider...
	==> default: Importing base box 'ubuntu/xenial64'...

	...
	...

	==> default: /tmp/rk3288-miniarm-1497400581/zImage
	==> default: /tmp/rk3288-miniarm-1497400581/rk3288-miniarm.dtb

	real    6m42.125s
	user    0m0.000s
	sys     0m0.000s

	mt08@windows-pc MINGW64 /c/Vagrant/TB-Dev
	$ ls -1 ./rk3288-miniarm-*
	./rk3288-miniarm-1497400581.tgz

	mt08@windows-pc MINGW64 /c/Vagrant/TB-Dev
	```