Skip to content

Commit

Permalink
pw_toolchain: Switch arm_clang to rules_cc toolchains
Browse files Browse the repository at this point in the history
Switches the arm_clang toolchain to use rules_cc toolchains.

Bug: b/346388161
Change-Id: I305232344993a1e1ffcd16f5537bbe1313655ee0
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/237196
Lint: Lint 🤖 <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
Docs-Not-Needed: Armando Montanez <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Pigweed-Auto-Submit: Armando Montanez <[email protected]>
  • Loading branch information
armandomontanez authored and CQ Bot Account committed Oct 9, 2024
1 parent 7d78c54 commit 60e7e4f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 143 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ cipd_repository(

cipd_repository(
name = "llvm_toolchain_device",
build_file = "@pw_toolchain//build_external:llvm_clang_legacy.BUILD",
build_file = "//pw_toolchain/build_external:llvm_clang.BUILD",
path = "fuchsia/third_party/clang/${os}-${arch}",
tag = "git_revision:0dcada94bb1ae79f0edd91013038098c62a96b3b",
)
Expand Down
217 changes: 76 additions & 141 deletions pw_toolchain/arm_clang/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,233 +12,163 @@
# License for the specific language governing permissions and limitations under
# the License.

load(
"@pw_toolchain//cc_toolchain:defs.bzl",
"pw_cc_action_files",
"pw_cc_feature",
"pw_cc_flag_set",
"pw_cc_toolchain",
)
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

filegroup(name = "empty")

pw_cc_flag_set(
cc_args(
name = "cortex-m0",
actions = [
"@pw_toolchain//actions:all_asm_actions",
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@pw_toolchain//actions:all_link_actions",
"@rules_cc//cc/toolchains/actions:assembly_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
flags = [
args = [
"-mcpu=cortex-m0",
"-mfloat-abi=soft",
],
)

pw_cc_flag_set(
cc_args(
name = "cortex-m0plus",
actions = [
"@pw_toolchain//actions:all_asm_actions",
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@pw_toolchain//actions:all_link_actions",
"@rules_cc//cc/toolchains/actions:assembly_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
flags = [
args = [
"-mcpu=cortex-m0plus",
"-mfloat-abi=soft",
],
)

pw_cc_flag_set(
cc_args(
name = "armv6m-none-eabi",
actions = [
"@pw_toolchain//actions:all_asm_actions",
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@pw_toolchain//actions:all_link_actions",
"@rules_cc//cc/toolchains/actions:assembly_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
flags = [
args = [
"--target=armv6m-none-eabi",
],
)

pw_cc_flag_set(
cc_args(
name = "armv8m.main-none-eabi",
actions = [
"@pw_toolchain//actions:all_asm_actions",
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@pw_toolchain//actions:all_link_actions",
"@rules_cc//cc/toolchains/actions:assembly_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
flags = [
args = [
"--target=armv8m.main-none-eabi",
],
)

pw_cc_flag_set(
cc_args(
name = "cxx_compiler_flags",
actions = [
"@pw_toolchain//actions:all_compiler_actions",
"@rules_cc//cc/toolchains/actions:compile_actions",
],
flags = [
args = [
"-fno-register-global-dtors-with-atexit",
"-fno-c++-static-destructors",
"-fno-use-cxa-atexit",
"-fno-threadsafe-statics",
"-D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES",
],
)

pw_cc_flag_set(
name = "llvm-libc_link_flags",
actions = ["@pw_toolchain//actions:all_link_actions"],
flags = [
"-nostdlib++",
"-nostartfiles",
"-Wl,-lc++",
],
)

pw_cc_action_files(
name = "cxx-lib-config-files",
srcs = [
data = [
"//pw_libcxx:cxx_lib_configs",
],
actions = [
"@pw_toolchain//actions:all_compiler_actions",
],
)

pw_cc_action_files(
name = "llvm-libc_compile_files",
srcs = [
"@llvm_toolchain_device//:llvm-libc_arm-none-eabi_compile_files",
],
actions = [
"@pw_toolchain//actions:all_compiler_actions",
],
)

pw_cc_action_files(
name = "llvm-libc_link_files",
srcs = [
"@llvm_toolchain_device//:llvm-libc_arm-none-eabi_link_files",
],
actions = [
"@pw_toolchain//actions:all_link_actions",
],
)

pw_cc_flag_set(
cc_args(
name = "cortex_common",
actions = [
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
)

pw_cc_flag_set(
cc_args(
name = "cortex_common_link",
actions = ["@pw_toolchain//actions:all_link_actions"],
flags = [
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
args = [
"-Wl,--gc-sections",
],
)

# Additional arm_clang specific warning flags
pw_cc_flag_set(
cc_args(
name = "warnings",
actions = [
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
flags = [
args = [
# This can't be in common, because proto headers in host builds trigger
"-Wundef",
# Silence the really verbose ARM warnings.
"-Wno-psabi",
],
)

pw_cc_flag_set(
cc_args(
name = "thumb_abi",
actions = [
"@pw_toolchain//actions:all_asm_actions",
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@pw_toolchain//actions:all_link_actions",
"@rules_cc//cc/toolchains/actions:assembly_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
flags = [
args = [
"-mabi=aapcs",
"-mthumb",
],
)

pw_cc_flag_set(
cc_args(
name = "cortex-m33",
actions = [
"@pw_toolchain//actions:all_asm_actions",
"@pw_toolchain//actions:all_c_compiler_actions",
"@pw_toolchain//actions:all_cpp_compiler_actions",
"@pw_toolchain//actions:all_link_actions",
"@rules_cc//cc/toolchains/actions:assembly_actions",
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
flags = [
args = [
"-mcpu=cortex-m33",
"-mfloat-abi=soft",
],
)

pw_cc_feature(
name = "compiler_is_clang",
enabled = True,
feature_name = "compiler_is_clang",
)

pw_cc_feature(
name = "linker_is_clang",
enabled = True,
feature_name = "linker_is_clang",
)

pw_cc_toolchain(
cc_toolchain(
name = "arm_clang_toolchain_cortex-m",
action_configs = [
"@llvm_toolchain_device//:ar",
"@llvm_toolchain_device//:clang",
"@llvm_toolchain_device//:clang++",
"@llvm_toolchain_device//:lld",
"@llvm_toolchain_device//:llvm-cov",
"@llvm_toolchain_device//:llvm-objcopy",
"@llvm_toolchain_device//:llvm-objdump",
"@llvm_toolchain_device//:llvm-strip",
],
compiler = "clang", # Useful for distinguishing gcc vs clang.
extra_action_files = [
":cxx-lib-config-files",
":llvm-libc_compile_files",
":llvm-libc_link_files",
],
flag_sets = [
args = [
":cxx_compiler_flags",
":llvm-libc_link_flags",
"@llvm_toolchain_device//:llvm-libc_args",
":thumb_abi",
":cortex_common",
":cortex_common_link",
":warnings",
"@pw_toolchain//flag_sets:oz",
"@pw_toolchain//flag_sets:debugging",
"@pw_toolchain//flag_sets:reduced_size",
"@pw_toolchain//flag_sets:no_canonical_prefixes",
"@pw_toolchain//flag_sets:no_rtti",
"@pw_toolchain//flag_sets:wno_register",
"@pw_toolchain//flag_sets:wnon_virtual_dtor",
"//pw_toolchain/cc:common_warnings",
"//pw_toolchain/cc:color_diagnostics",
"//pw_toolchain/cc/args:oz",
"//pw_toolchain/cc/args:debugging",
"//pw_toolchain/cc/args:reduced_size",
"//pw_toolchain/cc/args:no_canonical_prefixes",
"//pw_toolchain/cc/args:no_rtti",
"//pw_toolchain/cc/args:wno_register",
"//pw_toolchain/cc/args:wnon_virtual_dtor",
"//pw_toolchain/cc/args:common_warnings",
"//pw_toolchain/cc/args:color_diagnostics",
] + select({
"@pw_toolchain//constraints/arm_mcpu:cortex-m0": [
":cortex-m0",
Expand All @@ -254,20 +184,25 @@ pw_cc_toolchain(
],
"@pw_toolchain//constraints/arm_mcpu:none": [],
}),
enabled_features = [
"@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
"//pw_toolchain/cc/capability:compiler_is_clang",
"//pw_toolchain/cc/capability:linker_is_clang",
] + select({
"//pw_toolchain/cc:c++17_enabled": ["//pw_toolchain/cc/args:c++17_feature"],
"//conditions:default": [],
}) + select({
"//pw_toolchain/cc:c++20_enabled": ["//pw_toolchain/cc/args:c++20_feature"],
"//conditions:default": [],
}),
tags = ["manual"], # Don't try to build this in wildcard builds.
target_compatible_with = select({
"@pw_toolchain//constraints/arm_mcpu:cortex-m0": [],
"@pw_toolchain//constraints/arm_mcpu:cortex-m0plus": [],
"@pw_toolchain//constraints/arm_mcpu:cortex-m33": [],
"@pw_toolchain//constraints/arm_mcpu:none": ["@platforms//:incompatible"],
}),
toolchain_features = [
"//pw_toolchain/cc:c++17_feature",
"//pw_toolchain/cc:c++20_feature",
":compiler_is_clang",
":linker_is_clang",
],
toolchain_identifier = "arm-clang-toolchain",
tool_map = "@llvm_toolchain_device//:all_tools",
visibility = ["//pw_toolchain:__pkg__"],
)

Expand Down
36 changes: 36 additions & 0 deletions pw_toolchain/build_external/llvm_clang.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# the License.

load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:args_list.bzl", "cc_args_list")
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
Expand Down Expand Up @@ -293,3 +295,37 @@ filegroup(
]),
visibility = ["//visibility:public"],
)

cc_args(
name = "llvm-libc_link_args",
actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
args = selects.with_or({
ALL_CORTEX_M_CPUS: [
"-nostdlib++",
"-nostartfiles",
"-Wl,-lc++",
],
"//conditions:default": [],
}),
data = [":llvm-libc_arm-none-eabi_link_files"],
visibility = ["//visibility:private"],
)

cc_args(
name = "llvm-libc_compile_args",
actions = ["@rules_cc//cc/toolchains/actions:compile_actions"],
args = selects.with_or({
ALL_CORTEX_M_CPUS: [],
"//conditions:default": [],
}),
data = [":llvm-libc_arm-none-eabi_compile_files"],
visibility = ["//visibility:private"],
)

cc_args_list(
name = "llvm-libc_args",
args = [
":llvm-libc_compile_args",
":llvm-libc_link_args",
]
)
2 changes: 1 addition & 1 deletion pw_toolchain/register_toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def register_pigweed_cxx_toolchains(
# Fetch llvm toolchain for device.
cipd_repository(
name = "llvm_toolchain_device",
build_file = "@pw_toolchain//build_external:llvm_clang_legacy.BUILD",
build_file = "@pigweed//pw_toolchain/build_external:llvm_clang.BUILD",
path = "fuchsia/third_party/clang/${os}-${arch}",
tag = "git_revision:b5e4d323badbd24324bfab4366b670977b16df07" if not clang_tag else clang_tag,
)
Expand Down

0 comments on commit 60e7e4f

Please sign in to comment.