blob: 0362da6287acda67cb801498c1db8f4bddfcd285 [file] [log] [blame]
Wyatt Hepler3d0e3152021-04-29 17:08:31 -07001# Copyright 2021 The Pigweed Authors
Keir Mierle3cee8792020-01-22 17:08:13 -08002#
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
Armando Montanezfb3d3fb2020-06-09 18:12:12 -070015import("//build_overrides/pigweed.gni")
16
Keir Mierle3cee8792020-01-22 17:08:13 -080017import("$dir_pw_build/facade.gni")
18import("$dir_pw_docgen/docs.gni")
19import("$dir_pw_unit_test/test.gni")
Wyatt Heplerd49f8fe2020-10-15 10:13:47 -070020
Alexei Frolov4c0428a2020-06-10 10:46:04 -070021declare_args() {
22 # Backend for the pw_assert module.
23 pw_assert_BACKEND = ""
24}
25
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070026config("public_include_path") {
Keir Mierle3cee8792020-01-22 17:08:13 -080027 include_dirs = [ "public" ]
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070028 visibility = [ ":*" ]
Keir Mierle3cee8792020-01-22 17:08:13 -080029}
30
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070031# Depending on dir_pw_assert provides both assert and check.
32group("pw_assert") {
33 public_deps = [
34 ":assert",
35 ":check",
36 ]
37}
38
39# Wrap :pw_assert with facade-style targets, so it can be used as if it were
40# created with pw_facade.
41group("facade") {
42 public_deps = [
43 ":assert",
44 ":check.facade",
45 ]
46}
47
48group("pw_assert.facade") {
49 public_deps = [ ":facade" ]
50}
51
52# Provides the rich PW_CHECK macros.
53pw_facade("check") {
Alexei Frolov4c0428a2020-06-10 10:46:04 -070054 backend = pw_assert_BACKEND
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070055 public_configs = [ ":public_include_path" ]
Keir Mierled1211ed2020-04-03 13:20:46 -070056 public = [
Wyatt Heplera59998f2021-03-19 14:35:10 -070057 "public/pw_assert/check.h",
58 "public/pw_assert/internal/check_impl.h",
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070059 "public/pw_assert/options.h",
Wyatt Heplera59998f2021-03-19 14:35:10 -070060 "public/pw_assert/short.h",
Keir Mierled1211ed2020-04-03 13:20:46 -070061 ]
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070062 public_deps = [ dir_pw_preprocessor ]
Keir Mierle854adec2020-09-03 14:07:19 -070063}
64
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070065# Provide "pw_assert/assert.h" in its own source set, so it can be used without
66# depending on pw_assert_BACKEND. This makes it possible to use PW_ASSERT in
67# situations that might result in circular dependencies, such as in low-level
68# headers like polyfill or span. See the docs for more discussion around where
69# to use which assert headers.
Keir Mierle854adec2020-09-03 14:07:19 -070070#
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070071# The implementation function pw_assert_HandleFailure() must be provided at link
72# time. It is linked by the pw_assert:pw_assert (or pw_assert:check) targets.
73pw_source_set("assert") {
74 public_configs = [ ":public_include_path" ]
Keir Mierle854adec2020-09-03 14:07:19 -070075 public = [
Wyatt Heplera59998f2021-03-19 14:35:10 -070076 "public/pw_assert/assert.h",
Keir Mierle854adec2020-09-03 14:07:19 -070077
78 # Needed for PW_ASSERT_ENABLE_DEBUG. Note that depending on :pw_assert to
79 # get options.h won't work here since it will trigger the circular include
Wyatt Hepler3d0e3152021-04-29 17:08:31 -070080 # problem that the :assert target is intended solve.
Keir Mierle854adec2020-09-03 14:07:19 -070081 "public/pw_assert/options.h",
82 ]
Wyatt Heplerdd3e8812020-09-29 11:14:28 -070083 public_deps = [ dir_pw_preprocessor ]
Keir Mierle854adec2020-09-03 14:07:19 -070084}
85
Wyatt Hepler8bd4fb02021-05-03 15:30:58 -070086# pw_assert is low-level and ubiquitous. Because of this, it can often cause
87# circular dependencies. This target collects dependencies from the backend that
88# cannot be used because they would cause circular deps.
89#
90# This group ("$dir_pw_assert:deps") must listed in pw_build_LINK_DEPS if
91# pw_assert_BACKEND is set.
92#
93# pw_assert backends must provide their own "deps" group that collects their
94# actual dependencies. The backend "deps" group may be empty.
95group("deps") {
96 public_deps = []
97
98 if (pw_assert_BACKEND != "") {
99 public_deps += [ get_label_info(pw_assert_BACKEND, "dir") + ":deps" ]
100
101 # Make sure this target is listed in pw_build_LINK_DEPS. This
102 # ensures these dependencies are available during linking, even if nothing
103 # else in the build depends on them.
104 _deps_label = get_label_info(":$target_name", "label_no_toolchain")
105 _deps_is_in_link_dependencies = false
106
107 foreach(label, pw_build_LINK_DEPS) {
108 if (get_label_info(label, "label_no_toolchain") == _deps_label) {
109 _deps_is_in_link_dependencies = true
110 }
111 }
112
113 # TODO(pwbug/372): Update projects with pw_assert to link the :deps target.
114 _disable_this_check_for_now = true
115 not_needed([
116 "_deps_is_in_link_dependencies",
117 "_deps_label",
118 ])
119
120 assert(_disable_this_check_for_now || _deps_is_in_link_dependencies,
121 "\$dir_pw_assert:$target_name must be listed in " +
122 "pw_build_LINK_DEPS when pw_assert_BACKEND is set")
123 }
124}
125
Keir Mierle854adec2020-09-03 14:07:19 -0700126# Note: While this is technically a test, doesn't verify any of the output and
127# is more of a compile test. The results can be visually verified if desired.
Wyatt Hepler3d0e3152021-04-29 17:08:31 -0700128pw_test("assert_test") {
129 configs = [ ":public_include_path" ]
130 sources = [ "assert_test.cc" ]
Keir Mierle854adec2020-09-03 14:07:19 -0700131 deps = [ ":pw_assert" ]
Keir Mierle3cee8792020-01-22 17:08:13 -0800132}
133
134pw_test_group("tests") {
Armando Montanezceb66f52020-06-04 12:56:38 -0700135 tests = [
Wyatt Hepler3d0e3152021-04-29 17:08:31 -0700136 ":assert_test",
Armando Montanezceb66f52020-06-04 12:56:38 -0700137 ":assert_backend_compile_test",
138 ":assert_facade_test",
139 ]
Keir Mierle3cee8792020-01-22 17:08:13 -0800140}
141
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700142# The assert facade test doesn't require a backend since a fake one is
Keir Mierle854adec2020-09-03 14:07:19 -0700143# provided. However, since this doesn't depend on the backend it re-includes
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700144# the facade headers.
145pw_test("assert_facade_test") {
Wyatt Hepler3d0e3152021-04-29 17:08:31 -0700146 configs = [ ":public_include_path" ] # For internal/assert_impl.h
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700147 sources = [
148 "assert_facade_test.cc",
149 "fake_backend.cc",
Wyatt Heplera59998f2021-03-19 14:35:10 -0700150 "public/pw_assert/internal/check_impl.h",
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700151 "pw_assert_test/fake_backend.h",
152 ]
Keir Mierle854adec2020-09-03 14:07:19 -0700153 deps = [
Wyatt Hepler3d0e3152021-04-29 17:08:31 -0700154 ":assert",
Keir Mierle854adec2020-09-03 14:07:19 -0700155 dir_pw_status,
156 ]
Alexei Frolov844ff0f2020-05-06 12:15:29 -0700157
158 # TODO(frolv): Fix this test on the QEMU target.
Alexei Frolov4c0428a2020-06-10 10:46:04 -0700159 enable_if = pw_build_EXECUTABLE_TARGET_TYPE != "lm3s6965evb_executable"
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700160}
161
Armando Montanezceb66f52020-06-04 12:56:38 -0700162pw_test("assert_backend_compile_test") {
Alexei Frolov4c0428a2020-06-10 10:46:04 -0700163 enable_if = pw_assert_BACKEND != ""
Armando Montanezceb66f52020-06-04 12:56:38 -0700164 deps = [
165 ":pw_assert",
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700166 dir_pw_status,
Alexei Frolov4c0428a2020-06-10 10:46:04 -0700167 pw_assert_BACKEND,
Armando Montanezceb66f52020-06-04 12:56:38 -0700168 ]
169 sources = [
Armando Montanezceb66f52020-06-04 12:56:38 -0700170 "assert_backend_compile_test.cc",
Michael Spanga99220e2020-06-11 20:07:16 -0400171 "assert_backend_compile_test_c.c",
Armando Montanezceb66f52020-06-04 12:56:38 -0700172 ]
Keir Mierle3cee8792020-01-22 17:08:13 -0800173}
174
175pw_doc_group("docs") {
Rob Mohra0ba54f2020-02-27 11:43:49 -0800176 sources = [ "docs.rst" ]
Keir Mierle3cee8792020-01-22 17:08:13 -0800177}