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