由于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的线程数
编译过程中可能会出现各种错误,可参考以下链接处理:
Tensorflow交叉编译错误集锦
编译完成后,会生成如下目标文件:
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仅支持树莓派)
编译成功后,会生成如下目标文件:
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交叉编译环境搭建完成!