From ac32f33d19f8997dd81b55bc5e5dc62fb840cf06 Mon Sep 17 00:00:00 2001 From: Svante Karlsson Date: Tue, 12 Aug 2025 11:03:11 +0200 Subject: [PATCH 1/5] initial arm64 support from prebuilt toolchain --- ARM64_USAGE.md | 57 ++++++++++ FORK_USAGE.md | 85 +++++++++++++++ extentions/gcc.bzl | 118 +++++++++++++++++---- platforms/BUILD | 9 ++ rules/gcc.bzl | 15 +++ test/.bazelrc | 9 ++ test/BUILD | 1 + test/MODULE.bazel | 45 ++++++-- toolchain/internal/cc_toolchain_config.bzl | 29 ++--- toolchain/internal/toolchain.BUILD | 4 +- toolchain/third_party/gcc.BUILD.tpl | 54 ++++++++++ 11 files changed, 381 insertions(+), 45 deletions(-) create mode 100644 ARM64_USAGE.md create mode 100644 FORK_USAGE.md create mode 100644 toolchain/third_party/gcc.BUILD.tpl diff --git a/ARM64_USAGE.md b/ARM64_USAGE.md new file mode 100644 index 0000000..7180e19 --- /dev/null +++ b/ARM64_USAGE.md @@ -0,0 +1,57 @@ +# ARM64 Support for SCORE Toolchains GCC + +## Using ARM64 Toolchain + +### In your MODULE.bazel: + +```python +bazel_dep(name = "score_toolchains_gcc", version = "0.5") + +gcc = use_extension("@score_toolchains_gcc//extentions:gcc.bzl", "gcc") +# Default toolchains for x86_64 and aarch64 are automatically configured + +use_repo(gcc, "gcc_toolchain_x86_64", "gcc_toolchain_aarch64") +register_toolchains( + "@gcc_toolchain_x86_64//:host_gcc_12", + "@gcc_toolchain_aarch64//:host_gcc_12", +) +``` + +### Building for ARM64: + +Add to your project's `.bazelrc`: +``` +# ARM64 build configuration +build:arm64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux +build:aarch64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux +``` + +Then build with: +```bash +# Using the config +bazel build --config=arm64 //your:target +# or +bazel build --config=aarch64 //your:target + +# Direct platform specification (without .bazelrc) +bazel build --platforms=@score_toolchains_gcc//platforms:aarch64-linux //your:target +``` + +## Cross-compilation vs Native Compilation + +- **Cross-compilation** (x86_64 host → ARM64 target): + ```python + target_arch = "aarch64", + exec_arch = "x86_64", + ``` + +- **Native compilation** (ARM64 host → ARM64 target): + ```python + target_arch = "aarch64", + exec_arch = "aarch64", + ``` + + +## Example + +See the `test/` directory for a complete working example that supports both x86_64 and ARM64 builds. \ No newline at end of file diff --git a/FORK_USAGE.md b/FORK_USAGE.md new file mode 100644 index 0000000..0b06f17 --- /dev/null +++ b/FORK_USAGE.md @@ -0,0 +1,85 @@ +# Using the ARM64-enabled Fork + +This fork includes: +- ARM64 (aarch64) platform support +- Default toolchains pointing to `skarlsson/toolchains_gcc_packages` v0.0.2-pr3 +- Both x86_64 and aarch64 GCC 12 toolchains + +Until these changes are merged into the official SCORE toolchains_gcc, you can use this fork locally. + +## Step 1: Clone the fork and checkout the ARM64 branch + +```bash +# As a submodule +git submodule add -b arm64-from-prebuilt https://github.com/skarlsson/toolchains_gcc.git + +# Or clone directly +git clone -b arm64-from-prebuilt https://github.com/skarlsson/toolchains_gcc.git +``` + +## Step 2: Configure MODULE.bazel + +```python +module( + name = "your_project", + version = "0.0.1", +) + +# Declare dependency on the version this fork is based on +bazel_dep(name = "score_toolchains_gcc", version = "0.5") + +# Override with the local fork (e.g., cloned as submodule) +local_path_override( + module_name = "score_toolchains_gcc", + path = "path/to/toolchains_gcc", # Adjust path to where you cloned the fork +) + +# Use the extension - default toolchains are automatically configured +gcc = use_extension("@score_toolchains_gcc//extentions:gcc.bzl", "gcc") + +# Optional: Configure warning flags and features (see test/MODULE.bazel for examples) +# gcc.warning_flags(...) +# gcc.extra_features(...) + +use_repo(gcc, "gcc_toolchain_x86_64", "gcc_toolchain_aarch64") + +# Register toolchains +register_toolchains( + "@gcc_toolchain_x86_64//:host_gcc_12", + "@gcc_toolchain_aarch64//:host_gcc_12", +) +``` + +## Step 3: Configure .bazelrc + +Add to your project's `.bazelrc`: + +```bash +# ARM64/AArch64 build configurations +build:arm64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux +build:aarch64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux + +# Optional: explicit x86_64 configuration +build:x86_64 --platforms=@score_toolchains_gcc//platforms:x86_64-linux +``` + +## Step 4: Build + +```bash +# For ARM64 +bazel build --config=arm64 //your:target +# or +bazel build --config=aarch64 //your:target + +# For x86_64 (default) +bazel build //your:target +# or explicitly +bazel build --config=x86_64 //your:target +``` + +## Switching back to official release + +Once ARM64 support is merged and published, simply: +1. Remove the `archive_override` block +2. Update the version in `bazel_dep` to the new release +3. Keep your .bazelrc configurations - they'll continue to work! \ No newline at end of file diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl index 9051096..8429e61 100644 --- a/extentions/gcc.bzl +++ b/extentions/gcc.bzl @@ -21,49 +21,125 @@ def _gcc_impl(mctx): if not mod.is_root: fail("Only the root module can use the 'gcc' extension") - toolchain_info = None - features = [] - warning_flags = None + toolchains = [] + features_by_name = {} + warning_flags_by_name = {} for mod in mctx.modules: for tag in mod.tags.toolchain: - toolchain_info = { + toolchains.append({ "name": tag.name, "url": tag.url, "strip_prefix": tag.strip_prefix, "sha256": tag.sha256, - } + "target_arch": tag.target_arch, + "exec_arch": tag.exec_arch, + }) for tag in mod.tags.extra_features: + name = tag.name + if name not in features_by_name: + features_by_name[name] = [] for feature in tag.features: - features.append(feature) + features_by_name[name].append(feature) for tag in mod.tags.warning_flags: - warning_flags = { - "minimal_warnings":tag.minimal_warnings, - "strict_warnings":tag.strict_warnings, - "treat_warnings_as_errors":tag.treat_warnings_as_errors + name = tag.name + warning_flags_by_name[name] = { + "minimal_warnings": tag.minimal_warnings, + "strict_warnings": tag.strict_warnings, + "treat_warnings_as_errors": tag.treat_warnings_as_errors } - if toolchain_info: + # If no toolchains specified, use defaults + if not toolchains: + # Default toolchains from skarlsson/toolchains_gcc_packages + toolchains = [ + { + "name": "gcc_toolchain_x86_64", + "url": "https://github.com/skarlsson/toolchains_gcc_packages/releases/download/v0.0.2-pr3/x86_64-unknown-linux-gnu_gcc12.tar.gz", + "sha256": "ede0289c89a633f6f0b7fa5079a1fbc912e9a8607d01a792e3d78f4939d4e96a", + "strip_prefix": "x86_64-unknown-linux-gnu", + "target_arch": "x86_64", + "exec_arch": "x86_64", + }, + { + "name": "gcc_toolchain_aarch64", + "url": "https://github.com/skarlsson/toolchains_gcc_packages/releases/download/v0.0.2-pr3/aarch64-unknown-linux-gnu_gcc12.tar.gz", + "sha256": "cd9fcac29bea3dd7e52724166491a10e152f58188c9f25568e6f50b6c38f6dc3", + "strip_prefix": "aarch64-unknown-linux-gnu", + "target_arch": "aarch64", + "exec_arch": "x86_64", + }, + ] + + for toolchain_info in toolchains: + name = toolchain_info["name"] + target_arch = toolchain_info["target_arch"] + + # Determine target triple based on architecture + target_triple = "%s-unknown-linux-gnu" % target_arch + http_archive( - name = "%s_gcc" % toolchain_info["name"], + name = "%s_gcc" % name, urls = [toolchain_info["url"]], - build_file = "@score_toolchains_gcc//toolchain/third_party:gcc.BUILD", + build_file_content = """ +# Generated BUILD file for gcc toolchain +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "all_files", + srcs = glob(["*/**/*"]), +) + +filegroup( + name = "bin", + srcs = ["bin"], +) + +filegroup( + name = "ar", + srcs = ["bin/{triple}-ar"], +) + +filegroup( + name = "gcc", + srcs = ["bin/{triple}-gcc"], +) + +filegroup( + name = "gcov", + srcs = ["bin/{triple}-gcov"], +) + +filegroup( + name = "gpp", + srcs = ["bin/{triple}-g++"], +) + +filegroup( + name = "strip", + srcs = ["bin/{triple}-strip"], +) + +filegroup( + name = "sysroot_dir", + srcs = ["{triple}/sysroot"], +) +""".format(triple = target_triple), sha256 = toolchain_info["sha256"], strip_prefix = toolchain_info["strip_prefix"], ) gcc_toolchain( - name = toolchain_info["name"], - gcc_repo = "%s_gcc" % toolchain_info["name"], - extra_features = features, - warning_flags = warning_flags, + name = name, + gcc_repo = "%s_gcc" % name, + extra_features = features_by_name.get(name, []), + warning_flags = warning_flags_by_name.get(name, None), + target_arch = toolchain_info["target_arch"], + exec_arch = toolchain_info["exec_arch"], ) - else: - fail("Cannot create gcc toolchain repository, some info is missing!") - gcc = module_extension( implementation = _gcc_impl, tag_classes = { @@ -73,6 +149,8 @@ gcc = module_extension( "url": attr.string(doc = "Url to the toolchain package."), "strip_prefix": attr.string(doc = "Strip prefix from toolchain package.", default=""), "sha256": attr.string(doc = "Checksum of the package"), + "target_arch": attr.string(doc = "Target architecture (x86_64 or aarch64)", default="x86_64"), + "exec_arch": attr.string(doc = "Execution architecture (x86_64 or aarch64)", default="x86_64"), }, ), "warning_flags": tag_class( diff --git a/platforms/BUILD b/platforms/BUILD index f8803bb..59859c4 100644 --- a/platforms/BUILD +++ b/platforms/BUILD @@ -19,3 +19,12 @@ platform( ], visibility = ["//visibility:public"], ) + +platform( + name = "aarch64-linux", + constraint_values = [ + "@platforms//cpu:aarch64", + "@platforms//os:linux", + ], + visibility = ["//visibility:public"], +) diff --git a/rules/gcc.bzl b/rules/gcc.bzl index 224aa53..41a069e 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -37,12 +37,23 @@ def _impl(rctx): rctx (repository_ctx): The Bazel repository context, providing access to attributes and methods for creating repository rules. """ + # Map architecture names to platform CPU values + arch_map = { + "x86_64": "x86_64", + "aarch64": "aarch64", + } + + # Determine target system name based on architecture + target_system = "%s-linux" % rctx.attr.target_arch + rctx.template( "BUILD", rctx.attr._cc_tolchain_build, { "%{gcc_repo}": rctx.attr.gcc_repo, "%{tc_name}": "host_gcc_12", + "%{exec_cpu}": arch_map.get(rctx.attr.exec_arch, "x86_64"), + "%{target_cpu}": arch_map.get(rctx.attr.target_arch, "x86_64"), }, ) minimal_warnings = "[]" @@ -73,6 +84,8 @@ def _impl(rctx): "%{treat_warnings_as_errors_switch}": "True" if "treat_warnings_as_errors" in rctx.attr.extra_features else "False", "%{third_party_warnings_flags}": third_party_warnings, "%{third_party_warnings_switch}": "True" if "third_party_warnings" in rctx.attr.extra_features else "False", + "%{target_cpu}": rctx.attr.target_arch, + "%{target_system}": target_system, }, ) @@ -82,6 +95,8 @@ gcc_toolchain = repository_rule( "gcc_repo": attr.string(doc="The URL of the GCC binary package."), "extra_features": attr.string_list(doc="A list of extra features to enable in the toolchain."), "warning_flags": attr.string_list_dict(doc="A dictionary mapping warning categories to lists of warning flags."), + "target_arch": attr.string(default="x86_64", doc="Target architecture (x86_64 or aarch64)."), + "exec_arch": attr.string(default="x86_64", doc="Execution architecture (x86_64 or aarch64)."), "_cc_toolchain_config_bzl": attr.label( default = "@score_toolchains_gcc//toolchain/internal:cc_toolchain_config.bzl", doc = "Path to the cc_toolchain_config.bzl template file.", diff --git a/test/.bazelrc b/test/.bazelrc index 126f7c3..2fb7978 100644 --- a/test/.bazelrc +++ b/test/.bazelrc @@ -17,3 +17,12 @@ common --registry=https://bcr.bazel.build build:x86_64-linux --incompatible_strict_action_env build:x86_64-linux --platforms=@score_toolchains_gcc//platforms:x86_64-linux build:x86_64-linux --sandbox_writable_path=/var/tmp + +# ARM64/AArch64 configurations +build:arm64 --incompatible_strict_action_env +build:arm64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux +build:arm64 --sandbox_writable_path=/var/tmp + +build:aarch64 --incompatible_strict_action_env +build:aarch64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux +build:aarch64 --sandbox_writable_path=/var/tmp diff --git a/test/BUILD b/test/BUILD index 63402bb..54af413 100644 --- a/test/BUILD +++ b/test/BUILD @@ -19,4 +19,5 @@ cc_binary( cc_binary( name = "main_pthread_cpp", srcs = ["main_pthread.cpp"], + features = ["use_pthread"], ) diff --git a/test/MODULE.bazel b/test/MODULE.bazel index 2da31e3..7e9b59e 100644 --- a/test/MODULE.bazel +++ b/test/MODULE.bazel @@ -24,21 +24,46 @@ local_path_override( ) gcc = use_extension("@score_toolchains_gcc//extentions:gcc.bzl", "gcc") -gcc.toolchain( - url = "https://github.com/eclipse-score/toolchains_gcc_packages/releases/download/0.0.1/x86_64-unknown-linux-gnu_gcc12.tar.gz", - sha256 = "457f5f20f57528033cb840d708b507050d711ae93e009388847e113b11bf3600", - strip_prefix = "x86_64-unknown-linux-gnu", + +# Use default toolchains - no configuration needed! +# The defaults already include x86_64 and aarch64 toolchains + +# Add warning flags for both toolchains +gcc.warning_flags( + name = "gcc_toolchain_x86_64", + minimal_warnings = ["-Wall", "-Wno-error=deprecated-declarations"], + strict_warnings = ["-Wextra", "-Wpedantic"], + treat_warnings_as_errors = ["-Werror"], +) + +gcc.warning_flags( + name = "gcc_toolchain_aarch64", + minimal_warnings = ["-Wall", "-Wno-error=deprecated-declarations"], + strict_warnings = ["-Wextra", "-Wpedantic"], + treat_warnings_as_errors = ["-Werror"], +) + +# Enable extra features for both toolchains +gcc.extra_features( + name = "gcc_toolchain_x86_64", + features = [ + "minimal_warnings", + "treat_warnings_as_errors", + ], ) + gcc.extra_features( + name = "gcc_toolchain_aarch64", features = [ "minimal_warnings", "treat_warnings_as_errors", ], ) -gcc.warning_flags( - minimal_warnings = ["-Wall", "-Wno-error=deprecated-declarations"], - strict_warnings = ["-Wextra", "-Wpedantic"], - treat_warnings_as_errors = ["-Werror"], + +use_repo(gcc, "gcc_toolchain_x86_64", "gcc_toolchain_aarch64") + +# Register both toolchains +register_toolchains( + "@gcc_toolchain_x86_64//:host_gcc_12", + "@gcc_toolchain_aarch64//:host_gcc_12", ) -use_repo(gcc, "gcc_toolchain", "gcc_toolchain_gcc") -register_toolchains("@gcc_toolchain//:host_gcc_12") diff --git a/toolchain/internal/cc_toolchain_config.bzl b/toolchain/internal/cc_toolchain_config.bzl index 3ded6c0..1b55b69 100644 --- a/toolchain/internal/cc_toolchain_config.bzl +++ b/toolchain/internal/cc_toolchain_config.bzl @@ -149,20 +149,23 @@ def _impl(ctx): ], ) + # Architecture-specific compile flags + arch_flag_sets = [] + if "%{target_cpu}" == "x86_64": + arch_flag_sets.append(flag_set( + actions = all_compile_actions, + flag_groups = [ + flag_group( + flags = ["-m64"], + ), + ], + )) + # For aarch64, no specific architecture flag is needed + default_compile_flags_feature = feature( name = "default_compile_flags", enabled = True, - flag_sets = [ - flag_set( - actions = all_compile_actions, - flag_groups = [ - flag_group( - flags = [ - "-m64", - ], - ), - ], - ), + flag_sets = arch_flag_sets + [ flag_set( actions = all_cpp_compile_actions, flag_groups = [ @@ -383,8 +386,8 @@ def _impl(ctx): features = features, action_configs = action_configs, host_system_name = "local", - target_system_name = "x86_64-linux", - target_cpu = "x86_64", + target_system_name = "%{target_system}", + target_cpu = "%{target_cpu}", target_libc = "unknown", toolchain_identifier = toolchain_full_name, tool_paths = tool_paths, diff --git a/toolchain/internal/toolchain.BUILD b/toolchain/internal/toolchain.BUILD index b873303..b76cf2d 100644 --- a/toolchain/internal/toolchain.BUILD +++ b/toolchain/internal/toolchain.BUILD @@ -48,11 +48,11 @@ cc_toolchain( toolchain( name = "%{tc_name}", exec_compatible_with = [ - "@platforms//cpu:x86_64", + "@platforms//cpu:%{exec_cpu}", "@platforms//os:linux", ], target_compatible_with = [ - "@platforms//cpu:x86_64", + "@platforms//cpu:%{target_cpu}", "@platforms//os:linux", ], toolchain = ":cc_toolchain", diff --git a/toolchain/third_party/gcc.BUILD.tpl b/toolchain/third_party/gcc.BUILD.tpl new file mode 100644 index 0000000..5d64eb3 --- /dev/null +++ b/toolchain/third_party/gcc.BUILD.tpl @@ -0,0 +1,54 @@ +# ******************************************************************************* +# Copyright (c) 2025 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "all_files", + srcs = glob(["*/**/*"]), +) + +filegroup( + name = "bin", + srcs = ["bin"], +) + +filegroup( + name = "ar", + srcs = ["bin/%{target_triple}-ar"], +) + +filegroup( + name = "gcc", + srcs = ["bin/%{target_triple}-gcc"], +) + +filegroup( + name = "gcov", + srcs = ["bin/%{target_triple}-gcov"], +) + +filegroup( + name = "gpp", + srcs = ["bin/%{target_triple}-g++"], +) + +filegroup( + name = "strip", + srcs = ["bin/%{target_triple}-strip"], +) + +filegroup( + name = "sysroot_dir", + srcs = ["%{target_triple}/sysroot"], +) \ No newline at end of file From 1ac2ff1ce09da8c673694e32169941981018c21f Mon Sep 17 00:00:00 2001 From: Svante Karlsson Date: Thu, 11 Sep 2025 08:36:33 +0200 Subject: [PATCH 2/5] point to official release --- extentions/gcc.bzl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl index 8429e61..a2ef3f1 100644 --- a/extentions/gcc.bzl +++ b/extentions/gcc.bzl @@ -53,20 +53,19 @@ def _gcc_impl(mctx): # If no toolchains specified, use defaults if not toolchains: - # Default toolchains from skarlsson/toolchains_gcc_packages toolchains = [ { "name": "gcc_toolchain_x86_64", - "url": "https://github.com/skarlsson/toolchains_gcc_packages/releases/download/v0.0.2-pr3/x86_64-unknown-linux-gnu_gcc12.tar.gz", - "sha256": "ede0289c89a633f6f0b7fa5079a1fbc912e9a8607d01a792e3d78f4939d4e96a", + "url": "https://github.com/eclipse-score/toolchains_gcc_packages/releases/download/v0.0.3/x86_64-unknown-linux-gnu_gcc12.tar.gz", + "sha256": "8fa85c2a93a6bef1cf866fa658495a2416dfeec692e4246063b791abf18da083", "strip_prefix": "x86_64-unknown-linux-gnu", "target_arch": "x86_64", "exec_arch": "x86_64", }, { "name": "gcc_toolchain_aarch64", - "url": "https://github.com/skarlsson/toolchains_gcc_packages/releases/download/v0.0.2-pr3/aarch64-unknown-linux-gnu_gcc12.tar.gz", - "sha256": "cd9fcac29bea3dd7e52724166491a10e152f58188c9f25568e6f50b6c38f6dc3", + "url": "https://github.com/eclipse-score/toolchains_gcc_packages/releases/download/v0.0.3/aarch64-unknown-linux-gnu_gcc12.tar.gz", + "sha256": "57153340625581b199408391b895c84651382d3edd4c60fadbf0399f9dad21e1", "strip_prefix": "aarch64-unknown-linux-gnu", "target_arch": "aarch64", "exec_arch": "x86_64", From ad5d4b8dec6379d8df1a54c3b094c887e0aba2b3 Mon Sep 17 00:00:00 2001 From: Svante Karlsson Date: Thu, 11 Sep 2025 09:22:52 +0200 Subject: [PATCH 3/5] cleanup --- FORK_USAGE.md | 85 -------------------------------------------------- test/README.md | 7 +++++ 2 files changed, 7 insertions(+), 85 deletions(-) delete mode 100644 FORK_USAGE.md diff --git a/FORK_USAGE.md b/FORK_USAGE.md deleted file mode 100644 index 0b06f17..0000000 --- a/FORK_USAGE.md +++ /dev/null @@ -1,85 +0,0 @@ -# Using the ARM64-enabled Fork - -This fork includes: -- ARM64 (aarch64) platform support -- Default toolchains pointing to `skarlsson/toolchains_gcc_packages` v0.0.2-pr3 -- Both x86_64 and aarch64 GCC 12 toolchains - -Until these changes are merged into the official SCORE toolchains_gcc, you can use this fork locally. - -## Step 1: Clone the fork and checkout the ARM64 branch - -```bash -# As a submodule -git submodule add -b arm64-from-prebuilt https://github.com/skarlsson/toolchains_gcc.git - -# Or clone directly -git clone -b arm64-from-prebuilt https://github.com/skarlsson/toolchains_gcc.git -``` - -## Step 2: Configure MODULE.bazel - -```python -module( - name = "your_project", - version = "0.0.1", -) - -# Declare dependency on the version this fork is based on -bazel_dep(name = "score_toolchains_gcc", version = "0.5") - -# Override with the local fork (e.g., cloned as submodule) -local_path_override( - module_name = "score_toolchains_gcc", - path = "path/to/toolchains_gcc", # Adjust path to where you cloned the fork -) - -# Use the extension - default toolchains are automatically configured -gcc = use_extension("@score_toolchains_gcc//extentions:gcc.bzl", "gcc") - -# Optional: Configure warning flags and features (see test/MODULE.bazel for examples) -# gcc.warning_flags(...) -# gcc.extra_features(...) - -use_repo(gcc, "gcc_toolchain_x86_64", "gcc_toolchain_aarch64") - -# Register toolchains -register_toolchains( - "@gcc_toolchain_x86_64//:host_gcc_12", - "@gcc_toolchain_aarch64//:host_gcc_12", -) -``` - -## Step 3: Configure .bazelrc - -Add to your project's `.bazelrc`: - -```bash -# ARM64/AArch64 build configurations -build:arm64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux -build:aarch64 --platforms=@score_toolchains_gcc//platforms:aarch64-linux - -# Optional: explicit x86_64 configuration -build:x86_64 --platforms=@score_toolchains_gcc//platforms:x86_64-linux -``` - -## Step 4: Build - -```bash -# For ARM64 -bazel build --config=arm64 //your:target -# or -bazel build --config=aarch64 //your:target - -# For x86_64 (default) -bazel build //your:target -# or explicitly -bazel build --config=x86_64 //your:target -``` - -## Switching back to official release - -Once ARM64 support is merged and published, simply: -1. Remove the `archive_override` block -2. Update the version in `bazel_dep` to the new release -3. Keep your .bazelrc configurations - they'll continue to work! \ No newline at end of file diff --git a/test/README.md b/test/README.md index 285865c..eb0cae3 100644 --- a/test/README.md +++ b/test/README.md @@ -6,6 +6,13 @@ bazel build //:main_cpp ``` +### Building code arm64 gcc toolchain +```bash +bazel build --config=arm64 //:main_cpp +#or +bazel build --config=aarch64 //:main_cpp +``` + ### Building code with host gcc toolchain and enabled pthread ```bash From 9431de7071f75bc52ea470e54a53810dd673fdf5 Mon Sep 17 00:00:00 2001 From: Svante Karlsson Date: Thu, 11 Sep 2025 09:36:01 +0200 Subject: [PATCH 4/5] cleanup --- ARM64_USAGE.md | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/ARM64_USAGE.md b/ARM64_USAGE.md index 7180e19..477a72c 100644 --- a/ARM64_USAGE.md +++ b/ARM64_USAGE.md @@ -1,11 +1,9 @@ -# ARM64 Support for SCORE Toolchains GCC - -## Using ARM64 Toolchain +## Using aarch64/arm64 toolchain ### In your MODULE.bazel: ```python -bazel_dep(name = "score_toolchains_gcc", version = "0.5") +bazel_dep(name = "score_toolchains_gcc", version = "0.0.7") gcc = use_extension("@score_toolchains_gcc//extentions:gcc.bzl", "gcc") # Default toolchains for x86_64 and aarch64 are automatically configured @@ -17,7 +15,7 @@ register_toolchains( ) ``` -### Building for ARM64: +### Building for arm64: Add to your project's `.bazelrc`: ``` @@ -37,21 +35,6 @@ bazel build --config=aarch64 //your:target bazel build --platforms=@score_toolchains_gcc//platforms:aarch64-linux //your:target ``` -## Cross-compilation vs Native Compilation - -- **Cross-compilation** (x86_64 host → ARM64 target): - ```python - target_arch = "aarch64", - exec_arch = "x86_64", - ``` - -- **Native compilation** (ARM64 host → ARM64 target): - ```python - target_arch = "aarch64", - exec_arch = "aarch64", - ``` - - ## Example -See the `test/` directory for a complete working example that supports both x86_64 and ARM64 builds. \ No newline at end of file +See the `test/` directory for a complete working example that supports both x86_64 and aarch64 builds. From 1b04c71ba145f35b3866ec9e214b1bfe399a7142 Mon Sep 17 00:00:00 2001 From: Svante Karlsson Date: Thu, 11 Sep 2025 16:21:31 +0200 Subject: [PATCH 5/5] Removed redundant arch_map - The mapping was 1:1 and served no purpose Added build_file override capability - Allows custom BUILD files for non-standard GCC structures Removed unused template files - Cleaned up the toolchain/third_party directory --- extentions/gcc.bzl | 29 ++++++++++++---- rules/gcc.bzl | 10 ++---- toolchain/third_party/BUILD | 16 --------- toolchain/third_party/gcc.BUILD | 54 ----------------------------- toolchain/third_party/gcc.BUILD.tpl | 54 ----------------------------- 5 files changed, 24 insertions(+), 139 deletions(-) delete mode 100644 toolchain/third_party/BUILD delete mode 100644 toolchain/third_party/gcc.BUILD delete mode 100644 toolchain/third_party/gcc.BUILD.tpl diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl index a2ef3f1..ddc1375 100644 --- a/extentions/gcc.bzl +++ b/extentions/gcc.bzl @@ -34,6 +34,7 @@ def _gcc_impl(mctx): "sha256": tag.sha256, "target_arch": tag.target_arch, "exec_arch": tag.exec_arch, + "build_file": tag.build_file, }) for tag in mod.tags.extra_features: @@ -61,6 +62,7 @@ def _gcc_impl(mctx): "strip_prefix": "x86_64-unknown-linux-gnu", "target_arch": "x86_64", "exec_arch": "x86_64", + "build_file": None, }, { "name": "gcc_toolchain_aarch64", @@ -69,6 +71,7 @@ def _gcc_impl(mctx): "strip_prefix": "aarch64-unknown-linux-gnu", "target_arch": "aarch64", "exec_arch": "x86_64", + "build_file": None, }, ] @@ -79,10 +82,21 @@ def _gcc_impl(mctx): # Determine target triple based on architecture target_triple = "%s-unknown-linux-gnu" % target_arch - http_archive( - name = "%s_gcc" % name, - urls = [toolchain_info["url"]], - build_file_content = """ + if toolchain_info.get("build_file"): + # Use custom build_file if provided + http_archive( + name = "%s_gcc" % name, + urls = [toolchain_info["url"]], + build_file = toolchain_info["build_file"], + sha256 = toolchain_info["sha256"], + strip_prefix = toolchain_info["strip_prefix"], + ) + else: + # Use default BUILD file content + http_archive( + name = "%s_gcc" % name, + urls = [toolchain_info["url"]], + build_file_content = """ # Generated BUILD file for gcc toolchain package(default_visibility = ["//visibility:public"]) @@ -126,9 +140,9 @@ filegroup( srcs = ["{triple}/sysroot"], ) """.format(triple = target_triple), - sha256 = toolchain_info["sha256"], - strip_prefix = toolchain_info["strip_prefix"], - ) + sha256 = toolchain_info["sha256"], + strip_prefix = toolchain_info["strip_prefix"], + ) gcc_toolchain( name = name, @@ -150,6 +164,7 @@ gcc = module_extension( "sha256": attr.string(doc = "Checksum of the package"), "target_arch": attr.string(doc = "Target architecture (x86_64 or aarch64)", default="x86_64"), "exec_arch": attr.string(doc = "Execution architecture (x86_64 or aarch64)", default="x86_64"), + "build_file": attr.label(doc = "Label of custom BUILD file for the toolchain. If not provided, uses default generated content.", mandatory=False), }, ), "warning_flags": tag_class( diff --git a/rules/gcc.bzl b/rules/gcc.bzl index 41a069e..22fd64f 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -37,12 +37,6 @@ def _impl(rctx): rctx (repository_ctx): The Bazel repository context, providing access to attributes and methods for creating repository rules. """ - # Map architecture names to platform CPU values - arch_map = { - "x86_64": "x86_64", - "aarch64": "aarch64", - } - # Determine target system name based on architecture target_system = "%s-linux" % rctx.attr.target_arch @@ -52,8 +46,8 @@ def _impl(rctx): { "%{gcc_repo}": rctx.attr.gcc_repo, "%{tc_name}": "host_gcc_12", - "%{exec_cpu}": arch_map.get(rctx.attr.exec_arch, "x86_64"), - "%{target_cpu}": arch_map.get(rctx.attr.target_arch, "x86_64"), + "%{exec_cpu}": rctx.attr.exec_arch, + "%{target_cpu}": rctx.attr.target_arch, }, ) minimal_warnings = "[]" diff --git a/toolchain/third_party/BUILD b/toolchain/third_party/BUILD deleted file mode 100644 index 6e19dcb..0000000 --- a/toolchain/third_party/BUILD +++ /dev/null @@ -1,16 +0,0 @@ -# ******************************************************************************* -# Copyright (c) 2025 Contributors to the Eclipse Foundation -# -# See the NOTICE file(s) distributed with this work for additional -# information regarding copyright ownership. -# -# This program and the accompanying materials are made available under the -# terms of the Apache License Version 2.0 which is available at -# https://www.apache.org/licenses/LICENSE-2.0 -# -# SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* - -exports_files([ - "gcc.BUILD", -]) diff --git a/toolchain/third_party/gcc.BUILD b/toolchain/third_party/gcc.BUILD deleted file mode 100644 index a38799a..0000000 --- a/toolchain/third_party/gcc.BUILD +++ /dev/null @@ -1,54 +0,0 @@ -# ******************************************************************************* -# Copyright (c) 2025 Contributors to the Eclipse Foundation -# -# See the NOTICE file(s) distributed with this work for additional -# information regarding copyright ownership. -# -# This program and the accompanying materials are made available under the -# terms of the Apache License Version 2.0 which is available at -# https://www.apache.org/licenses/LICENSE-2.0 -# -# SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all_files", - srcs = glob(["*/**/*"]), -) - -filegroup( - name = "bin", - srcs = ["bin"], -) - -filegroup( - name = "ar", - srcs = ["bin/x86_64-unknown-linux-gnu-ar"], -) - -filegroup( - name = "gcc", - srcs = ["bin/x86_64-unknown-linux-gnu-gcc"], -) - -filegroup( - name = "gcov", - srcs = ["bin/x86_64-unknown-linux-gnu-gcov"], -) - -filegroup( - name = "gpp", - srcs = ["bin/x86_64-unknown-linux-gnu-g++"], -) - -filegroup( - name = "strip", - srcs = ["bin/x86_64-unknown-linux-gnu-strip"], -) - -filegroup( - name = "sysroot_dir", - srcs = ["x86_64-unknown-linux-gnu/sysroot"], -) diff --git a/toolchain/third_party/gcc.BUILD.tpl b/toolchain/third_party/gcc.BUILD.tpl deleted file mode 100644 index 5d64eb3..0000000 --- a/toolchain/third_party/gcc.BUILD.tpl +++ /dev/null @@ -1,54 +0,0 @@ -# ******************************************************************************* -# Copyright (c) 2025 Contributors to the Eclipse Foundation -# -# See the NOTICE file(s) distributed with this work for additional -# information regarding copyright ownership. -# -# This program and the accompanying materials are made available under the -# terms of the Apache License Version 2.0 which is available at -# https://www.apache.org/licenses/LICENSE-2.0 -# -# SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* - -package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all_files", - srcs = glob(["*/**/*"]), -) - -filegroup( - name = "bin", - srcs = ["bin"], -) - -filegroup( - name = "ar", - srcs = ["bin/%{target_triple}-ar"], -) - -filegroup( - name = "gcc", - srcs = ["bin/%{target_triple}-gcc"], -) - -filegroup( - name = "gcov", - srcs = ["bin/%{target_triple}-gcov"], -) - -filegroup( - name = "gpp", - srcs = ["bin/%{target_triple}-g++"], -) - -filegroup( - name = "strip", - srcs = ["bin/%{target_triple}-strip"], -) - -filegroup( - name = "sysroot_dir", - srcs = ["%{target_triple}/sysroot"], -) \ No newline at end of file