diff --git a/bazel/extentions/common.bzl b/bazel/extentions/common.bzl deleted file mode 100644 index 8cb9a5a..0000000 --- a/bazel/extentions/common.bzl +++ /dev/null @@ -1,193 +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 -# ******************************************************************************* - -""" Common functions for Bazel module extensions. -""" -load("@score_bazel_cpp_toolchains//packages:version_matrix.bzl", "VERSION_MATRIX") - -# Common module attributes for package/sdp achive. -archive_attrs = { - "name": attr.string( - default = "", - doc = "package name of toolchain, default set to toolchain toolchain name + `_pkg`.", - ), - "build_file": attr.string( - mandatory = False, - default = "", - doc = "The path to the BUILD file of selected archive.", - ), - "url": attr.string( - mandatory = False, - default = "", - doc = "Url to the toolchain archive." - ), - "strip_prefix": attr.string( - mandatory = False, - default = "", - doc = "Strip prefix from toolchain archive.", - ), - "sha256": attr.string( - mandatory = False, - default = "", - doc = "Checksum of the archive." - ), -} - -# Coomon module attributes for toolchain configuration. -tc_module_attrs = { - "package_to_link": attr.string( - mandatory = False, - default = "", - doc = "Name of the toolchain package to be linked with this toolchain, default set to toolchain name + `_pkg`.", - ), - "use_default_package": attr.bool( - default = False, - doc = "Whether to use the default package from the version matrix, default set to False.", - ), - "target_cpu": attr.string( - mandatory = True, - values = [ - "x86_64", - "arm64", - ], - doc = "Target platform CPU", - ), - "version": attr.string( - default = "", - doc = "Version of the GCC(QCC driver in case of QNX) toolchain.", - ), - "extra_compile_flags": attr.string_list( - mandatory = False, - default = [], - doc = "List of additional flags to be passed to compiler.", - ), - "extra_link_flags": attr.string_list( - mandatory = False, - default = [], - doc = "List of additional flags to be passed to linker.", - ), -} - -def _get_archives(tags): - """Gets archive information from given tags. - - Args: - tags: A list of tags containing archive information. - - Returns: - dict: A dictionary with archive information. - """ - archives = [] - for tag in tags: - archives.append({ - "name": tag.name, - "url": tag.url, - "strip_prefix": tag.strip_prefix, - "sha256": tag.sha256, - "build_file": tag.build_file, - }) - return archives - -def _get_toolchains(tags, family): - """Gets toolchain information from given tags. - - Args: - tags: A list of tags containing toolchain information. - family: An identifier that holds the driver type (gcc, qcc or llvm). - - Returns: - dict: A dictionary with toolchain information. - """ - toolchains = [] - for tag in tags: - toolchain = { - "name": tag.name, - "tc_cpu": tag.target_cpu, - "tc_os": tag.target_os, - "tc_family": family, - "tc_family_version": tag.version, - "package_to_link": tag.package_to_link, - "use_default_package": tag.use_default_package, - "extra_compile_flags": tag.extra_compile_flags, - "extra_link_flags": tag.extra_link_flags, - } - if hasattr(tag, "qnx_license_path"): - toolchain.update({"qnx_license_path": tag.qnx_license_path}) - if hasattr(tag, "license_info_url"): - toolchain.update({"license_info_url": tag.license_info_url}) - if hasattr(tag, "license_info_variable"): - toolchain.update({"license_info_variable": tag.license_info_variable}) - if hasattr(tag, "sdp_version"): - toolchain.update({"sdp_version": tag.sdp_version}) - toolchains.append(toolchain) - return toolchains - -def get_info(mctx, family): - """Gets raw info from module ctx about toolchain properties. - - Args: - mctx: A bazel object holding module information. - family: An identifier that holds the driver type (gcc, qcc or llvm). - - Returns: - list: A list of dictionaries with toolchain and archive information. - """ - root = None - for mod in mctx.modules: - if not mod.is_root: - fail("Only the root module can use the '{}' extension!".format(family)) - root = mod - - toolchains = _get_toolchains(mod.tags.toolchain, family) - - tags = None - if family == "gcc": - tags = mod.tags.package - elif family == "qcc": - tags = mod.tags.sdp - else: - fail("Unsupported family: {}".format(family)) - archives = _get_archives(tags) - - for toolchain in toolchains: - if toolchain["package_to_link"] == "" and toolchain["use_default_package"]: - pkg_name = "{}_pkg".format(toolchain["name"]) - # Check if archive with such name exists - for archive in archives: - if archive["name"] == pkg_name: - toolchain["package_to_link"] = pkg_name - break - if toolchain["package_to_link"] == "": - # Get the archive from matix - identifier = toolchain["tc_family"] - version = toolchain["tc_family_version"] - if family == "qcc": - identifier = "sdp" - version = toolchain["sdp_version"] - matrix = VERSION_MATRIX["{cpu}-{os}-{identifier}-{version}".format( - cpu = toolchain["tc_cpu"], - os = toolchain["tc_os"], - identifier = identifier, - version = version, - )] - archive = { - "name": pkg_name, - "url": matrix["url"], - "strip_prefix": matrix["strip_prefix"], - "sha256": matrix["sha256"], - "build_file": matrix["build_file"], - } - archives.append(archive) - toolchain["package_to_link"] = pkg_name - return toolchains, archives - diff --git a/bazel/extentions/gcc.bzl b/bazel/extentions/gcc.bzl deleted file mode 100644 index 3a478dc..0000000 --- a/bazel/extentions/gcc.bzl +++ /dev/null @@ -1,75 +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 -# ******************************************************************************* - -""" Module extension for setting up GCC toolchains in Bazel. -""" -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@score_bazel_cpp_toolchains//bazel/extentions:common.bzl", "get_info", "tc_module_attrs", _attrs_archive = "archive_attrs") -load("@score_bazel_cpp_toolchains//bazel/rules:gcc.bzl", "gcc_toolchain") - -# GCC interface API for toolchain tag class -_attrs_gcc_tc = {} -_attrs_gcc_tc.update(tc_module_attrs) -_attrs_gcc_tc.update({ - "name": attr.string( - default = "score_gcc_toolchain", - doc = "Toolchain repo name, default set to `score_gcc_toolchain`.", - ), - "target_os": attr.string( - default = "linux", - doc = "Target operating system for the GCC toolchain.", - ), -}) - -# GCC interface API for archive tag class -archive_attrs = _attrs_archive - -def _gcc_impl(mctx): - """Extracts information about toolchain and instantiates nessesary rules for toolchain declaration. - - Args: - mctx: A bazel object holding module information. - - """ - toolchains, archives = get_info(mctx, "gcc") - for archive_info in archives: - http_archive( - name = archive_info["name"], - urls = [archive_info["url"]], - build_file = archive_info["build_file"], - sha256 = archive_info["sha256"], - strip_prefix = archive_info["strip_prefix"], - ) - - for toolchain_info in toolchains: - gcc_toolchain( - name = toolchain_info["name"], - tc_pkg_repo = toolchain_info["package_to_link"], - tc_cpu = toolchain_info["tc_cpu"], - tc_os = toolchain_info["tc_os"], - gcc_version = toolchain_info["tc_family_version"], - extra_compile_flags = toolchain_info["extra_compile_flags"], - extra_link_flags = toolchain_info["extra_link_flags"], - ) - -gcc = module_extension( - implementation = _gcc_impl, - tag_classes = { - "toolchain": tag_class( - attrs = _attrs_gcc_tc, - ), - "package": tag_class( - attrs = _attrs_archive, - ), - } -) \ No newline at end of file diff --git a/bazel/extentions/qcc.bzl b/bazel/extentions/qcc.bzl deleted file mode 100644 index 57e189d..0000000 --- a/bazel/extentions/qcc.bzl +++ /dev/null @@ -1,89 +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 -# ******************************************************************************* - -""" Module extension for setting up QCC toolchains in Bazel. -""" -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@score_bazel_cpp_toolchains//bazel/extentions:common.bzl", "get_info", "tc_module_attrs", _attrs_archive = "archive_attrs") -load("@score_bazel_cpp_toolchains//bazel/rules:qcc.bzl", "qcc_toolchain") - -# QCC interface API for toolchain tag class -_attrs_qcc_tc = {} -_attrs_qcc_tc.update(tc_module_attrs) -_attrs_qcc_tc.update({ - "name": attr.string( - default = "score_qcc_toolchain", - doc = "Toolchain repo name, default set to `score_qcc_toolchain`.", - ), - "target_os": attr.string( - default = "qnx", - doc = "Target operating system for the QCC toolchain.", - ), - "license_info_url": attr.string( - default = "", - doc = "URL of the QNX license server.", - ), - "license_info_variable": attr.string( - default = "LM_LICENSE_FILE", - doc = "QNX License info variable.", - ), - "sdp_version": attr.string( - default = "8.0.0", - doc = "Version of the SDP package.", - ), - "qnx_license_path": attr.string( - default = "/opt/score_qnx/license/licenses", - doc = "Path to the shared license file.", - ), -}) - -# QCC interface API for archive (SDP) tag class -archive_attrs = _attrs_archive - -def _impl(mctx): - """Implementation of the module extension.""" - toolchains, archives = get_info(mctx, "qcc") - for archive_info in archives: - http_archive( - name = archive_info["name"], - urls = [archive_info["url"]], - build_file = archive_info["build_file"], - sha256 = archive_info["sha256"], - strip_prefix = archive_info["strip_prefix"], - ) - - for toolchain_info in toolchains: - qcc_toolchain( - name = toolchain_info["name"], - tc_pkg_repo = toolchain_info["package_to_link"], - tc_cpu = toolchain_info["tc_cpu"], - qcc_version = toolchain_info["tc_family_version"], - sdp_version = toolchain_info["sdp_version"], - license_info_variable = toolchain_info["license_info_variable"], - license_info_value = toolchain_info["license_info_url"], - extra_compile_flags = toolchain_info["extra_compile_flags"], - extra_link_flags = toolchain_info["extra_link_flags"], - qnx_license_path = toolchain_info["qnx_license_path"], - ) - -qcc = module_extension( - implementation = _impl, - tag_classes = { - "toolchain": tag_class( - attrs = _attrs_qcc_tc, - ), - "sdp": tag_class( - attrs = _attrs_archive, - ), - }, -) \ No newline at end of file diff --git a/bazel/rules/gcc.bzl b/bazel/rules/gcc.bzl deleted file mode 100644 index d4d729f..0000000 --- a/bazel/rules/gcc.bzl +++ /dev/null @@ -1,84 +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 -# ******************************************************************************* - -""" Module rule for defining GCC toolchains in Bazel. -""" - -load("@score_bazel_cpp_toolchains//bazel/rules:common.bzl", "get_flag_groups") - -def _impl(rctx): - """ Implementation of the gcc_toolchain repository rule. - - Args: - rctx: The repository context. - """ - - rctx.template( - "BUILD", - rctx.attr._cc_toolchain_common_build, - { - "%{tc_pkg_repo}": rctx.attr.tc_pkg_repo, - "%{tc_cpu}": rctx.attr.tc_cpu, - "%{tc_family}": "gcc", - "%{tc_os}": rctx.attr.tc_os, - "%{tc_version}": rctx.attr.gcc_version, - }, - ) - - rctx.template( - "gcc/BUILD", - rctx.attr._cc_toolchain_gcc_build, - { - "%{tc_pkg_repo}": rctx.attr.tc_pkg_repo, - "%{tc_cpu}": rctx.attr.tc_cpu, - }, - ) - - extra_compile_flags = get_flag_groups(rctx.attr.extra_compile_flags) - extra_link_flags = get_flag_groups(rctx.attr.extra_link_flags) - - rctx.template( - "gcc/cc_toolchain_config.bzl", - rctx.attr._cc_toolchain_config, - { - "%{tc_version}": rctx.attr.gcc_version, - "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", - "%{extra_compile_flags}":extra_compile_flags, - "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", - "%{extra_link_flags}": extra_link_flags, - }, - ) - -gcc_toolchain = repository_rule( - implementation = _impl, - attrs = { - "tc_pkg_repo": attr.string(doc="The label name of toolchain tarbal."), - "tc_cpu": attr.string(doc="Target platform CPU."), - "tc_os": attr.string(doc="Target platform OS."), - "gcc_version": attr.string(doc="GCC version number"), - "extra_compile_flags": attr.string_list(doc="Extra/Additional compile flags."), - "extra_link_flags": attr.string_list(doc="Extra/Additional link flags."), - "_cc_toolchain_config": attr.label( - default = "@score_bazel_cpp_toolchains//templates/gcc:cc_toolchain_config.bzl.template", - doc = "Path to the cc_config.bzl template file.", - ), - "_cc_toolchain_common_build": attr.label( - default = "@score_bazel_cpp_toolchains//templates/common:BUILD.template", - doc = "Path to the Bazel BUILD file template for the toolchain.", - ), - "_cc_toolchain_gcc_build": attr.label( - default = "@score_bazel_cpp_toolchains//templates/gcc:BUILD.template", - doc = "Path to the Bazel BUILD file template for the toolchain.", - ), - }, -) \ No newline at end of file diff --git a/bazel/rules/qcc.bzl b/bazel/rules/qcc.bzl deleted file mode 100644 index 56451ef..0000000 --- a/bazel/rules/qcc.bzl +++ /dev/null @@ -1,109 +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 -# ******************************************************************************* - -""" Module rule for defining GCC toolchains in Bazel. -""" - -# ******************************************************************************* -# 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 -# ******************************************************************************* - -""" Module rule for defining GCC toolchains in Bazel. -""" - -load("@score_bazel_cpp_toolchains//bazel/rules:common.bzl", "get_flag_groups") - -def _impl(rctx): - """ Implementation of the gcc_toolchain repository rule. - - Args: - rctx: The repository context. - """ - - rctx.template( - "BUILD", - rctx.attr._cc_toolchain_common_build, - { - "%{tc_pkg_repo}": rctx.attr.tc_pkg_repo, - "%{tc_cpu}": rctx.attr.tc_cpu, - "%{tc_family}": "qcc", - "%{tc_os}": "qnx", - "%{tc_version}": rctx.attr.qcc_version, - }, - ) - - rctx.template( - "qcc/BUILD", - rctx.attr._cc_toolchain_qcc_build, - { - "%{tc_pkg_repo}": rctx.attr.tc_pkg_repo, - }, - ) - - extra_compile_flags = get_flag_groups(rctx.attr.extra_compile_flags) - extra_link_flags = get_flag_groups(rctx.attr.extra_link_flags) - - rctx.template( - "qcc/cc_toolchain_config.bzl", - rctx.attr._cc_toolchain_config, - { - "%{tc_cpu}": "aarch64le" if rctx.attr.tc_cpu == "arm64" else rctx.attr.tc_cpu, - "%{tc_cpu_cxx}": "aarch64le" if rctx.attr.tc_cpu == "arm64" else rctx.attr.tc_cpu, - "%{tc_version}": rctx.attr.qcc_version, - "%{sdp_version}": rctx.attr.sdp_version, - "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", - "%{extra_compile_flags}":extra_compile_flags, - "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", - "%{extra_link_flags}": extra_link_flags, - "%{qnx_license_path}": rctx.attr.qnx_license_path, - "%{use_license_info}": "False" if rctx.attr.license_info_value == "" else "True", - "%{license_info_variable}": rctx.attr.license_info_variable, - "%{license_info_value}": rctx.attr.license_info_value, - }, - ) - -qcc_toolchain = repository_rule( - implementation = _impl, - attrs = { - "tc_pkg_repo": attr.string(doc="The label name of toolchain tarbal"), - "tc_cpu": attr.string(doc="Target platform CPU"), - "qcc_version": attr.string(doc="GCC version number"), - "sdp_version": attr.string(doc="SDP version number"), - "qnx_license_path": attr.string(doc="QNX Lincese path"), - "license_info_variable": attr.string(doc="QNX License info variable name (custom settings)"), - "license_info_value": attr.string(doc="QNX License info value (custom settings)"), - "extra_compile_flags": attr.string_list(doc="Extra/Additional compile flags."), - "extra_link_flags": attr.string_list(doc="Extra/Additional link flags."), - "_cc_toolchain_config": attr.label( - default = "@score_bazel_cpp_toolchains//templates/qcc:cc_toolchain_config.bzl.template", - doc = "Path to the cc_config.bzl template file.", - ), - "_cc_toolchain_common_build": attr.label( - default = "@score_bazel_cpp_toolchains//templates/common:BUILD.template", - doc = "Path to the Bazel BUILD file template for the toolchain.", - ), - "_cc_toolchain_qcc_build": attr.label( - default = "@score_bazel_cpp_toolchains//templates/qcc:BUILD.template", - doc = "Path to the Bazel BUILD file template for the toolchain.", - ), - }, -) \ No newline at end of file diff --git a/configurations/common/flags.bzl b/configurations/common/flags.bzl deleted file mode 100644 index e69de29..0000000 diff --git a/configurations/qcc/.keep b/configurations/qcc/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/examples/.bazelrc b/examples/.bazelrc index c26866b..ee241d5 100644 --- a/examples/.bazelrc +++ b/examples/.bazelrc @@ -11,15 +11,11 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -# ------------------------------------------------------------------------------- # Link to S-CORE Bazel Registry -# ------------------------------------------------------------------------------- common --registry=https://raw.githubusercontent.com/eclipse-score/bazel_registry/main/ common --registry=https://bcr.bazel.build -# ------------------------------------------------------------------------------- -# Shared configuration for simple example execution -# ------------------------------------------------------------------------------- +# Shared configuration for simple test execution build:shared --incompatible_strict_action_env build:shared --sandbox_writable_path=/var/tmp build:shared --host_platform=@score_bazel_platforms//:x86_64-linux @@ -28,41 +24,54 @@ build:shared --host_platform=@score_bazel_platforms//:x86_64-linux # Config dedicated to host platform CPU:x86_64 and OS:Linux # ------------------------------------------------------------------------------- build:x86_64-linux --config=shared -build:x86_64-linux --platforms=@score_bazel_platforms//:x86_64-linux +build:x86_64-linux --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix # ------------------------------------------------------------------------------- # Different toolchain configuration for x86_64-linux # ------------------------------------------------------------------------------- build:host_config_1 --config=x86_64-linux -build:host_config_1 --extra_toolchains=@score_gcc_toolchain//:x86_64-linux-gcc-12.2.0 +build:host_config_1 --extra_toolchains=@score_gcc_toolchain//:x86_64-linux-gcc_12.2.0-posix build:host_config_1 --features=use_pthread # ------------------------------------------------------------------------------- # Different toolchain configuration for x86_64-linux # ------------------------------------------------------------------------------- build:host_config_2 --config=x86_64-linux -build:host_config_2 --extra_toolchains=@my_toolchain//:x86_64-linux-gcc-12.2.0 +build:host_config_2 --extra_toolchains=@my_toolchain//:x86_64-linux-gcc_12.2.0-posix + +# ------------------------------------------------------------------------------- +# Config dedicated to target platform CPU:aarch64 and OS:linux +# ------------------------------------------------------------------------------- +build:aarch64-linux --config=shared +build:aarch64-linux --platforms=@score_bazel_platforms//:aarch64-linux-gcc_12.2.0-posix + +# ------------------------------------------------------------------------------- +# Different toolchain configuration for aarch64-linux +# ------------------------------------------------------------------------------- +build:target_config_3 --config=aarch64-linux +build:target_config_3 --extra_toolchains=@score_aarch64_gcc_toolchain//:aarch64-linux-gcc_12.2.0-posix + # ------------------------------------------------------------------------------- # Config dedicated to target platform CPU:x86_64 and OS:QNX # ------------------------------------------------------------------------------- build:x86_64-qnx --config=shared -build:x86_64-qnx --platforms=@score_bazel_platforms//:x86_64-qnx +build:x86_64-qnx --platforms=@score_bazel_platforms//:x86_64-qnx-sdp_8.0.0-posix # ------------------------------------------------------------------------------- # Different toolchain configuration for x86_64-qnx # ------------------------------------------------------------------------------- build:target_config_1 --config=x86_64-qnx -build:target_config_1 --extra_toolchains=@score_qcc_toolchain//:x86_64-qnx-qcc-12.2.0 +build:target_config_1 --extra_toolchains=@score_qcc_toolchain//:x86_64-qnx-sdp_8.0.0-posix # ------------------------------------------------------------------------------- -# Config dedicated to target platform CPU:arm64 and OS:QNX +# Config dedicated to target platform CPU:aarch64 and OS:QNX # ------------------------------------------------------------------------------- -build:arm64-qnx --config=shared -build:arm64-qnx --platforms=@score_bazel_platforms//:arm64-qnx +build:aarch64-qnx --config=shared +build:aarch64-qnx --platforms=@score_bazel_platforms//:aarch64-qnx-sdp_8.0.0-posix # ------------------------------------------------------------------------------- -# Different toolchain configuration for arm64-qnx +# Different toolchain configuration for aarch64-qnx # ------------------------------------------------------------------------------- -build:target_2_config_1 --config=arm64-qnx -build:target_2_config_1 --extra_toolchains=@score_qcc_arm64_toolchain//:arm64-qnx-qcc-12.2.0 +build:target_config_2 --config=aarch64-qnx +build:target_config_2 --extra_toolchains=@score_qcc_arm_toolchain//:aarch64-qnx-sdp_8.0.0-posix diff --git a/examples/BUILD b/examples/BUILD index d60eff6..68ceb11 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -23,4 +23,16 @@ cc_binary( cc_binary( name = "main_pthread_cpp", srcs = ["main_pthread.cpp"], +) + +cc_library( + name = "math_lib", + srcs = ["math_lib.cpp"], + hdrs = ["math_lib.h"], +) + +cc_test( + name = "math_lib_test", + srcs = ["math_lib_test.cpp"], + deps = [":math_lib"], ) \ No newline at end of file diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 81c2c0f..6e3aabd 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -14,7 +14,6 @@ module( name = "test_score_bazel_cpp_toolchains", version = "0.1.0", - compatibility_level = 0, ) # ******************************************************************************* @@ -26,7 +25,12 @@ bazel_dep(name = "bazel_skylib", version = "1.8.1") # Constraint values for specifying platforms and toolchains # ******************************************************************************* bazel_dep(name = "platforms", version = "1.0.0") -bazel_dep(name = "score_bazel_platforms", version = "0.0.2") +bazel_dep(name = "score_bazel_platforms", version = "0.0.4") +# git_override( +# module_name = "score_bazel_platforms", +# remote = "https://github.com/eclipse-score/bazel_platforms.git/", +# commit = "583e52c58f1f110357aa93fcba3c01cbd7b51675", +# ) # ******************************************************************************* # C++ Rules for Bazel @@ -46,30 +50,51 @@ local_path_override( ) # ******************************************************************************* -# Setting GCC (CPU:x86_64|OS:Linux|v:12.2.0) +# Init GCC extention +# Legend: +# * CPU: Control Processeor Unit +# * OS: Operating System +# * V: Version (GCC or SDP) +# * ES: Runtime-EcoSystem +# # ******************************************************************************* -gcc = use_extension("@score_bazel_cpp_toolchains//bazel/extentions:gcc.bzl", "gcc") +gcc = use_extension("@score_bazel_cpp_toolchains//extentions:gcc.bzl", "gcc", dev_dependency = True) -# Define toolchains with default package from version matrix +# ******************************************************************************* +# Setting default GCC (CPU:x86_64|OS:Linux|V:12.2.0|ES:posix) +# ******************************************************************************* gcc.toolchain( + name = "score_gcc_toolchain", target_cpu = "x86_64", + target_os = "linux", version = "12.2.0", use_default_package = True, ) -use_repo(gcc, "score_gcc_toolchain", "score_gcc_toolchain_pkg") -# The host package for the toolchain x86_64-linux-gcc-12.2.0 manually defined below -gcc.package( +# ******************************************************************************* +# Setting default GCC (CPU:aarch64|OS:Linux|V:12.2.0|ES:posix) +# ******************************************************************************* +gcc.toolchain( + name = "score_aarch64_gcc_toolchain", + target_cpu = "aarch64", + target_os = "linux", + version = "12.2.0", + use_default_package = True, +) + +# ******************************************************************************* +# Setting custom GCC (CPU:x86_64|OS:Linux|V:12.2.0|ES:posix) +# ******************************************************************************* +gcc.sdp( name = "my_toolchain_pkg", url = "https://github.com/eclipse-score/toolchains_gcc_packages/releases/download/0.0.1/x86_64-unknown-linux-gnu_gcc12.tar.gz", strip_prefix = "x86_64-unknown-linux-gnu", sha256 = "457f5f20f57528033cb840d708b507050d711ae93e009388847e113b11bf3600", build_file = "@score_bazel_cpp_toolchains//packages/linux/x86_64/gcc/12.2.0:gcc.BUILD", ) -# Define toolchains with custom package gcc.toolchain( name = "my_toolchain", - package_to_link = "my_toolchain_pkg", + sdp_to_link = "my_toolchain_pkg", target_os = "linux", target_cpu = "x86_64", version = "12.2.0", @@ -77,23 +102,60 @@ gcc.toolchain( "-lpthread", ] ) -use_repo(gcc, "my_toolchain", "my_toolchain_pkg") # ******************************************************************************* -# Setting QCC (CPU:x86_64|OS:QNX|v:8.0) +# Setting GCC (CPU:x86_64|OS:QNX|version(sdp):8.0.0|ES:posix) # ******************************************************************************* -qcc = use_extension("@score_bazel_cpp_toolchains//bazel/extentions:qcc.bzl", "qcc") -qcc.toolchain( +gcc.toolchain( + name = "score_qcc_toolchain", target_cpu = "x86_64", + target_os = "qnx", + sdp_version = "8.0.0", version = "12.2.0", use_default_package = True, ) -use_repo(qcc, "score_qcc_toolchain", "score_qcc_toolchain_pkg") -qcc.toolchain( - name = "score_qcc_arm64_toolchain", - target_cpu = "arm64", +# ******************************************************************************* +# Setting GCC (CPU:aarch64|OS:QNX|version(sdp):8.0.0|ES:posix) +# ******************************************************************************* +gcc.toolchain( + name = "score_qcc_arm_toolchain", + target_cpu = "aarch64", + target_os = "qnx", + sdp_version = "8.0.0", version = "12.2.0", use_default_package = True, ) -use_repo(qcc, "score_qcc_arm64_toolchain", "score_qcc_arm64_toolchain_pkg") \ No newline at end of file + +# TODO: Not yet supported +# ******************************************************************************* +# Setting local host GCC (CPU:x86_64|OS:Linux|V:unknown|ES:host) +# ******************************************************************************* +# gcc.toolchain( +# name = "score_system_toolchain", +# target_cpu = "x86_64", +# target_os = "linux", +# use_system_toolchain = True, +# ) + +# TODO: Not yet supported +# ******************************************************************************* +# Setting AutoSD GCC (CPU:x86_64|OS:Linux|V:unknown|ES:autosd) +# ******************************************************************************* +# gcc.toolchain( +# name = "score_autosd_9_toolchain", +# target_cpu = "x86_64", +# target_os = "linux", +# runtime_ecosystem = "autosd", +# version = "9", +# use_system_toolchain = True, +# ) + +use_repo( + gcc, + "score_gcc_toolchain", + "score_aarch64_gcc_toolchain", + "my_toolchain", + "score_qcc_toolchain", + "score_qcc_arm_toolchain", +) \ No newline at end of file diff --git a/examples/math_lib.cpp b/examples/math_lib.cpp new file mode 100644 index 0000000..bd2d6df --- /dev/null +++ b/examples/math_lib.cpp @@ -0,0 +1,25 @@ +/******************************************************************************** +* Copyright (c) 2026 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 +********************************************************************************/ + +#include "math_lib.h" + +int add(int a, int b) { + return a + b; +} + +int sub(int a, int b) { + if (a >= b) { + return a - b; + } + return b - a; // branch to demonstrate partial coverage +} \ No newline at end of file diff --git a/examples/math_lib.h b/examples/math_lib.h new file mode 100644 index 0000000..ab890b0 --- /dev/null +++ b/examples/math_lib.h @@ -0,0 +1,17 @@ +/******************************************************************************** +* Copyright (c) 2026 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 +********************************************************************************/ + +#pragma once + +int add(int a, int b); +int sub(int a, int b); \ No newline at end of file diff --git a/examples/math_lib_test.cpp b/examples/math_lib_test.cpp new file mode 100644 index 0000000..ac3436d --- /dev/null +++ b/examples/math_lib_test.cpp @@ -0,0 +1,23 @@ +/******************************************************************************** +* Copyright (c) 2026 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 +********************************************************************************/ + +#include "math_lib.h" +#include + +int main() { + assert(add(2, 3) == 5); + assert(sub(5, 3) == 2); + + // Note: sub(3, 5) is NOT tested → coverage will show a missed branch + return 0; +} \ No newline at end of file diff --git a/configurations/common/BUILD b/extentions/BUILD similarity index 100% rename from configurations/common/BUILD rename to extentions/BUILD diff --git a/extentions/gcc.bzl b/extentions/gcc.bzl new file mode 100644 index 0000000..6b6b8fe --- /dev/null +++ b/extentions/gcc.bzl @@ -0,0 +1,280 @@ +# ******************************************************************************* +# 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 +# ******************************************************************************* + +""" Module extension for setting up GCC toolchains in Bazel. +""" +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@score_bazel_cpp_toolchains//packages:version_matrix.bzl", "VERSION_MATRIX") +load("@score_bazel_cpp_toolchains//rules:gcc.bzl", "gcc_toolchain") + +# GCC interface API for archive tag class +_attrs_sdp = { + "name": attr.string( + default = "", + doc = "package name of toolchain, default set to toolchain toolchain name + `_pkg`.", + ), + "build_file": attr.string( + mandatory = False, + default = "", + doc = "The path to the BUILD file of selected archive.", + ), + "url": attr.string( + mandatory = False, + default = "", + doc = "Url to the toolchain archive." + ), + "strip_prefix": attr.string( + mandatory = False, + default = "", + doc = "Strip prefix from toolchain archive.", + ), + "sha256": attr.string( + mandatory = False, + default = "", + doc = "Checksum of the archive." + ), +} + +# GCC interface API for toolchain tag class +_attrs_tc = { + "name": attr.string( + mandatory = True, + doc = "Toolchain repo name, default set to `score_gcc_toolchain`.", + ), + "sdp_to_link": attr.string( + mandatory = False, + default = "", + doc = "Name of the toolchain package to be linked with this toolchain, default set to toolchain name + `_pkg`.", + ), + "use_default_package": attr.bool( + default = False, + doc = "Whether to use the default package from the version matrix, default set to False.", + ), + "use_system_toolchain": attr.bool( + default = False, + doc = "TBD", + ), + "runtime_ecosystem": attr.string( + default = "posix", + mandatory = False, + doc = "TBD", + ), + "target_cpu": attr.string( + mandatory = True, + values = [ + "x86_64", + "aarch64", + ], + doc = "Target platform CPU", + ), + "target_os": attr.string( + mandatory = True, + values = [ + "linux", + "qnx", + ], + doc = "Target platform OS", + ), + "version": attr.string( + default = "", + mandatory = False, + doc = "Version of the GCC toolchain.", + ), + "license_info_url": attr.string( + default = "", + mandatory = False, + doc = "URL of the QNX license server.", + ), + "license_info_variable": attr.string( + default = "", + mandatory = False, + doc = "QNX License info variable.", + ), + "sdp_version": attr.string( + default = "8.0.0", + mandatory = False, + doc = "Version of the SDP package.", + ), + "license_path": attr.string( + default = "/opt/score_qnx/license/licenses", + mandatory = False, + doc = "Path to the shared license file.", + ), + "extra_compile_flags": attr.string_list( + mandatory = False, + default = [], + doc = "List of additional flags to be passed to compiler.", + ), + "extra_link_flags": attr.string_list( + mandatory = False, + default = [], + doc = "List of additional flags to be passed to linker.", + ), +} + +def _get_packages(tags): + """Gets archive information from given tags. + + Args: + tags: A list of tags containing archive information. + + Returns: + dict: A dictionary with archive information. + """ + packages = [] + for tag in tags: + packages.append({ + "name": tag.name, + "url": tag.url, + "strip_prefix": tag.strip_prefix, + "sha256": tag.sha256, + "build_file": tag.build_file, + }) + return packages + +def _get_toolchains(tags): + """Gets toolchain information from given tags. + + Args: + tags: A list of tags containing toolchain information. + + Returns: + dict: A dictionary with toolchain information. + """ + toolchains = [] + for tag in tags: + toolchain = { + "name": tag.name, + "tc_cpu": tag.target_cpu, + "tc_os": tag.target_os, + "tc_family": "gcc", # TODO: it can be llvm, check if we need to pull it in common + "tc_version": tag.version, + "sdp_to_link": tag.sdp_to_link, + "use_default_package": tag.use_default_package, + "use_system_toolchain": tag.use_system_toolchain, + "tc_extra_compile_flags": tag.extra_compile_flags, + "tc_extra_link_flags": tag.extra_link_flags, + "sdp_version": tag.sdp_version, + "tc_license_info_variable": tag.license_info_variable, + "tc_license_info_url": tag.license_info_url, + "tc_license_path": tag.license_path, + "tc_runtime_ecosystem": tag.runtime_ecosystem, + "cc_toolchain_config": "@score_bazel_cpp_toolchains//templates/{}:cc_toolchain_config.bzl.template".format(tag.target_os), + "cc_toolchain_flags": "@score_bazel_cpp_toolchains//templates/{}:cc_toolchain_flags.bzl.template".format(tag.target_os), + } + toolchains.append(toolchain) + return toolchains + +def _create_and_link_sdp(toolchain_info): + """ TODO: Write docstring + """ + pkg_name = "{}_pkg".format(toolchain_info["name"]) + identifier = "gcc" + version = toolchain_info["tc_version"] + if toolchain_info["tc_os"] == "qnx": + identifier = "sdp" + version = toolchain_info["sdp_version"] + matrix_key = "{cpu}-{os}-{identifier}-{version}".format( + cpu = toolchain_info["tc_cpu"], + os = toolchain_info["tc_os"], + identifier = identifier, + version = version, + ) + matrix = VERSION_MATRIX[matrix_key] + toolchain_info["sdp_to_link"] = pkg_name + return { + "name": pkg_name, + "url": matrix["url"], + "strip_prefix": matrix["strip_prefix"], + "sha256": matrix["sha256"], + "build_file": matrix["build_file"], + } + +def _get_info(mctx): + """Gets raw info from module ctx about toolchain properties. + + Args: + mctx: A bazel object holding module information. + family: An identifier that holds the driver type (gcc, qcc or llvm). + + Returns: + list: A list of dictionaries with toolchain and archive information. + """ + root = None + for mod in mctx.modules: + if not mod.is_root: + fail("Only the root module can use the 'gcc' extension!") + root = mod + + toolchains = _get_toolchains(mod.tags.toolchain) + packages = _get_packages(mod.tags.sdp) + + for tc in toolchains: + # need to be sure not to link package in case of system toolchain. + if tc["use_system_toolchain"]: + continue + + if tc["use_default_package"]: + packages.append(_create_and_link_sdp(tc)) + + return toolchains, packages + +def _impl(mctx): + """Extracts information about toolchain and instantiates nessesary rules for toolchain declaration. + + Args: + mctx: A bazel object holding module information. + + """ + toolchains, archives = _get_info(mctx) + for archive_info in archives: + http_archive( + name = archive_info["name"], + urls = [archive_info["url"]], + build_file = archive_info["build_file"], + sha256 = archive_info["sha256"], + strip_prefix = archive_info["strip_prefix"], + ) + + for toolchain_info in toolchains: + gcc_toolchain( + name = toolchain_info["name"], + extra_compile_flags = toolchain_info["tc_extra_compile_flags"], + extra_link_flags = toolchain_info["tc_extra_link_flags"], + license_info_variable = toolchain_info["tc_license_info_variable"], + license_info_value = toolchain_info["tc_license_info_url"], + license_path = toolchain_info["tc_license_path"], + sdp_version = toolchain_info["sdp_version"], + tc_cpu = toolchain_info["tc_cpu"], + tc_os = toolchain_info["tc_os"], + tc_pkg_repo = toolchain_info["sdp_to_link"], + tc_system_toolchain = toolchain_info["use_system_toolchain"], + tc_runtime_ecosystem = toolchain_info["tc_runtime_ecosystem"], + gcc_version = toolchain_info["tc_version"], + cc_toolchain_config = toolchain_info["cc_toolchain_config"], + cc_toolchain_flags = toolchain_info["cc_toolchain_flags"], + ) + +gcc = module_extension( + implementation = _impl, + tag_classes = { + "toolchain": tag_class( + attrs = _attrs_tc, + doc = "Toolchain configuration parameters that define toolchain." + ), + "sdp": tag_class( + attrs = _attrs_sdp, + doc = "Software Development Package (short sdp) is tarball holding binaries of toolchain.", + ), + } +) \ No newline at end of file diff --git a/bazel/extentions/BUILD b/packages/linux/aarch64/gcc/12.2.0/BUILD similarity index 91% rename from bazel/extentions/BUILD rename to packages/linux/aarch64/gcc/12.2.0/BUILD index b3007e6..fecdade 100644 --- a/bazel/extentions/BUILD +++ b/packages/linux/aarch64/gcc/12.2.0/BUILD @@ -9,4 +9,8 @@ # https://www.apache.org/licenses/LICENSE-2.0 # # SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* \ No newline at end of file +# ******************************************************************************* + +exports_files([ + "gcc.BUILD", +]) \ No newline at end of file diff --git a/packages/linux/aarch64/gcc/12.2.0/gcc.BUILD b/packages/linux/aarch64/gcc/12.2.0/gcc.BUILD new file mode 100644 index 0000000..232cd52 --- /dev/null +++ b/packages/linux/aarch64/gcc/12.2.0/gcc.BUILD @@ -0,0 +1,56 @@ +# ******************************************************************************* +# 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 +# ******************************************************************************* + +"""Build file for GCC external package""" + +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "all_files", + srcs = glob(["*/**/*"]), +) + +filegroup( + name = "bin", + srcs = ["bin"], +) + +filegroup( + name = "ar", + srcs = ["bin/aarch64-unknown-linux-gnu-ar"], +) + +filegroup( + name = "cc", + srcs = ["bin/aarch64-unknown-linux-gnu-gcc"], +) + +filegroup( + name = "gcov", + srcs = ["bin/aarch64-unknown-linux-gnu-gcov"], +) + +filegroup( + name = "cxx", + srcs = ["bin/aarch64-unknown-linux-gnu-g++"], +) + +filegroup( + name = "strip", + srcs = ["bin/aarch64-unknown-linux-gnu-strip"], +) + +filegroup( + name = "sysroot_dir", + srcs = ["aarch64-unknown-linux-gnu/sysroot"], +) \ No newline at end of file diff --git a/packages/linux/arm64/gcc/12.2.0/.keep b/packages/linux/arm64/gcc/12.2.0/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/packages/linux/x86_64/gcc/12.2.0/gcc.BUILD b/packages/linux/x86_64/gcc/12.2.0/gcc.BUILD index 4c2a915..9eba51a 100644 --- a/packages/linux/x86_64/gcc/12.2.0/gcc.BUILD +++ b/packages/linux/x86_64/gcc/12.2.0/gcc.BUILD @@ -31,7 +31,7 @@ filegroup( ) filegroup( - name = "gcc", + name = "cc", srcs = ["bin/x86_64-unknown-linux-gnu-gcc"], ) @@ -41,7 +41,7 @@ filegroup( ) filegroup( - name = "gpp", + name = "cxx", srcs = ["bin/x86_64-unknown-linux-gnu-g++"], ) diff --git a/configurations/gcc/BUILD b/packages/qnx/aarch64/sdp/7.1.0/BUILD similarity index 100% rename from configurations/gcc/BUILD rename to packages/qnx/aarch64/sdp/7.1.0/BUILD diff --git a/packages/qnx/arm64/sdp/7.1.0/sdp.BUILD b/packages/qnx/aarch64/sdp/7.1.0/sdp.BUILD similarity index 100% rename from packages/qnx/arm64/sdp/7.1.0/sdp.BUILD rename to packages/qnx/aarch64/sdp/7.1.0/sdp.BUILD diff --git a/configurations/qcc/BUILD b/packages/qnx/aarch64/sdp/8.0.0/BUILD similarity index 100% rename from configurations/qcc/BUILD rename to packages/qnx/aarch64/sdp/8.0.0/BUILD diff --git a/packages/qnx/arm64/sdp/8.0.0/sdp.BUILD b/packages/qnx/aarch64/sdp/8.0.0/sdp.BUILD similarity index 97% rename from packages/qnx/arm64/sdp/8.0.0/sdp.BUILD rename to packages/qnx/aarch64/sdp/8.0.0/sdp.BUILD index 0a6357d..04ce428 100644 --- a/packages/qnx/arm64/sdp/8.0.0/sdp.BUILD +++ b/packages/qnx/aarch64/sdp/8.0.0/sdp.BUILD @@ -34,12 +34,12 @@ filegroup( ) filegroup( - name = "qcc", + name = "cc", srcs = ["host/linux/x86_64/usr/bin/qcc"], ) filegroup( - name = "qpp", + name = "cxx", srcs = ["host/linux/x86_64/usr/bin/q++"], ) diff --git a/packages/qnx/x86_64/sdp/8.0.0/sdp.BUILD b/packages/qnx/x86_64/sdp/8.0.0/sdp.BUILD index cfc0dc8..b170d12 100644 --- a/packages/qnx/x86_64/sdp/8.0.0/sdp.BUILD +++ b/packages/qnx/x86_64/sdp/8.0.0/sdp.BUILD @@ -34,12 +34,12 @@ filegroup( ) filegroup( - name = "qcc", + name = "cc", srcs = ["host/linux/x86_64/usr/bin/qcc"], ) filegroup( - name = "qpp", + name = "cxx", srcs = ["host/linux/x86_64/usr/bin/q++"], ) diff --git a/packages/version_matrix.bzl b/packages/version_matrix.bzl index 6a7a024..ea62447 100644 --- a/packages/version_matrix.bzl +++ b/packages/version_matrix.bzl @@ -15,15 +15,15 @@ """ VERSION_MATRIX = { - "arm64-linux-gcc-12.2.0": { - "url": "", - "build_file": "", - "strip_prefix": "", - "sha256": "", + "aarch64-linux-gcc-12.2.0": { + "url": "https://github.com/eclipse-score/toolchains_gcc_packages/releases/download/v0.0.3/aarch64-unknown-linux-gnu_gcc12.tar.gz", + "build_file": "@score_bazel_cpp_toolchains//packages/linux/aarch64/gcc/12.2.0:gcc.BUILD", + "strip_prefix": "aarch64-unknown-linux-gnu", + "sha256": "57153340625581b199408391b895c84651382d3edd4c60fadbf0399f9dad21e1", }, - "arm64-qnx-sdp-8.0.0": { + "aarch64-qnx-sdp-8.0.0": { "url": "https://www.qnx.com/download/download/79858/installation.tgz", - "build_file": "@score_bazel_cpp_toolchains//packages/qnx/arm64/sdp/8.0.0:sdp.BUILD", + "build_file": "@score_bazel_cpp_toolchains//packages/qnx/aarch64/sdp/8.0.0:sdp.BUILD", "strip_prefix": "installation", "sha256": "f2e0cb21c6baddbcb65f6a70610ce498e7685de8ea2e0f1648f01b327f6bac63", }, diff --git a/packages/qnx/arm64/sdp/7.1.0/BUILD b/rules/BUILD similarity index 100% rename from packages/qnx/arm64/sdp/7.1.0/BUILD rename to rules/BUILD diff --git a/bazel/rules/common.bzl b/rules/common.bzl similarity index 100% rename from bazel/rules/common.bzl rename to rules/common.bzl diff --git a/rules/gcc.bzl b/rules/gcc.bzl new file mode 100644 index 0000000..dc209d1 --- /dev/null +++ b/rules/gcc.bzl @@ -0,0 +1,199 @@ +# ******************************************************************************* +# 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 +# ******************************************************************************* + +""" Module rule for defining GCC toolchains in Bazel. +""" + +load("@score_bazel_cpp_toolchains//rules:common.bzl", "get_flag_groups") + +def dict_union(x, y): + """ TODO: Write docstrings + """ + z = {} + z.update(x) + z.update(y) + return z + +def _get_cc_config_linux(rctx): + """ TODO: Write docstring + """ + return """ +filegroup( + name = "all_files", + srcs = [ + "@{tc_pkg_repo}//:all_files", + "gcov_wrapper", + ] +) + +cc_toolchain_config( + name = "cc_toolchain_config", + ar_binary = "@{tc_pkg_repo}//:ar", + cc_binary = "@{tc_pkg_repo}//:cc", + cxx_binary = "@{tc_pkg_repo}//:cxx", + gcov_binary = "@{tc_pkg_repo}//:gcov", + strip_binary = "@{tc_pkg_repo}//:strip", + sysroot = "@{tc_pkg_repo}//:sysroot_dir", + target_cpu = "{tc_cpu}", + target_os = "{tc_os}", + visibility = ["//visibility:public"], +) +""".format( + tc_pkg_repo = rctx.attr.tc_pkg_repo, + tc_cpu = rctx.attr.tc_cpu, + tc_os = rctx.attr.tc_os, + ) + +def _get_cc_config_qnx(rctx): + """ TODO: Write docstring + """ + return """ +filegroup( + name = "all_files", + srcs = [ + "@{tc_pkg_repo}//:all_files", + ] +) + +cc_toolchain_config( + name = "cc_toolchain_config", + ar_binary = "@{tc_pkg_repo}//:ar", + cc_binary = "@{tc_pkg_repo}//:cc", + cxx_binary = "@{tc_pkg_repo}//:cxx", + strip_binary = "@{tc_pkg_repo}//:strip", + host_dir = "@{tc_pkg_repo}//:host_dir", + target_dir = "@{tc_pkg_repo}//:target_dir", + cxx_builtin_include_directories = "@{tc_pkg_repo}//:cxx_builtin_include_directories", + target_cpu = "{tc_cpu}", + target_os = "{tc_os}", + visibility = ["//visibility:public"], +) +""".format( + tc_pkg_repo = rctx.attr.tc_pkg_repo, + tc_cpu = rctx.attr.tc_cpu, + tc_os = rctx.attr.tc_os, + ) + +def _impl(rctx): + """ Implementation of the gcc_toolchain repository rule. + + Args: + rctx: The repository context. + """ + tc_identifier = "gcc_{}".format(rctx.attr.gcc_version) + if rctx.attr.tc_os == "qnx": + tc_identifier = "sdp_{}".format(rctx.attr.sdp_version) + + if rctx.attr.tc_os == "qnx": + cc_toolchain_config = _get_cc_config_qnx(rctx) + elif rctx.attr.tc_os == "linux": + cc_toolchain_config = _get_cc_config_linux(rctx) + else: + fail("Unsupported OS detected!") + + rctx.template( + "BUILD", + rctx.attr._cc_toolchain_build, + { + "%{cc_toolchain_config}": cc_toolchain_config, + "%{tc_pkg_repo}": rctx.attr.tc_pkg_repo, + "%{tc_cpu}": rctx.attr.tc_cpu, + "%{tc_os}": rctx.attr.tc_os, + "%{tc_version}": rctx.attr.gcc_version, + "%{tc_identifier}": tc_identifier, + "%{tc_runtime_es}": rctx.attr.tc_runtime_ecosystem, + }, + ) + + extra_compile_flags = get_flag_groups(rctx.attr.extra_compile_flags) + extra_link_flags = get_flag_groups(rctx.attr.extra_link_flags) + + + template_dict = { + "%{tc_version}": rctx.attr.gcc_version, + "%{tc_identifier}": "gcc", + "%{tc_cpu}": "aarch64le" if rctx.attr.tc_cpu == "aarch64" else rctx.attr.tc_cpu, + "%{tc_runtime_es}": rctx.attr.tc_runtime_ecosystem, + "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", + "%{extra_compile_flags}":extra_compile_flags, + "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", + "%{extra_link_flags}": extra_link_flags, + } + + if rctx.attr.tc_os == "qnx": + extra_template_dict = { + "%{tc_cpu_cxx}": "aarch64le" if rctx.attr.tc_cpu == "aarch64" else rctx.attr.tc_cpu, + "%{sdp_version}": rctx.attr.sdp_version, + "%{license_path}": rctx.attr.license_path, + "%{use_license_info}": "False" if rctx.attr.license_info_value == "" else "True", + "%{license_info_variable}": rctx.attr.license_info_variable, + "%{license_info_value}": rctx.attr.license_info_value, + } + template_dict = dict_union(template_dict, extra_template_dict) + + rctx.template( + "cc_toolchain_config.bzl", + rctx.attr.cc_toolchain_config, + template_dict, + ) + + rctx.template( + "flags.bzl", + rctx.attr.cc_toolchain_flags, + {}, + ) + + if rctx.attr.tc_os == "linux": + # There is an issue with gcov and cc_toolchain config. + # See: https://github.com/bazelbuild/rules_cc/issues/351 + rctx.template( + "gcov_wrapper", + rctx.attr._cc_gcov_wrapper_script, + { + "%{tc_gcov_path}": "external/score_bazel_cpp_toolchains++gcc+{repo}/bin/{cpu}-unknown-linux-gnu-gcov".format( + repo = rctx.attr.tc_pkg_repo, + cpu = "aarch64le" if rctx.attr.tc_cpu == "aarch64" else rctx.attr.tc_cpu, + ), + }, + ) + +gcc_toolchain = repository_rule( + implementation = _impl, + attrs = { + "tc_pkg_repo": attr.string(doc="The label name of toolchain tarbal."), + "tc_cpu": attr.string(doc="Target platform CPU."), + "tc_os": attr.string(doc="Target platform OS."), + "gcc_version": attr.string(doc="GCC version number"), + "extra_compile_flags": attr.string_list(doc="Extra/Additional compile flags."), + "extra_link_flags": attr.string_list(doc="Extra/Additional link flags."), + "sdp_version": attr.string(doc="SDP version number"), + "license_path": attr.string(doc="Lincese path"), + "license_info_variable": attr.string(doc="License info variable name (custom settings)"), + "license_info_value": attr.string(doc="License info value (custom settings)"), + "tc_runtime_ecosystem": attr.string(doc="Runtime ecosystem."), + "tc_system_toolchain": attr.bool(doc="Boolean flag to state if this is a system toolchain"), + "cc_toolchain_config": attr.label( + doc = "Path to the cc_config.bzl template file.", + ), + "cc_toolchain_flags": attr.label( + doc = "Path to the Bazel BUILD file template for the toolchain.", + ), + "_cc_toolchain_build": attr.label( + default = "@score_bazel_cpp_toolchains//templates:BUILD.template", + doc = "Path to the Bazel BUILD file template for the toolchain.", + ), + "_cc_gcov_wrapper_script": attr.label( + default = "@score_bazel_cpp_toolchains//templates/linux:cc_gcov_wrapper.template", + ) + }, +) \ No newline at end of file diff --git a/packages/qnx/arm64/sdp/8.0.0/BUILD b/templates/BUILD similarity index 100% rename from packages/qnx/arm64/sdp/8.0.0/BUILD rename to templates/BUILD diff --git a/templates/common/BUILD.template b/templates/BUILD.template similarity index 80% rename from templates/common/BUILD.template rename to templates/BUILD.template index 8b48e93..fbfad1b 100644 --- a/templates/common/BUILD.template +++ b/templates/BUILD.template @@ -15,27 +15,30 @@ """ load("@rules_cc//cc:defs.bzl", "cc_toolchain") +load(":cc_toolchain_config.bzl", "cc_toolchain_config") filegroup( name = "empty", ) +%{cc_toolchain_config} + cc_toolchain( name = "cc_toolchain", all_files = "@%{tc_pkg_repo}//:all_files", ar_files = "@%{tc_pkg_repo}//:all_files", as_files = "@%{tc_pkg_repo}//:all_files", compiler_files = "@%{tc_pkg_repo}//:all_files", - coverage_files = "@%{tc_pkg_repo}//:all_files", + coverage_files = ":all_files", dwp_files = ":empty", linker_files = "@%{tc_pkg_repo}//:all_files", objcopy_files = ":empty", strip_files = "@%{tc_pkg_repo}//:all_files", - toolchain_config = "//%{tc_family}:cc_toolchain_config", + toolchain_config = ":cc_toolchain_config", ) toolchain( - name = "%{tc_cpu}-%{tc_os}-%{tc_family}-%{tc_version}", + name = "%{tc_cpu}-%{tc_os}-%{tc_identifier}-%{tc_runtime_es}", exec_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:linux", @@ -43,6 +46,8 @@ toolchain( target_compatible_with = [ "@platforms//cpu:%{tc_cpu}", "@platforms//os:%{tc_os}", + "@score_bazel_platforms//runtime_es:%{tc_runtime_es}", + "@score_bazel_platforms//version:%{tc_identifier}", ], toolchain = ":cc_toolchain", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", diff --git a/templates/gcc/BUILD.template b/templates/gcc/BUILD.template deleted file mode 100644 index 6c812b0..0000000 --- a/templates/gcc/BUILD.template +++ /dev/null @@ -1,33 +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 -# ******************************************************************************* - -""" GCC Toolchain definition file for CC configuration. -""" - -load(":cc_toolchain_config.bzl", "cc_toolchain_config") - -filegroup( - name = "empty", -) - -cc_toolchain_config( - name = "cc_toolchain_config", - ar_binary = "@%{tc_pkg_repo}//:ar", - cc_binary = "@%{tc_pkg_repo}//:gcc", - cxx_binary = "@%{tc_pkg_repo}//:gpp", - gcov_binary = "@%{tc_pkg_repo}//:gcov", - strip_binary = "@%{tc_pkg_repo}//:strip", - sysroot = "@%{tc_pkg_repo}//:sysroot_dir", - target_cpu = "%{tc_cpu}", - visibility = ["//visibility:public"], -) \ No newline at end of file diff --git a/templates/common/BUILD b/templates/linux/BUILD similarity index 100% rename from templates/common/BUILD rename to templates/linux/BUILD diff --git a/bazel/rules/BUILD b/templates/linux/cc_gcov_wrapper.template similarity index 91% rename from bazel/rules/BUILD rename to templates/linux/cc_gcov_wrapper.template index b3007e6..6bf28b0 100644 --- a/bazel/rules/BUILD +++ b/templates/linux/cc_gcov_wrapper.template @@ -9,4 +9,8 @@ # https://www.apache.org/licenses/LICENSE-2.0 # # SPDX-License-Identifier: Apache-2.0 -# ******************************************************************************* \ No newline at end of file +# ******************************************************************************* + +set -x + +exec "%{tc_gcov_path}" "$@" \ No newline at end of file diff --git a/templates/gcc/cc_toolchain_config.bzl.template b/templates/linux/cc_toolchain_config.bzl.template similarity index 88% rename from templates/gcc/cc_toolchain_config.bzl.template rename to templates/linux/cc_toolchain_config.bzl.template index 2ec4ae9..cd3600a 100644 --- a/templates/gcc/cc_toolchain_config.bzl.template +++ b/templates/linux/cc_toolchain_config.bzl.template @@ -17,13 +17,14 @@ load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "action_config", "feature", + "feature_set", "flag_group", "flag_set", "tool", "tool_path", "with_feature_set", ) -load("@score_bazel_cpp_toolchains//configurations/gcc:flags.bzl", +load(":flags.bzl", "UNFILTERED_COMPILE_FLAGS", "DEFAULT_COMPILE_FLAGS", "DEFAULT_x86_64_COMPILE_FLAGS", @@ -347,6 +348,31 @@ def _impl(ctx): supports_pic_feature = feature(name = "supports_pic", enabled = True) + coverage_feature = feature(name = "coverage") + gcc_coverage_map_format_feature = feature( + name = "gcc_coverage_map_format", + provides = ["profile"], + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.preprocess_assemble, + ], + flag_groups = [flag_group( + expand_if_available = "gcov_gcno_file", + flags = ["-fprofile-arcs", "-ftest-coverage"], + )], + ), + flag_set( + actions = all_link_actions, + flag_groups = [flag_group(flags = ["--coverage"])], + ), + ], + requires = [feature_set(features = ["coverage"])], + ) + # The order of the features is relevant, they are applied in this specific order. # A command line parameter from a feature at the end of the list will appear # after a command line parameter from a feature at the beginning of the list. @@ -369,25 +395,33 @@ def _impl(ctx): supports_dynamic_linker_feature, supports_pic_feature, supports_fission_feature, + coverage_feature, + gcc_coverage_map_format_feature, ] cxx_builtin_include_directories = [] - tool_paths = [] + + # TODO: Once https://github.com/bazelbuild/rules_cc/issues/351 is fixed remove this. + tool_paths = [tool_path(name = "gcov", path = "gcov_wrapper")] + + sysroot = None + if ctx.attr.sysroot != None: + sysroot = ctx.attr.sysroot[DefaultInfo].files.to_list()[0].path return cc_common.create_cc_toolchain_config_info( ctx = ctx, - abi_version = "gcc-%{tc_version}", + abi_version = "%{tc_abi_version}", abi_libc_version = "unknown", - builtin_sysroot = ctx.file.sysroot.path, + builtin_sysroot = sysroot, compiler = "gcc", cxx_builtin_include_directories = cxx_builtin_include_directories, features = features, action_configs = action_configs, host_system_name = "local", - target_system_name = "%s-linux" % (ctx.attr.target_cpu), + target_system_name = "%{tc_cpu}-%{tc_os}", target_cpu = ctx.attr.target_cpu, target_libc = "unknown", - toolchain_identifier = "gcc-%{tc_version}", + toolchain_identifier = "%{tc_identifier}", tool_paths = tool_paths, ) @@ -400,7 +434,11 @@ cc_toolchain_config = rule( "cxx_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), "gcov_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), "target_cpu": attr.string(mandatory = True), + "target_os": attr.string(mandatory = True), "strip_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "sysroot": attr.label(allow_single_file = True, mandatory = True), + "sysroot": attr.label(default = None), + "host_dir": attr.label(default = None), + "target_dir": attr.label(default = None), + "cxx_builtin_include_directories": attr.label(default = None), }, ) \ No newline at end of file diff --git a/configurations/gcc/flags.bzl b/templates/linux/cc_toolchain_flags.bzl.template similarity index 88% rename from configurations/gcc/flags.bzl rename to templates/linux/cc_toolchain_flags.bzl.template index 9ec147e..942d445 100644 --- a/configurations/gcc/flags.bzl +++ b/templates/linux/cc_toolchain_flags.bzl.template @@ -14,7 +14,24 @@ """ Common compile and link flags for GCC toolchains. """ -load("@score_bazel_cpp_toolchains//bazel/rules:common.bzl", "get_flag_group") +load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "flag_group") + +def get_flag_group(flags): + """Helper function to create a flag_group. + + Args: + flags (list[str]): A list of compiler or linker flags. + + Returns: + flag_group: A Bazel flag_group object containing the provided flags. + """ + if len(flags): + return [ + flag_group( + flags = flags, + ) + ] + return [] # Flags that should always be applied, without filtering. UNFILTERED_COMPILE_FLAGS = get_flag_group([ @@ -155,12 +172,14 @@ ALL_WALL_CXX_WARNINGS = get_flag_group([ "-Wcatch-value", "-Wclass-memaccess", "-Wdelete-non-virtual-dtor", - "-Wenum-int-mismatch", + # "-Wenum-int-mismatch", not supported in GCC12.2 + # "-Wargument-mismatch", cc1plus: warning: command-line option '-Wduplicate-decl-specifier' is valid for C/ObjC but not for C++ "-Wmismatched-new-delete", - "-Woverloaded-virtual=1", + "-Woverloaded-virtual", + # -Woverloaded-virtual=1, not supported in GCC12.2 "-Wpessimizing-move", "-Wrange-loop-construct", - "-Wself-move", + # "-Wself-move", not supported in GCC12.2 ]) ALL_WALL_WARNINGS = get_flag_group([ diff --git a/templates/qcc/BUILD b/templates/qcc/BUILD deleted file mode 100644 index e69de29..0000000 diff --git a/templates/qcc/BUILD.template b/templates/qcc/BUILD.template deleted file mode 100644 index d111380..0000000 --- a/templates/qcc/BUILD.template +++ /dev/null @@ -1,33 +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 -# ******************************************************************************* - -""" GCC toolchain configuration declaration. -""" - -load(":cc_toolchain_config.bzl", "cc_toolchain_config") - -filegroup( - name = "empty", -) - -cc_toolchain_config( - name = "cc_toolchain_config", - ar_binary = "@%{tc_pkg_repo}//:ar", - cc_binary = "@%{tc_pkg_repo}//:qcc", - cxx_binary = "@%{tc_pkg_repo}//:qpp", - strip_binary = "@%{tc_pkg_repo}//:strip", - qnx_host = "@%{tc_pkg_repo}//:host_dir", - qnx_target = "@%{tc_pkg_repo}//:target_dir", - cxx_builtin_include_directories = "@%{tc_pkg_repo}//:cxx_builtin_include_directories", - visibility = ["//visibility:public"], -) \ No newline at end of file diff --git a/templates/gcc/BUILD b/templates/qnx/BUILD similarity index 100% rename from templates/gcc/BUILD rename to templates/qnx/BUILD diff --git a/templates/qcc/cc_toolchain_config.bzl.template b/templates/qnx/cc_toolchain_config.bzl.template similarity index 97% rename from templates/qcc/cc_toolchain_config.bzl.template rename to templates/qnx/cc_toolchain_config.bzl.template index 3aebdfa..8f52503 100644 --- a/templates/qcc/cc_toolchain_config.bzl.template +++ b/templates/qnx/cc_toolchain_config.bzl.template @@ -25,7 +25,7 @@ load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool_path", "with_feature_set", ) -load("@score_bazel_cpp_toolchains//configurations/qcc:flags.bzl", +load(":flags.bzl", "UNFILTERED_COMPILE_FLAGS", "DEFAULT_COMPILE_FLAGS", "DEFAULT_C_COMPILE_FLAGS", @@ -209,7 +209,7 @@ def _impl(ctx): flag_sets = [ flag_set( actions = all_link_actions, - flag_groups = [flag_group(flags = ["-V%{tc_version},gcc_nto%{tc_cpu_cxx}"])], + flag_groups = [flag_group(flags = ["-V%{tc_version},gcc_nto%{tc_cpu}_cxx"])], ), flag_set( actions = all_link_actions, @@ -335,10 +335,10 @@ def _impl(ctx): env_set( actions = all_compile_actions + all_link_actions, env_entries = [ - env_entry(key = "QNX_HOST", value = "/proc/self/cwd/" + ctx.file.qnx_host.path), - env_entry(key = "QNX_TARGET", value = "/proc/self/cwd/" + ctx.file.qnx_target.path), + env_entry(key = "QNX_HOST", value = "/proc/self/cwd/" + ctx.file.host_dir.path), + env_entry(key = "QNX_TARGET", value = "/proc/self/cwd/" + ctx.file.target_dir.path), env_entry(key = "QNX_CONFIGURATION_EXCLUSIVE", value = "/var/tmp/.qnx"), - env_entry(key = "QNX_SHARED_LICENSE_FILE", value = "%{qnx_license_path}"), + env_entry(key = "QNX_SHARED_LICENSE_FILE", value = "%{license_path}"), ], ), env_set( @@ -450,8 +450,10 @@ cc_toolchain_config = rule( "cc_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), "cxx_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), "strip_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "qnx_host": attr.label(allow_single_file = True, mandatory = True), - "qnx_target": attr.label(allow_single_file = True, mandatory = True), + "host_dir": attr.label(allow_single_file = True, mandatory = True), + "target_dir": attr.label(allow_single_file = True, mandatory = True), + "target_cpu": attr.string(mandatory = True), + "target_os": attr.string(mandatory = True), "cxx_builtin_include_directories": attr.label(allow_files = True, mandatory = True), }, ) \ No newline at end of file diff --git a/configurations/qcc/flags.bzl b/templates/qnx/cc_toolchain_flags.bzl.template similarity index 88% rename from configurations/qcc/flags.bzl rename to templates/qnx/cc_toolchain_flags.bzl.template index 24d4871..ba18bc6 100644 --- a/configurations/qcc/flags.bzl +++ b/templates/qnx/cc_toolchain_flags.bzl.template @@ -14,7 +14,24 @@ """ Common compile and link flags for GCC toolchains. """ -load("@score_bazel_cpp_toolchains//bazel/rules:common.bzl", "get_flag_group") +load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "flag_group") + +def get_flag_group(flags): + """Helper function to create a flag_group. + + Args: + flags (list[str]): A list of compiler or linker flags. + + Returns: + flag_group: A Bazel flag_group object containing the provided flags. + """ + if len(flags): + return [ + flag_group( + flags = flags, + ) + ] + return [] # Flags that should always be applied, without filtering. UNFILTERED_COMPILE_FLAGS = get_flag_group([ @@ -100,4 +117,3 @@ ALL_WALL_WARNINGS = get_flag_group([]) WARNINGS_AS_ERRORS = get_flag_group([ "-Werror", ]) -