James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 1 | # Implement a macro glog_library() that the BUILD.bazel file can load. |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 2 | |
| 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 Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 9 | # |
| 10 | # Known issue: the namespace parameter is not supported on Win32. |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 11 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 12 | def 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 | |
| 19 | expand_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 | |
| 28 | def dict_union(x, y): |
| 29 | z = {} |
| 30 | z.update(x) |
| 31 | z.update(y) |
| 32 | return z |
| 33 | |
| 34 | def glog_library(namespace = "google", with_gflags = 1, **kwargs): |
| 35 | if native.repository_name() != "@": |
James Kuszmaul | 126dcff | 2022-08-12 16:30:05 -0700 | [diff] [blame] | 36 | repo_name = native.repository_name()[1:] # Strip the first leading @ |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 37 | gendir = "$(GENDIR)/external/" + repo_name |
| 38 | src_windows = "external/%s/src/windows" % repo_name |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 39 | else: |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 40 | 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 Kuszmaul | 126dcff | 2022-08-12 16:30:05 -0700 | [diff] [blame] | 56 | # 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 Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 62 | "-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", |
Austin Schuh | e5c62fa | 2022-09-17 16:25:01 -0700 | [diff] [blame] | 98 | "-DHAVE_EXECINFO_H", |
| 99 | "-DHAVE_EXECINFO_BACKTRACE", |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 100 | "-fvisibility-inlines-hidden", |
| 101 | "-fvisibility=hidden", |
| 102 | ] |
| 103 | |
| 104 | freebsd_only_copts = [ |
| 105 | # Enable declaration of _Unwind_Backtrace |
| 106 | "-D_GNU_SOURCE", |
| 107 | ] |
| 108 | |
| 109 | darwin_only_copts = [ |
| 110 | # For stacktrace. |
| 111 | "-DHAVE_DLADDR", |
| 112 | # Avoid deprecated syscall(). |
| 113 | "-DHAVE_PTHREAD_THREADID_NP", |
| 114 | ] |
| 115 | |
| 116 | windows_only_copts = [ |
| 117 | # Override -DGLOG_EXPORT= from the cc_library's defines. |
| 118 | "-DGLOG_EXPORT=__declspec(dllexport)", |
| 119 | "-DGLOG_NO_ABBREVIATED_SEVERITIES", |
| 120 | "-DHAVE_SNPRINTF", |
| 121 | "-I" + src_windows, |
| 122 | ] |
| 123 | |
| 124 | clang_cl_only_copts = [ |
| 125 | # Allow the override of -DGLOG_EXPORT. |
| 126 | "-Wno-macro-redefined", |
| 127 | ] |
| 128 | |
| 129 | windows_only_srcs = [ |
| 130 | "src/glog/log_severity.h", |
| 131 | "src/windows/dirent.h", |
| 132 | "src/windows/port.cc", |
| 133 | "src/windows/port.h", |
| 134 | ] |
| 135 | |
| 136 | gflags_deps = ["@com_github_gflags_gflags//:gflags"] if with_gflags else [] |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 137 | |
| 138 | native.cc_library( |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 139 | name = "glog", |
| 140 | visibility = ["//visibility:public"], |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 141 | srcs = [ |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 142 | ":config_h", |
| 143 | "src/base/commandlineflags.h", |
| 144 | "src/base/googleinit.h", |
| 145 | "src/base/mutex.h", |
| 146 | "src/demangle.cc", |
| 147 | "src/demangle.h", |
| 148 | "src/logging.cc", |
| 149 | "src/raw_logging.cc", |
| 150 | "src/signalhandler.cc", |
| 151 | "src/stacktrace.h", |
| 152 | "src/stacktrace_generic-inl.h", |
| 153 | "src/stacktrace_libunwind-inl.h", |
| 154 | "src/stacktrace_powerpc-inl.h", |
| 155 | "src/stacktrace_unwind-inl.h", |
| 156 | "src/stacktrace_windows-inl.h", |
| 157 | "src/stacktrace_x86-inl.h", |
| 158 | "src/symbolize.cc", |
| 159 | "src/symbolize.h", |
| 160 | "src/utilities.cc", |
| 161 | "src/utilities.h", |
| 162 | "src/vlog_is_on.cc", |
| 163 | ] + select({ |
| 164 | "@bazel_tools//src/conditions:windows": windows_only_srcs, |
| 165 | "//conditions:default": [], |
| 166 | }), |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 167 | hdrs = [ |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 168 | "src/glog/log_severity.h", |
| 169 | "src/glog/platform.h", |
| 170 | ":logging_h", |
| 171 | ":raw_logging_h", |
| 172 | ":stl_logging_h", |
| 173 | ":vlog_is_on_h", |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 174 | ], |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 175 | strip_include_prefix = "src", |
| 176 | defines = select({ |
| 177 | # GLOG_EXPORT is normally set by export.h, but that's not |
| 178 | # generated for Bazel. |
| 179 | "@bazel_tools//src/conditions:windows": [ |
| 180 | "GLOG_EXPORT=", |
| 181 | "GLOG_DEPRECATED=__declspec(deprecated)", |
| 182 | "GLOG_NO_ABBREVIATED_SEVERITIES", |
| 183 | ], |
| 184 | "//conditions:default": [ |
| 185 | "GLOG_DEPRECATED=__attribute__((deprecated))", |
| 186 | "GLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))", |
| 187 | ], |
| 188 | }), |
James Kuszmaul | 126dcff | 2022-08-12 16:30:05 -0700 | [diff] [blame] | 189 | deps = gflags_deps + select({ |
| 190 | "@bazel_tools//src/conditions:windows": [":strip_include_prefix_hack"], |
| 191 | "//conditions:default": [], |
| 192 | }), |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 193 | copts = |
| 194 | select({ |
| 195 | "@bazel_tools//src/conditions:windows": common_copts + windows_only_copts, |
| 196 | "@bazel_tools//src/conditions:darwin": common_copts + linux_or_darwin_copts + darwin_only_copts, |
| 197 | "@bazel_tools//src/conditions:freebsd": common_copts + linux_or_darwin_copts + freebsd_only_copts, |
| 198 | ":wasm": common_copts + wasm_copts, |
| 199 | "//conditions:default": common_copts + linux_or_darwin_copts, |
| 200 | }) + |
| 201 | select({ |
| 202 | ":clang-cl": clang_cl_only_copts, |
James Kuszmaul | 126dcff | 2022-08-12 16:30:05 -0700 | [diff] [blame] | 203 | "//conditions:default": [], |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 204 | }), |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 205 | **kwargs |
| 206 | ) |
| 207 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 208 | # Workaround https://github.com/bazelbuild/bazel/issues/6337 by declaring |
| 209 | # the dependencies without strip_include_prefix. |
| 210 | native.cc_library( |
| 211 | name = "strip_include_prefix_hack", |
| 212 | hdrs = [ |
| 213 | "src/glog/log_severity.h", |
| 214 | ":logging_h", |
| 215 | ":raw_logging_h", |
| 216 | ":stl_logging_h", |
| 217 | ":vlog_is_on_h", |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 218 | ], |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 219 | ) |
| 220 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 221 | expand_template( |
| 222 | name = "config_h", |
| 223 | template = "src/config.h.cmake.in", |
| 224 | out = "glog_internal/config.h", |
| 225 | substitutions = {"#cmakedefine": "//cmakedefine"}, |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 226 | ) |
| 227 | |
James Kuszmaul | ba0ac1a | 2022-08-12 16:29:30 -0700 | [diff] [blame] | 228 | common_config = { |
| 229 | "@ac_cv_cxx11_atomic@": "1", |
| 230 | "@ac_cv_cxx11_constexpr@": "1", |
| 231 | "@ac_cv_cxx11_chrono@": "1", |
| 232 | "@ac_cv_cxx11_nullptr_t@": "1", |
| 233 | "@ac_cv_cxx_using_operator@": "1", |
| 234 | "@ac_cv_have_inttypes_h@": "0", |
| 235 | "@ac_cv_have_u_int16_t@": "0", |
| 236 | "@ac_cv_have_glog_export@": "0", |
| 237 | "@ac_google_start_namespace@": "namespace google {", |
| 238 | "@ac_google_end_namespace@": "}", |
| 239 | "@ac_google_namespace@": "google", |
| 240 | } |
| 241 | |
| 242 | posix_config = dict_union(common_config, { |
| 243 | "@ac_cv___attribute___noinline@": "__attribute__((noinline))", |
| 244 | "@ac_cv___attribute___noreturn@": "__attribute__((noreturn))", |
| 245 | "@ac_cv___attribute___printf_4_5@": "__attribute__((__format__(__printf__, 4, 5)))", |
| 246 | "@ac_cv_have___builtin_expect@": "1", |
| 247 | "@ac_cv_have___uint16@": "0", |
| 248 | "@ac_cv_have_libgflags@": "1" if with_gflags else "0", |
| 249 | "@ac_cv_have_mode_t@": "1", |
| 250 | "@ac_cv_have_ssize_t@": "1", |
| 251 | "@ac_cv_have_stdint_h@": "1", |
| 252 | "@ac_cv_have_systypes_h@": "1", |
| 253 | "@ac_cv_have_uint16_t@": "1", |
| 254 | "@ac_cv_have_unistd_h@": "1", |
| 255 | }) |
| 256 | |
| 257 | windows_config = dict_union(common_config, { |
| 258 | "@ac_cv___attribute___noinline@": "", |
| 259 | "@ac_cv___attribute___noreturn@": "__declspec(noreturn)", |
| 260 | "@ac_cv___attribute___printf_4_5@": "", |
| 261 | "@ac_cv_have___builtin_expect@": "0", |
| 262 | "@ac_cv_have___uint16@": "1", |
| 263 | "@ac_cv_have_libgflags@": "0", |
| 264 | "@ac_cv_have_mode_t@": "0", |
| 265 | "@ac_cv_have_ssize_t@": "0", |
| 266 | "@ac_cv_have_stdint_h@": "0", |
| 267 | "@ac_cv_have_systypes_h@": "0", |
| 268 | "@ac_cv_have_uint16_t@": "0", |
| 269 | "@ac_cv_have_unistd_h@": "0", |
| 270 | }) |
| 271 | |
| 272 | [ |
| 273 | expand_template( |
| 274 | name = "%s_h" % f, |
| 275 | template = "src/glog/%s.h.in" % f, |
| 276 | out = "src/glog/%s.h" % f, |
| 277 | substitutions = select({ |
| 278 | "@bazel_tools//src/conditions:windows": windows_config, |
| 279 | "//conditions:default": posix_config, |
| 280 | }), |
| 281 | ) |
| 282 | for f in [ |
| 283 | "vlog_is_on", |
| 284 | "stl_logging", |
| 285 | "raw_logging", |
| 286 | "logging", |
Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [diff] [blame] | 287 | ] |
| 288 | ] |