由於PC版Linux多數內核為X86或X64,而目標芯片為ARMv7,直接編譯出來的版本,是無法直接用於芯片的,所以,需要配置交叉編譯環境。
參考了該鏈接:https://www.cnblogs.com/jojodru/p/7744630.html,根據自己編譯環境搭建的心得,寫下此文,用來與同行人共勉!
安裝交叉編譯環境步驟如下:
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_flag: "-U_FORTIFY_SOURCE" 103 compiler_flag: "-fstack-protector" 104 compiler_flag: "-fPIE" 105 linker_flag: "-pie" 106 linker_flag: "-Wl,-z,relro,-z,now" 107 108 # Enable coloring even if there's no attached terminal. Bazel removes the 109 # escape sequences if --nocolor is specified. 110 compiler_flag: "-fdiagnostics-color=always" 111 112 # All warnings are enabled. Maybe enable -Werror as well? 113 compiler_flag: "-Wall" 114 # Enable a few more warnings that aren't part of -Wall. 115 compiler_flag: "-Wunused-but-set-parameter" 116 # But disable some that are problematic. 117 compiler_flag: "-Wno-free-nonheap-object" # has false positives 118 119 # Keep stack frames for debugging, even in opt mode. 120 compiler_flag: "-fno-omit-frame-pointer" 121 122 # Stamp the binary with a unique identifier. 123 linker_flag: "-Wl,--build-id=md5" 124 linker_flag: "-Wl,--hash-style=gnu" 125 126 compilation_mode_flags { 127 mode: DBG 128 # Enable debug symbols. 129 compiler_flag: "-g" 130 } 131 compilation_mode_flags { 132 mode: OPT 133 134 # No debug symbols. 135 # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or 136 # even generally? However, that can't happen here, as it requires special 137 # handling in Bazel. 138 compiler_flag: "-g0" 139 140 # Conservative choice for -O 141 # -O3 can increase binary size and even slow down the resulting binaries. 142 # Profile first and / or use FDO if you need better performance than this. 143 compiler_flag: "-O3" 144 145 # Disable assertions 146 compiler_flag: "-DNDEBUG -mcpu=cortex-a9 -mfpu=neon" #default value:cortex-a7 147 #-mcpu=cortex-m3 need compile with -mthumb 148 149 # Removal of unused code and data at link time 150 # Uncomment fllowing flag when software deploy finally 151 # compiler_flag: "-ffunction-sections" 152 # compiler_flag: "-fdata-sections" 153 # linker_flag: "-Wl,--gc-sections" 154 } 155 } 156 157 158 toolchain { 159 toolchain_identifier: "local" 160 abi_libc_version: "local" 161 abi_version: "local" 162 builtin_sysroot: "" 163 compiler: "compiler" 164 compiler_flag: "-U_FORTIFY_SOURCE" 165 compiler_flag: "-D_FORTIFY_SOURCE=2" 166 compiler_flag: "-fstack-protector" 167 compiler_flag: "-Wall" 168 compiler_flag: "-Wl,-z,-relro,-z,now" 169 compiler_flag: "-B/usr/bin" 170 compiler_flag: "-B/usr/bin" 171 compiler_flag: "-Wunused-but-set-parameter" 172 compiler_flag: "-Wno-free-nonheap-object" 173 compiler_flag: "-fno-omit-frame-pointer" 174 compiler_flag: "-isystem" 175 compiler_flag: "/usr/include" 176 cxx_builtin_include_directory: "/usr/include/c++/5.4.0" 177 cxx_builtin_include_directory: "/usr/include/c++/5" 178 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5/include" 179 cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/5.4.0" 180 cxx_builtin_include_directory: "/usr/include/c++/5.4.0/backward" 181 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/include" 182 cxx_builtin_include_directory: "/usr/local/include" 183 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/include-fixed" 184 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed" 185 cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu" 186 cxx_builtin_include_directory: "/usr/include" 187 cxx_flag: "-std=c++11" 188 host_system_name: "local" 189 linker_flag: "-lstdc++" 190 linker_flag: "-lm" 191 linker_flag: "-Wl,-no-as-needed" 192 linker_flag: "-B/usr/bin" 193 linker_flag: "-B/usr/bin" 194 linker_flag: "-pass-exit-codes" 195 needsPic: true 196 objcopy_embed_flag: "-I" 197 objcopy_embed_flag: "binary" 198 supports_fission: false 199 supports_gold_linker: false 200 supports_incremental_linker: false 201 supports_interface_shared_objects: false 202 supports_normalizing_ar: false 203 supports_start_end_lib: false 204 supports_thin_archives: false 205 target_cpu: "k8" 206 target_libc: "local" 207 target_system_name: "local" 208 unfiltered_cxx_flag: "-fno-canonical-system-headers" 209 unfiltered_cxx_flag: "-Wno-builtin-macro-redefined" 210 unfiltered_cxx_flag: "-D__DATE__=\"redacted\"" 211 unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\"" 212 unfiltered_cxx_flag: "-D__TIME__=\"redacted\"" 213 tool_path {name: "ar" path: "/usr/bin/ar" } 214 tool_path {name: "cpp" path: "/usr/bin/cpp" } 215 tool_path {name: "dwp" path: "/usr/bin/dwp" } 216 tool_path {name: "gcc" path: "/usr/bin/gcc" } 217 tool_path {name: "gcov" path: "/usr/bin/gcov" } 218 tool_path {name: "ld" path: "/usr/bin/ld" } 219 tool_path {name: "nm" path: "/usr/bin/nm" } 220 tool_path {name: "objcopy" path: "/usr/bin/objcopy" } 221 tool_path {name: "objdump" path: "/usr/bin/objdump" } 222 tool_path {name: "strip" path: "/usr/bin/strip" } 223 224 compilation_mode_flags { 225 mode: DBG 226 compiler_flag: "-g" 227 } 228 compilation_mode_flags { 229 mode: OPT 230 compiler_flag: "-g0" 231 compiler_flag: "-O3" 232 compiler_flag: "-DNDEBUG" 233 compiler_flag: "-ffunction-sections" 234 compiler_flag: "-fdata-sections" 235 linker_flag: "-Wl,--gc-sections" 236 } 237 linking_mode_flags { mode: DYNAMIC } 238 }
交叉編譯器描述文件的具體語法參考:https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain
3.3、新建CROSSTOOL
CROSSTOOL文件負描述交叉編譯器的各種編譯選項和鏈接選項:

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_flag: "-U_FORTIFY_SOURCE" 103 compiler_flag: "-fstack-protector" 104 compiler_flag: "-fPIE" 105 linker_flag: "-pie" 106 linker_flag: "-Wl,-z,relro,-z,now" 107 108 # Enable coloring even if there's no attached terminal. Bazel removes the 109 # escape sequences if --nocolor is specified. 110 compiler_flag: "-fdiagnostics-color=always" 111 112 # All warnings are enabled. Maybe enable -Werror as well? 113 compiler_flag: "-Wall" 114 # Enable a few more warnings that aren't part of -Wall. 115 compiler_flag: "-Wunused-but-set-parameter" 116 # But disable some that are problematic. 117 compiler_flag: "-Wno-free-nonheap-object" # has false positives 118 119 # Keep stack frames for debugging, even in opt mode. 120 compiler_flag: "-fno-omit-frame-pointer" 121 122 # Stamp the binary with a unique identifier. 123 linker_flag: "-Wl,--build-id=md5" 124 linker_flag: "-Wl,--hash-style=gnu" 125 126 compilation_mode_flags { 127 mode: DBG 128 # Enable debug symbols. 129 compiler_flag: "-g" 130 } 131 compilation_mode_flags { 132 mode: OPT 133 134 # No debug symbols. 135 # Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or 136 # even generally? However, that can't happen here, as it requires special 137 # handling in Bazel. 138 compiler_flag: "-g0" 139 140 # Conservative choice for -O 141 # -O3 can increase binary size and even slow down the resulting binaries. 142 # Profile first and / or use FDO if you need better performance than this. 143 compiler_flag: "-O3" 144 145 # Disable assertions 146 compiler_flag: "-DNDEBUG -mcpu=cortex-a9 -mfpu=neon" #default value:cortex-a7 147 #-mcpu=cortex-m3 need compile with -mthumb 148 149 # Removal of unused code and data at link time 150 # Uncomment fllowing flag when software deploy finally 151 # compiler_flag: "-ffunction-sections" 152 # compiler_flag: "-fdata-sections" 153 # linker_flag: "-Wl,--gc-sections" 154 } 155 } 156 157 158 toolchain { 159 toolchain_identifier: "local" 160 abi_libc_version: "local" 161 abi_version: "local" 162 builtin_sysroot: "" 163 compiler: "compiler" 164 compiler_flag: "-U_FORTIFY_SOURCE" 165 compiler_flag: "-D_FORTIFY_SOURCE=2" 166 compiler_flag: "-fstack-protector" 167 compiler_flag: "-Wall" 168 compiler_flag: "-Wl,-z,-relro,-z,now" 169 compiler_flag: "-B/usr/bin" 170 compiler_flag: "-B/usr/bin" 171 compiler_flag: "-Wunused-but-set-parameter" 172 compiler_flag: "-Wno-free-nonheap-object" 173 compiler_flag: "-fno-omit-frame-pointer" 174 compiler_flag: "-isystem" 175 compiler_flag: "/usr/include" 176 cxx_builtin_include_directory: "/usr/include/c++/5.4.0" 177 cxx_builtin_include_directory: "/usr/include/c++/5" 178 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5/include" 179 cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/5.4.0" 180 cxx_builtin_include_directory: "/usr/include/c++/5.4.0/backward" 181 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/include" 182 cxx_builtin_include_directory: "/usr/local/include" 183 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5.4.0/include-fixed" 184 cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed" 185 cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu" 186 cxx_builtin_include_directory: "/usr/include" 187 cxx_flag: "-std=c++11" 188 host_system_name: "local" 189 linker_flag: "-lstdc++" 190 linker_flag: "-lm" 191 linker_flag: "-Wl,-no-as-needed" 192 linker_flag: "-B/usr/bin" 193 linker_flag: "-B/usr/bin" 194 linker_flag: "-pass-exit-codes" 195 needsPic: true 196 objcopy_embed_flag: "-I" 197 objcopy_embed_flag: "binary" 198 supports_fission: false 199 supports_gold_linker: false 200 supports_incremental_linker: false 201 supports_interface_shared_objects: false 202 supports_normalizing_ar: false 203 supports_start_end_lib: false 204 supports_thin_archives: false 205 target_cpu: "k8" 206 target_libc: "local" 207 target_system_name: "local" 208 unfiltered_cxx_flag: "-fno-canonical-system-headers" 209 unfiltered_cxx_flag: "-Wno-builtin-macro-redefined" 210 unfiltered_cxx_flag: "-D__DATE__=\"redacted\"" 211 unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\"" 212 unfiltered_cxx_flag: "-D__TIME__=\"redacted\"" 213 tool_path {name: "ar" path: "/usr/bin/ar" } 214 tool_path {name: "cpp" path: "/usr/bin/cpp" } 215 tool_path {name: "dwp" path: "/usr/bin/dwp" } 216 tool_path {name: "gcc" path: "/usr/bin/gcc" } 217 tool_path {name: "gcov" path: "/usr/bin/gcov" } 218 tool_path {name: "ld" path: "/usr/bin/ld" } 219 tool_path {name: "nm" path: "/usr/bin/nm" } 220 tool_path {name: "objcopy" path: "/usr/bin/objcopy" } 221 tool_path {name: "objdump" path: "/usr/bin/objdump" } 222 tool_path {name: "strip" path: "/usr/bin/strip" } 223 224 compilation_mode_flags { 225 mode: DBG 226 compiler_flag: "-g" 227 } 228 compilation_mode_flags { 229 mode: OPT 230 compiler_flag: "-g0" 231 compiler_flag: "-O3" 232 compiler_flag: "-DNDEBUG" 233 compiler_flag: "-ffunction-sections" 234 compiler_flag: "-fdata-sections" 235 linker_flag: "-Wl,--gc-sections" 236 } 237 linking_mode_flags { mode: DYNAMIC } 238 }
其中,有一些重要的參數,這里簡單介紹一下:
default_toolchain:默認交叉編譯工具鏈,分為本地機和目標機
cortex A8和A9都是VFPv3,而cortex A5則用上了VFPv4了。另外M4的浮點也是VFPv4
3.4、新建BUILD

1 package(default_visibility = ["//visibility:public"]) 2 3 cc_toolchain_suite( 4 name = "toolchain", 5 toolchains = { 6 "armv7|compiler": ":cc-compiler-armv7", 7 "k8|compiler": ":cc-compiler-local", 8 }, 9 ) 10 11 filegroup( 12 name = "empty", 13 srcs = [], 14 ) 15 16 filegroup( 17 name = "arm_linux_all_files", 18 srcs = [ 19 "@toolchain_target_armv7//:compiler_pieces", 20 ], 21 ) 22 23 cc_toolchain( 24 name = "cc-compiler-local", 25 all_files = ":empty", 26 compiler_files = ":empty", 27 cpu = "local", 28 dwp_files = ":empty", 29 dynamic_runtime_libs = [":empty"], 30 linker_files = ":empty", 31 objcopy_files = ":empty", 32 static_runtime_libs = [":empty"], 33 strip_files = ":empty", 34 supports_param_files = 1, 35 ) 36 37 cc_toolchain( 38 name = "cc-compiler-armv7", 39 all_files = ":arm_linux_all_files", 40 compiler_files = ":arm_linux_all_files", 41 cpu = "armv7", 42 dwp_files = ":empty", 43 dynamic_runtime_libs = [":empty"], 44 linker_files = ":arm_linux_all_files", 45 objcopy_files = "arm_linux_all_files", 46 static_runtime_libs = [":empty"], 47 strip_files = "arm_linux_all_files", 48 supports_param_files = 1, 49 )
3.5、添加nsync交叉編譯支持
nsync官方交叉編譯器與自帶交叉編譯器可能不一致,所以,需要改動nsync的編譯設置。
問題:如何找到nsync的配置代碼位置?
3.5.1、 ll ~/.cache/bazel 列出bazel_lyra(當前主目錄)
3.5.2、 ll ~/.cache/bazel/bazel_lyra 列出各文件夾,其中包含自動生成的隨機文件夾,如果包含多個,可通過時間來判斷。找到最新的即可。
3.5.3、本文中隨機文件夾為:7924169126bef9c95805dc831e19e9c3,進入該文件夾下/external/nsync,打開文件BUILD,添加一個新的編譯器需要添加多個位置代碼:

3.5.3.1、添加config_setting config_setting( name = "armv7", values = {"cpu": "armeabi-v7a"}, ) config_setting( name = "armv8", values = {"cpu": "arm64-v8a"}, ) 3.5.3.2、在NSYNC_OPTS中添加: # Options for C build, rather then C++11 build. NSYNC_OPTS = select({ # Select the OS include directory. ":gcc_linux_x86_32_1": ["-I" + pkg_path_name() + "/platform/linux"], ":gcc_linux_x86_64_1": ["-I" + pkg_path_name() + "/platform/linux"], ":gcc_linux_x86_64_2": ["-I" + pkg_path_name() + "/platform/linux"], ":gcc_linux_aarch64": ["-I" + pkg_path_name() + "/platform/linux"], ":gcc_linux_ppc64": ["-I" + pkg_path_name() + "/platform/linux"], ":gcc_linux_s390x": ["-I" + pkg_path_name() + "/platform/linux"], ":clang_macos_x86_64": ["-I" + pkg_path_name() + "/platform/macos"], ":freebsd": ["-I" + pkg_path_name() + "/platform/freebsd"], ":ios_x86_64": ["-I" + pkg_path_name() + "/platform/macos"], ":android_x86_32": ["-I" + pkg_path_name() + "/platform/linux"], ":android_x86_64": ["-I" + pkg_path_name() + "/platform/linux"], ":android_armeabi": ["-I" + pkg_path_name() + "/platform/linux"], ":android_arm": ["-I" + pkg_path_name() + "/platform/linux"], ":android_arm64": ["-I" + pkg_path_name() + "/platform/linux"], ":armv7": ["-I" + pkg_path_name() + "/platform/linux"], ":armv8": ["-I" + pkg_path_name() + "/platform/linux"], ":msvc_windows_x86_64": ["-I" + pkg_path_name() + "/platform/win32"], "//conditions:default": [], }) + select({ # Select the compiler include directory. ":gcc_linux_x86_32_1": ["-I" + pkg_path_name() + "/platform/gcc"], ":gcc_linux_x86_64_1": ["-I" + pkg_path_name() + "/platform/gcc"], ":gcc_linux_x86_64_2": ["-I" + pkg_path_name() + "/platform/gcc"], ":gcc_linux_aarch64": ["-I" + pkg_path_name() + "/platform/gcc"], ":gcc_linux_ppc64": ["-I" + pkg_path_name() + "/platform/gcc"], ":gcc_linux_s390x": ["-I" + pkg_path_name() + "/platform/gcc"], ":clang_macos_x86_64": ["-I" + pkg_path_name() + "/platform/clang"], ":freebsd": ["-I" + pkg_path_name() + "/platform/clang"], ":ios_x86_64": ["-I" + pkg_path_name() + "/platform/clang"], ":android_x86_32": ["-I" + pkg_path_name() + "/platform/gcc"], ":android_x86_64": ["-I" + pkg_path_name() + "/platform/gcc"], ":android_armeabi": ["-I" + pkg_path_name() + "/platform/gcc"], ":android_arm": ["-I" + pkg_path_name() + "/platform/gcc"], ":android_arm64": ["-I" + pkg_path_name() + "/platform/gcc"], ":armv7": ["-I" + pkg_path_name() + "/platform/gcc"], ":armv8": ["-I" + pkg_path_name() + "/platform/gcc"], ":msvc_windows_x86_64": ["-I" + pkg_path_name() + "/platform/msvc"], }) + select({ # Apple deprecated their atomics library, yet recent versions have no # working version of stdatomic.h; so some recent versions need one, and # other versions prefer the other. For the moment, just ignore the # depreaction. ":clang_macos_x86_64": ["-Wno-deprecated-declarations"], "//conditions:default": [], }) + NSYNC_OPTS_GENERIC 3.5.3.3、在NSYNC_SRC_PLATFORM中添加: NSYNC_SRC_PLATFORM = select({ ":gcc_linux_x86_32_1": NSYNC_SRC_LINUX, ":gcc_linux_x86_64_1": NSYNC_SRC_LINUX, ":gcc_linux_x86_64_2": NSYNC_SRC_LINUX, ":gcc_linux_aarch64": NSYNC_SRC_LINUX, ":gcc_linux_ppc64": NSYNC_SRC_LINUX, ":gcc_linux_s390x": NSYNC_SRC_LINUX, ":clang_macos_x86_64": NSYNC_SRC_MACOS, ":freebsd": NSYNC_SRC_FREEBSD, ":ios_x86_64": NSYNC_SRC_MACOS, ":android_x86_32": NSYNC_SRC_ANDROID, ":android_x86_64": NSYNC_SRC_ANDROID, ":android_armeabi": NSYNC_SRC_ANDROID, ":android_arm": NSYNC_SRC_ANDROID, ":android_arm64": NSYNC_SRC_ANDROID, ":armv7": NSYNC_SRC_LINUX, ":armv8": NSYNC_SRC_LINUX, ":msvc_windows_x86_64": NSYNC_SRC_WINDOWS, }) 3.5.3.4、在NSYNC_TEST_SRC_PLATFORM中添加: NSYNC_TEST_SRC_PLATFORM = select({ ":gcc_linux_x86_32_1": NSYNC_TEST_SRC_LINUX, ":gcc_linux_x86_64_1": NSYNC_TEST_SRC_LINUX, ":gcc_linux_x86_64_2": NSYNC_TEST_SRC_LINUX, ":gcc_linux_aarch64": NSYNC_TEST_SRC_LINUX, ":gcc_linux_ppc64": NSYNC_TEST_SRC_LINUX, ":gcc_linux_s390x": NSYNC_TEST_SRC_LINUX, ":clang_macos_x86_64": NSYNC_TEST_SRC_MACOS, ":freebsd": NSYNC_TEST_SRC_FREEBSD, ":ios_x86_64": NSYNC_TEST_SRC_MACOS, ":android_x86_32": NSYNC_TEST_SRC_ANDROID, ":android_x86_64": NSYNC_TEST_SRC_ANDROID, ":android_armeabi": NSYNC_TEST_SRC_ANDROID, ":android_arm": NSYNC_TEST_SRC_ANDROID, ":android_arm64": NSYNC_TEST_SRC_ANDROID, ":armv7": NSYNC_TEST_SRC_LINUX, ":armv8": NSYNC_TEST_SRC_LINUX, ":msvc_windows_x86_64": NSYNC_TEST_SRC_WINDOWS, })
4、運行configure配置腳本
cd tensorflow
./configure
配置過程注意選項:
4.1 python庫位置
4.2 optimization flags: 默認為-march=native,交叉編譯時需要修改,可根據實際芯片架構設置,如:march=armv7-a
4.3 其它選項一律選No即可
5、shell腳本:build_armv7.sh(交叉編譯Tensorflow主體部分):

bazel build --copt="-fPIC" --copt="-march=armv7-a" --cxxopt="fPIC" --cxxopt="-march=armv7-a" --verbose_failures --crosstool_top=//arm_compiler:toolchain --cpu=armv7 --config=opt tensorflow/examples/label_image/...
可通過設置參數:-jobs 來指定編譯線程數,如果不指定,默認采用CPU核心數x2的線程數
編譯過程中可能會出現各種錯誤,可參考以下鏈接處理:
編譯完成后,會生成如下目標文件:
bazel-bin/tensorflow/libtensorflow_framework.so
bazel-bin/tensorflow/examples/label_image
6、交叉編譯Tensorflow-lite
交叉編譯過程參考tensorfow/contrib/lite/g3doc/rpi.md
編譯之前對多線程代碼進行修改,默認線程數為4,參考以下代碼:

const Eigen::ThreadPoolDevice& GetThreadPoolDevice() { const int thread_count = 4; static Eigen::ThreadPool* tp = new Eigen::ThreadPool(thread_count); static EigenThreadPoolWrapper* thread_pool_wrapper = new EigenThreadPoolWrapper(tp); static Eigen::ThreadPoolDevice* device = new Eigen::ThreadPoolDevice(thread_pool_wrapper, thread_count); return *device; } 將上述代碼修改為: const Eigen::ThreadPoolDevice& GetThreadPoolDevice() { int thread_count = 1; const char *val = getenv("OMP_NUM_THREADS"); if (val != nullptr) { thread_count = atoi(val); } static Eigen::ThreadPool* tp = new Eigen::ThreadPool(thread_count); static EigenThreadPoolWrapper* thread_pool_wrapper = new EigenThreadPoolWrapper(tp); static Eigen::ThreadPoolDevice* device = new Eigen::ThreadPoolDevice(thread_pool_wrapper, thread_count); return *device; }
第一步:
./tensorflow/contrib/lite/download_dependencies.sh 下載依賴(成功的話,只需執行一次)
第二步:
參考文件:./tensorflow/contrib/lite/build_rpi_lib.sh (目前Tensorflow僅支持樹莓派),制作iMX6的腳本文件:build_imx6_lib.sh

1 set -e 2 3 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 4 cd "$SCRIPT_DIR/../../.." 5 6 CC_PREFIX=arm-poky-linux-gnueabi- make -j 3 -f tensorflow/contrib/lite/Makefile TARGET=imx6 TARGET_ARCH=armv7a
第三步:
將下載好的flatbuffer拷貝進目錄:./tensorflow/contrib/lite/schema
編譯成功后,會生成如下目標文件:
tensorflow/contrib/lite/gen/lib/rpi_armv7/libtensorflow-lite.a
tensorflow/contrib/lite/gen/bin/rpi_armv7/benchmark_model
7、交叉編譯Tensorflow-lite的label_image測試工具

bazel build --copt="-fPIC" --copt="-march=armv7-a" --cxxopt="-fPIC" --cxxopt="-march=armv7-a" --verbose_failures --crosstool_top=//arm_compiler:toolchain --cpu=armv7 --config=opt //tensorflow/contrib/lite/examples/label_image:label_image
8、本地編譯TOCO模型轉換工具
TOCO轉換工具與模型預測無關,建議運行在X86機器上。
為了最小化運行時環境,使用了Flatbuffer這種輕量級的數據存儲格式,所以,在測試前需要使用TOCO工具進行模型轉換:
bazel build tensorflow/contrib/lite/toco:toco (關閉交叉編譯鏈選項)
完成編譯后生成如下目標文件:
bazel-bin/tensorflow/contrib/lite/toco/toco (toco工具編譯出來之后盡量不要移動位置,建議原地執行)
到此,Tensorflow-lite交叉編譯環境搭建完成!