blob: 2e7a8d4c5b057bd365f3bd4d223432846a1fcf34 [file] [log] [blame]
James Kuszmaulba0ac1a2022-08-12 16:29:30 -07001# Implement a macro glog_library() that the BUILD.bazel file can load.
Austin Schuh906616c2019-01-21 20:25:11 -08002
3# By default, glog is built with gflags support. You can change this behavior
4# by using glog_library(with_gflags=0)
5#
6# This file is inspired by the following sample BUILD files:
7# https://github.com/google/glog/issues/61
8# https://github.com/google/glog/files/393474/BUILD.txt
James Kuszmaulba0ac1a2022-08-12 16:29:30 -07009#
10# Known issue: the namespace parameter is not supported on Win32.
Austin Schuh906616c2019-01-21 20:25:11 -080011
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070012def expand_template_impl(ctx):
13 ctx.actions.expand_template(
14 template = ctx.file.template,
15 output = ctx.outputs.out,
16 substitutions = ctx.attr.substitutions,
17 )
18
19expand_template = rule(
20 implementation = expand_template_impl,
21 attrs = {
22 "template": attr.label(mandatory = True, allow_single_file = True),
23 "substitutions": attr.string_dict(mandatory = True),
24 "out": attr.output(mandatory = True),
25 },
26)
27
28def dict_union(x, y):
29 z = {}
30 z.update(x)
31 z.update(y)
32 return z
33
34def glog_library(namespace = "google", with_gflags = 1, **kwargs):
35 if native.repository_name() != "@":
James Kuszmaul126dcff2022-08-12 16:30:05 -070036 repo_name = native.repository_name()[1:] # Strip the first leading @
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070037 gendir = "$(GENDIR)/external/" + repo_name
38 src_windows = "external/%s/src/windows" % repo_name
Austin Schuh906616c2019-01-21 20:25:11 -080039 else:
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070040 gendir = "$(GENDIR)"
41 src_windows = "src/windows"
42
43 # Config setting for WebAssembly target.
44 native.config_setting(
45 name = "wasm",
46 values = {"cpu": "wasm"},
47 )
48
49 # Detect when building with clang-cl on Windows.
50 native.config_setting(
51 name = "clang-cl",
52 values = {"compiler": "clang-cl"},
53 )
54
55 common_copts = [
James Kuszmaul126dcff2022-08-12 16:30:05 -070056 # Disable warnings that exists in glog.
57 "-Wno-sign-compare",
58 "-Wno-unused-function",
59 "-Wno-unused-local-typedefs",
60 "-Wno-unused-variable",
61 "-Wno-format-nonliteral",
James Kuszmaulba0ac1a2022-08-12 16:29:30 -070062 "-DGLOG_BAZEL_BUILD",
63 # Inject a C++ namespace.
64 "-DGOOGLE_NAMESPACE='%s'" % namespace,
65 "-DHAVE_CXX11_NULLPTR_T",
66 "-DHAVE_STDINT_H",
67 "-DHAVE_STRING_H",
68 "-DGLOG_CUSTOM_PREFIX_SUPPORT",
69 "-I%s/glog_internal" % gendir,
70 ] + (["-DHAVE_LIB_GFLAGS"] if with_gflags else [])
71
72 wasm_copts = [
73 # Disable warnings that exists in glog.
74 "-Wno-sign-compare",
75 "-Wno-unused-function",
76 "-Wno-unused-local-typedefs",
77 "-Wno-unused-variable",
78 # Allows src/base/mutex.h to include pthread.h.
79 "-DHAVE_PTHREAD",
80 # Allows src/logging.cc to determine the host name.
81 "-DHAVE_SYS_UTSNAME_H",
82 # For src/utilities.cc.
83 "-DHAVE_SYS_TIME_H",
84 "-DHAVE_UNWIND_H",
85 # Enable dumping stacktrace upon sigaction.
86 "-DHAVE_SIGACTION",
87 # For logging.cc.
88 "-DHAVE_PREAD",
89 "-DHAVE___ATTRIBUTE__",
90 ]
91
92 linux_or_darwin_copts = wasm_copts + [
93 "-DGLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
94 # For src/utilities.cc.
95 "-DHAVE_SYS_SYSCALL_H",
96 # For src/logging.cc to create symlinks.
97 "-DHAVE_UNISTD_H",
98 "-fvisibility-inlines-hidden",
99 "-fvisibility=hidden",
100 ]
101
102 freebsd_only_copts = [
103 # Enable declaration of _Unwind_Backtrace
104 "-D_GNU_SOURCE",
105 ]
106
107 darwin_only_copts = [
108 # For stacktrace.
109 "-DHAVE_DLADDR",
110 # Avoid deprecated syscall().
111 "-DHAVE_PTHREAD_THREADID_NP",
112 ]
113
114 windows_only_copts = [
115 # Override -DGLOG_EXPORT= from the cc_library's defines.
116 "-DGLOG_EXPORT=__declspec(dllexport)",
117 "-DGLOG_NO_ABBREVIATED_SEVERITIES",
118 "-DHAVE_SNPRINTF",
119 "-I" + src_windows,
120 ]
121
122 clang_cl_only_copts = [
123 # Allow the override of -DGLOG_EXPORT.
124 "-Wno-macro-redefined",
125 ]
126
127 windows_only_srcs = [
128 "src/glog/log_severity.h",
129 "src/windows/dirent.h",
130 "src/windows/port.cc",
131 "src/windows/port.h",
132 ]
133
134 gflags_deps = ["@com_github_gflags_gflags//:gflags"] if with_gflags else []
Austin Schuh906616c2019-01-21 20:25:11 -0800135
136 native.cc_library(
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700137 name = "glog",
138 visibility = ["//visibility:public"],
Austin Schuh906616c2019-01-21 20:25:11 -0800139 srcs = [
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700140 ":config_h",
141 "src/base/commandlineflags.h",
142 "src/base/googleinit.h",
143 "src/base/mutex.h",
144 "src/demangle.cc",
145 "src/demangle.h",
146 "src/logging.cc",
147 "src/raw_logging.cc",
148 "src/signalhandler.cc",
149 "src/stacktrace.h",
150 "src/stacktrace_generic-inl.h",
151 "src/stacktrace_libunwind-inl.h",
152 "src/stacktrace_powerpc-inl.h",
153 "src/stacktrace_unwind-inl.h",
154 "src/stacktrace_windows-inl.h",
155 "src/stacktrace_x86-inl.h",
156 "src/symbolize.cc",
157 "src/symbolize.h",
158 "src/utilities.cc",
159 "src/utilities.h",
160 "src/vlog_is_on.cc",
161 ] + select({
162 "@bazel_tools//src/conditions:windows": windows_only_srcs,
163 "//conditions:default": [],
164 }),
Austin Schuh906616c2019-01-21 20:25:11 -0800165 hdrs = [
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700166 "src/glog/log_severity.h",
167 "src/glog/platform.h",
168 ":logging_h",
169 ":raw_logging_h",
170 ":stl_logging_h",
171 ":vlog_is_on_h",
Austin Schuh906616c2019-01-21 20:25:11 -0800172 ],
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700173 strip_include_prefix = "src",
174 defines = select({
175 # GLOG_EXPORT is normally set by export.h, but that's not
176 # generated for Bazel.
177 "@bazel_tools//src/conditions:windows": [
178 "GLOG_EXPORT=",
179 "GLOG_DEPRECATED=__declspec(deprecated)",
180 "GLOG_NO_ABBREVIATED_SEVERITIES",
181 ],
182 "//conditions:default": [
183 "GLOG_DEPRECATED=__attribute__((deprecated))",
184 "GLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
185 ],
186 }),
James Kuszmaul126dcff2022-08-12 16:30:05 -0700187 deps = gflags_deps + select({
188 "@bazel_tools//src/conditions:windows": [":strip_include_prefix_hack"],
189 "//conditions:default": [],
190 }),
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700191 copts =
192 select({
193 "@bazel_tools//src/conditions:windows": common_copts + windows_only_copts,
194 "@bazel_tools//src/conditions:darwin": common_copts + linux_or_darwin_copts + darwin_only_copts,
195 "@bazel_tools//src/conditions:freebsd": common_copts + linux_or_darwin_copts + freebsd_only_copts,
196 ":wasm": common_copts + wasm_copts,
197 "//conditions:default": common_copts + linux_or_darwin_copts,
198 }) +
199 select({
200 ":clang-cl": clang_cl_only_copts,
James Kuszmaul126dcff2022-08-12 16:30:05 -0700201 "//conditions:default": [],
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700202 }),
Austin Schuh906616c2019-01-21 20:25:11 -0800203 **kwargs
204 )
205
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700206 # Workaround https://github.com/bazelbuild/bazel/issues/6337 by declaring
207 # the dependencies without strip_include_prefix.
208 native.cc_library(
209 name = "strip_include_prefix_hack",
210 hdrs = [
211 "src/glog/log_severity.h",
212 ":logging_h",
213 ":raw_logging_h",
214 ":stl_logging_h",
215 ":vlog_is_on_h",
Austin Schuh906616c2019-01-21 20:25:11 -0800216 ],
Austin Schuh906616c2019-01-21 20:25:11 -0800217 )
218
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700219 expand_template(
220 name = "config_h",
221 template = "src/config.h.cmake.in",
222 out = "glog_internal/config.h",
223 substitutions = {"#cmakedefine": "//cmakedefine"},
Austin Schuh906616c2019-01-21 20:25:11 -0800224 )
225
James Kuszmaulba0ac1a2022-08-12 16:29:30 -0700226 common_config = {
227 "@ac_cv_cxx11_atomic@": "1",
228 "@ac_cv_cxx11_constexpr@": "1",
229 "@ac_cv_cxx11_chrono@": "1",
230 "@ac_cv_cxx11_nullptr_t@": "1",
231 "@ac_cv_cxx_using_operator@": "1",
232 "@ac_cv_have_inttypes_h@": "0",
233 "@ac_cv_have_u_int16_t@": "0",
234 "@ac_cv_have_glog_export@": "0",
235 "@ac_google_start_namespace@": "namespace google {",
236 "@ac_google_end_namespace@": "}",
237 "@ac_google_namespace@": "google",
238 }
239
240 posix_config = dict_union(common_config, {
241 "@ac_cv___attribute___noinline@": "__attribute__((noinline))",
242 "@ac_cv___attribute___noreturn@": "__attribute__((noreturn))",
243 "@ac_cv___attribute___printf_4_5@": "__attribute__((__format__(__printf__, 4, 5)))",
244 "@ac_cv_have___builtin_expect@": "1",
245 "@ac_cv_have___uint16@": "0",
246 "@ac_cv_have_libgflags@": "1" if with_gflags else "0",
247 "@ac_cv_have_mode_t@": "1",
248 "@ac_cv_have_ssize_t@": "1",
249 "@ac_cv_have_stdint_h@": "1",
250 "@ac_cv_have_systypes_h@": "1",
251 "@ac_cv_have_uint16_t@": "1",
252 "@ac_cv_have_unistd_h@": "1",
253 })
254
255 windows_config = dict_union(common_config, {
256 "@ac_cv___attribute___noinline@": "",
257 "@ac_cv___attribute___noreturn@": "__declspec(noreturn)",
258 "@ac_cv___attribute___printf_4_5@": "",
259 "@ac_cv_have___builtin_expect@": "0",
260 "@ac_cv_have___uint16@": "1",
261 "@ac_cv_have_libgflags@": "0",
262 "@ac_cv_have_mode_t@": "0",
263 "@ac_cv_have_ssize_t@": "0",
264 "@ac_cv_have_stdint_h@": "0",
265 "@ac_cv_have_systypes_h@": "0",
266 "@ac_cv_have_uint16_t@": "0",
267 "@ac_cv_have_unistd_h@": "0",
268 })
269
270 [
271 expand_template(
272 name = "%s_h" % f,
273 template = "src/glog/%s.h.in" % f,
274 out = "src/glog/%s.h" % f,
275 substitutions = select({
276 "@bazel_tools//src/conditions:windows": windows_config,
277 "//conditions:default": posix_config,
278 }),
279 )
280 for f in [
281 "vlog_is_on",
282 "stl_logging",
283 "raw_logging",
284 "logging",
Austin Schuh906616c2019-01-21 20:25:11 -0800285 ]
286 ]