1# Copyright 2022 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15load("@rules_cc//cc/toolchains:args.bzl", "cc_args") 16load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain") 17 18licenses(["notice"]) 19 20cc_library( 21 name = "newlib_os_interface_stubs", 22 srcs = ["newlib_os_interface_stubs.cc"], 23 linkopts = [ 24 "-Wl,--wrap=__sread", 25 "-Wl,--wrap=__swrite", 26 "-Wl,--wrap=__sseek", 27 "-Wl,--wrap=__sclose", 28 ], 29 # Only built as part of the ARM GCC toolchain. 30 tags = ["manual"], 31 visibility = ["//visibility:public"], 32 deps = ["//pw_assert"], 33 alwayslink = 1, 34) 35 36cc_library( 37 name = "arm_none_eabi_gcc_support", 38 # Only built as part of the ARM GCC toolchain. 39 tags = ["manual"], 40 visibility = ["//visibility:public"], 41 deps = [ 42 ":newlib_os_interface_stubs", 43 "//pw_toolchain:wrap_abort", 44 ], 45) 46 47# Additional arm_gcc specific warning flags 48cc_args( 49 name = "warnings", 50 actions = [ 51 "@rules_cc//cc/toolchains/actions:compile_actions", 52 ], 53 args = [ 54 # This can't be in common, because proto headers in host builds trigger 55 "-Wundef", 56 # Silence the really verbose ARM warnings. 57 "-Wno-psabi", 58 ], 59) 60 61cc_args( 62 name = "thumb_abi", 63 actions = [ 64 "@rules_cc//cc/toolchains/actions:assembly_actions", 65 "@rules_cc//cc/toolchains/actions:compile_actions", 66 "@rules_cc//cc/toolchains/actions:link_actions", 67 ], 68 args = [ 69 "-mabi=aapcs", 70 "-mthumb", 71 ], 72) 73 74cc_args( 75 name = "unified_asm_syntax", 76 actions = [ 77 "@rules_cc//cc/toolchains/actions:assembly_actions", 78 "@rules_cc//cc/toolchains/actions:compile_actions", 79 "@rules_cc//cc/toolchains/actions:link_actions", 80 ], 81 args = [ 82 "-masm-syntax-unified", 83 ], 84) 85 86# This flag prevents Arm GCC from printing the resolved paths of symlinks, 87# which prevents compilation actions from being hermetic. See 88# https://github.com/bazelbuild/bazel/issues/21981 and 89# https://pwbug.dev/319665090. 90cc_args( 91 name = "no_canonical_system_headers", 92 actions = [ 93 "@rules_cc//cc/toolchains/actions:compile_actions", 94 ], 95 args = [ 96 "-fno-canonical-system-headers", 97 ], 98) 99 100cc_args( 101 name = "cortex_common", 102 actions = [ 103 "@rules_cc//cc/toolchains/actions:compile_actions", 104 ], 105 args = [ 106 "-ffreestanding", 107 "-specs=nano.specs", 108 "-specs=nosys.specs", 109 ], 110) 111 112cc_args( 113 name = "cortex_common_link", 114 actions = ["@rules_cc//cc/toolchains/actions:link_actions"], 115 args = [ 116 "-Wl,--gc-sections", 117 "-specs=nano.specs", 118 "-specs=nosys.specs", 119 "-lstdc++", 120 "-lnosys", 121 "-lc", 122 "-lm", 123 "-Wl,--no-warn-rwx-segment", 124 ], 125) 126 127cc_args( 128 name = "cortex-m0", 129 actions = [ 130 "@rules_cc//cc/toolchains/actions:assembly_actions", 131 "@rules_cc//cc/toolchains/actions:compile_actions", 132 "@rules_cc//cc/toolchains/actions:link_actions", 133 ], 134 args = [ 135 "-mcpu=cortex-m0", 136 "-mfloat-abi=soft", 137 ], 138) 139 140cc_args( 141 name = "cortex-m0plus", 142 actions = [ 143 "@rules_cc//cc/toolchains/actions:assembly_actions", 144 "@rules_cc//cc/toolchains/actions:compile_actions", 145 "@rules_cc//cc/toolchains/actions:link_actions", 146 ], 147 args = [ 148 "-mcpu=cortex-m0plus", 149 "-mfloat-abi=soft", 150 ], 151) 152 153cc_args( 154 name = "cortex-m3", 155 actions = [ 156 "@rules_cc//cc/toolchains/actions:assembly_actions", 157 "@rules_cc//cc/toolchains/actions:compile_actions", 158 "@rules_cc//cc/toolchains/actions:link_actions", 159 ], 160 args = [ 161 "-mcpu=cortex-m3", 162 "-mfloat-abi=soft", 163 ], 164) 165 166cc_args( 167 name = "cortex-m4", 168 actions = [ 169 "@rules_cc//cc/toolchains/actions:assembly_actions", 170 "@rules_cc//cc/toolchains/actions:compile_actions", 171 "@rules_cc//cc/toolchains/actions:link_actions", 172 ], 173 args = [ 174 "-mcpu=cortex-m4", 175 "-mfloat-abi=hard", 176 "-mfpu=fpv4-sp-d16", 177 # Used by some pigweed tests/targets to correctly handle hardware FPU 178 # behavior. 179 "-DPW_ARMV7M_ENABLE_FPU=1", 180 ], 181) 182 183cc_args( 184 name = "cortex-m4+nofp", 185 actions = [ 186 "@rules_cc//cc/toolchains/actions:assembly_actions", 187 "@rules_cc//cc/toolchains/actions:compile_actions", 188 "@rules_cc//cc/toolchains/actions:link_actions", 189 ], 190 args = [ 191 "-mcpu=cortex-m4+nofp", 192 "-mfloat-abi=soft", 193 ], 194) 195 196cc_args( 197 name = "cortex-m7", 198 actions = [ 199 "@rules_cc//cc/toolchains/actions:assembly_actions", 200 "@rules_cc//cc/toolchains/actions:compile_actions", 201 "@rules_cc//cc/toolchains/actions:link_actions", 202 ], 203 args = [ 204 "-mcpu=cortex-m7", 205 "-mfloat-abi=hard", 206 "-mfpu=fpv5-d16", 207 # Used by some pigweed tests/targets to correctly handle hardware FPU 208 # behavior. 209 "-DPW_ARMV7M_ENABLE_FPU=1", 210 ], 211) 212 213cc_args( 214 name = "cortex-m33", 215 actions = [ 216 "@rules_cc//cc/toolchains/actions:assembly_actions", 217 "@rules_cc//cc/toolchains/actions:compile_actions", 218 "@rules_cc//cc/toolchains/actions:link_actions", 219 ], 220 args = [ 221 "-mcpu=cortex-m33", 222 "-mfloat-abi=hard", 223 "-mfpu=fpv5-sp-d16", 224 # Used by some pigweed tests/targets to correctly handle hardware FPU 225 # behavior. 226 "-DPW_ARMV7M_ENABLE_FPU=1", 227 ], 228) 229 230cc_args( 231 name = "cortex-m33+nofp", 232 actions = [ 233 "@rules_cc//cc/toolchains/actions:assembly_actions", 234 "@rules_cc//cc/toolchains/actions:compile_actions", 235 "@rules_cc//cc/toolchains/actions:link_actions", 236 ], 237 args = [ 238 "-mcpu=cortex-m33+nofp", 239 "-mfloat-abi=soft", 240 ], 241) 242 243cc_toolchain( 244 name = "arm_gcc_toolchain_cortex-m", 245 args = [ 246 "//pw_toolchain/cc/args:oz", 247 "//pw_toolchain/cc/args:debugging", 248 "//pw_toolchain/cc/args:reduced_size", 249 "//pw_toolchain/cc/args:no_canonical_prefixes", 250 "//pw_toolchain/cc/args:no_rtti", 251 "//pw_toolchain/cc/args:wno_register", 252 "//pw_toolchain/cc/args:wnon_virtual_dtor", 253 "//pw_toolchain/cc/args:common_warnings", 254 "//pw_toolchain/cc/args:color_diagnostics", 255 ] + select({ 256 "@pw_toolchain//constraints/arm_mcpu:cortex-m0": [":cortex-m0"], 257 "@pw_toolchain//constraints/arm_mcpu:cortex-m0plus": [":cortex-m0plus"], 258 "@pw_toolchain//constraints/arm_mcpu:cortex-m3": [":cortex-m3"], 259 "@pw_toolchain//constraints/arm_mcpu:cortex-m33": [":cortex-m33"], 260 "@pw_toolchain//constraints/arm_mcpu:cortex-m33+nofp": [":cortex-m33+nofp"], 261 "@pw_toolchain//constraints/arm_mcpu:cortex-m4": [":cortex-m4"], 262 "@pw_toolchain//constraints/arm_mcpu:cortex-m4+nofp": [":cortex-m4+nofp"], 263 "@pw_toolchain//constraints/arm_mcpu:cortex-m7": [":cortex-m7"], 264 "@pw_toolchain//constraints/arm_mcpu:none": [], 265 }) + [ 266 ":thumb_abi", 267 # TODO(b/353576440): Enable unified assembly syntax. 268 # ":unified_asm_syntax", 269 ":cortex_common", 270 ":cortex_common_link", 271 ":no_canonical_system_headers", 272 ":warnings", 273 ], 274 enabled_features = [ 275 "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", 276 "//pw_toolchain/cc/capability:compiler_is_gcc", 277 "//pw_toolchain/cc/capability:linker_is_gcc", 278 ] + select({ 279 "//pw_toolchain/cc:c++17_enabled": ["//pw_toolchain/cc/args:c++17_feature"], 280 "//conditions:default": [], 281 }) + select({ 282 "//pw_toolchain/cc:c++20_enabled": ["//pw_toolchain/cc/args:c++20_feature"], 283 "//conditions:default": [], 284 }), 285 known_features = [ 286 "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features", 287 "//pw_toolchain/cc/args:c++17_feature", 288 "//pw_toolchain/cc/args:c++20_feature", 289 "//pw_toolchain/cc/capability:compiler_is_gcc", 290 "//pw_toolchain/cc/capability:linker_is_gcc", 291 ], 292 target_compatible_with = select({ 293 "@pw_toolchain//constraints/arm_mcpu:cortex-m0": [], 294 "@pw_toolchain//constraints/arm_mcpu:cortex-m0plus": [], 295 "@pw_toolchain//constraints/arm_mcpu:cortex-m3": [], 296 "@pw_toolchain//constraints/arm_mcpu:cortex-m33": [], 297 "@pw_toolchain//constraints/arm_mcpu:cortex-m33+nofp": [], 298 "@pw_toolchain//constraints/arm_mcpu:cortex-m4": [], 299 "@pw_toolchain//constraints/arm_mcpu:cortex-m4+nofp": [], 300 "@pw_toolchain//constraints/arm_mcpu:cortex-m7": [], 301 "@pw_toolchain//constraints/arm_mcpu:none": ["@platforms//:incompatible"], 302 }), 303 tool_map = "@gcc_arm_none_eabi_toolchain//:all_tools", 304 visibility = ["//pw_toolchain:__pkg__"], 305) 306 307toolchain( 308 name = "arm_gcc_cc_toolchain_cortex-m0", 309 target_compatible_with = [ 310 "@pw_toolchain//constraints/arm_mcpu:cortex-m0", 311 ], 312 toolchain = ":arm_gcc_toolchain_cortex-m", 313 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 314) 315 316toolchain( 317 name = "arm_gcc_cc_toolchain_cortex-m0plus", 318 target_compatible_with = [ 319 "@pw_toolchain//constraints/arm_mcpu:cortex-m0plus", 320 ], 321 toolchain = ":arm_gcc_toolchain_cortex-m", 322 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 323) 324 325toolchain( 326 name = "arm_gcc_cc_toolchain_cortex-m3", 327 target_compatible_with = [ 328 "@pw_toolchain//constraints/arm_mcpu:cortex-m3", 329 ], 330 toolchain = ":arm_gcc_toolchain_cortex-m", 331 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 332) 333 334toolchain( 335 name = "arm_gcc_cc_toolchain_cortex-m4", 336 target_compatible_with = [ 337 "@pw_toolchain//constraints/arm_mcpu:cortex-m4", 338 ], 339 toolchain = ":arm_gcc_toolchain_cortex-m", 340 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 341) 342 343toolchain( 344 name = "arm_gcc_cc_toolchain_cortex-m4+nofp", 345 target_compatible_with = [ 346 "@pw_toolchain//constraints/arm_mcpu:cortex-m4+nofp", 347 ], 348 toolchain = ":arm_gcc_toolchain_cortex-m", 349 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 350) 351 352toolchain( 353 name = "arm_gcc_cc_toolchain_cortex-m7", 354 target_compatible_with = [ 355 "@pw_toolchain//constraints/arm_mcpu:cortex-m7", 356 ], 357 toolchain = ":arm_gcc_toolchain_cortex-m", 358 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 359) 360 361toolchain( 362 name = "arm_gcc_cc_toolchain_cortex-m33", 363 target_compatible_with = [ 364 "@pw_toolchain//constraints/arm_mcpu:cortex-m33", 365 ], 366 toolchain = ":arm_gcc_toolchain_cortex-m", 367 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 368) 369 370toolchain( 371 name = "arm_gcc_cc_toolchain_cortex-m33+nofp", 372 target_compatible_with = [ 373 "@pw_toolchain//constraints/arm_mcpu:cortex-m33+nofp", 374 ], 375 toolchain = ":arm_gcc_toolchain_cortex-m", 376 toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", 377) 378