xref: /aosp_15_r20/external/executorch/third-party/gtest_defs.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copied from fbsource/third-party/googletest
2
3COMPILER_FLAGS = [
4    "-std=c++17",
5]
6COMPILER_FLAGS_ATEN = [
7    "-std=c++17",
8    "-D_GLIBCXX_USE_CXX11_ABI=0",  # `libtorch` is built without CXX11_ABI so gtest needs to be compiled in the same way
9]
10
11# define_gtest_targets
12def define_gtest_targets():
13    # Library that defines the FRIEND_TEST macro.
14    # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
15    native.cxx_library(
16        name = "gtest_prod",
17        public_system_include_directories = ["googletest/googletest/include"],
18        raw_headers = ["googletest/googletest/include/gtest/gtest_prod.h"],
19        visibility = ["PUBLIC"],
20    )
21
22    for aten_mode in (True, False):
23        aten_suffix = "_aten" if aten_mode else ""
24
25        # # Google Test
26        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
27        native.cxx_library(
28            name = "gtest" + aten_suffix,
29            srcs = native.glob(
30                [
31                    "googletest/googletest/src/*.cc",
32                ],
33                exclude = [
34                    "googletest/googletest/src/gtest-all.cc",
35                    "googletest/googletest/src/gtest_main.cc",
36                ],
37            ),
38            include_directories = [
39                "googletest/googletest",
40            ],
41            public_system_include_directories = [
42                "googletest/googletest/include",
43            ],
44            raw_headers = native.glob([
45                "googletest/googletest/include/gtest/**/*.h",
46                "googletest/googletest/src/*.h",
47            ]),
48            visibility = ["PUBLIC"],
49            compiler_flags = COMPILER_FLAGS_ATEN if aten_mode else COMPILER_FLAGS,
50            # TODO: gtest crashes after the test finishes when pthread is used, the root
51            # cause is unclear. So it's turned off here for now. The error is as follows:
52            # googletest/include/gtest/internal/gtest-port.h:1771:: pthread_key_delete(key_)failed with error 22
53            exported_preprocessor_flags = ["-DGTEST_HAS_PTHREAD=0"],
54        )
55
56        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
57        native.cxx_library(
58            name = "gmock" + aten_suffix,
59            srcs = native.glob(
60                [
61                    "googletest/googlemock/src/*.cc",
62                ],
63                exclude = [
64                    "googletest/googlemock/src/gmock-all.cc",
65                    "googletest/googlemock/src/gmock_main.cc",
66                ],
67            ),
68            include_directories = [
69                "googletest/googletest",
70            ],
71            public_system_include_directories = [
72                "googletest/googlemock/include",
73            ],
74            raw_headers = native.glob([
75                "googletest/googlemock/include/gmock/**/*.h",
76            ]),
77            exported_deps = [":gtest" + aten_suffix],
78            visibility = ["PUBLIC"],
79            compiler_flags = COMPILER_FLAGS_ATEN if aten_mode else COMPILER_FLAGS,
80        )
81
82        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
83        native.cxx_library(
84            name = "gtest_headers" + aten_suffix,
85            include_directories = [
86                "googletest/googletest",
87            ],
88            public_system_include_directories = [
89                "googletest/googlemock/include",
90                "googletest/googletest/include",
91            ],
92            raw_headers = native.glob([
93                "googletest/googlemock/include/gmock/**/*.h",
94                "googletest/googletest/include/gtest/**/*.h",
95                "googletest/googletest/src/*.h",
96            ]),
97            visibility = ["PUBLIC"],
98            compiler_flags = COMPILER_FLAGS_ATEN if aten_mode else COMPILER_FLAGS,
99        )
100
101        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
102        native.cxx_library(
103            name = "gtest_main" + aten_suffix,
104            srcs = ["googletest/googletest/src/gtest_main.cc"],
105            visibility = ["PUBLIC"],
106            exported_deps = [":gtest" + aten_suffix],
107            compiler_flags = COMPILER_FLAGS_ATEN if aten_mode else COMPILER_FLAGS,
108        )
109
110        # # The following rules build samples of how to use gTest.
111        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
112        native.cxx_library(
113            name = "gtest_sample_lib" + aten_suffix,
114            srcs = [
115                "googletest/googletest/samples/sample1.cc",
116                "googletest/googletest/samples/sample2.cc",
117                "googletest/googletest/samples/sample4.cc",
118            ],
119            public_system_include_directories = [
120                "googletest/googletest/samples",
121            ],
122            raw_headers = [
123                "googletest/googletest/samples/prime_tables.h",
124                "googletest/googletest/samples/sample1.h",
125                "googletest/googletest/samples/sample2.h",
126                "googletest/googletest/samples/sample3-inl.h",
127                "googletest/googletest/samples/sample4.h",
128            ],
129        )
130
131        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
132        native.cxx_library(
133            name = "gtest_samples" + aten_suffix,
134            # All Samples except:
135            #   sample9 (main)
136            #   sample10 (main and takes a command line option and needs to be separate)
137            srcs = [
138                "googletest/googletest/samples/sample1_unittest.cc",
139                "googletest/googletest/samples/sample2_unittest.cc",
140                "googletest/googletest/samples/sample3_unittest.cc",
141                "googletest/googletest/samples/sample4_unittest.cc",
142                "googletest/googletest/samples/sample5_unittest.cc",
143                "googletest/googletest/samples/sample6_unittest.cc",
144                "googletest/googletest/samples/sample7_unittest.cc",
145                "googletest/googletest/samples/sample8_unittest.cc",
146            ],
147            deps = [
148                ":gtest_main" + aten_suffix,
149                ":gtest_sample_lib" + aten_suffix,
150            ],
151        )
152
153        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
154        native.cxx_library(
155            name = "sample9_unittest" + aten_suffix,
156            srcs = ["googletest/googletest/samples/sample9_unittest.cc"],
157            deps = [":gtest" + aten_suffix],
158        )
159
160        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
161        native.cxx_library(
162            name = "sample10_unittest" + aten_suffix,
163            srcs = ["googletest/googletest/samples/sample10_unittest.cc"],
164            deps = [":gtest" + aten_suffix],
165        )
166