xref: /aosp_15_r20/external/pigweed/pw_assert/BUILD.gn (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 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
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/facade.gni")
18import("$dir_pw_build/module_config.gni")
19import("$dir_pw_docgen/docs.gni")
20import("$dir_pw_unit_test/test.gni")
21import("backend.gni")
22
23declare_args() {
24  # The build target that overrides the default configuration options for this
25  # module. This should point to a source set that provides defines through a
26  # public config (which may -include a file or add defines directly).
27  pw_assert_CONFIG = pw_build_DEFAULT_MODULE_CONFIG
28}
29
30config("public_include_path") {
31  include_dirs = [ "public" ]
32  visibility = [ ":*" ]
33}
34
35config("assert_backend_overrides") {
36  include_dirs = [ "assert_compatibility_public_overrides" ]
37  visibility = [ ":*" ]
38}
39
40config("libc_assert_overrides") {
41  include_dirs = [ "libc_assert_public_overrides" ]
42  visibility = [ ":*" ]
43}
44
45config("print_and_abort_check_backend_overrides") {
46  include_dirs = [ "print_and_abort_check_public_overrides" ]
47  visibility = [ ":*" ]
48}
49
50config("print_and_abort_assert_backend_overrides") {
51  include_dirs = [ "print_and_abort_assert_public_overrides" ]
52  visibility = [ ":*" ]
53}
54
55pw_source_set("config") {
56  public = [ "public/pw_assert/config.h" ]
57  public_configs = [ ":public_include_path" ]
58  public_deps = [ pw_assert_CONFIG ]
59}
60
61# Depending on dir_pw_assert provides both assert and check.
62group("pw_assert") {
63  public_deps = [
64    ":assert",
65    ":check",
66    ":config",
67  ]
68}
69
70# Wrap :pw_assert with facade-style targets, so it can be used as if it were
71# created with pw_facade.
72group("facade") {
73  public_deps = [
74    ":assert",
75    ":check.facade",
76  ]
77}
78
79group("pw_assert.facade") {
80  public_deps = [ ":facade" ]
81}
82
83# Provides the rich PW_CHECK macros.
84pw_facade("check") {
85  backend = pw_assert_BACKEND
86  public_configs = [ ":public_include_path" ]
87  public = [
88    "public/pw_assert/check.h",
89    "public/pw_assert/internal/check_impl.h",
90    "public/pw_assert/short.h",
91  ]
92  public_deps = [
93    ":config",
94    dir_pw_preprocessor,
95  ]
96
97  require_link_deps = [ ":impl" ]
98}
99
100# Provide "pw_assert/assert.h" in its own source set, so it can be used without
101# depending on pw_assert_BACKEND.
102pw_facade("assert") {
103  backend = pw_assert_LITE_BACKEND
104  public_configs = [ ":public_include_path" ]
105  public = [ "public/pw_assert/assert.h" ]
106  public_deps = [ ":config" ]
107}
108
109# This backend to pw_assert's PW_ASSERT()/PW_DASSERT() macros provides backwards
110# compatibility with pw_assert's previous C-symbol based API.
111#
112# Warning: The assert facade is in a transitional state, and this target is
113# likely to be removed as the pw_assert API is reassessed. (b/235149326)
114pw_source_set("assert_compatibility_backend") {
115  public_configs = [ ":assert_backend_overrides" ]
116  public_deps = [ dir_pw_preprocessor ]
117  public = [
118    "assert_compatibility_public_overrides/pw_assert_backend/assert_backend.h",
119  ]
120}
121
122group("assert_compatibility_backend.impl") {
123}
124
125pw_source_set("libc_assert") {
126  public_configs = [ ":public_include_path" ]
127  public = [
128    "libc_assert_public_overrides/assert.h",
129    "libc_assert_public_overrides/cassert",
130    "public/pw_assert/internal/libc_assert.h",
131  ]
132  public_deps = [
133    ":assert",
134    dir_pw_preprocessor,
135  ]
136}
137
138pw_source_set("print_and_abort") {
139  public_configs = [ ":public_include_path" ]
140  public_deps = [ ":config" ]
141  public = [ "public/pw_assert/internal/print_and_abort.h" ]
142  visibility = [ ":*" ]
143}
144
145# This backend to pw_assert's PW_CHECK()/PW_DCHECK() macros prints the assert
146# expression, evaluated expression, file/line number, function, and user message
147# with printf, then aborts. It is intended for use with host builds.
148pw_source_set("print_and_abort_check_backend") {
149  public_configs = [ ":print_and_abort_check_backend_overrides" ]
150  public_deps = [ ":print_and_abort" ]
151  public = [
152    "print_and_abort_check_public_overrides/pw_assert_backend/check_backend.h",
153  ]
154}
155
156group("print_and_abort_check_backend.impl") {
157}
158
159# This backend to pw_assert's PW_ASSERT()/PW_DASSERT() macros prints the assert
160# expression, file/line number, and function with printf, then aborts. It is
161# intended for use with host builds.
162pw_source_set("print_and_abort_assert_backend") {
163  public_configs = [ ":print_and_abort_assert_backend_overrides" ]
164  public_deps = [
165    ":config",
166    ":print_and_abort",
167  ]
168  public = [ "print_and_abort_assert_public_overrides/pw_assert_backend/assert_backend.h" ]
169}
170
171group("print_and_abort_assert_backend.impl") {
172}
173
174# pw_assert is low-level and ubiquitous. Because of this, it can often cause
175# circular dependencies. This target collects dependencies from the backend that
176# cannot be used because they would cause circular deps.
177#
178# This group ("$dir_pw_assert:impl") must listed in pw_build_LINK_DEPS if
179# pw_assert_BACKEND is set.
180#
181# pw_assert backends must provide their own "impl" target that collects their
182# actual dependencies. The backend "impl" group may be empty if everything can
183# go directly in the backend target without causing circular dependencies.
184group("impl") {
185  public_deps = []
186
187  if (pw_assert_BACKEND != "") {
188    public_deps +=
189        [ get_label_info(pw_assert_BACKEND, "label_no_toolchain") + ".impl" ]
190  }
191  if (pw_assert_LITE_BACKEND != "") {
192    public_deps += [ get_label_info(pw_assert_LITE_BACKEND,
193                                    "label_no_toolchain") + ".impl" ]
194  }
195}
196
197# Note: While this is technically a test, doesn't verify any of the output and
198# is more of a compile test. The results can be visually verified if desired.
199pw_test("assert_test") {
200  configs = [ ":public_include_path" ]
201  sources = [ "assert_test.cc" ]
202  deps = [
203    ":pw_assert",
204    dir_pw_status,
205  ]
206}
207
208pw_test_group("tests") {
209  tests = [
210    ":assert_test",
211    ":assert_backend_compile_test",
212    ":assert_facade_test",
213  ]
214}
215
216# The assert facade test doesn't require a backend since a fake one is
217# provided. However, since this doesn't depend on the backend it re-includes
218# the facade headers.
219pw_test("assert_facade_test") {
220  configs = [ ":public_include_path" ]  # For internal/check_impl.h
221  sources = [
222    "assert_facade_test.cc",
223    "fake_backend.cc",
224    "public/pw_assert/internal/check_impl.h",
225    "pw_assert_test/fake_backend.h",
226  ]
227  deps = [
228    ":pw_assert",
229    dir_pw_result,
230    dir_pw_span,
231    dir_pw_status,
232    dir_pw_string,
233  ]
234  negative_compilation_tests = true
235
236  # TODO(frolv): Fix this test on the QEMU target.
237  enable_if = pw_build_EXECUTABLE_TARGET_TYPE != "lm3s6965evb_executable"
238}
239
240pw_test("assert_backend_compile_test") {
241  enable_if = pw_assert_BACKEND != ""
242  deps = [
243    ":pw_assert",
244    dir_pw_status,
245    pw_assert_BACKEND,
246  ]
247  sources = [
248    "assert_backend_compile_test.cc",
249    "assert_backend_compile_test_c.c",
250  ]
251}
252
253pw_doc_group("docs") {
254  sources = [
255    "backends.rst",
256    "docs.rst",
257  ]
258}
259