由于PC版Linux多数内核为X86或X64,而目标芯片为ARMv7,直接编译出来的版本,是无法直接用于芯片的,所以,需要配置交叉编译环境。
安装交叉编译环境步骤如下:
1、安装Bazel
方法一:参考该链接:https://www.cnblogs.com/jimchen1218/p/11551380.html ,第3小节。
方法二:
1) sudo apt-get install openjdk-8-jdk
2) 添加Bazel源:
sudo apt-get install curl
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
3) 安装Bazel
sudo apt-get update
sudo apt-get install bazel (安装过程中,出现googleapi网站失败问题,尝试多次几次)
4) 升级 Bazel
sudo apt-get upgrade bazel
2、设置交叉编译链
当前交叉编译SDK包目录:/home/jim/tf2arm/sysroots/x86_64-linux
在/etc/bash.bashrc最后增加如下指令:
export ARCH=arm export PATH=/home/jim/tf2arm/sysroots/x86_64-linux/bin/:$PATH export CROSS_COMPILE=arm-poky-linux-gnueabi- export CC=/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-gcc export CXX=/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-g++ export LD=/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-ld export AR=/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-ar export AS=/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-as export RANLIB=/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-ranlib
# 修改完成之后需要重启命令行才能生效
# 通过如下指令来确认交叉编译链是否已经设置好
echo $CC
显示/home/jim/tf2arm/sysroots/x86_64-linux/bin/arm-poky-linux-gnueabi-gcc时表示交叉编译链已经设置好
当需要更换为本机编译时屏蔽上面的指令即可
3、准备交叉编译
以下为需要新建与修改的交叉编译相关的Bazel配置脚本:
./WORKSPACE
./arm_compiler
../BUILD
../build_armv7.sh
../cross_toolchain_target_armv7.BUILD
../CROSSTOOL
3.1 修改WORKSPACE
在Tensorflow根目录下添加如下内容:
new_local_repository( name ='toolchain_target_armv7', #交叉编译器别名 path ='/home/jim/tf2arm/sysroots/x86_64-linux', #交叉编译器路径,可根据需要自行修改 build_file = 'arm_compiler/cross_toolchain_target_armv7.BUILD' #交叉编译器描述文件 )
3.2、新建交叉编译器描述文件:
新建cross_toolchain_target_armv7.BUILD:
1 major_version: "local" 2 minor_version: "" 3 default_target_cpu: "armv7" 4 5 default_toolchain { 6 cpu: "armv7" 7 toolchain_identifier: "arm-poky-linux-gnueabi" 8 } 9 10 default_toolchain { 11 cpu: "k8" 12 toolchain_identifier: "local" 13 } 14 15 toolchain { 16 abi_version: "gcc" 17 abi_libc_version: "glibc_2.23" 18 builtin_sysroot: "" 19 compiler: "compiler" 20 host_system_name: "armv7" 21 needsPic: true 22 supports_gold_linker: false 23 supports_incremental_linker: false 24 supports_fission: false 25 supports_interface_shared_objects: false 26 supports_normalizing_ar: true 27 supports_start_end_lib: false 28 supports_thin_archives: true 29 target_libc: "glibc_2.23" 30 target_cpu: "armv7" 31 target_system_name: "armv7" 32 toolchain_identifier: "arm-poky-linux-gnueabi" 33 34 tool_path { name: "ar" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-ar" } 35 tool_path { name: "compat-ld" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-ld" } 36 tool_path { name: "cpp" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-cpp" } 37 tool_path { name: "dwp" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-dwp" } 38 tool_path { name: "gcc" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc" } 39 tool_path { name: "gcov" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcov" } 40 tool_path { name: "ld" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-ld" } 41 tool_path { name: "nm" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-nm" } 42 tool_path { name: "objcopy" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-objcopy" } 43 objcopy_embed_flag: "-I" 44 objcopy_embed_flag: "binary" 45 tool_path { name: "objdump" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-objdump" } 46 tool_path { name: "strip" path: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-strip" } 47 48 compiler_flag: "-nostdinc" 49 compiler_flag: "-isystem" 50 compiler_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include" 51 compiler_flag: "-isystem" 52 compiler_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/include" 53 compiler_flag: "-isystem" 54 compiler_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/include-fixed" 55 compiler_flag: "-isystem" 56 compiler_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include/c++/5.3.0" 57 compiler_flag: "-isystem" 58 compiler_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include/c++/5.3.0/arm-poky-linux-gnueabi" 59 60 cxx_flag: "-isystem" 61 cxx_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include" 62 cxx_flag: "-isystem" 63 cxx_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/include" 64 cxx_flag: "-isystem" 65 cxx_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/include-fixed" 66 cxx_flag: "-isystem" 67 cxx_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include/c++/5.3.0" 68 cxx_flag: "-isystem" 69 cxx_flag: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include/c++/5.3.0/arm-poky-linux-gnueabi" 70 cxx_flag: "-std=c++11" 71 72 cxx_builtin_include_directory: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include" 73 cxx_builtin_include_directory: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/include" 74 cxx_builtin_include_directory: "/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/5.3.0/include-fixed" 75 cxx_builtin_include_directory: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include/c++/5.3.0" 76 cxx_builtin_include_directory: "/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/include/c++/5.3.0/arm-poky-linux-gnueabi" 77 78 79 linker_flag: "-lstdc++" 80 linker_flag: "-L/home/lyra/tf2arm/sysroots/x86_64-linux/lib" 81 linker_flag: "-L/home/lyra/tf2arm/sysroots/x86_64-linux/usr/lib" 82 linker_flag: "-L/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/lib" 83 linker_flag: "-L/home/lyra/tf2arm/sysroots/x86_64-linux/cortexa9hf/usr/lib" 84 linker_flag: "-Wl,--dynamic-linker=/lib/ld-linux-aarch64.so.1" 85 86 # Anticipated future default. 87 # This makes GCC and Clang do what we want when called through symlinks. 88 unfiltered_cxx_flag: "-no-canonical-prefixes" 89 linker_flag: "-no-canonical-prefixes" 90 91 # Make C++ compilation deterministic. Use linkstamping instead of these 92 # compiler symbols. 93 unfiltered_cxx_flag: "-Wno-builtin-macro-redefined" 94 unfiltered_cxx_flag: "-D__DATE__=\"redacted\"" 95 unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\"" 96 unfiltered_cxx_flag: "-D__TIME__=\"redacted\"" 97 98 # Security hardening on by default. 99 # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases. 100 # We need to undef it before redefining it as some distributions now have 101 # it enabled by default. 102 compiler_fl