xref: /aosp_15_r20/external/pigweed/pw_thread/BUILD.bazel (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2020 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_python//python:proto.bzl", "py_proto_library")
16load("//pw_build:compatibility.bzl", "host_backend_alias")
17load("//pw_build:pw_facade.bzl", "pw_facade")
18load(
19    "//pw_protobuf_compiler:pw_proto_library.bzl",
20    "pwpb_proto_library",
21    "raw_rpc_proto_library",
22)
23load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
24
25package(default_visibility = ["//visibility:public"])
26
27licenses(["notice"])
28
29pw_facade(
30    name = "id",
31    hdrs = [
32        "public/pw_thread/id.h",
33    ],
34    backend = ":id_backend",
35    strip_include_prefix = "public",
36)
37
38label_flag(
39    name = "id_backend",
40    build_setting_default = ":id_unspecified_backend",
41)
42
43host_backend_alias(
44    name = "id_unspecified_backend",
45    backend = "//pw_thread_stl:id",
46)
47
48cc_library(
49    name = "config",
50    hdrs = ["public/pw_thread/config.h"],
51    strip_include_prefix = "public",
52    deps = [":config_override"],
53)
54
55label_flag(
56    name = "config_override",
57    build_setting_default = "//pw_build:default_module_config",
58)
59
60cc_library(
61    name = "thread_info",
62    hdrs = ["public/pw_thread/thread_info.h"],
63    strip_include_prefix = "public",
64    deps = ["//pw_span"],
65)
66
67pw_facade(
68    name = "thread_iteration",
69    hdrs = [
70        "public/pw_thread/thread_iteration.h",
71    ],
72    backend = ":iteration_backend",
73    strip_include_prefix = "public",
74    deps = [
75        ":thread_info",
76        "//pw_function",
77        "//pw_status",
78    ],
79)
80
81label_flag(
82    name = "iteration_backend",
83    build_setting_default = ":iteration_unspecified_backend",
84)
85
86host_backend_alias(
87    name = "iteration_unspecified_backend",
88    backend = "//pw_thread_stl:thread_iteration",
89)
90
91pw_facade(
92    name = "sleep",
93    srcs = [
94        "sleep.cc",
95    ],
96    hdrs = [
97        "public/pw_thread/sleep.h",
98    ],
99    backend = ":sleep_backend",
100    implementation_deps = [
101        ":thread",
102    ],
103    strip_include_prefix = "public",
104    deps = [
105        "//pw_chrono:system_clock",
106        "//pw_preprocessor",
107    ],
108)
109
110label_flag(
111    name = "sleep_backend",
112    build_setting_default = ":sleep_unspecified_backend",
113)
114
115host_backend_alias(
116    name = "sleep_unspecified_backend",
117    backend = "//pw_thread_stl:sleep",
118)
119
120pw_facade(
121    name = "thread",
122    srcs = [
123        "public/pw_thread/config.h",
124        "thread.cc",
125    ],
126    hdrs = [
127        "public/pw_thread/detached_thread.h",
128        "public/pw_thread/thread.h",
129    ],
130    backend = ":thread_backend",
131    strip_include_prefix = "public",
132    deps = [
133        ":id",
134        ":options",
135        ":thread_core",
136        "//pw_function",
137    ],
138)
139
140cc_library(
141    name = "options",
142    hdrs = ["public/pw_thread/options.h"],
143    strip_include_prefix = "public",
144)
145
146label_flag(
147    name = "thread_backend",
148    build_setting_default = ":thread_unspecified_backend",
149)
150
151host_backend_alias(
152    name = "thread_unspecified_backend",
153    backend = "//pw_thread_stl:thread",
154)
155
156cc_library(
157    name = "thread_core",
158    hdrs = [
159        "public/pw_thread/thread_core.h",
160    ],
161    strip_include_prefix = "public",
162    deps = [
163        "//pw_log",
164        "//pw_status",
165    ],
166)
167
168cc_library(
169    name = "thread_snapshot_service",
170    srcs = [
171        "pw_thread_private/thread_snapshot_service.h",
172        "thread_snapshot_service.cc",
173    ],
174    hdrs = ["public/pw_thread/thread_snapshot_service.h"],
175    strip_include_prefix = "public",
176    deps = [
177        "//pw_protobuf",
178        "//pw_rpc/raw:server_api",
179        "//pw_span",
180        "//pw_status",
181        ":config",
182        ":thread_pwpb",
183        ":thread_info",
184        ":thread_iteration",
185        ":thread_snapshot_service_pwpb",
186        ":thread_snapshot_service_raw_rpc",
187        # TODO(amontanez): This should depend on FreeRTOS but our third parties
188        # currently do not have Bazel support.
189    ],
190)
191
192pw_facade(
193    name = "test_thread_context",
194    hdrs = [
195        "public/pw_thread/test_thread_context.h",
196    ],
197    backend = ":test_thread_context_backend",
198    strip_include_prefix = "public",
199)
200
201label_flag(
202    name = "test_thread_context_backend",
203    build_setting_default = ":test_thread_context_unspecified_backend",
204)
205
206host_backend_alias(
207    name = "test_thread_context_unspecified_backend",
208    backend = "//pw_thread_stl:test_thread_context",
209)
210
211pw_facade(
212    name = "yield",
213    srcs = [
214        "yield.cc",
215    ],
216    hdrs = [
217        "public/pw_thread/yield.h",
218    ],
219    backend = ":yield_backend",
220    implementation_deps = [
221        ":thread",
222    ],
223    strip_include_prefix = "public",
224    deps = [
225        "//pw_preprocessor",
226    ],
227)
228
229label_flag(
230    name = "yield_backend",
231    build_setting_default = ":yield_unspecified_backend",
232)
233
234host_backend_alias(
235    name = "yield_unspecified_backend",
236    backend = "//pw_thread_stl:yield",
237)
238
239cc_library(
240    name = "snapshot",
241    srcs = [
242        "snapshot.cc",
243    ],
244    hdrs = [
245        "public/pw_thread/snapshot.h",
246    ],
247    strip_include_prefix = "public",
248    deps = [
249        ":config",
250        ":thread",
251        ":thread_pwpb",
252        "//pw_bytes",
253        "//pw_function",
254        "//pw_log",
255        "//pw_protobuf",
256        "//pw_status",
257    ],
258)
259
260cc_library(
261    name = "non_portable_test_thread_options",
262    hdrs = [
263        "public/pw_thread/non_portable_test_thread_options.h",
264    ],
265    strip_include_prefix = "public",
266    deps = [
267        ":thread",
268    ],
269)
270
271# To instantiate this as a pw_cc_test, depend on this pw_cc_library and the
272# pw_cc_library which implements the backend for non_portable_test_thread_options. See
273# //pw_thread_stl:thread_backend_test as an example.
274cc_library(
275    name = "thread_facade_test",
276    testonly = True,
277    srcs = [
278        "thread_facade_test.cc",
279    ],
280    deps = [
281        ":non_portable_test_thread_options",
282        ":thread",
283        "//pw_chrono:system_clock",
284        "//pw_sync:binary_semaphore",
285        "//pw_unit_test",
286    ],
287    alwayslink = True,
288)
289
290pw_cc_test(
291    name = "test_thread_context_facade_test",
292    srcs = [
293        "test_thread_context_facade_test.cc",
294    ],
295    # TODO: b/317922402 - On Windows, this test can easily hang indefinitely.
296    # Disable on Windows until we can test with the native Windows SDK libraries
297    # for threading.
298    # TODO: b/361369192 - This test behaves unusually on rp2.
299    target_compatible_with = select({
300        "@pico-sdk//bazel/constraint:rp2040": ["@platforms//:incompatible"],
301        "@pico-sdk//bazel/constraint:rp2350": ["@platforms//:incompatible"],
302        "@platforms//os:windows": ["@platforms//:incompatible"],
303        "//conditions:default": [],
304    }),
305    deps = [
306        ":test_thread_context",
307        ":thread",
308        "//pw_sync:binary_semaphore",
309        "//pw_unit_test",
310    ],
311)
312
313pw_cc_test(
314    name = "id_facade_test",
315    srcs = [
316        "id_facade_test.cc",
317    ],
318    deps = [
319        ":thread",
320        "//pw_unit_test",
321    ],
322)
323
324pw_cc_test(
325    name = "options_test",
326    srcs = ["options_test.cc"],
327    deps = [
328        ":options",
329        "//pw_compilation_testing:negative_compilation_testing",
330    ],
331)
332
333pw_cc_test(
334    name = "sleep_facade_test",
335    srcs = [
336        "sleep_facade_test.cc",
337        "sleep_facade_test_c.c",
338    ],
339    deps = [
340        ":sleep",
341        ":thread",
342        "//pw_chrono:system_clock",
343        "//pw_preprocessor",
344        "//pw_unit_test",
345    ],
346)
347
348pw_cc_test(
349    name = "thread_info_test",
350    srcs = [
351        "thread_info_test.cc",
352    ],
353    deps = [
354        ":thread_info",
355        "//pw_span",
356    ],
357)
358
359pw_cc_test(
360    name = "thread_snapshot_service_test",
361    srcs = [
362        "pw_thread_private/thread_snapshot_service.h",
363        "thread_snapshot_service_test.cc",
364    ],
365    # TODO: https://pwbug.dev/342662853 - This test fails under ASAN.
366    tags = ["noasan"],
367    deps = [
368        ":thread_info",
369        ":thread_iteration",
370        ":thread_pwpb",
371        ":thread_snapshot_service",
372        ":thread_snapshot_service_pwpb",
373        "//pw_protobuf",
374        "//pw_span",
375        "//pw_sync:thread_notification",
376    ],
377)
378
379pw_cc_test(
380    name = "yield_facade_test",
381    srcs = [
382        "yield_facade_test.cc",
383        "yield_facade_test_c.c",
384    ],
385    deps = [
386        ":thread",
387        ":yield",
388        "//pw_preprocessor",
389        "//pw_unit_test",
390    ],
391)
392
393proto_library(
394    name = "thread_proto",
395    srcs = ["pw_thread_protos/thread.proto"],
396    strip_import_prefix = "/pw_thread",
397    deps = [
398        "//pw_tokenizer:tokenizer_proto",
399    ],
400)
401
402py_proto_library(
403    name = "thread_proto_py_pb2",
404    deps = [":thread_proto"],
405)
406
407proto_library(
408    name = "thread_snapshot_service_proto",
409    srcs = ["pw_thread_protos/thread_snapshot_service.proto"],
410    strip_import_prefix = "/pw_thread",
411    deps = [
412        ":thread_proto",
413    ],
414)
415
416pwpb_proto_library(
417    name = "thread_snapshot_service_pwpb",
418    deps = [":thread_snapshot_service_proto"],
419)
420
421raw_rpc_proto_library(
422    name = "thread_snapshot_service_raw_rpc",
423    deps = [":thread_snapshot_service_proto"],
424)
425
426py_proto_library(
427    name = "thread_snapshot_service_py_pb2",
428    deps = [":thread_snapshot_service_proto"],
429)
430
431pwpb_proto_library(
432    name = "thread_pwpb",
433    deps = [":thread_proto"],
434)
435
436filegroup(
437    name = "doxygen",
438    srcs = [
439        "public/pw_thread/options.h",
440        "public/pw_thread/test_thread_context.h",
441        "public/pw_thread/thread.h",
442    ],
443)
444