ESP8266系列-如何搭建自己平台的编译器



为什么要搭建自己平台的编译器

在乐鑫的官网上已经有公布了编译好的编译器,:http://bbs.espressif.com/viewtopic.php?f=57&t=2
但是这里的编译器只是适合32位系统上的;
image.png-39.4kB
若你的系统是64位的,或者你在其他的平台上,那么就得自己编译生成对应平台的编译器了;
下面以64位Ubuntu系统为例子,演示如何编译自己平台的esp8266编译器;

Ubuntu 基本信息

alex@alex-virtual-machine:/$ uname -a
Linux alex-virtual-machine 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
alex@alex-virtual-machine:/$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

第一步,安装编译需要的软件包

sudo apt-get install git autoconf build-essential gperf bison flex texinfo libtool libncurses5-dev wget gawk libc6-dev-amd64 python-serial libexpat-dev

第二步,下载编译器源码

# 创建源码目录
cd ~/
mkdir esptool/buildTool
cd esptool/buildTool
# 下载源码
git clone -b lx106 git://github.com/jcmvbkbc/crosstool-NG.git

一定要注意下载的是 lx106 分支的代码,因为ESP8266内置Tensilical L106 32bit微处理器

第三部,生成配置文件

cd crosstool-NG
#生成.configure
./bootstrap
#将检查依赖文件包,并通过--prefix配置安装路径为当前目录
#若有提示确实的依赖文件,解决方法:sudo apt-get install libtool
./configure --prefix=`pwd`
make
make install
./ct-ng xtensa-lx106-elf

运行过程日志

alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$ ./bootstrap
Running autoconf...
Done. You may now run:
    ./configure
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$ ./configure --prefix=`pwd`
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking whether sed understands -r -i -e... yes
checking whether ln -s works... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for ranlib... ranlib
checking for gobjcopy... no
checking for objcopy... objcopy
checking for absolute path to objcopy... /usr/bin/objcopy
checking for gobjdump... no
checking for objdump... objdump
checking for absolute path to objdump... /usr/bin/objdump
checking for greadelf... no
checking for readelf... readelf
checking for absolute path to readelf... /usr/bin/readelf
checking for gperf... gperf
checking for absolute path to gperf... /usr/bin/gperf
checking for bison... bison
checking for flex... flex
checking for makeinfo... makeinfo
checking for cut... cut
checking for stat... stat
checking for readlink... readlink
checking for wget... wget
checking for tar... tar
checking for gzip... gzip
checking for bzip2... bzip2
checking for patch... /usr/bin/patch
checking for bash >= 3.1... /bin/bash
checking for GNU awk... /usr/bin/gawk
checking for GNU make >= 3.80... /usr/bin/make
checking whether /usr/bin/make sets $(MAKE)... yes
checking for make 3.81... yes
checking for GNU libtool >= 1.5.26... /usr/bin/libtool
checking for GNU libtool >= 1.5.26... /usr/bin/libtool
checking for GNU libtoolize >= 1.5.26... /usr/bin/libtoolize
checking for GNU automake >= 1.10... /usr/bin/automake
checking for xz... xz
checking for cvs... no
checking for svn... no
checking for inline... inline
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible malloc... yes
checking for stdlib.h... (cached) yes
checking for GNU libc compatible realloc... yes
checking for size_t... yes
checking for working alloca.h... yes
checking for alloca... yes
checking libintl.h usability... yes
checking libintl.h presence... yes
checking for libintl.h... yes
checking whether gettext is declared... yes
checking ncurses/ncurses.h usability... no
checking ncurses/ncurses.h presence... no
checking for ncurses/ncurses.h... no
checking ncurses/curses.h usability... no
checking ncurses/curses.h presence... no
checking for ncurses/curses.h... no
checking ncursesw/curses.h usability... no
checking ncursesw/curses.h presence... no
checking for ncursesw/curses.h... no
checking ncurses.h usability... yes
checking ncurses.h presence... yes
checking for ncurses.h... yes
checking for library containing initscr... -lncurses
configure: creating ./config.status
config.status: creating Makefile
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$ make
  SED    'ct-ng'
  SED    'scripts/crosstool-NG.sh'
  SED    'scripts/saveSample.sh'
  SED    'scripts/showTuple.sh'
  GEN    'config/configure.in'
  GEN    'paths.mk'
  GEN    'paths.sh'
  SED    'docs/ct-ng.1'
  GZIP   'docs/ct-ng.1.gz'
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$ make install
  GEN    'config/configure.in'
  GEN    'paths.mk'
  GEN    'paths.sh'
  INST    'ct-ng'
  INSTDIR 'config/'
  INSTDIR 'contrib/'
  INSTDIR 'patches/'
  INSTDIR 'scripts/'
  INST    'steps.mk'
  INST    'paths'
  INSTDIR 'samples/'
  INST    'kconfig/'
  INST    'docs/*.txt'
  INST    'ct-ng.1.gz'

For auto-completion, do not forget to install 'ct-ng.comp' into
your bash completion directory (usually /etc/bash_completion.d)
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$ ./ct-ng xtensa-lx106-elf
  CONF  config/config.in
#
# configuration saved
#

***********************************************************

Initially reported by: Max Filippov 
URL: http://www.esp8266.com/viewtopic.php?f=9&t=224

***********************************************************

WARNING! This sample may enable experimental features.
         Please be sure to review the configuration prior
         to building and using your toolchain!
Now, you have been warned!

***********************************************************

Now configured for "xtensa-lx106-elf"

第四步,修改crosstool_NG配置&编译

  • 在crosstool-NG目录下执行./ct-ng menuconfig进行配置
./ct-ng menuconfig

image.png-87.3kB

  • 配置多线程编译
    “Paths and misc options”->“Number of parallel jobs”

image.png-128.4kB

  • 保存编译中间步骤
    “Paths and misc options”->“Debug crosstool-NG”->“Save intermediate steps”

image.png-141.1kB

  • 编译交叉编译工具(这个过程比较久,我编译了33分钟,要耐心等待~~~)
./ct-ng build

运行日志

alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$ ./ct-ng build
[INFO ]  Performing some trivial sanity checks
[INFO ]  Build started 20170730.163954
[INFO ]  Building environment variables
[INFO ]  =================================================================
[INFO ]  Retrieving needed toolchain components' tarballs
[INFO ]  Retrieving needed toolchain components' tarballs: done in 0.15s (at 00:17)
[INFO ]  =================================================================
[INFO ]  Extracting and patching toolchain components
[INFO ]  Extracting and patching toolchain components: done in 0.88s (at 00:18)
[INFO ]  Saving state to restart at step 'libc_check_config'...
[INFO ]  Saving state to restart at step 'companion_libs_for_build'...
[INFO ]  Saving state to restart at step 'binutils_for_build'...
[INFO ]  Saving state to restart at step 'companion_libs_for_host'...
[INFO ]  =================================================================
[INFO ]  Installing GMP for host
[INFO ]  Installing GMP for host: done in 139.49s (at 02:38)
[INFO ]  =================================================================
[INFO ]  Installing MPFR for host
[INFO ]  Installing MPFR for host: done in 51.10s (at 03:29)
[INFO ]  =================================================================
[INFO ]  Installing ISL for host
[INFO ]  Installing ISL for host: done in 109.78s (at 05:19)
[INFO ]  =================================================================
[INFO ]  Installing CLooG for host
[INFO ]  Installing CLooG for host: done in 19.59s (at 05:39)
[INFO ]  =================================================================
[INFO ]  Installing MPC for host
[INFO ]  Installing MPC for host: done in 21.96s (at 06:01)
[INFO ]  Saving state to restart at step 'binutils_for_host'...
[INFO ]  =================================================================
[INFO ]  Installing binutils for host
[INFO ]  Installing binutils for host: done in 209.94s (at 09:32)
[INFO ]  Saving state to restart at step 'cc_core_pass_1'...
[INFO ]  Saving state to restart at step 'kernel_headers'...
[INFO ]  Saving state to restart at step 'libc_start_files'...
[INFO ]  Saving state to restart at step 'cc_core_pass_2'...
[INFO ]  Saving state to restart at step 'libc'...
[INFO ]  Saving state to restart at step 'cc_for_build'...
[INFO ]  Saving state to restart at step 'cc_for_host'...
[INFO ]  =================================================================
[INFO ]  Installing final compiler
[INFO ]  Installing final compiler: done in 965.90s (at 26:12)
[INFO ]  Saving state to restart at step 'libelf_for_target'...
[INFO ]  Saving state to restart at step 'binutils_for_target'...
[INFO ]  Saving state to restart at step 'debug'...
[INFO ]  =================================================================
[INFO ]  Installing cross-gdb
[INFO ]  Installing cross-gdb: done in 336.23s (at 32:36)
[INFO ]  Saving state to restart at step 'test_suite'...
[INFO ]  Saving state to restart at step 'finish'...
[INFO ]  =================================================================
[INFO ]  Cleaning-up the toolchain's directory
[INFO ]    Stripping all toolchain executables
[INFO ]  Cleaning-up the toolchain's directory: done in 2.31s (at 33:07)
[INFO ]  Build completed at 20170730.171300
[INFO ]  (elapsed: 33:06.05)
[INFO ]  Finishing installation (may take a few seconds)...
[33:07] / alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG$

编译生成文件在根目录下的builds下,文件名为xtensa-lx106-elf

alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ ls
xtensa-lx106-elf

第五步,添加esp8266 依赖头文件

#编译ESP8266必须添加以下库文件,不然就会报找不到相应库(如: xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: cannot find -lhal)
wget https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a
wget https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a
wget https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz
## 设置可读可写可执行权限
sudo chmod -R u+x+r+w xtensa-lx106-elf/
sudo chmod 777 libc.a libhal.a
cp libc.a libhal.a xtensa-lx106-elf/xtensa-lx106-elf/sysroot/lib/
tar -xvf include.tgz
sudo chmod 777 include/*
sudo cp -rf include xtensa-lx106-elf/xtensa-lx106-elf/

过程日志

alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ wget https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a
--2017-07-30 17:19:01--  https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a
Resolving github.com (github.com)... 192.30.255.113, 192.30.255.112
Connecting to github.com (github.com)|192.30.255.113|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/esp8266/esp8266-wiki/master/libs/libc.a [following]
--2017-07-30 17:19:02--  https://raw.githubusercontent.com/esp8266/esp8266-wiki/master/libs/libc.a
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.72.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.72.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2754022 (2.6M) [application/octet-stream]
Saving to: ‘libc.a’

100%[=====================================================================================================>] 2,754,022   4.78KB/s   in 7m 38s

2017-07-30 17:26:41 (5.87 KB/s) - ‘libc.a’ saved [2754022/2754022]

alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ wget https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a
--2017-07-30 17:27:29--  https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a
Resolving github.com (github.com)... 192.30.255.112, 192.30.255.113
Connecting to github.com (github.com)|192.30.255.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/esp8266/esp8266-wiki/master/libs/libhal.a [following]
--2017-07-30 17:27:30--  https://raw.githubusercontent.com/esp8266/esp8266-wiki/master/libs/libhal.a
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.72.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.72.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 347624 (339K) [application/octet-stream]
Saving to: ‘libhal.a’

100%[=====================================================================================================>] 347,624     15.1KB/s   in 24s

2017-07-30 17:27:57 (13.9 KB/s) - ‘libhal.a’ saved [347624/347624]

alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ wget https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz
--2017-07-30 17:28:07--  https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz
Resolving github.com (github.com)... 192.30.255.112, 192.30.255.113
Connecting to github.com (github.com)|192.30.255.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/esp8266/esp8266-wiki/master/include.tgz [following]
--2017-07-30 17:28:08--  https://raw.githubusercontent.com/esp8266/esp8266-wiki/master/include.tgz
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.72.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.72.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 979111 (956K) [application/octet-stream]
Saving to: ‘include.tgz’

100%[=====================================================================================================>] 979,111     75.2KB/s   in 10s

2017-07-30 17:28:19 (93.5 KB/s) - ‘include.tgz’ saved [979111/979111]
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ ls
include.tgz  libc.a  libhal.a  xtensa-lx106-elf
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ sudo chmod -R u+x+r+w xtensa-lx106-elf/
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ cp libc.a libhal.a xtensa-lx106-elf/xtensa-lx106-elf/sysroot/lib/
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ sudo chmod 777 include/*
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ sudo cp -rf include xtensa-lx106-elf/xtensa-lx106-elf/

第六步,添加编译工具到环境变量

  • 把生成的编译器工具下的bin路径添加到环境变量中
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ ls
include  include.tgz  libc.a  libhal.a  xtensa-lx106-elf
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ echo "export PATH=~/esptool/buildTool/crosstool-NG/builds/xtensa-lx106-elf/bin:$PATH" >>~/.profile
alex@alex-virtual-machine:~/esptool/buildTool/crosstool-NG/builds$ source ~/.profile

编译验证

成功~
image.png-67.8kB

编译好编译工具

如果你不想走以上的流程,那么可以使用我编译好的,这个编译器可以在64位是Ubuntu1402上运行。

下载链接:http://pan.baidu.com/s/1jHLNQTo 密码:s70o

下载完成之后,参考第六步,把工具添加到环境变量中去。

hope you like it~
by AlexLin


文章作者: Alex.Lin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex.Lin !
 上一篇
Hexo自动备份教程 Hexo自动备份教程
2017-08-01 Alex.Lin
下一篇 
PC小技巧 PC小技巧
Win7 开热点 输入命令:netsh wlan set hostednetwork mode=allow ssid=livdran2012 key=100100100 。mode:是否启用虚拟WiFi网卡,改为disallow则为禁止。s
2017-07-01
  目录