文章目录
安装交叉编译工具aarch64-liinux-gun
安装bazel
编译tensorflow c++
进入官网www.linaro.org,根据对应系统的gcc版本下载对应的交叉编译工具(建议选择Binaries版本无需安装)
下载解压。bin目录为gcc的位置,建议添加到系统路径中,在命令行中输入
export PATH=/usr/local/share/gcc-linaro-5.4.1-2017.05-x86_64_aarch64-linux-gnu/bin:$PATH(注意该命令只针对当前窗口,关闭之后失效,如果想永久添加建议将其写入/etc/profile中 并执行source /etc/proflie 生效)进入bazel官网下载(作者版本为bazel0.15,可能需要jdk支持,注意bazel版本很重要,同时建议安装安装版后缀名为.sh),验证
下载tensorflow源码(作者编译的是tensorflow1.10)并解压,在编译时必须能够科学上网
进入tensorflow根目录中vim WORKSPACE
在底部填入,注意修改path
new_local_repository(
name = "aarch64_compiler",
path = "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/",
build_file = "aarch64_compiler.BUILD",
)
进入根目录
新建aarch64_compiler.BUILD
填入注意所有的srcs的路径都要修改
package(default_visibility = ['//visibility:public'])
filegroup(
name = 'gcc',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc',
],
)
filegroup(
name = 'ar',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar',
],
)
filegroup(
name = 'ld',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ld',
],
)
filegroup(
name = 'nm',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-nm',
],
)
filegroup(
name = 'objcopy',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-objcopy',
],
)
filegroup(
name = 'objdump',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-objdump',
],
)
filegroup(
name = 'strip',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-strip',
],
)
filegroup(
name = 'as',
srcs = [
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-as',
],
)
filegroup(
name = 'compiler_pieces',
srcs = glob([
'usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/lib/gcc/aarch64-linux-gnu/5.4.1/**',
]),
)
filegroup(
name = 'compiler_components',
srcs = [
':gcc',
':ar',
':ld',
':nm',
':objcopy',
':objdump',
':strip',
':as',
],
)
在根目录下进入tools,新建目录aarch64_compiler,进入目录新建BUILD和CROSSTOOL文件内容分别为:
BUILD:
package(default_visibility = ["//visibility:public"])
cc_toolchain_suite(
name = 'toolchain',
toolchains = {
'aarch64|compiler':':gcc-linux-aarch64',
},
)
filegroup(
name = "empty",
srcs = [],
)
cc_toolchain(
name = 'gcc-linux-aarch64',
all_files = ':empty',
compiler_files = ':empty',
cpu = 'aarch64',
dwp_files = ':empty',
dynamic_runtime_libs = [':empty'],
linker_files = ':empty',
objcopy_files = 'empty',
static_runtime_libs = [':empty'],
strip_files = 'empty',
supports_param_files = 1,
)
~
CROSSTOOL(注意修改路径,同时注意与GitHub(在参考文献中)上的区别,GitHub作者在cxx_builtin_include_directory少加了一个库路径,导致作者被坑了好久)
major_version: "local"
minor_version: ""
default_target_cpu: "aarch64"
default_toolchain {
cpu: "aarch64"
toolchain_identifier: "aarch64-linux-gnu"
}
toolchain {
abi_version: "aarch64"
abi_libc_version: "aarch64"
builtin_sysroot: ""
compiler: "compiler"
host_system_name: "aarch64"
needsPic: true
supports_gold_linker: true
supports_incremental_linker: false
supports_fission: false
supports_interface_shared_objects: false
supports_normalizing_ar: false
#supports_start_end_lib: true
target_libc: "aarch64"
target_cpu: "aarch64"
target_system_name: "aarch64"
toolchain_identifier: "aarch64-linux-gnu"
cxx_flag: "-std=c++11"
linker_flag: "-lstdc++"
linker_flag: "-lm"
#linker_flag: "-fuse-ld=gold"
linker_flag: "-Wl,-no-as-needed"
linker_flag: "-Wl,-z,relro,-z,now"
linker_flag: "-pass-exit-codes"
cxx_builtin_include_directory: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/include/c++/5.4.1/"
cxx_builtin_include_directory: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/include/c++/5.4.1/backward"
cxx_builtin_include_directory: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/include/"
cxx_builtin_include_directory: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/lib/gcc/aarch64-linux-gnu/5.4.1/include"
cxx_builtin_include_directory: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/lib/gcc/aarch64-linux-gnu/5.4.1/include-fixed"
cxx_builtin_include_directory: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc/usr/include"
objcopy_embed_flag: "-I"
objcopy_embed_flag: "binary"
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\""
compiler_flag: "-U_FORTIFY_SOURCE"
compiler_flag: "-fstack-protector"
compiler_flag: "-Wall"
compiler_flag: "-Wunused-but-set-parameter"
compiler_flag: "-Wno-free-nonheap-object"
compiler_flag: "-fno-omit-frame-pointer"
compiler_flag: "-isystem"
compiler_flag: "usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc/usr/include"
tool_path { name: "ld" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ld" }
tool_path { name: "cpp" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-cpp" }
tool_path { name: "dwp" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-dwp" }
tool_path { name: "gcov" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcov" }
tool_path { name: "nm" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-nm" }
tool_path { name: "objcopy" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-objcopy" }
tool_path { name: "objdump" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-objdump" }
tool_path { name: "strip" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-strip" }
tool_path { name: "gcc" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc" }
tool_path { name: "ar" path: "/usr/local/share/gcc-linaro-5.4.1-2017.01-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-ar" }
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: "-O2"
compiler_flag: "-D_FORTIFY_SOURCE=1"
# Disable assertions
compiler_flag: "-DNDEBUG"
# Removal of unused code and data at link time (can this increase binary size in some cases?).
compiler_flag: "-ffunction-sections"
compiler_flag: "-fdata-sections"
linker_flag: "-Wl,--gc-sections"
}
linking_mode_flags { mode: DYNAMIC }
feature {
name: 'coverage'
provides: 'profile'
flag_set {
action: 'preprocess-assemble'
action: 'c-compile'
action: 'c++-compile'
action: 'c++-header-parsing'
action: 'c++-header-preprocessing'
action: 'c++-module-compile'
flag_group {
flag: '-fprofile-arcs'
flag: '-ftest-coverage'
}
}
flag_set {
action: 'c++-link-interface-dynamic-library'
action: 'c++-link-dynamic-library'
action: 'c++-link-executable'
flag_group {
flag: '-lgcov'
}
}
}
}
在根目录下执行./configure,选择编译选项,作者为保险只修改了第一个选项python的路径,其他全部为NO
然后执行
bazel build -c opt //tensorflow:libtensorflow_cc.so --cpu=aarch64 --crosstool_top=//tools/aarch64_compiler:toolchain --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --verbose_failures
大概3000次action后完成。
在tensorflow-1.10.0/bazel-bin/tensorflow中找到tensorflow_cc.so 和 tensorflow_framework.so动态库
在编译完成后进入目录
tensorflow-1.10.0/tensorflow/contrib/lite/download_dependencies.sh执行下载后续编译需要的依赖,其中主要需要eigen和protobuf等。在downloads找到对应的库文件。
测试在CMake文件中需要包含的动态库:tensorflow_cc.so, tensorflow_framework.so
库文件:eigen,abls,protobuf,bazel-gengiles,tensorflow等等
可以在编译出错后查看缺少的文件,然后将文件添加到库中
https://github.com/zhangcliff/tensorflow-c-mnist 下载项目后将动态库拷到lib中并修改CMakeList.txt文件,添加库。
问题一
编译开始会下载一些依赖包可能由于版本更新的问题无法下载
建议Google搜索,替换对应的下载地址(有些地址出现在tensorflow-1.10.0/tensorflow/workspace.bzl中可以替换,论科学上网的重要性)
问题二
在编译时出现找不到ld的情况