Skip to content

Commit

Permalink
Add embedded toolchains for cross-compiling
Browse files Browse the repository at this point in the history
Added Bazel toolchain configuration for ARM's GNU toolchain.
- https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain
- GCC 8.3-2019.03

This configuration handles following CPU and build config_settings.
- armhf: linux_armhf
- aarch64: linux_aarch64

PiperOrigin-RevId: 306351085
Change-Id: I479d52e2243be9f75888ea81c7250e9072825da5
  • Loading branch information
terryheo authored and tensorflower-gardener committed Apr 14, 2020
1 parent cf50b1f commit 4961f18
Show file tree
Hide file tree
Showing 12 changed files with 951 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .bazelrc
Expand Up @@ -73,6 +73,10 @@
# tensorflow_testing_rbe_linux: RBE options to use RBE with tensorflow-testing project on linux
# tensorflow_testing_rbe_win: RBE options to use RBE with tensorflow-testing project on windows
#
# Embedded Linux options (experimental and only tested with TFLite build yet)
# elinux: General Embedded Linux options shared by all flavors.
# elinux_aarch64: Embedded Linux options for aarch64 (ARM64) CPU support.
# elinux_armhf: Embedded Linux options for armhf (ARMv7) CPU support.



Expand Down Expand Up @@ -432,6 +436,14 @@ build:tensorflow_testing_rbe_linux --config=rbe_linux

common:tensorflow_testing_rbe_win --remote_instance_name=projects/tensorflow-testing/instances/windows
build:tensorflow_testing_rbe_win --config=tensorflow_testing_rbe

# TFLite build configs for generic embedded Linux
build:elinux --crosstool_top=@local_config_embedded_arm//:toolchain
build:elinux --host_crosstool_top=@bazel_tools//tools/cpp:toolchain
build:elinux_aarch64 --config=elinux
build:elinux_aarch64 --cpu=aarch64
build:elinux_armhf --config=elinux
build:elinux_armhf --cpu=armhf
# END TF REMOTE BUILD EXECUTION OPTIONS

# Default options should come above this line
Expand Down
6 changes: 6 additions & 0 deletions tensorflow/BUILD
Expand Up @@ -214,6 +214,12 @@ config_setting(
visibility = ["//visibility:public"],
)

config_setting(
name = "linux_armhf",
values = {"cpu": "armhf"},
visibility = ["//visibility:public"],
)

config_setting(
name = "linux_x86_64",
values = {"cpu": "k8"},
Expand Down
14 changes: 14 additions & 0 deletions tensorflow/lite/kernels/internal/BUILD
Expand Up @@ -30,6 +30,10 @@ NEON_FLAGS_IF_APPLICABLE = select({
"-O3",
"-mfpu=neon",
],
":armhf": [
"-O3",
"-mfpu=neon",
],
":armv7a": [
"-O3",
"-mfpu=neon",
Expand Down Expand Up @@ -90,6 +94,13 @@ config_setting(
},
)

config_setting(
name = "armhf",
values = {
"cpu": "armhf",
},
)

config_setting(
name = "armv7a",
values = {
Expand Down Expand Up @@ -718,6 +729,9 @@ cc_library(
":armeabi-v7a": [
":neon_tensor_utils",
],
":armhf": [
":neon_tensor_utils",
],
":armv7a": [
":neon_tensor_utils",
],
Expand Down
7 changes: 7 additions & 0 deletions tensorflow/opensource_only.files
Expand Up @@ -218,6 +218,13 @@ tensorflow/third_party/toolchains/cpus/arm/arm_compiler_configure.bzl
tensorflow/third_party/toolchains/cpus/arm/cc_config.bzl.tpl
tensorflow/third_party/toolchains/cpus/py/BUILD
tensorflow/third_party/toolchains/cpus/py3/BUILD
tensorflow/third_party/toolchains/embedded/README.md
tensorflow/third_party/toolchains/embedded/arm-linux/BUILD
tensorflow/third_party/toolchains/embedded/arm-linux/README.md
tensorflow/third_party/toolchains/embedded/arm-linux/aarch64-linux-toolchain.BUILD
tensorflow/third_party/toolchains/embedded/arm-linux/arm_linux_toolchain_configure.bzl
tensorflow/third_party/toolchains/embedded/arm-linux/armhf-linux-toolchain.BUILD
tensorflow/third_party/toolchains/embedded/arm-linux/cc_config.bzl.tpl
tensorflow/third_party/toolchains/java/BUILD
tensorflow/third_party/toolchains/preconfig/generate/BUILD
tensorflow/third_party/toolchains/preconfig/generate/archives.bzl
Expand Down
31 changes: 31 additions & 0 deletions tensorflow/workspace.bzl
Expand Up @@ -12,6 +12,7 @@ load("//third_party/systemlibs:syslibs_configure.bzl", "syslibs_configure")
load("//third_party/toolchains/remote:configure.bzl", "remote_execution_configure")
load("//third_party/toolchains/clang6:repo.bzl", "clang6_configure")
load("//third_party/toolchains/cpus/arm:arm_compiler_configure.bzl", "arm_compiler_configure")
load("//third_party/toolchains/embedded/arm-linux:arm_linux_toolchain_configure.bzl", "arm_linux_toolchain_configure")
load("//third_party:repo.bzl", "tf_http_archive")
load("//third_party/clang_toolchain:cc_configure_clang.bzl", "cc_download_clang_toolchain")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
Expand Down Expand Up @@ -110,6 +111,14 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""):
remote_config_repo_aarch64 = "../aarch64_compiler",
)

# TFLite crossbuild toolchain for embeddeds Linux
arm_linux_toolchain_configure(
name = "local_config_embedded_arm",
build_file = clean_dep("//third_party/toolchains/embedded/arm-linux:BUILD"),
aarch64_repo = "../aarch64_linux_toolchain",
armhf_repo = "../armhf_linux_toolchain",
)

mkl_repository(
name = "mkl_linux",
build_file = clean_dep("//third_party/mkl:mkl.BUILD"),
Expand Down Expand Up @@ -253,6 +262,28 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""):
],
)

tf_http_archive(
name = "aarch64_linux_toolchain",
build_file = clean_dep("//third_party/toolchains/embedded/arm-linux:aarch64-linux-toolchain.BUILD"),
sha256 = "8ce3e7688a47d8cd2d8e8323f147104ae1c8139520eca50ccf8a7fa933002731",
strip_prefix = "gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz",
"https://developer.arm.com/-/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz",
],
)

tf_http_archive(
name = "armhf_linux_toolchain",
build_file = clean_dep("//third_party/toolchains/embedded/arm-linux:armhf-linux-toolchain.BUILD"),
sha256 = "d4f6480ecaa99e977e3833cc8a8e1263f9eecd1ce2d022bb548a24c4f32670f5",
strip_prefix = "gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz",
"https://developer.arm.com/-/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf.tar.xz",
],
)

tf_http_archive(
name = "libxsmm_archive",
build_file = clean_dep("//third_party:libxsmm.BUILD"),
Expand Down
4 changes: 4 additions & 0 deletions third_party/toolchains/embedded/README.md
@@ -0,0 +1,4 @@
# Embedded toolchains

This repository contains toolchains for various embedded systems such as
Raspberry Pi and Coral development boards.
66 changes: 66 additions & 0 deletions third_party/toolchains/embedded/arm-linux/BUILD
@@ -0,0 +1,66 @@
load(":cc_config.bzl", "cc_toolchain_config")

package(default_visibility = ["//visibility:public"])

licenses(["restricted"]) # GPLv3

cc_toolchain_suite(
name = "toolchain",
toolchains = {
"aarch64": ":cc-compiler-aarch64",
"armhf": ":cc-compiler-armhf",
},
)

filegroup(
name = "empty",
srcs = [],
)

filegroup(
name = "aarch64_toolchain_all_files",
srcs = [
"@aarch64_linux_toolchain//:compiler_pieces",
],
)

filegroup(
name = "armhf_toolchain_all_files",
srcs = [
"@armhf_linux_toolchain//:compiler_pieces",
],
)

cc_toolchain_config(
name = "aarch64_toolchain_config",
cpu = "aarch64",
)

cc_toolchain_config(
name = "armhf_toolchain_config",
cpu = "armhf",
)

cc_toolchain(
name = "cc-compiler-aarch64",
all_files = ":aarch64_toolchain_all_files",
compiler_files = ":aarch64_toolchain_all_files",
dwp_files = ":empty",
linker_files = ":aarch64_toolchain_all_files",
objcopy_files = "aarch64_toolchain_all_files",
strip_files = "aarch64_toolchain_all_files",
supports_param_files = 1,
toolchain_config = ":aarch64_toolchain_config",
)

cc_toolchain(
name = "cc-compiler-armhf",
all_files = ":armhf_toolchain_all_files",
compiler_files = ":armhf_toolchain_all_files",
dwp_files = ":empty",
linker_files = ":armhf_toolchain_all_files",
objcopy_files = "armhf_toolchain_all_files",
strip_files = "armhf_toolchain_all_files",
supports_param_files = 1,
toolchain_config = ":armhf_toolchain_config",
)
10 changes: 10 additions & 0 deletions third_party/toolchains/embedded/arm-linux/README.md
@@ -0,0 +1,10 @@
# ARM GCC toolchains

This repository contains Bazel C++ toolchain configurations for ARM GCC.
The following toolchains are configured:

source: https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
- gcc 8.3
- glibc 2.28

target cpu: aarch64, armhf
@@ -0,0 +1,81 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "gcc",
srcs = [
"bin/aarch64-linux-gnu-gcc",
],
)

filegroup(
name = "ar",
srcs = [
"bin/aarch64-linux-gnu-ar",
],
)

filegroup(
name = "ld",
srcs = [
"bin/aarch64-linux-gnu-ld",
],
)

filegroup(
name = "nm",
srcs = [
"bin/aarch64-linux-gnu-nm",
],
)

filegroup(
name = "objcopy",
srcs = [
"bin/aarch64-linux-gnu-objcopy",
],
)

filegroup(
name = "objdump",
srcs = [
"bin/aarch64-linux-gnu-objdump",
],
)

filegroup(
name = "strip",
srcs = [
"bin/aarch64-linux-gnu-strip",
],
)

filegroup(
name = "as",
srcs = [
"bin/aarch64-linux-gnu-as",
],
)

filegroup(
name = "compiler_pieces",
srcs = glob([
"aarch64-linux-gnu/**",
"libexec/**",
"lib/gcc/aarch64-linux-gnu/**",
"include/**",
]),
)

filegroup(
name = "compiler_components",
srcs = [
":ar",
":as",
":gcc",
":ld",
":nm",
":objcopy",
":objdump",
":strip",
],
)
@@ -0,0 +1,30 @@
"""Repository rule for ARM cross compiler autoconfiguration."""

def _tpl(repository_ctx, tpl, substitutions = {}, out = None):
if not out:
out = tpl
repository_ctx.template(
out,
Label("//third_party/toolchains/embedded/arm-linux:%s.tpl" % tpl),
substitutions,
)

def _arm_linux_toolchain_configure_impl(repository_ctx):
_tpl(repository_ctx, "cc_config.bzl", {
"%{AARCH64_COMPILER_PATH}%": str(repository_ctx.path(
repository_ctx.attr.aarch64_repo,
)),
"%{ARMHF_COMPILER_PATH}%": str(repository_ctx.path(
repository_ctx.attr.armhf_repo,
)),
})
repository_ctx.symlink(repository_ctx.attr.build_file, "BUILD")

arm_linux_toolchain_configure = repository_rule(
implementation = _arm_linux_toolchain_configure_impl,
attrs = {
"aarch64_repo": attr.string(mandatory = True, default = ""),
"armhf_repo": attr.string(mandatory = True, default = ""),
"build_file": attr.label(),
},
)

0 comments on commit 4961f18

Please sign in to comment.