diff options
author | Vincent Ambo <tazjin@google.com> | 2020-05-27T00·26+0100 |
---|---|---|
committer | Vincent Ambo <tazjin@google.com> | 2020-05-27T00·26+0100 |
commit | afe04691aca3f669f517adaeb5bd4a87a481fb4a (patch) | |
tree | 60ae6c91a3959b2f6486256e26ff126e598d1e5d /bazel |
Squashed 'third_party/glog/' content from commit 9ef754a3023
git-subtree-dir: third_party/glog git-subtree-split: 9ef754a3023e6fd10f20fe53dfca96dd898182e3
Diffstat (limited to 'bazel')
-rw-r--r-- | bazel/example/BUILD | 8 | ||||
-rw-r--r-- | bazel/example/main.cc | 22 | ||||
-rw-r--r-- | bazel/glog.bzl | 219 |
3 files changed, 249 insertions, 0 deletions
diff --git a/bazel/example/BUILD b/bazel/example/BUILD new file mode 100644 index 000000000000..6b10e2b2fb3f --- /dev/null +++ b/bazel/example/BUILD @@ -0,0 +1,8 @@ +cc_test( + name = "main", + size = "small", + srcs = ["main.cc"], + deps = [ + "//:glog", + ], +) diff --git a/bazel/example/main.cc b/bazel/example/main.cc new file mode 100644 index 000000000000..fef01dc15752 --- /dev/null +++ b/bazel/example/main.cc @@ -0,0 +1,22 @@ +#include <gflags/gflags.h> +#include <glog/logging.h> +#include <glog/stl_logging.h> + +int main(int argc, char* argv[]) { + // Initialize Google's logging library. + google::InitGoogleLogging(argv[0]); + + // Optional: parse command line flags + gflags::ParseCommandLineFlags(&argc, &argv, true); + + LOG(INFO) << "Hello, world!"; + + // glog/stl_logging.h allows logging STL containers. + std::vector<int> x; + x.push_back(1); + x.push_back(2); + x.push_back(3); + LOG(INFO) << "ABC, it's easy as " << x; + + return 0; +} diff --git a/bazel/glog.bzl b/bazel/glog.bzl new file mode 100644 index 000000000000..a46e2d148ba8 --- /dev/null +++ b/bazel/glog.bzl @@ -0,0 +1,219 @@ +# Implement a macro glog_library() that the BUILD file can load. + +# By default, glog is built with gflags support. You can change this behavior +# by using glog_library(with_gflags=0) +# +# This file is inspired by the following sample BUILD files: +# https://github.com/google/glog/issues/61 +# https://github.com/google/glog/files/393474/BUILD.txt +# +# Known issue: the namespace parameter is not supported on Win32. + +def glog_library(namespace = "google", with_gflags = 1, **kwargs): + if native.repository_name() != "@": + repo_name = native.repository_name().lstrip("@") + gendir = "$(GENDIR)/external/" + repo_name + src_windows = "external/%s/src/windows" % repo_name + else: + gendir = "$(GENDIR)" + src_windows = "src/windows" + + # Config setting for WebAssembly target. + native.config_setting( + name = "wasm", + values = {"cpu": "wasm"}, + ) + + common_copts = [ + "-DGLOG_BAZEL_BUILD", + "-DHAVE_STDINT_H", + "-DHAVE_STRING_H", + "-DHAVE_UNWIND_H", + ] + (["-DHAVE_LIB_GFLAGS"] if with_gflags else []) + + wasm_copts = [ + # Disable warnings that exists in glog. + "-Wno-sign-compare", + "-Wno-unused-function", + "-Wno-unused-local-typedefs", + "-Wno-unused-variable", + # Inject a C++ namespace. + "-DGOOGLE_NAMESPACE='%s'" % namespace, + # Allows src/base/mutex.h to include pthread.h. + "-DHAVE_PTHREAD", + # Allows src/logging.cc to determine the host name. + "-DHAVE_SYS_UTSNAME_H", + # For src/utilities.cc. + "-DHAVE_SYS_TIME_H", + # Enable dumping stacktrace upon sigaction. + "-DHAVE_SIGACTION", + # For logging.cc. + "-DHAVE_PREAD", + "-DHAVE___ATTRIBUTE__", + "-I%s/glog_internal" % gendir, + ] + + linux_or_darwin_copts = wasm_copts + [ + # For src/utilities.cc. + "-DHAVE_SYS_SYSCALL_H", + ] + + freebsd_only_copts = [ + # Enable declaration of _Unwind_Backtrace + "-D_GNU_SOURCE", + ] + + darwin_only_copts = [ + # For stacktrace. + "-DHAVE_DLADDR", + ] + + windows_only_copts = [ + "-DHAVE_SNPRINTF", + "-I" + src_windows, + ] + + windows_only_srcs = [ + "src/glog/log_severity.h", + "src/windows/config.h", + "src/windows/port.cc", + "src/windows/port.h", + ] + + gflags_deps = ["@com_github_gflags_gflags//:gflags"] if with_gflags else [] + + native.cc_library( + name = "glog", + visibility = ["//visibility:public"], + srcs = [ + "src/base/commandlineflags.h", + "src/base/googleinit.h", + "src/base/mutex.h", + "src/demangle.cc", + "src/demangle.h", + "src/logging.cc", + "src/raw_logging.cc", + "src/signalhandler.cc", + "src/stacktrace.h", + "src/stacktrace_generic-inl.h", + "src/stacktrace_libunwind-inl.h", + "src/stacktrace_powerpc-inl.h", + "src/stacktrace_windows-inl.h", + "src/stacktrace_x86-inl.h", + "src/stacktrace_x86_64-inl.h", + "src/symbolize.cc", + "src/symbolize.h", + "src/utilities.cc", + "src/utilities.h", + "src/vlog_is_on.cc", + ] + select({ + "@bazel_tools//src/conditions:windows": windows_only_srcs, + "//conditions:default": [":config_h"], + }), + copts = + select({ + "@bazel_tools//src/conditions:windows": common_copts + windows_only_copts, + "@bazel_tools//src/conditions:darwin": common_copts + linux_or_darwin_copts + darwin_only_copts, + "@bazel_tools//src/conditions:freebsd": common_copts + linux_or_darwin_copts + freebsd_only_copts, + ":wasm": common_copts + wasm_copts, + "//conditions:default": common_copts + linux_or_darwin_copts, + }), + deps = [ + ":glog_headers", + ] + gflags_deps, + **kwargs + ) + + # glog headers vary depending on the os. + native.cc_library( + name = "glog_headers", + deps = select({ + "@bazel_tools//src/conditions:windows": [":windows_glog_headers"], + "//conditions:default": [":default_glog_headers"], + }), + ) + + native.cc_library( + name = "windows_glog_headers", + hdrs = native.glob(["src/windows/glog/*.h"]), + strip_include_prefix = "src/windows", + # We need to override the default GOOGLE_GLOG_DLL_DECL from + # src/windows/glog/*.h to match src/windows/config.h. + defines = ["GOOGLE_GLOG_DLL_DECL=__declspec(dllexport)"], + deps = [":strip_include_prefix_hack"], + ) + + # Workaround https://github.com/bazelbuild/bazel/issues/6337 by declaring + # the dependencies without strip_include_prefix. + native.cc_library( + name = "strip_include_prefix_hack", + hdrs = native.glob(["src/windows/*.h"]), + ) + + native.cc_library( + name = "default_glog_headers", + strip_include_prefix = "src", + hdrs = [ + "src/glog/log_severity.h", + ":logging_h", + ":raw_logging_h", + ":stl_logging_h", + ":vlog_is_on_h", + ], + ) + native.genrule( + name = "config_h", + srcs = [ + "src/config.h.cmake.in", + ], + outs = [ + "glog_internal/config.h", + ], + cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $< > $@", + ) + + native.genrule( + name = "gen_sh", + outs = [ + "gen.sh", + ], + cmd = r'''\ +#!/bin/sh +cat > $@ <<"EOF" +sed -e 's/@ac_cv_cxx_using_operator@/1/g' \ + -e 's/@ac_cv_have_unistd_h@/1/g' \ + -e 's/@ac_cv_have_stdint_h@/1/g' \ + -e 's/@ac_cv_have_systypes_h@/1/g' \ + -e 's/@ac_cv_have_libgflags@/{}/g' \ + -e 's/@ac_cv_have_uint16_t@/1/g' \ + -e 's/@ac_cv_have___builtin_expect@/1/g' \ + -e 's/@ac_cv_have_.*@/0/g' \ + -e 's/@ac_google_start_namespace@/namespace google {{/g' \ + -e 's/@ac_google_end_namespace@/}}/g' \ + -e 's/@ac_google_namespace@/google/g' \ + -e 's/@ac_cv___attribute___noinline@/__attribute__((noinline))/g' \ + -e 's/@ac_cv___attribute___noreturn@/__attribute__((noreturn))/g' \ + -e 's/@ac_cv___attribute___printf_4_5@/__attribute__((__format__ (__printf__, 4, 5)))/g' +EOF +'''.format(int(with_gflags)), + ) + + [ + native.genrule( + name = "%s_h" % f, + srcs = [ + "src/glog/%s.h.in" % f, + ], + outs = [ + "src/glog/%s.h" % f, + ], + cmd = "$(location :gen_sh) < $< > $@", + tools = [":gen_sh"], + ) + for f in [ + "vlog_is_on", + "stl_logging", + "raw_logging", + "logging", + ] + ] |