xref: /aosp_15_r20/external/pigweed/pw_build/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2019 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("//pw_build:glob_dirs.bzl", "glob_dirs", "match_dir", "match_dir_internal")
16load("//pw_build:load_phase_test.bzl", "pw_string_comparison_test", "pw_string_list_comparison_test", "return_error")
17load("//pw_build:pw_cc_binary.bzl", "pw_cc_binary_with_map")
18load("//pw_build:pw_cc_blob_library.bzl", "pw_cc_blob_info", "pw_cc_blob_library")
19load("//pw_build:pw_copy_and_patch_file_test.bzl", "pw_copy_and_patch_file_test")
20load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
21
22package(default_visibility = ["//visibility:public"])
23
24licenses(["notice"])
25
26config_setting(
27    name = "kythe",
28    values = {
29        "define": "kythe_corpus=pigweed.googlesource.com/pigweed/pigweed",
30    },
31)
32
33pw_cc_blob_info(
34    name = "test_blob_aligned",
35    alignas = "512",
36    file_path = "test_blob_0123.bin",
37    symbol_name = "kFirstBlob0123",
38)
39
40pw_cc_blob_info(
41    name = "test_blob_unaligned",
42    file_path = "test_blob_0123.bin",
43    symbol_name = "kSecondBlob0123",
44)
45
46pw_cc_blob_library(
47    name = "test_blob",
48    blobs = [
49        ":test_blob_aligned",
50        ":test_blob_unaligned",
51    ],
52    namespace = "test::ns",
53    out_header = "pw_build/test_blob.h",
54)
55
56pw_cc_test(
57    name = "cc_blob_library_test",
58    srcs = ["cc_blob_library_test.cc"],
59    deps = [":test_blob"],
60)
61
62pw_cc_binary_with_map(
63    name = "cc_binary_with_map",
64    srcs = ["empty_main.cc"],
65    # Only enable on platforms that support -Map linker flag
66    target_compatible_with = ["@platforms//os:linux"],
67)
68
69# Bazel produces root-relative file paths without the -ffile-prefix-map option.
70pw_cc_test(
71    name = "file_prefix_map_test",
72    srcs = [
73        "file_prefix_map_test.cc",
74        "pw_build_private/file_prefix_map_test.h",
75    ],
76    defines = [
77        "PW_BUILD_EXPECTED_HEADER_PATH=\\\"pw_build/pw_build_private/file_prefix_map_test.h\\\"",
78        "PW_BUILD_EXPECTED_SOURCE_PATH=\\\"pw_build/file_prefix_map_test.cc\\\"",
79    ],
80)
81
82label_flag(
83    name = "default_module_config",
84    # The default module config is empty.
85    build_setting_default = ":empty_cc_library",
86)
87
88cc_library(
89    name = "test_module_config",
90    defines = [
91        "PW_THREAD_FREERTOS_CONFIG_JOINING_ENABLED=1",
92    ],
93)
94
95pw_cc_test(
96    name = "module_config_test",
97    srcs = ["module_config_test.cc"],
98    # This test requires a special configuration. It's run in CI, and can be
99    # run manually via,
100    #
101    #   bazel build \
102    #     --config=stm32f429i_freertos \
103    #     --//pw_thread_freertos:config_override=//pw_build:test_module_config \
104    #     //pw_build:module_config_test
105    tags = ["manual"],
106    deps = ["//pw_thread:thread"],
107)
108
109# This empty library is used as a placeholder for label flags that need to
110# point to a library of some kind, but don't actually need the dependency to
111# amount to anything.
112cc_library(
113    name = "empty_cc_library",
114)
115
116# A special target used instead of a cc_library as the default condition in
117# backend multiplexer select statements to signal that a facade is in an
118# unconfigured state. This produces better error messages than e.g. using an
119# invalid label.
120#
121# If you're a user whose build errored out because a library transitively
122# depended on this target: the platform you're targeting did not specify which
123# backend to use for some facade. Look at the previous step in the dependency
124# chain (printed with the error) to figure out which one.
125cc_library(
126    name = "unspecified_backend",
127    target_compatible_with = ["@platforms//:incompatible"],
128)
129
130# Additional libraries that all binaries using Pigweed should be linked against.
131#
132# This is analogous to GN's pw_build_LINK_DEPS. See
133# https://pigweed.dev/build_system.html#docs-build-system-bazel-link-extra-lib
134# for more details.
135cc_library(
136    name = "default_link_extra_lib",
137    deps = [
138        "//pw_assert:assert_backend_impl",
139        "//pw_assert:check_backend_impl",
140        "//pw_log:backend_impl",
141    ],
142)
143
144# Linker script utility PW_MUST_PLACE
145cc_library(
146    name = "must_place",
147    hdrs = ["public/pw_build/must_place.ld.h"],
148    strip_include_prefix = "public",
149)
150
151cc_library(
152    name = "linker_symbol",
153    hdrs = ["public/pw_build/linker_symbol.h"],
154    strip_include_prefix = "public",
155    deps = [
156        "//third_party/fuchsia:stdcompat",
157    ],
158)
159
160pw_cc_test(
161    name = "linker_symbol_test",
162    srcs = ["linker_symbol_test.cc"],
163    linkopts = [
164        "-T$(location linker_symbol_test.ld)",
165    ],
166    target_compatible_with = [
167        "@platforms//os:linux",
168    ],
169    deps = [
170        ":linker_symbol",
171        ":linker_symbol_test.ld",
172    ],
173)
174
175pw_string_list_comparison_test(
176    name = "simple_glob",
177    actual = glob_dirs(
178        ["test_data/**"],
179        exclude = ["test_data/pw_copy_and_patch_file/**"],
180    ),
181    expected = [
182        "test_data",
183        "test_data/glob_dirs",
184        "test_data/glob_dirs/nested_1",
185        "test_data/glob_dirs/nested_1/foo",
186        "test_data/glob_dirs/nested_2",
187    ],
188)
189
190pw_string_comparison_test(
191    name = "single_match",
192    actual = match_dir(["test_data/glob_dirs/nested_1"]),
193    expected = "test_data/glob_dirs/nested_1",
194)
195
196pw_string_comparison_test(
197    name = "no_match_single",
198    actual = match_dir(["test_data/glob_dirs/nested_1/nope"]),
199    expected = None,
200)
201
202pw_string_comparison_test(
203    name = "no_match_single_fail",
204    actual = match_dir_internal(
205        ["test_data/glob_dirs/**/*.txt"],
206        _fail_callback = return_error,
207        allow_empty = False,
208    ),
209    expected = "glob pattern [\"test_data/glob_dirs/**/*.txt\"] didn't match anything, but allow_empty is set to False",
210)
211
212pw_string_comparison_test(
213    name = "bad_match_multiple",
214    actual = match_dir_internal(
215        ["test_data/glob_dirs/**"],
216        _fail_callback = return_error,
217        allow_empty = False,
218    ),
219    expected = "glob pattern [\"test_data/glob_dirs/**\"] matches multiple directories when only one was requested: [\"test_data/glob_dirs\", \"test_data/glob_dirs/nested_1\", \"test_data/glob_dirs/nested_1/foo\", \"test_data/glob_dirs/nested_2\"]",
220)
221
222pw_string_comparison_test(
223    name = "match_exclusions",
224    actual = match_dir(
225        ["test_data/glob_dirs/**"],
226        allow_empty = False,
227        exclude = ["test_data/glob_dirs/*/**"],
228    ),
229    expected = "test_data/glob_dirs",
230)
231
232pw_string_list_comparison_test(
233    name = "single_match_glob",
234    actual = glob_dirs(["test_data/glob_dirs/nested_1"]),
235    expected = [
236        "test_data/glob_dirs/nested_1",
237    ],
238)
239
240pw_string_list_comparison_test(
241    name = "partial_match_glob",
242    actual = glob_dirs(["test_data/glob_dirs/nested_*"]),
243    expected = [
244        "test_data/glob_dirs/nested_1",
245        "test_data/glob_dirs/nested_2",
246    ],
247)
248
249pw_string_list_comparison_test(
250    name = "nested_match_glob",
251    actual = glob_dirs(["test_data/glob_dirs/*/foo"]),
252    expected = [
253        "test_data/glob_dirs/nested_1/foo",
254    ],
255)
256
257pw_string_list_comparison_test(
258    name = "no_match_glob",
259    actual = glob_dirs(["test_data/glob_dirs/others/**"]),
260    expected = [
261    ],
262)
263
264pw_copy_and_patch_file_test(
265    name = "patch_file_basic",
266    src = "test_data/pw_copy_and_patch_file/basic.txt",
267    out = "test_data/pw_copy_and_patch_file/patched_basic.txt",
268    expected = "test_data/pw_copy_and_patch_file/basic.expected",
269    patch_file = "test_data/pw_copy_and_patch_file/basic.patch",
270)
271
272filegroup(
273    name = "doxygen",
274    srcs = [
275        "public/pw_build/linker_symbol.h",
276        "public/pw_build/must_place.ld.h",
277    ],
278)
279