Add aarch64 cross compile toolchain

This commit is contained in:
Lukas Geiger 2020-04-09 17:55:14 +01:00
parent ceb8991c52
commit b437c2bdfa
5 changed files with 232 additions and 7 deletions

View File

@ -50,6 +50,16 @@ filegroup(
]), ]),
) )
filegroup(
name = "aarch64_compiler_pieces",
srcs = glob([
"aarch64-none-linux-gnu/**",
"libexec/**",
"lib/gcc/aarch64-none-linux-gnu/**",
"include/**",
]),
)
filegroup( filegroup(
name = "compiler_components", name = "compiler_components",
srcs = [ srcs = [

View File

@ -110,7 +110,8 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""):
arm_compiler_configure( arm_compiler_configure(
name = "local_config_arm_compiler", name = "local_config_arm_compiler",
build_file = clean_dep("//third_party/toolchains/cpus/arm:BUILD"), build_file = clean_dep("//third_party/toolchains/cpus/arm:BUILD"),
remote_config_repo = "../arm_compiler", remote_config_repo_arm = "../arm_compiler",
remote_config_repo_aarch64 = "../aarch64_compiler",
) )
mkl_repository( mkl_repository(
@ -222,6 +223,20 @@ def tf_repositories(path_prefix = "", tf_repo_name = ""):
], ],
) )
tf_http_archive(
# This is the latest `aarch64-none-linux-gnu` compiler provided by ARM
# See https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads
# The archive contains GCC version 9.2.1
name = "aarch64_compiler",
build_file = "//:arm_compiler.BUILD",
sha256 = "8dfe681531f0bd04fb9c53cf3c0a3368c616aa85d48938eebe2b516376e06a66",
strip_prefix = "gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu",
urls = [
"https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz",
"https://developer.arm.com/-/media/Files/downloads/gnu-a/9.2-2019.12/binrel/gcc-arm-9.2-2019.12-x86_64-aarch64-none-linux-gnu.tar.xz",
],
)
tf_http_archive( tf_http_archive(
name = "libxsmm_archive", name = "libxsmm_archive",
build_file = clean_dep("//third_party:libxsmm.BUILD"), build_file = clean_dep("//third_party:libxsmm.BUILD"),

View File

@ -12,6 +12,7 @@ cc_toolchain_suite(
"armeabi|compiler": ":cc-compiler-armeabi", "armeabi|compiler": ":cc-compiler-armeabi",
"local|compiler": ":cc-compiler-local", "local|compiler": ":cc-compiler-local",
"armeabi": ":cc-compiler-armeabi", "armeabi": ":cc-compiler-armeabi",
"aarch64": ":cc-compiler-aarch64",
"k8": ":cc-compiler-local", "k8": ":cc-compiler-local",
"piii": ":cc-compiler-local", "piii": ":cc-compiler-local",
"arm": ":cc-compiler-local", "arm": ":cc-compiler-local",
@ -31,6 +32,13 @@ filegroup(
], ],
) )
filegroup(
name = "aarch64_linux_all_files",
srcs = [
"@aarch64_compiler//:aarch64_compiler_pieces",
],
)
cc_toolchain_config( cc_toolchain_config(
name = "local_config", name = "local_config",
cpu = "local", cpu = "local",
@ -66,3 +74,21 @@ cc_toolchain(
toolchain_config = ":armeabi_config", toolchain_config = ":armeabi_config",
toolchain_identifier = "arm-linux-gnueabihf", toolchain_identifier = "arm-linux-gnueabihf",
) )
cc_toolchain_config(
name = "aarch64_config",
cpu = "aarch64",
)
cc_toolchain(
name = "cc-compiler-aarch64",
all_files = ":aarch64_linux_all_files",
compiler_files = ":aarch64_linux_all_files",
dwp_files = ":empty",
linker_files = ":aarch64_linux_all_files",
objcopy_files = "aarch64_linux_all_files",
strip_files = "aarch64_linux_all_files",
supports_param_files = 1,
toolchain_config = ":aarch64_config",
toolchain_identifier = "aarch64-linux-gnu",
)

View File

@ -22,7 +22,10 @@ def _arm_compiler_configure_impl(repository_ctx):
python_include_path = "/usr/include/python2.7" python_include_path = "/usr/include/python2.7"
_tpl(repository_ctx, "cc_config.bzl", { _tpl(repository_ctx, "cc_config.bzl", {
"%{ARM_COMPILER_PATH}%": str(repository_ctx.path( "%{ARM_COMPILER_PATH}%": str(repository_ctx.path(
repository_ctx.attr.remote_config_repo, repository_ctx.attr.remote_config_repo_arm,
)),
"%{AARCH64_COMPILER_PATH}%": str(repository_ctx.path(
repository_ctx.attr.remote_config_repo_aarch64,
)), )),
"%{PYTHON_INCLUDE_PATH}%": python_include_path, "%{PYTHON_INCLUDE_PATH}%": python_include_path,
}) })
@ -31,7 +34,8 @@ def _arm_compiler_configure_impl(repository_ctx):
arm_compiler_configure = repository_rule( arm_compiler_configure = repository_rule(
implementation = _arm_compiler_configure_impl, implementation = _arm_compiler_configure_impl,
attrs = { attrs = {
"remote_config_repo": attr.string(mandatory = False, default = ""), "remote_config_repo_arm": attr.string(mandatory = False, default = ""),
"remote_config_repo_aarch64": attr.string(mandatory = False, default = ""),
"build_file": attr.label(), "build_file": attr.label(),
}, },
) )

View File

@ -18,6 +18,8 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
def _impl(ctx): def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
toolchain_identifier = "arm-linux-gnueabihf" toolchain_identifier = "arm-linux-gnueabihf"
elif (ctx.attr.cpu == "aarch64"):
toolchain_identifier = "aarch64-linux-gnu"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
toolchain_identifier = "local_linux" toolchain_identifier = "local_linux"
else: else:
@ -25,6 +27,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
host_system_name = "armeabi" host_system_name = "armeabi"
elif (ctx.attr.cpu == "aarch64"):
host_system_name = "aarch64"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
host_system_name = "local" host_system_name = "local"
else: else:
@ -32,6 +36,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
target_system_name = "armeabi" target_system_name = "armeabi"
elif (ctx.attr.cpu == "aarch64"):
target_system_name = "aarch64"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
target_system_name = "local" target_system_name = "local"
else: else:
@ -39,6 +45,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
target_cpu = "armeabi" target_cpu = "armeabi"
elif (ctx.attr.cpu == "aarch64"):
target_cpu = "aarch64"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
target_cpu = "local" target_cpu = "local"
else: else:
@ -46,6 +54,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
target_libc = "armeabi" target_libc = "armeabi"
elif (ctx.attr.cpu == "aarch64"):
target_libc = "aarch64"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
target_libc = "local" target_libc = "local"
else: else:
@ -55,6 +65,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
abi_version = "armeabi" abi_version = "armeabi"
elif (ctx.attr.cpu == "aarch64"):
abi_version = "aarch64"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
abi_version = "local" abi_version = "local"
else: else:
@ -62,6 +74,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
abi_libc_version = "armeabi" abi_libc_version = "armeabi"
elif (ctx.attr.cpu == "aarch64"):
abi_libc_version = "aarch64"
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
abi_libc_version = "local" abi_libc_version = "local"
else: else:
@ -127,6 +141,8 @@ def _impl(ctx):
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi"):
action_configs = [] action_configs = []
elif (ctx.attr.cpu == "aarch64"):
action_configs = []
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
action_configs = [objcopy_embed_data_action] action_configs = [objcopy_embed_data_action]
else: else:
@ -165,7 +181,7 @@ def _impl(ctx):
], ],
) )
if (ctx.attr.cpu == "armeabi"): if (ctx.attr.cpu == "armeabi" or ctx.attr.cpu == "aarch64"):
unfiltered_compile_flags_feature = feature( unfiltered_compile_flags_feature = feature(
name = "unfiltered_compile_flags", name = "unfiltered_compile_flags",
enabled = True, enabled = True,
@ -340,6 +356,107 @@ def _impl(ctx):
"%{ARM_COMPILER_PATH}%/arm-rpi-linux-gnueabihf/include/c++/6.5.0/", "%{ARM_COMPILER_PATH}%/arm-rpi-linux-gnueabihf/include/c++/6.5.0/",
"-isystem", "-isystem",
"%{PYTHON_INCLUDE_PATH}%", "%{PYTHON_INCLUDE_PATH}%",
],
),
],
),
],
)
elif (ctx.attr.cpu == "aarch64"):
default_compile_flags_feature = feature(
name = "default_compile_flags",
enabled = True,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.lto_backend,
ACTION_NAMES.clif_match,
],
flag_groups = [
flag_group(
flags = [
"-U_FORTIFY_SOURCE",
"-D_FORTIFY_SOURCE=1",
"-fstack-protector",
"-DRASPBERRY_PI",
],
),
],
),
flag_set(
actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.lto_backend,
ACTION_NAMES.clif_match,
],
flag_groups = [flag_group(flags = ["-g"])],
with_features = [with_feature_set(features = ["dbg"])],
),
flag_set(
actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.preprocess_assemble,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.lto_backend,
ACTION_NAMES.clif_match,
],
flag_groups = [
flag_group(
flags = [
"-g0",
"-O2",
"-DNDEBUG",
"-ffunction-sections",
"-fdata-sections",
],
),
],
with_features = [with_feature_set(features = ["opt"])],
),
flag_set(
actions = [
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.lto_backend,
ACTION_NAMES.clif_match,
],
flag_groups = [
flag_group(
flags = [
"-std=c++11",
"-isystem",
"%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/include/c++/9.2.1/",
"-isystem",
"%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include",
"-isystem",
"%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include-fixed",
"-isystem",
"%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/libc/usr/include/",
"-isystem",
"%{PYTHON_INCLUDE_PATH}%",
"-isystem", "-isystem",
"/usr/include/", "/usr/include/",
], ],
@ -467,7 +584,7 @@ def _impl(ctx):
), ),
], ],
) )
elif (ctx.attr.cpu == "armeabi"): elif (ctx.attr.cpu == "armeabi" or ctx.attr.cpu == "aarch64"):
default_link_flags_feature = feature( default_link_flags_feature = feature(
name = "default_link_flags", name = "default_link_flags",
enabled = True, enabled = True,
@ -542,7 +659,7 @@ def _impl(ctx):
sysroot_feature, sysroot_feature,
unfiltered_compile_flags_feature, unfiltered_compile_flags_feature,
] ]
elif (ctx.attr.cpu == "armeabi"): elif (ctx.attr.cpu == "armeabi" or ctx.attr.cpu == "aarch64"):
features = [ features = [
default_compile_flags_feature, default_compile_flags_feature,
default_link_flags_feature, default_link_flags_feature,
@ -566,6 +683,15 @@ def _impl(ctx):
"/usr/include", "/usr/include",
"/tmp/openblas_install/include/", "/tmp/openblas_install/include/",
] ]
elif (ctx.attr.cpu == "aarch64"):
cxx_builtin_include_directories = [
"%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/include/c++/9.2.1/",
"%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include",
"%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-none-linux-gnu/9.2.1/include-fixed",
"%{AARCH64_COMPILER_PATH}%/aarch64-none-linux-gnu/libc/usr/include/",
"/usr/include",
"/tmp/openblas_install/include/",
]
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
cxx_builtin_include_directories = ["/usr/lib/gcc/", "/usr/local/include", "/usr/include"] cxx_builtin_include_directories = ["/usr/lib/gcc/", "/usr/local/include", "/usr/include"]
else: else:
@ -619,6 +745,50 @@ def _impl(ctx):
path = "%{ARM_COMPILER_PATH}%/bin/arm-rpi-linux-gnueabihf-strip", path = "%{ARM_COMPILER_PATH}%/bin/arm-rpi-linux-gnueabihf-strip",
), ),
] ]
elif (ctx.attr.cpu == "aarch64"):
tool_paths = [
tool_path(
name = "ar",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-ar",
),
tool_path(name = "compat-ld", path = "/bin/false"),
tool_path(
name = "cpp",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-cpp",
),
tool_path(
name = "dwp",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-dwp",
),
tool_path(
name = "gcc",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-gcc",
),
tool_path(
name = "gcov",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-gcov",
),
tool_path(
name = "ld",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-ld",
),
tool_path(
name = "nm",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-nm",
),
tool_path(
name = "objcopy",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-objcopy",
),
tool_path(
name = "objdump",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-objdump",
),
tool_path(
name = "strip",
path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-none-linux-gnu-strip",
),
]
elif (ctx.attr.cpu == "local"): elif (ctx.attr.cpu == "local"):
tool_paths = [ tool_paths = [
tool_path(name = "ar", path = "/usr/bin/ar"), tool_path(name = "ar", path = "/usr/bin/ar"),
@ -666,7 +836,7 @@ def _impl(ctx):
cc_toolchain_config = rule( cc_toolchain_config = rule(
implementation = _impl, implementation = _impl,
attrs = { attrs = {
"cpu": attr.string(mandatory=True, values=["armeabi", "local"]), "cpu": attr.string(mandatory=True, values=["armeabi", "aarch64", "local"]),
}, },
provides = [CcToolchainConfigInfo], provides = [CcToolchainConfigInfo],
executable = True, executable = True,