• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:cc_library.bzl", "cc_library")
16load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
17load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
18
19licenses(["notice"])
20
21cc_library(
22    name = "newlib_os_interface_stubs",
23    srcs = ["newlib_os_interface_stubs.cc"],
24    implementation_deps = ["//pw_assert:check"],
25    linkopts = [
26        "-Wl,--wrap=__sread",
27        "-Wl,--wrap=__swrite",
28        "-Wl,--wrap=__sseek",
29        "-Wl,--wrap=__sclose",
30    ],
31    # Only built as part of the ARM GCC toolchain.
32    tags = ["manual"],
33    visibility = ["//visibility:public"],
34    alwayslink = 1,
35)
36
37cc_library(
38    name = "arm_none_eabi_gcc_support",
39    # Only built as part of the ARM GCC toolchain.
40    tags = ["manual"],
41    visibility = ["//visibility:public"],
42    deps = [
43        ":newlib_os_interface_stubs",
44        "//pw_toolchain:wrap_abort",
45    ],
46)
47
48# Additional arm_gcc specific warning flags
49cc_args(
50    name = "warnings",
51    actions = [
52        "@rules_cc//cc/toolchains/actions:compile_actions",
53    ],
54    args = [
55        # This can't be in common, because proto headers in host builds trigger
56        "-Wundef",
57        # Silence the really verbose ARM warnings.
58        "-Wno-psabi",
59    ],
60)
61
62cc_args(
63    name = "thumb_abi",
64    actions = [
65        "@rules_cc//cc/toolchains/actions:assembly_actions",
66        "@rules_cc//cc/toolchains/actions:compile_actions",
67        "@rules_cc//cc/toolchains/actions:link_actions",
68    ],
69    args = [
70        "-mabi=aapcs",
71        "-mthumb",
72    ],
73)
74
75cc_args(
76    name = "unified_asm_syntax",
77    actions = [
78        "@rules_cc//cc/toolchains/actions:assembly_actions",
79        "@rules_cc//cc/toolchains/actions:compile_actions",
80        "@rules_cc//cc/toolchains/actions:link_actions",
81    ],
82    args = [
83        "-masm-syntax-unified",
84    ],
85)
86
87# This flag prevents Arm GCC from printing the resolved paths of symlinks,
88# which prevents compilation actions from being hermetic. See
89# https://github.com/bazelbuild/bazel/issues/21981 and
90# https://pwbug.dev/319665090.
91cc_args(
92    name = "no_canonical_system_headers",
93    actions = [
94        "@rules_cc//cc/toolchains/actions:compile_actions",
95    ],
96    args = [
97        "-fno-canonical-system-headers",
98    ],
99)
100
101cc_args(
102    name = "cortex_common",
103    actions = [
104        "@rules_cc//cc/toolchains/actions:compile_actions",
105    ],
106    args = [
107        "-ffreestanding",
108        "-specs=nano.specs",
109        "-specs=nosys.specs",
110    ],
111)
112
113cc_args(
114    name = "cortex_common_link",
115    actions = ["@rules_cc//cc/toolchains/actions:link_actions"],
116    args = [
117        "-Wl,--gc-sections",
118        "-specs=nano.specs",
119        "-specs=nosys.specs",
120        "-lstdc++",
121        "-lnosys",
122        "-lc",
123        "-lm",
124        "-Wl,--no-warn-rwx-segment",
125    ],
126)
127
128cc_args(
129    name = "cortex-m0",
130    actions = [
131        "@rules_cc//cc/toolchains/actions:assembly_actions",
132        "@rules_cc//cc/toolchains/actions:compile_actions",
133        "@rules_cc//cc/toolchains/actions:link_actions",
134    ],
135    args = [
136        "-mcpu=cortex-m0",
137        "-mfloat-abi=soft",
138    ],
139)
140
141cc_args(
142    name = "cortex-m0plus",
143    actions = [
144        "@rules_cc//cc/toolchains/actions:assembly_actions",
145        "@rules_cc//cc/toolchains/actions:compile_actions",
146        "@rules_cc//cc/toolchains/actions:link_actions",
147    ],
148    args = [
149        "-mcpu=cortex-m0plus",
150        "-mfloat-abi=soft",
151    ],
152)
153
154cc_args(
155    name = "cortex-m3",
156    actions = [
157        "@rules_cc//cc/toolchains/actions:assembly_actions",
158        "@rules_cc//cc/toolchains/actions:compile_actions",
159        "@rules_cc//cc/toolchains/actions:link_actions",
160    ],
161    args = [
162        "-mcpu=cortex-m3",
163        "-mfloat-abi=soft",
164    ],
165)
166
167cc_args(
168    name = "cortex-m4",
169    actions = [
170        "@rules_cc//cc/toolchains/actions:assembly_actions",
171        "@rules_cc//cc/toolchains/actions:compile_actions",
172        "@rules_cc//cc/toolchains/actions:link_actions",
173    ],
174    args = [
175        "-mcpu=cortex-m4",
176        "-mfloat-abi=hard",
177        "-mfpu=fpv4-sp-d16",
178        # Used by some pigweed tests/targets to correctly handle hardware FPU
179        # behavior.
180        "-DPW_ARMV7M_ENABLE_FPU=1",
181    ],
182)
183
184cc_args(
185    name = "cortex-m7",
186    actions = [
187        "@rules_cc//cc/toolchains/actions:assembly_actions",
188        "@rules_cc//cc/toolchains/actions:compile_actions",
189        "@rules_cc//cc/toolchains/actions:link_actions",
190    ],
191    args = [
192        "-mcpu=cortex-m7",
193        "-mfloat-abi=hard",
194        "-mfpu=fpv5-d16",
195        # Used by some pigweed tests/targets to correctly handle hardware FPU
196        # behavior.
197        "-DPW_ARMV7M_ENABLE_FPU=1",
198    ],
199)
200
201cc_args(
202    name = "cortex-m33",
203    actions = [
204        "@rules_cc//cc/toolchains/actions:assembly_actions",
205        "@rules_cc//cc/toolchains/actions:compile_actions",
206        "@rules_cc//cc/toolchains/actions:link_actions",
207    ],
208    args = [
209        "-mcpu=cortex-m33",
210        "-mfloat-abi=hard",
211        "-mfpu=fpv5-sp-d16",
212        # Used by some pigweed tests/targets to correctly handle hardware FPU
213        # behavior.
214        "-DPW_ARMV7M_ENABLE_FPU=1",
215    ],
216)
217
218cc_toolchain(
219    name = "arm_gcc_toolchain_cortex-m",
220    args = [
221        "//pw_toolchain/cc/args:oz",
222        "//pw_toolchain/cc/args:debugging",
223        "//pw_toolchain/cc/args:reduced_size",
224        "//pw_toolchain/cc/args:no_canonical_prefixes",
225        "//pw_toolchain/cc/args:no_rtti",
226        "//pw_toolchain/cc/args:wno_register",
227        "//pw_toolchain/cc/args:wnon_virtual_dtor",
228        "//pw_toolchain/cc/args:common_warnings",
229        "//pw_toolchain/cc/args:color_diagnostics",
230    ] + select({
231        "//pw_build/constraints/arm:cortex-m0": [":cortex-m0"],
232        "//pw_build/constraints/arm:cortex-m0plus": [":cortex-m0plus"],
233        "//pw_build/constraints/arm:cortex-m3": [":cortex-m3"],
234        "//pw_build/constraints/arm:cortex-m33": [":cortex-m33"],
235        "//pw_build/constraints/arm:cortex-m4": [":cortex-m4"],
236        "//pw_build/constraints/arm:cortex-m7": [":cortex-m7"],
237        "//pw_build/constraints/arm:none": [],
238    }) + [
239        ":thumb_abi",
240        # TODO(b/353576440): Enable unified assembly syntax.
241        # ":unified_asm_syntax",
242        ":cortex_common",
243        ":cortex_common_link",
244        ":no_canonical_system_headers",
245        ":warnings",
246    ],
247    enabled_features = [
248        "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
249        "//pw_toolchain/cc/capability:compiler_is_gcc",
250        "//pw_toolchain/cc/capability:linker_is_gcc",
251    ] + select({
252        "//pw_toolchain/cc:c++17_enabled": ["//pw_toolchain/cc/args:c++17_feature"],
253        "//conditions:default": [],
254    }) + select({
255        "//pw_toolchain/cc:c++20_enabled": ["//pw_toolchain/cc/args:c++20_feature"],
256        "//conditions:default": [],
257    }),
258    known_features = [
259        "@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features",
260        "//pw_toolchain/cc/args:c++17_feature",
261        "//pw_toolchain/cc/args:c++20_feature",
262        "//pw_toolchain/cc/capability:compiler_is_gcc",
263        "//pw_toolchain/cc/capability:linker_is_gcc",
264    ],
265    target_compatible_with = select({
266        "//pw_build/constraints/arm:cortex-m0": [],
267        "//pw_build/constraints/arm:cortex-m0plus": [],
268        "//pw_build/constraints/arm:cortex-m3": [],
269        "//pw_build/constraints/arm:cortex-m33": [],
270        "//pw_build/constraints/arm:cortex-m4": [],
271        "//pw_build/constraints/arm:cortex-m7": [],
272        "//pw_build/constraints/arm:none": ["@platforms//:incompatible"],
273    }),
274    tool_map = "@gcc_arm_none_eabi_toolchain//:all_tools",
275    visibility = ["//pw_toolchain:__pkg__"],
276)
277
278toolchain(
279    name = "arm_gcc_cc_toolchain_cortex-m0",
280    target_compatible_with = [
281        "//pw_build/constraints/arm:cortex-m0",
282    ],
283    toolchain = ":arm_gcc_toolchain_cortex-m",
284    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
285)
286
287toolchain(
288    name = "arm_gcc_cc_toolchain_cortex-m0plus",
289    target_compatible_with = [
290        "//pw_build/constraints/arm:cortex-m0plus",
291    ],
292    toolchain = ":arm_gcc_toolchain_cortex-m",
293    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
294)
295
296toolchain(
297    name = "arm_gcc_cc_toolchain_cortex-m3",
298    target_compatible_with = [
299        "//pw_build/constraints/arm:cortex-m3",
300    ],
301    toolchain = ":arm_gcc_toolchain_cortex-m",
302    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
303)
304
305toolchain(
306    name = "arm_gcc_cc_toolchain_cortex-m4",
307    target_compatible_with = [
308        "//pw_build/constraints/arm:cortex-m4",
309    ],
310    toolchain = ":arm_gcc_toolchain_cortex-m",
311    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
312)
313
314toolchain(
315    name = "arm_gcc_cc_toolchain_cortex-m7",
316    target_compatible_with = [
317        "//pw_build/constraints/arm:cortex-m7",
318    ],
319    toolchain = ":arm_gcc_toolchain_cortex-m",
320    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
321)
322
323toolchain(
324    name = "arm_gcc_cc_toolchain_cortex-m33",
325    target_compatible_with = [
326        "//pw_build/constraints/arm:cortex-m33",
327    ],
328    toolchain = ":arm_gcc_toolchain_cortex-m",
329    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
330)
331