• 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(
16    "@pw_toolchain//cc_toolchain:defs.bzl",
17    "pw_cc_flag_set",
18    "pw_cc_toolchain",
19)
20
21licenses(["notice"])
22
23cc_library(
24    name = "newlib_os_interface_stubs",
25    srcs = ["newlib_os_interface_stubs.cc"],
26    linkopts = [
27        "-Wl,--wrap=__sread",
28        "-Wl,--wrap=__swrite",
29        "-Wl,--wrap=__sseek",
30        "-Wl,--wrap=__sclose",
31    ],
32    visibility = ["//visibility:public"],
33    deps = ["//pw_assert"],
34    alwayslink = 1,
35)
36
37cc_library(
38    name = "arm_none_eabi_gcc_support",
39    visibility = ["//visibility:public"],
40    deps = [
41        ":newlib_os_interface_stubs",
42        "//pw_toolchain:wrap_abort",
43    ],
44)
45
46# Although we use similar warnings for clang and arm_gcc, we don't have one
47# centralized list, since we might want to use different warnings based on the
48# compiler in the future.
49pw_cc_flag_set(
50    name = "warnings",
51    actions = [
52        "@pw_toolchain//actions:all_c_compiler_actions",
53        "@pw_toolchain//actions:all_cpp_compiler_actions",
54    ],
55    flags = [
56        "-Wall",
57        "-Wextra",
58        # Make all warnings errors, except for the exemptions below.
59        "-Werror",
60        "-Wno-error=cpp",  # preprocessor #warning statement
61        "-Wno-error=deprecated-declarations",  # [[deprecated]] attribute
62        "-Wno-psabi",  # Silence the really verbose ARM warnings.
63    ],
64)
65
66pw_cc_flag_set(
67    name = "thumb_abi",
68    actions = [
69        "@pw_toolchain//actions:all_asm_actions",
70        "@pw_toolchain//actions:all_c_compiler_actions",
71        "@pw_toolchain//actions:all_cpp_compiler_actions",
72        "@pw_toolchain//actions:all_link_actions",
73    ],
74    flags = [
75        "-mabi=aapcs",
76        "-mthumb",
77    ],
78)
79
80# This flag prevents Arm GCC from printing the resolved paths of symlinks,
81# which prevents compilation actions from being hermetic. See
82# https://github.com/bazelbuild/bazel/issues/21981 and
83# https://pwbug.dev/319665090.
84pw_cc_flag_set(
85    name = "no_canonical_system_headers",
86    actions = [
87        "@pw_toolchain//actions:all_c_compiler_actions",
88        "@pw_toolchain//actions:all_cpp_compiler_actions",
89    ],
90    flags = [
91        "-fno-canonical-system-headers",
92    ],
93)
94
95pw_cc_flag_set(
96    name = "cortex_common",
97    actions = [
98        "@pw_toolchain//actions:all_c_compiler_actions",
99        "@pw_toolchain//actions:all_cpp_compiler_actions",
100    ],
101    flags = [
102        "-ffreestanding",
103        "-specs=nano.specs",
104        "-specs=nosys.specs",
105    ],
106)
107
108pw_cc_flag_set(
109    name = "cortex_common_link",
110    actions = ["@pw_toolchain//actions:all_link_actions"],
111    flags = [
112        "-Wl,--gc-sections",
113        "-specs=nano.specs",
114        "-specs=nosys.specs",
115        "-lstdc++",
116        "-lnosys",
117        "-lc",
118        "-lm",
119        "-Wl,--no-warn-rwx-segment",
120    ],
121)
122
123pw_cc_flag_set(
124    name = "cortex-m0",
125    actions = [
126        "@pw_toolchain//actions:all_asm_actions",
127        "@pw_toolchain//actions:all_c_compiler_actions",
128        "@pw_toolchain//actions:all_cpp_compiler_actions",
129        "@pw_toolchain//actions:all_link_actions",
130    ],
131    flags = [
132        "-mcpu=cortex-m0",
133        "-mfloat-abi=soft",
134    ],
135)
136
137pw_cc_flag_set(
138    name = "cortex-m3",
139    actions = [
140        "@pw_toolchain//actions:all_asm_actions",
141        "@pw_toolchain//actions:all_c_compiler_actions",
142        "@pw_toolchain//actions:all_cpp_compiler_actions",
143        "@pw_toolchain//actions:all_link_actions",
144    ],
145    flags = [
146        "-mcpu=cortex-m3",
147        "-mfloat-abi=soft",
148    ],
149)
150
151pw_cc_flag_set(
152    name = "cortex-m4",
153    actions = [
154        "@pw_toolchain//actions:all_asm_actions",
155        "@pw_toolchain//actions:all_c_compiler_actions",
156        "@pw_toolchain//actions:all_cpp_compiler_actions",
157        "@pw_toolchain//actions:all_link_actions",
158    ],
159    flags = [
160        "-mcpu=cortex-m4",
161        "-mfloat-abi=hard",
162    ],
163)
164
165pw_cc_flag_set(
166    name = "cortex-m4+nofp",
167    actions = [
168        "@pw_toolchain//actions:all_asm_actions",
169        "@pw_toolchain//actions:all_c_compiler_actions",
170        "@pw_toolchain//actions:all_cpp_compiler_actions",
171        "@pw_toolchain//actions:all_link_actions",
172    ],
173    flags = [
174        "-mcpu=cortex-m4+nofp",
175        "-mfloat-abi=soft",
176    ],
177)
178
179pw_cc_flag_set(
180    name = "cortex-m7",
181    actions = [
182        "@pw_toolchain//actions:all_asm_actions",
183        "@pw_toolchain//actions:all_c_compiler_actions",
184        "@pw_toolchain//actions:all_cpp_compiler_actions",
185        "@pw_toolchain//actions:all_link_actions",
186    ],
187    flags = [
188        "-mcpu=cortex-m7",
189        "-mfloat-abi=hard",
190    ],
191)
192
193pw_cc_flag_set(
194    name = "cortex-m33",
195    actions = [
196        "@pw_toolchain//actions:all_asm_actions",
197        "@pw_toolchain//actions:all_c_compiler_actions",
198        "@pw_toolchain//actions:all_cpp_compiler_actions",
199        "@pw_toolchain//actions:all_link_actions",
200    ],
201    flags = [
202        "-mcpu=cortex-m33",
203        "-mfloat-abi=hard",
204    ],
205)
206
207pw_cc_flag_set(
208    name = "cortex-m33+nofp",
209    actions = [
210        "@pw_toolchain//actions:all_asm_actions",
211        "@pw_toolchain//actions:all_c_compiler_actions",
212        "@pw_toolchain//actions:all_cpp_compiler_actions",
213        "@pw_toolchain//actions:all_link_actions",
214    ],
215    flags = [
216        "-mcpu=cortex-m33+nofp",
217        "-mfloat-abi=soft",
218    ],
219)
220
221pw_cc_toolchain(
222    name = "arm_gcc_toolchain_cortex-m",
223    action_configs = [
224        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-ar",
225        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-gcc",
226        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-g++",
227        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-gcov",
228        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-ld",
229        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-objcopy",
230        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-objdump",
231        "@gcc_arm_none_eabi_toolchain//:arm-none-eabi-strip",
232    ],
233    builtin_sysroot = "external/gcc_arm_none_eabi_toolchain",
234    compiler = "gcc",
235    cxx_builtin_include_directories = [
236        "%sysroot%/arm-none-eabi/include/newlib-nano",
237        "%sysroot%/arm-none-eabi/include/c++/12.2.1",
238        "%sysroot%/arm-none-eabi/include/c++/12.2.1/arm-none-eabi",
239        "%sysroot%/arm-none-eabi/include/c++/12.2.1/backward",
240        "%sysroot%/lib/gcc/arm-none-eabi/12.2.1/include",
241        "%sysroot%/lib/gcc/arm-none-eabi/12.2.1/include-fixed",
242        "%sysroot%/arm-none-eabi/include",
243    ],
244    flag_sets = [
245        "@pw_toolchain//flag_sets:o2",
246        "@pw_toolchain//flag_sets:c++17",
247        "@pw_toolchain//flag_sets:debugging",
248        "@pw_toolchain//flag_sets:reduced_size",
249        "@pw_toolchain//flag_sets:no_canonical_prefixes",
250        "@pw_toolchain//flag_sets:no_rtti",
251        "@pw_toolchain//flag_sets:wno_register",
252        "@pw_toolchain//flag_sets:wnon_virtual_dtor",
253    ] + select({
254        "@pw_toolchain//constraints/arm_mcpu:cortex-m0": [":cortex-m0"],
255        "@pw_toolchain//constraints/arm_mcpu:cortex-m3": [":cortex-m3"],
256        "@pw_toolchain//constraints/arm_mcpu:cortex-m33": [":cortex-m33"],
257        "@pw_toolchain//constraints/arm_mcpu:cortex-m33+nofp": [":cortex-m33+nofp"],
258        "@pw_toolchain//constraints/arm_mcpu:cortex-m4": [":cortex-m4"],
259        "@pw_toolchain//constraints/arm_mcpu:cortex-m4+nofp": [":cortex-m4+nofp"],
260        "@pw_toolchain//constraints/arm_mcpu:cortex-m7": [":cortex-m7"],
261        "@pw_toolchain//constraints/arm_mcpu:none": [],
262    }) + [
263        ":thumb_abi",
264        ":cortex_common",
265        ":cortex_common_link",
266        ":no_canonical_system_headers",
267        ":warnings",
268    ],
269    target_compatible_with = select({
270        "@pw_toolchain//constraints/arm_mcpu:cortex-m0": [],
271        "@pw_toolchain//constraints/arm_mcpu:cortex-m3": [],
272        "@pw_toolchain//constraints/arm_mcpu:cortex-m33": [],
273        "@pw_toolchain//constraints/arm_mcpu:cortex-m33+nofp": [],
274        "@pw_toolchain//constraints/arm_mcpu:cortex-m4": [],
275        "@pw_toolchain//constraints/arm_mcpu:cortex-m4+nofp": [],
276        "@pw_toolchain//constraints/arm_mcpu:cortex-m7": [],
277        "@pw_toolchain//constraints/arm_mcpu:none": ["@platforms//:incompatible"],
278    }),
279    toolchain_identifier = "arm-gcc-toolchain",
280)
281
282toolchain(
283    name = "arm_gcc_cc_toolchain_cortex-m0",
284    target_compatible_with = [
285        "@pw_toolchain//constraints/arm_mcpu:cortex-m0",
286    ],
287    toolchain = ":arm_gcc_toolchain_cortex-m",
288    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
289)
290
291toolchain(
292    name = "arm_gcc_cc_toolchain_cortex-m3",
293    target_compatible_with = [
294        "@pw_toolchain//constraints/arm_mcpu:cortex-m3",
295    ],
296    toolchain = ":arm_gcc_toolchain_cortex-m",
297    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
298)
299
300toolchain(
301    name = "arm_gcc_cc_toolchain_cortex-m4",
302    target_compatible_with = [
303        "@pw_toolchain//constraints/arm_mcpu:cortex-m4",
304    ],
305    toolchain = ":arm_gcc_toolchain_cortex-m",
306    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
307)
308
309toolchain(
310    name = "arm_gcc_cc_toolchain_cortex-m4+nofp",
311    target_compatible_with = [
312        "@pw_toolchain//constraints/arm_mcpu:cortex-m4+nofp",
313    ],
314    toolchain = ":arm_gcc_toolchain_cortex-m",
315    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
316)
317
318toolchain(
319    name = "arm_gcc_cc_toolchain_cortex-m7",
320    target_compatible_with = [
321        "@pw_toolchain//constraints/arm_mcpu:cortex-m7",
322    ],
323    toolchain = ":arm_gcc_toolchain_cortex-m",
324    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
325)
326
327toolchain(
328    name = "arm_gcc_cc_toolchain_cortex-m33",
329    target_compatible_with = [
330        "@pw_toolchain//constraints/arm_mcpu:cortex-m33",
331    ],
332    toolchain = ":arm_gcc_toolchain_cortex-m",
333    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
334)
335
336toolchain(
337    name = "arm_gcc_cc_toolchain_cortex-m33+nofp",
338    target_compatible_with = [
339        "@pw_toolchain//constraints/arm_mcpu:cortex-m33+nofp",
340    ],
341    toolchain = ":arm_gcc_toolchain_cortex-m",
342    toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
343)
344