From 4961f18733ca3967198393abf419e14476b4a85c Mon Sep 17 00:00:00 2001 From: Terry Heo Date: Mon, 13 Apr 2020 18:18:44 -0700 Subject: [PATCH] Add embedded toolchains for cross-compiling 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 --- .bazelrc | 12 + tensorflow/BUILD | 6 + tensorflow/lite/kernels/internal/BUILD | 14 + tensorflow/opensource_only.files | 7 + tensorflow/workspace.bzl | 31 + third_party/toolchains/embedded/README.md | 4 + .../toolchains/embedded/arm-linux/BUILD | 66 ++ .../toolchains/embedded/arm-linux/README.md | 10 + .../arm-linux/aarch64-linux-toolchain.BUILD | 81 +++ .../arm_linux_toolchain_configure.bzl | 30 + .../arm-linux/armhf-linux-toolchain.BUILD | 81 +++ .../embedded/arm-linux/cc_config.bzl.tpl | 609 ++++++++++++++++++ 12 files changed, 951 insertions(+) create mode 100644 third_party/toolchains/embedded/README.md create mode 100644 third_party/toolchains/embedded/arm-linux/BUILD create mode 100644 third_party/toolchains/embedded/arm-linux/README.md create mode 100644 third_party/toolchains/embedded/arm-linux/aarch64-linux-toolchain.BUILD create mode 100644 third_party/toolchains/embedded/arm-linux/arm_linux_toolchain_configure.bzl create mode 100644 third_party/toolchains/embedded/arm-linux/armhf-linux-toolchain.BUILD create mode 100644 third_party/toolchains/embedded/arm-linux/cc_config.bzl.tpl diff --git a/.bazelrc b/.bazelrc index f2eae57d4a11b4..24bfaae60b69de 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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. @@ -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 diff --git a/tensorflow/BUILD b/tensorflow/BUILD index 2e18d4cf0b29eb..36ce3fa4fe50b9 100644 --- a/tensorflow/BUILD +++ b/tensorflow/BUILD @@ -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"}, diff --git a/tensorflow/lite/kernels/internal/BUILD b/tensorflow/lite/kernels/internal/BUILD index f5d5b6da31f5d3..b095e0574feb6d 100644 --- a/tensorflow/lite/kernels/internal/BUILD +++ b/tensorflow/lite/kernels/internal/BUILD @@ -30,6 +30,10 @@ NEON_FLAGS_IF_APPLICABLE = select({ "-O3", "-mfpu=neon", ], + ":armhf": [ + "-O3", + "-mfpu=neon", + ], ":armv7a": [ "-O3", "-mfpu=neon", @@ -90,6 +94,13 @@ config_setting( }, ) +config_setting( + name = "armhf", + values = { + "cpu": "armhf", + }, +) + config_setting( name = "armv7a", values = { @@ -718,6 +729,9 @@ cc_library( ":armeabi-v7a": [ ":neon_tensor_utils", ], + ":armhf": [ + ":neon_tensor_utils", + ], ":armv7a": [ ":neon_tensor_utils", ], diff --git a/tensorflow/opensource_only.files b/tensorflow/opensource_only.files index 2ebfdbee26cc02..0324c9508cfcbc 100644 --- a/tensorflow/opensource_only.files +++ b/tensorflow/opensource_only.files @@ -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 diff --git a/tensorflow/workspace.bzl b/tensorflow/workspace.bzl index f35242261a3b5f..c29e4d191fd038 100755 --- a/tensorflow/workspace.bzl +++ b/tensorflow/workspace.bzl @@ -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") @@ -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"), @@ -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"), diff --git a/third_party/toolchains/embedded/README.md b/third_party/toolchains/embedded/README.md new file mode 100644 index 00000000000000..e148e4e7797d0c --- /dev/null +++ b/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. diff --git a/third_party/toolchains/embedded/arm-linux/BUILD b/third_party/toolchains/embedded/arm-linux/BUILD new file mode 100644 index 00000000000000..521460ddf42eb7 --- /dev/null +++ b/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", +) diff --git a/third_party/toolchains/embedded/arm-linux/README.md b/third_party/toolchains/embedded/arm-linux/README.md new file mode 100644 index 00000000000000..e1ff9984a96932 --- /dev/null +++ b/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 diff --git a/third_party/toolchains/embedded/arm-linux/aarch64-linux-toolchain.BUILD b/third_party/toolchains/embedded/arm-linux/aarch64-linux-toolchain.BUILD new file mode 100644 index 00000000000000..2b50aef57f56fb --- /dev/null +++ b/third_party/toolchains/embedded/arm-linux/aarch64-linux-toolchain.BUILD @@ -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", + ], +) diff --git a/third_party/toolchains/embedded/arm-linux/arm_linux_toolchain_configure.bzl b/third_party/toolchains/embedded/arm-linux/arm_linux_toolchain_configure.bzl new file mode 100644 index 00000000000000..da4282d0215da3 --- /dev/null +++ b/third_party/toolchains/embedded/arm-linux/arm_linux_toolchain_configure.bzl @@ -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(), + }, +) diff --git a/third_party/toolchains/embedded/arm-linux/armhf-linux-toolchain.BUILD b/third_party/toolchains/embedded/arm-linux/armhf-linux-toolchain.BUILD new file mode 100644 index 00000000000000..db2e9bbe1e1156 --- /dev/null +++ b/third_party/toolchains/embedded/arm-linux/armhf-linux-toolchain.BUILD @@ -0,0 +1,81 @@ +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 = [ + ":ar", + ":as", + ":gcc", + ":ld", + ":nm", + ":objcopy", + ":objdump", + ":strip", + ], +) diff --git a/third_party/toolchains/embedded/arm-linux/cc_config.bzl.tpl b/third_party/toolchains/embedded/arm-linux/cc_config.bzl.tpl new file mode 100644 index 00000000000000..06aaaecfa74aa5 --- /dev/null +++ b/third_party/toolchains/embedded/arm-linux/cc_config.bzl.tpl @@ -0,0 +1,609 @@ +load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "artifact_name_pattern", + "env_entry", + "env_set", + "feature", + "feature_set", + "flag_group", + "flag_set", + "make_variable", + "tool", + "tool_path", + "variable_with_value", + "with_feature_set", +) +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") + +def _impl(ctx): + if (ctx.attr.cpu == "aarch64"): + toolchain_identifier = "aarch64-linux-gnu" + host_system_name = "aarch64" + target_system_name = "aarch64" + target_cpu = "aarch64" + target_libc = "aarch64" + abi_version = "aarch64" + abi_libc_version = "aarch64" + elif (ctx.attr.cpu == "armhf"): + toolchain_identifier = "armhf-linux-gnueabihf" + host_system_name = "armhf" + target_system_name = "armhf" + target_cpu = "armhf" + target_libc = "armhf" + abi_version = "armhf" + abi_libc_version = "armhf" + else: + fail("Unreachable") + + compiler = "compiler" + + cc_target_os = None + + builtin_sysroot = None + + all_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ACTION_NAMES.lto_backend, + ] + + all_cpp_compile_actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.clif_match, + ] + + preprocessor_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.clif_match, + ] + + codegen_compile_actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ] + + all_link_actions = [ + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ] + + if (ctx.attr.cpu == "aarch64" or ctx.attr.cpu == "armhf"): + action_configs = [] + else: + fail("Unreachable") + + opt_feature = feature(name = "opt") + + dbg_feature = feature(name = "dbg") + + sysroot_feature = feature( + name = "sysroot", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + 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, + ACTION_NAMES.cpp_link_executable, + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, + ], + flag_groups = [ + flag_group( + flags = ["--sysroot=%{sysroot}"], + expand_if_available = "sysroot", + ), + ], + ), + ], + ) + + if (ctx.attr.cpu == "aarch64" or ctx.attr.cpu == "armhf"): + unfiltered_compile_flags_feature = feature( + name = "unfiltered_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 = [ + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + "-no-canonical-prefixes", + "-fno-canonical-system-headers", + ], + ), + ], + ), + ], + ) + else: + unfiltered_compile_flags_feature = None + + if (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 = [ + "-fstack-protector", # TODO: needed? + ], + ), + ], + ), + 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 = [ + "-isystem", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-linux-gnu/8.3.0/include", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-linux-gnu/8.3.0/include-fixed", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/aarch64-linux-gnu/include/c++/8.3.0/", + "-isystem", + "%{AARCH64_COMPILER_PATH}%/aarch64-linux-gnu/libc/usr/include/", + ], + ), + ], + ), + ], + ) + elif (ctx.attr.cpu == "armhf"): + 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 = [ + "-fstack-protector", + ], + ), + ], + ), + 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 = [ + "-isystem", + "%{ARMHF_COMPILER_PATH}%/lib/gcc/arm-linux-gnueabihf/8.3.0/include", + "-isystem", + "%{ARMHF_COMPILER_PATH}%/lib/gcc/arm-linux-gnueabihf/8.3.0/include-fixed", + "-isystem", + "%{ARMHF_COMPILER_PATH}%/arm-linux-gnueabihf/include/c++/8.3.0/", + "-isystem", + "%{ARMHF_COMPILER_PATH}%/arm-linux-gnueabihf/libc/usr/include/", + ], + ), + ], + ), + ], + ) + else: + default_compile_flags_feature = None + + if (ctx.attr.cpu == "aarch64"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-lstdc++", + "-Wl,-z,relro,-z,now", + "-no-canonical-prefixes", + "-pass-exit-codes", + "-Wl,--build-id=md5", + "-Wl,--hash-style=gnu", + ], + ), + ], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])], + with_features = [with_feature_set(features = ["opt"])], + ), + ], + ) + elif (ctx.attr.cpu == "armhf"): + default_link_flags_feature = feature( + name = "default_link_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = all_link_actions, + flag_groups = [ + flag_group( + flags = [ + "-lstdc++", + "-Wl,-z,relro,-z,now", + "-no-canonical-prefixes", + "-pass-exit-codes", + "-Wl,--build-id=md5", + "-Wl,--hash-style=gnu", + ], + ), + ], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])], + with_features = [with_feature_set(features = ["opt"])], + ), + ], + ) + else: + default_link_flags_feature = None + + supports_dynamic_linker_feature = feature(name = "supports_dynamic_linker", enabled = True) + + supports_pic_feature = feature(name = "supports_pic", enabled = True) + + user_compile_flags_feature = feature( + name = "user_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 = ["%{user_compile_flags}"], + iterate_over = "user_compile_flags", + expand_if_available = "user_compile_flags", + ), + ], + ), + ], + ) + + if (ctx.attr.cpu == "aarch64" or ctx.attr.cpu == "armhf"): + features = [ + default_compile_flags_feature, + default_link_flags_feature, + supports_dynamic_linker_feature, + supports_pic_feature, + opt_feature, + dbg_feature, + user_compile_flags_feature, + sysroot_feature, + unfiltered_compile_flags_feature, + ] + else: + fail("Unreachable") + + if (ctx.attr.cpu == "aarch64"): + cxx_builtin_include_directories = [ + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-linux-gnu/8.3.0/include", + "%{AARCH64_COMPILER_PATH}%/lib/gcc/aarch64-linux-gnu/8.3.0/include-fixed", + "%{AARCH64_COMPILER_PATH}%/aarch64-linux-gnu/include/c++/8.3.0/", + "%{AARCH64_COMPILER_PATH}%/aarch64-linux-gnu/libc/usr/include/", + ] + elif (ctx.attr.cpu == "armhf"): + cxx_builtin_include_directories = [ + "%{ARMHF_COMPILER_PATH}%/lib/gcc/arm-linux-gnueabihf/8.3.0/include", + "%{ARMHF_COMPILER_PATH}%/lib/gcc/arm-linux-gnueabihf/8.3.0/include-fixed", + "%{ARMHF_COMPILER_PATH}%/arm-linux-gnueabihf/include/c++/8.3.0/", + "%{ARMHF_COMPILER_PATH}%/arm-linux-gnueabihf/libc/usr/include/", + ] + else: + fail("Unreachable") + + artifact_name_patterns = [] + + make_variables = [] + + if (ctx.attr.cpu == "aarch64"): + tool_paths = [ + tool_path( + name = "ar", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-ar", + ), + tool_path(name = "compat-ld", path = "/bin/false"), + tool_path( + name = "cpp", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-cpp", + ), + tool_path( + name = "dwp", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-dwp", + ), + tool_path( + name = "gcc", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-gcc", + ), + tool_path( + name = "gcov", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-gcov", + ), + tool_path( + name = "ld", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-ld", + ), + tool_path( + name = "nm", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-nm", + ), + tool_path( + name = "objcopy", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-objcopy", + ), + tool_path( + name = "objdump", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-objdump", + ), + tool_path( + name = "strip", + path = "%{AARCH64_COMPILER_PATH}%/bin/aarch64-linux-gnu-strip", + ), + ] + elif (ctx.attr.cpu == "armhf"): + tool_paths = [ + tool_path( + name = "ar", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-ar", + ), + tool_path(name = "compat-ld", path = "/bin/false"), + tool_path( + name = "cpp", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-cpp", + ), + tool_path( + name = "dwp", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-dwp", + ), + tool_path( + name = "gcc", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-gcc", + ), + tool_path( + name = "gcov", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-gcov", + ), + tool_path( + name = "ld", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-ld", + ), + tool_path( + name = "nm", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-nm", + ), + tool_path( + name = "objcopy", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-objcopy", + ), + tool_path( + name = "objdump", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-objdump", + ), + tool_path( + name = "strip", + path = "%{ARMHF_COMPILER_PATH}%/bin/arm-linux-gnueabihf-strip", + ), + ] + else: + fail("Unreachable") + + + out = ctx.actions.declare_file(ctx.label.name) + ctx.actions.write(out, "Fake executable") + return [ + cc_common.create_cc_toolchain_config_info( + ctx = ctx, + features = features, + action_configs = action_configs, + artifact_name_patterns = artifact_name_patterns, + cxx_builtin_include_directories = cxx_builtin_include_directories, + toolchain_identifier = toolchain_identifier, + host_system_name = host_system_name, + target_system_name = target_system_name, + target_cpu = target_cpu, + target_libc = target_libc, + compiler = compiler, + abi_version = abi_version, + abi_libc_version = abi_libc_version, + tool_paths = tool_paths, + make_variables = make_variables, + builtin_sysroot = builtin_sysroot, + cc_target_os = cc_target_os + ), + DefaultInfo( + executable = out, + ), + ] +cc_toolchain_config = rule( + implementation = _impl, + attrs = { + "cpu": attr.string(mandatory=True, values=["aarch64", "armhf"]), + }, + provides = [CcToolchainConfigInfo], + executable = True, +)