由于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、设置交叉编译链
在/etc/bash.bashrc最后增加如下指令:
export ARCH=arm
export PATH=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/:$PATH
export CROSS_COMPILE=arm-linux-gnueabihf-
export CC=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-gcc
export CXX=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-g++
export LD=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-ld
export AR=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-ar
export AS=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-as
export RANLIB=/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-ranlib
# 修改完成之后需要重启命令行才能生效
# 通过如下指令来确认交叉编译链是否已经设置好
echo $CC
显示/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-gcc时表示交叉编译链
# 已经设置好
# 当需要更换为本机编译时屏蔽上面的指令即可
3、准备交叉编译
以下为需要新建与修改的交叉编译相关的Bazel配置脚本:
./WORKSPACE
./arm_compiler
../BUILD
../build_armv8.sh
../build_lite_armv8.sh
../build_armv7.sh
../build_lite_armv7.sh
../cross_toolchain_target_armv8.BUILD
../cross_toolchain_target_armv7.BUILD
../CROSSTOOL
3.1 修改WORKSPACE
在Tensorflow根目录下添加如下内容:
new_local_repository(
name =‘toolchain_target_armv7‘, #交叉编译器别名
path =‘/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux‘, #交叉编译器路径,可根据需要自行修改
build_file = ‘arm_compiler/cross_toolchain_target_armv7.BUILD‘ #交叉编译器描述文件
)
3.2、新建交叉编译器描述文件:
新建cross_toolchain_target_armv7.BUILD:
package(default_visibility = [‘//visibility:public‘])
filegroup(
name = ‘gcc‘,
srcs = [
‘bin/arm-linux-gnueabihf-gcc‘,
],
)
filegroup(
name = ‘ar‘,
srcs = [
‘bin/arm-linux-gnueabihf-ar‘,
],
)
filegroup(
name = ‘ld‘,
srcs = [
‘bin/arm-linux-gnueabihf-ld‘,
],
)
filegroup(
name = ‘nm‘,
srcs = [
‘bin/arm-linux-gnueabihf-nm‘,
],
)
filegroup(
name = ‘objcopy‘,
srcs = [
‘bin/arm-linux-gnueabihf-objcopy‘,
],
)
filegroup(
name = ‘objdump‘,
srcs = [
‘bin/arm-linux-gnueabihf-objdump‘,
],
)
filegroup(
name = ‘strip‘,
srcs = [
‘bin/arm-linux-gnueabihf-strip‘,
],
)
filegroup(
name = ‘as‘,
srcs = [
‘bin/arm-linux-gnueabihf-as‘,
],
)
filegroup(
name = ‘compiler_pieces‘,
srcs = glob([
‘arm-linux-gnueabihf/**‘,
‘libexec/**‘,
‘lib/gcc/arm-linux-gnueabihf/**‘,
‘include/**‘,
]),
)
filegroup(
name = ‘compiler_components‘,
srcs = [
‘:gcc‘,
‘:ar‘,
‘:ld‘,
‘:nm‘,
‘:objcopy‘,
‘:objdump‘,
‘:strip‘,
‘:as‘,
],
)
交叉编译器描述文件的具体语法参考:https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain
3.3、新建CROSSTOOL
CROSSTOOL文件负描述交叉编译器的各种编译选项和链接选项:
major_version: "local"
minor_version: ""
default_target_cpu: "armv7"
default_toolchain {
cpu: "armv7"
toolchain_identifier: "arm-linux-gnueabihf"
}
default_toolchain {
cpu: "k8"
toolchain_identifier: "local"
}
toolchain {
abi_version: "gcc"
abi_libc_version: "glibc_2.21"
builtin_sysroot: ""
compiler: "compiler"
host_system_name: "armv7"
needsPic: true
supports_gold_linker: false
supports_incremental_linker: false
supports_fission: false
supports_interface_shared_objects: false
supports_normalizing_ar: true
supports_start_end_lib: false
supports_thin_archives: true
target_libc: "glibc_2.21"
target_cpu: "armv7"
target_system_name: "armv7"
toolchain_identifier: "arm-linux-gnueabihf"
tool_path { name: "ar" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-ar" }
tool_path { name: "compat-ld" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-ld" }
tool_path { name: "cpp" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-cpp" }
tool_path { name: "dwp" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-dwp" }
tool_path { name: "gcc" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-gcc" }
tool_path { name: "gcov" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-gcov" }
tool_path { name: "ld" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-ld" }
tool_path { name: "nm" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-nm" }
tool_path { name: "objcopy" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-objcopy" }
objcopy_embed_flag: "-I"
objcopy_embed_flag: "binary"
tool_path { name: "objdump" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-objdump" }
tool_path { name: "strip" path: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/bin/arm-linux-gnueabihf-strip" }
compiler_flag: "-nostdinc"
compiler_flag: "-isystem"
compiler_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/libc/usr/include"
compiler_flag: "-isystem"
compiler_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib/gcc/arm-linux-gnueabihf/4.9.2/include"
compiler_flag: "-isystem"
compiler_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib/gcc/arm-linux-gnueabihf/4.9.2/include-fixed"
compiler_flag: "-isystem"
compiler_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/include/c++/4.9.2"
compiler_flag: "-isystem"
compiler_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/include/c++/4.9.2/arm-linux-gnueabihf"
cxx_flag: "-isystem"
cxx_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/libc/usr/include"
cxx_flag: "-isystem"
cxx_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib/gcc/arm-linux-gnueabihf/4.9.2/include"
cxx_flag: "-isystem"
cxx_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib/gcc/arm-linux-gnueabihf/4.9.2/include-fixed"
cxx_flag: "-isystem"
cxx_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/include/c++/4.9.2"
cxx_flag: "-isystem"
cxx_flag: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/include/c++/4.9.2/arm-linux-gnueabihf"
cxx_flag: "-std=c++11"
cxx_builtin_include_directory: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/libc/usr/include"
cxx_builtin_include_directory: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib/gcc/arm-linux-gnueabihf/4.9.2/include"
cxx_builtin_include_directory: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib/gcc/arm-linux-gnueabihf/4.9.2/include-fixed"
cxx_builtin_include_directory: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/include/c++/4.9.2"
cxx_builtin_include_directory: "/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/include/c++/4.9.2/arm-linux-gnueabihf"
linker_flag: "-lstdc++"
linker_flag: "-L/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/lib"
linker_flag: "-L/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/lib"
linker_flag: "-L/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/libc/lib"
linker_flag: "-L/opt/toolchain/gcc-linaro-arm-linux-gnueabihf-4.9-2014.09_linux/arm-linux-gnueabihf/libc/usr/lib"
linker_flag: "-Wl,--dynamic-linker=/lib/ld-linux-aarch64.so.1"
# Anticipated future default.
# This makes GCC and Clang do what we want when called through symlinks.
unfiltered_cxx_flag: "-no-canonical-prefixes"
linker_flag: "-no-canonical-prefixes"
# Make C++ compilation deterministic. Use linkstamping instead of these
# compiler symbols.
unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
# Security hardening on by default.
# Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases.
# We need to undef it before redefining it as some distributions now have
# it enabled by default.
compiler_flag: "-U_FORTIFY_SOURCE"
compiler_flag: "-fstack-protector"
compiler_flag: "-fPIE"
linker_flag: "-pie"
linker_flag: "-Wl,-z,relro,-z,now"
# Enable coloring even if there‘s no attached terminal. Bazel removes the
# escape sequences if --nocolor is specified.
compiler_flag: "-fdiagnostics-color=always"
# All warnings are enabled. Maybe enable -Werror as well?
compiler_flag: "-Wall"
# Enable a few more warnings that aren‘t part of -Wall.
compiler_flag: "-Wunused-but-set-parameter"
# But disable some that are problematic.
compiler_flag: "-Wno-free-nonheap-object" # has false positives
# Keep stack frames for debugging, even in opt mode.
compiler_flag: "-fno-omit-frame-pointer"
# Stamp the binary with a unique identifier.
linker_flag: "-Wl,--build-id=md5"
linker_flag: "-Wl,--hash-style=gnu"
compilation_mode_flags {
mode: DBG
# Enable debug symbols.
compiler_flag: "-g"
}
compilation_mode_flags {
mode: OPT
# No debug symbols.
# Maybe we should enable https://gcc.gnu.org/wiki/DebugFission for opt or
# even generally? However, that can‘t happen here, as it requires special
# handling in Bazel.
compiler_flag: "-g0"
# Conservative choice for -O
# -O3 can increase binary size and even slow down the resulting binaries.
# Profile first and / or use FDO if you need better performance than this.
compiler_flag: "-O3"
# Disable assertions
compiler_flag: "-DNDEBUG -mcpu=cortex-a7 -mfpu=neon"
# Removal of unused code and data at link time
# Uncomment fllowing flag when software deploy finally
# compiler_flag: "-ffunction-sections"
# compiler_flag: "-fdata-sections"
# linker_flag: "-Wl,--gc-sections"
}
}
toolchain {
toolchain_identifier: "local"
abi_libc_version: "local"
abi_version: "local"
builtin_sysroot: ""
compiler: "compiler"
compiler_flag: "-U_FORTIFY_SOURCE"
compiler_flag: "-D_FORTIFY_SOURCE=2"
compiler_flag: "-fstack-protector"
compiler_flag: "-Wall"
compiler_flag: "-Wl,-z,-relro,-z,now"
compiler_flag: "-B/usr/bin"
compiler_flag: "-B/usr/bin"
compiler_flag: "-Wunused-but-set-parameter"
compiler_flag: "-Wno-free-nonheap-object"
compiler_flag: "-fno-omit-frame-pointer"
compiler_flag: "-isystem"
compiler_flag: "/usr/include"
cxx_builtin_include_directory: "/usr/include/c++/4.8.5"
cxx_builtin_include_directory: "/usr/include/c++/4.8"
cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include"
cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu/c++/4.8"
cxx_builtin_include_directory: "/usr/include/c++/4.8/backward"
cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include"
cxx_builtin_include_directory: "/usr/local/include"
cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8.5/include-fixed"
cxx_builtin_include_directory: "/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed"
cxx_builtin_include_directory: "/usr/include/x86_64-linux-gnu"
cxx_builtin_include_directory: "/usr/include"
cxx_flag: "-std=c++11"
host_system_name: "local"
linker_flag: "-lstdc++"
linker_flag: "-lm"
linker_flag: "-Wl,-no-as-needed"
linker_flag: "-B/usr/bin"
linker_flag: "-B/usr/bin"
linker_flag: "-pass-exit-codes"
needsPic: true
objcopy_embed_flag: "-I"
objcopy_embed_flag: "binary"
supports_fission: false
supports_gold_linker: false
supports_incremental_linker: false
supports_interface_shared_objects: false
supports_normalizing_ar: false
supports_start_end_lib: false
supports_thin_archives: false
target_cpu: "k8"
target_libc: "local"
target_system_name: "local"
unfiltered_cxx_flag: "-fno-canonical-system-headers"
unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
tool_path {name: "ar" path: "/usr/bin/ar" }
tool_path {name: "cpp" path: "/usr/bin/cpp" }
tool_path {name: "dwp" path: "/usr/bin/dwp" }
tool_path {name: "gcc" path: "/usr/bin/gcc" }
tool_path {name: "gcov" path: "/usr/bin/gcov" }
tool_path {name: "ld" path: "/usr/bin/ld" }
tool_path {name: "nm" path: "/usr/bin/nm" }
tool_path {name: "objcopy" path: "/usr/bin/objcopy" }
tool_path {name: "objdump" path: "/usr/bin/objdump" }
tool_path {name: "strip" path: "/usr/bin/strip" }
compilation_mode_flags {
mode: DBG
compiler_flag: "-g"
}
compilation_mode_flags {
mode: OPT
compiler_flag: "-g0"
compiler_flag: "-O3"
compiler_flag: "-DNDEBUG"
compiler_flag: "-ffunction-sections"
compiler_flag: "-fdata-sections"
linker_flag: "-Wl,--gc-sections"
}
linking_mode_flags { mode: DYNAMIC }
}
3.4、新建BUILD
package(default_visibility = ["//visibility:public"])
cc_toolchain_suite(
name = "toolchain",
toolchains = {
"armv7|compiler": ":cc-compiler-armv7",
"k8|compiler": ":cc-compiler-local",
},
)
filegroup(
name = "empty",
srcs = [],
)
filegroup(
name = "arm_linux_all_files",
srcs = [
"@toolchain_target_armv7//:compiler_pieces",
],
)
cc_toolchain(
name = "cc-compiler-local",
all_files = ":empty",
compiler_files = ":empty",
cpu = "local",
dwp_files = ":empty",
dynamic_runtime_libs = [":empty"],
linker_files = ":empty",
objcopy_files = ":empty",
static_runtime_libs = [":empty"],
strip_files = ":empty",
supports_param_files = 1,
)
cc_toolchain(
name = "cc-compiler-armv7",
all_files = ":arm_linux_all_files",
compiler_files = ":arm_linux_all_files",
cpu = "armv7",
dwp_files = ":empty",
dynamic_runtime_libs = [":empty"],
linker_files = ":arm_linux_all_files",
objcopy_files = "arm_linux_all_files",
static_runtime_libs = [":empty"],
strip_files = "arm_linux_all_files",
supports_param_files = 1,
)
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,进入该文件夹下/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、交叉编译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的线程数
编译过程中可能会出现各种错误,根据错误LOG提示,解决即可。
编译完成后,会生成如下目标文件:
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/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交叉编译环境搭建完成!
针对ARMv7、ARMv8芯片的Tensorflow交叉编译环境配置
原文:https://www.cnblogs.com/jimchen1218/p/11558442.html