Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / build / chip / chip_test_suite.gni
1 # Copyright (c) 2020 Project CHIP Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import("//build_overrides/build.gni")
16 import("//build_overrides/chip.gni")
17
18 import("${chip_root}/build/chip/chip_test.gni")
19 import("${chip_root}/build/chip/tests.gni")
20 import("${dir_pw_unit_test}/test.gni")
21
22 assert(chip_build_tests)
23
24 # Define CHIP unit tests
25 #
26 # Simple usage
27 # chip_test_suite("tests") {
28 #   output_name = "libFooTests"
29 #
30 #   sources = [
31 #      "Common.h",      # add common sources here
32 #      "Common.cpp",
33 #   ]
34 #
35 #   test_sources = [
36 #     "TestFoo.cpp",    # Files are parsed for `CHIP_REGISTER_TEST_SUITE(...)`
37 #     "TestBar.cpp",    # and a driver is created automatically
38 #   ]
39 #
40 #   public_deps = [
41 #     "${chip_root}/src/lib/foo",         # add dependencies here
42 #     "${nlunit_test_root}:nlunit-test",
43 #   ]
44 # }
45 #
46 #
47 # Deprecated usage (writing own driver files):
48 #
49 # chip_test_suite("tests") {
50 #   output_name = "libFooTests"
51 #
52 #   sources = [
53 #     "TestDeclarations.h",
54 #     "TestFoo.cpp",
55 #     "TestBar.cpp",
56 #   ]
57 #
58 #   public_deps = [
59 #     "${chip_root}/src/lib/foo",         # add dependencies here
60 #     "${nlunit_test_root}:nlunit-test",
61 #   ]
62 #
63 #   tests = [
64 #     "TestFoo",  # Assumes TestFooDriver.cpp exists
65 #     "TestBar",  # Assumes TestBarDriver.cpp exists
66 #   ]
67 # }
68
69 #
70 template("chip_test_suite") {
71   _suite_name = target_name
72
73   # Ensures that the common library has sources containing both common
74   # and individual unit tests.
75   if (!defined(invoker.sources)) {
76     invoker.sources = []
77   }
78
79   if (defined(invoker.test_sources)) {
80     invoker.sources += invoker.test_sources
81   }
82
83   static_library("${_suite_name}_lib") {
84     forward_variables_from(invoker, "*", [ "tests" ])
85
86     output_dir = "${root_out_dir}/lib"
87
88     if (!defined(invoker.public_deps)) {
89       public_deps = []
90     }
91
92     # TODO: figure out a way to auto-define dependency on stdio logging.
93     #       This dep is required since libSupportLayer now does not include
94     #       a default implementation.
95     #
96     #       Tests such as TestInetEndPoint need to exercise the system event
97     #       loop however they do not seem to include a logging binding so this
98     #       is forcefully added here.
99     if (current_os != "zephyr") {
100       public_deps += [ "${chip_root}/src/platform/logging:stdio" ]
101     }
102   }
103
104   if (chip_link_tests) {
105     tests = []
106
107     if (defined(invoker.test_sources)) {
108       foreach(_test, invoker.test_sources) {
109         _test_name = string_replace(_test, ".cpp", "")
110
111         _driver_name = "${root_gen_dir}/${_test_name}.driver.cpp"
112
113         action("${_test_name}_generate_driver") {
114           script = "${chip_root}/scripts/gen_test_driver.py"
115
116           inputs = [ _test ]
117           outputs = [ _driver_name ]
118           args = [
119             "--input_file=" + rebase_path(_test, root_build_dir),
120             "--output_file=" + rebase_path(_driver_name, root_build_dir),
121           ]
122         }
123
124         chip_test(_test_name) {
125           sources = [ _driver_name ]
126           public_deps = [
127             ":${_suite_name}_lib",
128             ":${_test_name}_generate_driver",
129           ]
130         }
131
132         tests += [ _test_name ]
133       }
134     }
135
136     if (defined(invoker.tests)) {
137       foreach(_test, invoker.tests) {
138         chip_test(_test) {
139           sources = [ "${_test}Driver.cpp" ]
140
141           public_deps = [ ":${_suite_name}_lib" ]
142         }
143
144         tests += [ _test ]
145       }
146     }
147
148     group(_suite_name) {
149       deps = []
150       foreach(_test, tests) {
151         deps += [ ":${_test}" ]
152       }
153     }
154
155     group("${_suite_name}_run") {
156       deps = []
157       foreach(_test, tests) {
158         deps += [ ":${_test}_run" ]
159       }
160     }
161   } else {
162     group(_suite_name) {
163       deps = [ ":${_suite_name}_lib" ]
164     }
165   }
166 }