cros-bazel: Make {host,target} cpu string optional

Simplify `bazel_setup_crosstool` usage by letting users not passing
arguments {host, target} cpu string and capture
- host_cpu_str from $(tc-arch "${CBUILD}") and
- target_cpu_str from $(tc-arch "${CHOST}")

Some ebuilds need to convert cpu str to its own supported toolchain,
so we leave the arguments optional for them.
(e.g. tensorflow needs to build `amd64` arch in `k8` toolchain or it'll
just fail)

Add `arm64` toolchain which is equivalent to `aarch64` setup, since
the results of `tc-arch` for 64-bit arm platform is usually `arm64`.

Changes:
- Add optional args support
- Add `arm64` platform and constraints which is equivalent to `aarch64`

BUG=b:191123602
TEST=emerge-${BOARD} -j <cros-bazel packages>
TEST=emerge-arm64-generic -j <cros-bazel packages>

Disallow-Recycled-Builds: drallion-cq
Cq-Depend: chromium:2983918
Change-Id: I477679b5e21ef353c0db137ae62f03b9dd596586
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/2979091
Commit-Queue: Chih-Yang Hsia <paulhsia@chromium.org>
Tested-by: Chih-Yang Hsia <paulhsia@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/eclass/cros-bazel.eclass b/eclass/cros-bazel.eclass
index 2505824d..fa499d4 100644
--- a/eclass/cros-bazel.eclass
+++ b/eclass/cros-bazel.eclass
@@ -70,6 +70,8 @@
 	"@bazel_tools//platforms:linux",
 ]
 
+arm64_constraints = aarch64_constraints
+
 platform(
 	name = "amd64_platform",
 	constraint_values = amd64_constraints,
@@ -90,6 +92,11 @@
 	constraint_values = aarch64_constraints,
 )
 
+platform(
+	name = "arm64_platform",
+	constraint_values = arm64_constraints,
+)
+
 cc_toolchain_suite(
 	name = "toolchain",
 	toolchains = {
@@ -451,30 +458,34 @@
 }
 
 # @FUNCTION: bazel_setup_crosstool
-# @USAGE: <host cpu string> <target cpu string>
+# @USAGE: [<host cpu string> <target cpu string>]
 # @MAINTAINER:
 # Michael Martis <martis@chromium.org>
 # @DESCRIPTION:
-# Accepts Bazel "host" and "target" CPU strings, and creates Bazel targets
-# (under ${T}) that can be used to configure Bazel C++ compilation based on
-# Portage environment variables.
+# Creates Bazel targets (under ${T}) that can be used to configure
+# Bazel C++ compilation based on Portage environment variables.
 #
 # Also updates the bazelrc to specify the new crosstool targets by default.
 #
 # Should only be called once; subsequent calls will have no effect.
+# (Optional) Accepts Bazel "host" and "target" CPU strings as input arguments.
 bazel_setup_crosstool() {
+	if [[ $# -ne 0 && $# -ne 2 ]]; then
+		die "Must give exactly 0 or 2 arguments."
+	fi
+
 	if [[ -f "${BAZEL_CC_BAZELRC}" ]]; then
 		return
 	fi
 
 	bazel_setup_bazelrc
 
-	local host_cpu_str="${1}"
+	local host_cpu_str="${1:-$(tc-arch "${CBUILD}")}"
 	if [[ -z "${host_cpu_str}" ]]; then
 		die "Must specify host CPU string when generating Bazel CROSSTOOL targets."
 	fi
 
-	local target_cpu_str="${2}"
+	local target_cpu_str="${2:-$(tc-arch "${CHOST}")}"
 	if [[ -z "${target_cpu_str}" ]]; then
 		die "Must specify target CPU string when generating Bazel CROSSTOOL targets."
 	fi