diff --git a/ARM64_USAGE.md b/ARM64_USAGE.md new file mode 100644 index 0000000..477a72c --- /dev/null +++ b/ARM64_USAGE.md @@ -0,0 +1,40 @@ +## Using aarch64/arm64 toolchain + +### In your MODULE.bazel: + +```python +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 + +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 +``` + +## Example + +See the `test/` directory for a complete working example that supports both x86_64 and aarch64 builds. diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl index 9051096..ddc1375 100644 --- a/extentions/gcc.bzl +++ b/extentions/gcc.bzl @@ -21,49 +21,138 @@ 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, + "build_file": tag.build_file, + }) 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: - http_archive( - name = "%s_gcc" % toolchain_info["name"], - urls = [toolchain_info["url"]], - build_file = "@score_toolchains_gcc//toolchain/third_party:gcc.BUILD", - sha256 = toolchain_info["sha256"], - strip_prefix = toolchain_info["strip_prefix"], - ) + # If no toolchains specified, use defaults + if not toolchains: + toolchains = [ + { + "name": "gcc_toolchain_x86_64", + "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", + "build_file": None, + }, + { + "name": "gcc_toolchain_aarch64", + "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", + "build_file": None, + }, + ] + + 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 + + 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"]) + +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 +162,9 @@ 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"), + "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/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..22fd64f 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -37,12 +37,17 @@ def _impl(rctx): rctx (repository_ctx): The Bazel repository context, providing access to attributes and methods for creating repository rules. """ + # 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}": rctx.attr.exec_arch, + "%{target_cpu}": rctx.attr.target_arch, }, ) minimal_warnings = "[]" @@ -73,6 +78,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 +89,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/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 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/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"], -)