Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions ARM64_USAGE.md
Original file line number Diff line number Diff line change
@@ -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.
142 changes: 117 additions & 25 deletions extentions/gcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions platforms/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ platform(
],
visibility = ["//visibility:public"],
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have platform repository which we need to reuse for platform definitions. It's still not released, but that's just matter of adding it to S-CORE Bazel Registry.
Bazel Platform repo: https://github.com/eclipse-score/bazel_platforms

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skarlsson Whats the state of that pull request? That would be really great to have it in. Can you please fix the comments especially the platforms. Release is already there bazel_dep(name = "score_bazel_platforms", version = "0.0.2")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the earlier comment as a hint to wait until the you had a new implementation in place and I thought to give it a try again at a later time.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request - at the time it was done worked and I had a working arm64 baselibs on top of that - but I found it inefficient to have so many local forks so I decided to wait

platform(
name = "aarch64-linux",
constraint_values = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
visibility = ["//visibility:public"],
)
9 changes: 9 additions & 0 deletions rules/gcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[]"
Expand Down Expand Up @@ -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,
},
)

Expand All @@ -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.",
Expand Down
9 changes: 9 additions & 0 deletions test/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ cc_binary(
cc_binary(
name = "main_pthread_cpp",
srcs = ["main_pthread.cpp"],
features = ["use_pthread"],
)
45 changes: 35 additions & 10 deletions test/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
7 changes: 7 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions toolchain/internal/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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,
Expand Down
Loading