Skip to content

Commit

Permalink
pw_toolchain: Migrate host clang to rules_cc toolchains
Browse files Browse the repository at this point in the history
Migrates Pigweed to use the toolchain rules upstreamed to rules_cc.

Bug: b/346388161
Change-Id: I692153827f1f2fc871828bc8f594dcf82378f5f5
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/207550
Pigweed-Auto-Submit: Armando Montanez <amontanez@google.com>
Docs-Not-Needed: Armando Montanez <amontanez@google.com>
Commit-Queue: Armando Montanez <amontanez@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ted Pudlik <tpudlik@google.com>
  • Loading branch information
armandomontanez authored and CQ Bot Account committed Sep 20, 2024
1 parent 0ffb69d commit 130903f
Show file tree
Hide file tree
Showing 14 changed files with 1,317 additions and 3,104 deletions.
5 changes: 5 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import %workspace%/pw_build/pigweed.bazelrc
common --per_file_copt=external/.*@-w
common --host_per_file_copt=external/.*@-w

# TODO: https://pwbug.dev/324107025 - Temporary workaround until Sapphire
# migration is done or warnings are fixed.
common --per_file_copt=pw_bluetooth_sapphire/.*@-Wno-unused-parameter
common --host_per_file_copt=pw_bluetooth_sapphire/.*@-Wno-unused-parameter

# Picotool needs to build with exceptions and RTTI enabled.
common --per_file_copt=external.*picotool.*@-fexceptions,-frtti
common --host_per_file_copt=external.*picotool.*@-fexceptions,-frtti
Expand Down
10 changes: 7 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,12 @@ local_path_override(
path = "pw_toolchain_bazel",
)

xcode = use_repo_rule("@pw_toolchain//features/macos/private:xcode_command_line_tools.bzl", "pw_xcode_repository")
xcode_sdk_repository = use_repo_rule("//pw_toolchain:xcode.bzl", "xcode_sdk_repository")

xcode(name = "pw_xcode_command_line_tools")
xcode_sdk_repository(
name = "macos_sysroot",
build_file = "//pw_toolchain/host_clang:macos_sysroot.BUILD",
)

cipd_client_repository = use_repo_rule("//pw_env_setup/bazel/cipd_setup:cipd_rules.bzl", "_cipd_client_repository")

Expand All @@ -119,7 +122,7 @@ cipd_repository = use_repo_rule("//pw_env_setup/bazel/cipd_setup:cipd_rules.bzl"
# Fetch llvm toolchain.
cipd_repository(
name = "llvm_toolchain",
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:c58bc24fcf678c55b0bf522be89eff070507a005",
)
Expand All @@ -134,6 +137,7 @@ cipd_repository(
# Fetch linux sysroot for host builds.
cipd_repository(
name = "linux_sysroot",
build_file = "//pw_toolchain/host_clang:linux_sysroot.BUILD",
path = "fuchsia/third_party/sysroot/bionic",
tag = "git_revision:702eb9654703a7cec1cadf93a7e3aa269d053943",
)
Expand Down
3,330 changes: 396 additions & 2,934 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions pw_build/constraint/cortex-m/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

package(default_visibility = ["//visibility:public"])
26 changes: 26 additions & 0 deletions pw_build/constraint/cortex-m/lists.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
"""A programatic list of all known Cortex-M CPUs."""

ALL_CORTEX_M_CPUS = (
"@platforms//cpu:armv6-m",
"@platforms//cpu:armv7-m",
"@platforms//cpu:armv7e-m",
# TODO: https://github.com/bazelbuild/platforms/issues/100 - Kept for legacy
# reasons.
"@platforms//cpu:armv7e-mf",
# TODO: https://github.com/bazelbuild/platforms/issues/104 - Kept for legacy
# reasons.
"@platforms//cpu:armv8-m",
)
24 changes: 24 additions & 0 deletions pw_toolchain/action/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc/toolchains:actions.bzl", "cc_action_type")

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

# TODO: https://pwbug.dev/367352331 - Rename and upstream this to Bazel
# (rules_cc, bazeltools).
cc_action_type(
name = "objdump",
action_name = "objdump_embed_data",
)
252 changes: 252 additions & 0 deletions pw_toolchain/args/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")

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

# The common set of warnings for clang and arm_gcc.
cc_args(
name = "common_warnings",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = [
"-Wall",
"-Wextra",
"-Wimplicit-fallthrough",
"-Wcast-qual",
"-Wpointer-arith",
"-Wshadow",
"-Wredundant-decls",
# TODO: b/259746255 - Enable implicit conversion warnings once fixed.
# "-Wconversion",
# Make all warnings errors, except for the exemptions below.
"-Werror",
"-Wno-error=cpp", # preprocessor #warning statement
"-Wno-error=deprecated-declarations", # [[deprecated]] attribute
],
)

# This config contains warnings that are enabled for upstream Pigweed.
# This config MUST NOT be used downstream to allow for warnings to be
# added in the future without breaking downstream.
cc_args(
name = "internal_strict_warnings_flags",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = [
"-Wswitch-enum",
"-Wextra-semi",
] + select({
"@platforms//os:windows": [],
# TODO: b/243069432 - Enable pedantic warnings on Windows once passing.
"//conditions:default": [":pedantic_warnings"],
}),
visibility = ["//:__subpackages__"],
)

# Add `--features=internal_strict_warnings` to your Bazel invocation to enable.
cc_feature(
name = "internal_strict_warnings",
args = [":internal_strict_warnings_flags"],
feature_name = "internal_strict_warnings",
visibility = ["//:__subpackages__"],
)

# Allow uses of the register keyword, which may appear in C headers.
cc_args(
name = "wno_register",
actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
args = ["-Wno-register"],
)

# Issue a warning when a class appears to be polymorphic, yet it declares a
# non-virtual destructor
cc_args(
name = "wnon_virtual_dtor",
actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
args = ["-Wnon-virtual-dtor"],
)

# Prevent relative paths from being converted to absolute paths.
cc_args(
name = "no_canonical_prefixes",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = ["-no-canonical-prefixes"],
)

# Enables colors in compiler diagnostics. This uses the GCC spelling of the
# flag, which Clang supports as an undocumented extension.
cc_args(
name = "color_diagnostics",
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",
],
args = ["-fdiagnostics-color"],
)

cc_args(
name = "debugging",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = ["-g"],
)

# Compile without runtime type information (RTTI). This produces smaller binaries.
cc_args(
name = "no_rtti",
actions = ["@rules_cc//cc/toolchains/actions:cpp_compile_actions"],
args = ["-fno-rtti"],
)

# Optimization level option
cc_args(
name = "o2",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = ["-O2"],
)

# Optimization aggressively for size rather than speed option
cc_args(
name = "oz",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = ["-Oz"],
)

# Standard compiler flags to reduce output binary size.
cc_args(
name = "reduced_size",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-fno-common",
"-fno-exceptions",
"-ffunction-sections",
"-fdata-sections",
],
)

cc_feature(
name = "c++17_feature",
args = [":c++17"],
feature_name = "c++17",
)

# Compile for the C++17 standard.
cc_args(
name = "c++17",
actions = [
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = ["-std=c++17"],
)

cc_feature(
name = "c++20_feature",
args = [":c++20"],
feature_name = "c++20",
)

# Compile for the C++20 standard.
cc_args(
name = "c++20",
actions = [
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = ["-std=c++20"],
)

cc_args(
name = "asan",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-fsanitize=address",
"-DADDRESS_SANITIZER",
],
)

# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc.
cc_args(
name = "ubsan",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-fsanitize=undefined",
"-DUNDEFINED_SANITIZER",
],
)

# TODO: https://pwbug.dev/346388161 - Push this to upstream rules_cc.
cc_args(
name = "tsan",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-fsanitize=thread",
"-DTHREAD_SANITIZER",
],
)

cc_args(
name = "fuzztest",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile_actions",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
"@rules_cc//cc/toolchains/actions:link_actions",
],
args = [
"-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION",
"-UNDEBUG",
"-D_LIBCPP_ENABLE_ASSERTIONS=1",
] + select({
"@platforms//cpu:x86_64": ["-mcrc32"],
"//conditions:default": [],
}),
)
17 changes: 17 additions & 0 deletions pw_toolchain/build_external/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

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

licenses(["notice"])
Loading

0 comments on commit 130903f

Please sign in to comment.