Merge commit 'ea7aeac570d63a673a082ba216fa119a156620b2' into HEAD

Upgrade glog. Sarah was using LOG_EVERY_T in
2b7e98861367daaccd5f8ac79be7f1d1d23af89e, and it
seemed about time that we upgraded.

This also brings in a fix from Brian to correctly handle
defines in autocxx_library.

Change-Id: I998f1c1f11b7a8b57e1906acdec1f5dd3c995111
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/third_party/google-glog/bazel/glog.bzl b/third_party/google-glog/bazel/glog.bzl
index d674fb4..2e7a8d4 100644
--- a/third_party/google-glog/bazel/glog.bzl
+++ b/third_party/google-glog/bazel/glog.bzl
@@ -1,4 +1,4 @@
-# Implement a macro glog_library() that the BUILD file can load.
+# Implement a macro glog_library() that the BUILD.bazel file can load.
 
 # By default, glog is built with gflags support.  You can change this behavior
 # by using glog_library(with_gflags=0)
@@ -6,136 +6,281 @@
 # 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() != '@':
-        gendir = '$(GENDIR)/external/' + native.repository_name().lstrip('@')
+def expand_template_impl(ctx):
+    ctx.actions.expand_template(
+        template = ctx.file.template,
+        output = ctx.outputs.out,
+        substitutions = ctx.attr.substitutions,
+    )
+
+expand_template = rule(
+    implementation = expand_template_impl,
+    attrs = {
+        "template": attr.label(mandatory = True, allow_single_file = True),
+        "substitutions": attr.string_dict(mandatory = True),
+        "out": attr.output(mandatory = True),
+    },
+)
+
+def dict_union(x, y):
+    z = {}
+    z.update(x)
+    z.update(y)
+    return z
+
+def glog_library(namespace = "google", with_gflags = 1, **kwargs):
+    if native.repository_name() != "@":
+        repo_name = native.repository_name()[1:]  # Strip the first leading @
+        gendir = "$(GENDIR)/external/" + repo_name
+        src_windows = "external/%s/src/windows" % repo_name
     else:
-        gendir = '$(GENDIR)'
+        gendir = "$(GENDIR)"
+        src_windows = "src/windows"
+
+    # Config setting for WebAssembly target.
+    native.config_setting(
+        name = "wasm",
+        values = {"cpu": "wasm"},
+    )
+
+    # Detect when building with clang-cl on Windows.
+    native.config_setting(
+        name = "clang-cl",
+        values = {"compiler": "clang-cl"},
+    )
+
+    common_copts = [
+        # Disable warnings that exists in glog.
+        "-Wno-sign-compare",
+        "-Wno-unused-function",
+        "-Wno-unused-local-typedefs",
+        "-Wno-unused-variable",
+        "-Wno-format-nonliteral",
+        "-DGLOG_BAZEL_BUILD",
+        # Inject a C++ namespace.
+        "-DGOOGLE_NAMESPACE='%s'" % namespace,
+        "-DHAVE_CXX11_NULLPTR_T",
+        "-DHAVE_STDINT_H",
+        "-DHAVE_STRING_H",
+        "-DGLOG_CUSTOM_PREFIX_SUPPORT",
+        "-I%s/glog_internal" % gendir,
+    ] + (["-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",
+        # 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",
+        "-DHAVE_UNWIND_H",
+        # Enable dumping stacktrace upon sigaction.
+        "-DHAVE_SIGACTION",
+        # For logging.cc.
+        "-DHAVE_PREAD",
+        "-DHAVE___ATTRIBUTE__",
+    ]
+
+    linux_or_darwin_copts = wasm_copts + [
+        "-DGLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
+        # For src/utilities.cc.
+        "-DHAVE_SYS_SYSCALL_H",
+        # For src/logging.cc to create symlinks.
+        "-DHAVE_UNISTD_H",
+        "-fvisibility-inlines-hidden",
+        "-fvisibility=hidden",
+    ]
+
+    freebsd_only_copts = [
+        # Enable declaration of _Unwind_Backtrace
+        "-D_GNU_SOURCE",
+    ]
+
+    darwin_only_copts = [
+        # For stacktrace.
+        "-DHAVE_DLADDR",
+        # Avoid deprecated syscall().
+        "-DHAVE_PTHREAD_THREADID_NP",
+    ]
+
+    windows_only_copts = [
+        # Override -DGLOG_EXPORT= from the cc_library's defines.
+        "-DGLOG_EXPORT=__declspec(dllexport)",
+        "-DGLOG_NO_ABBREVIATED_SEVERITIES",
+        "-DHAVE_SNPRINTF",
+        "-I" + src_windows,
+    ]
+
+    clang_cl_only_copts = [
+        # Allow the override of -DGLOG_EXPORT.
+        "-Wno-macro-redefined",
+    ]
+
+    windows_only_srcs = [
+        "src/glog/log_severity.h",
+        "src/windows/dirent.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' ],
+        name = "glog",
+        visibility = ["//visibility:public"],
         srcs = [
-            ':config_h',
-            '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',
-        ],
+            ":config_h",
+            "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_unwind-inl.h",
+            "src/stacktrace_windows-inl.h",
+            "src/stacktrace_x86-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": [],
+        }),
         hdrs = [
-            ':logging_h',
-            ':raw_logging_h',
-            ':stl_logging_h',
-            ':vlog_is_on_h',
-            'src/glog/log_severity.h',
+            "src/glog/log_severity.h",
+            "src/glog/platform.h",
+            ":logging_h",
+            ":raw_logging_h",
+            ":stl_logging_h",
+            ":vlog_is_on_h",
         ],
-        strip_include_prefix = 'src',
-        copts = [
-            # Disable warnings that exists in glog.
-            '-Wno-sign-compare',
-            '-Wno-unused-function',
-            '-Wno-unused-local-typedefs',
-            '-Wno-unused-variable',
-            '-Wno-format-nonliteral',
-            "-DGLOG_BAZEL_BUILD",
-            # 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_SYSCALL_H',
-            '-DHAVE_SYS_TIME_H',
-            '-DHAVE_STDINT_H',
-            '-DHAVE_STRING_H',
-            # Enable dumping stacktrace upon sigaction.
-            '-DHAVE_SIGACTION',
-            '-DHAVE_EXECINFO_H',
-            # For logging.cc.
-            '-DHAVE_PREAD',
-            '-DHAVE___ATTRIBUTE__',
-
-            # Include generated header files.
-            '-I%s/glog_internal' % gendir,
-        ] + ([
-            # Use gflags to parse CLI arguments.
-            '-DHAVE_LIB_GFLAGS',
-        ] if with_gflags else []),
-        deps = [
-            '@com_github_gflags_gflags//:gflags',
-        ] if with_gflags else [],
+        strip_include_prefix = "src",
+        defines = select({
+            # GLOG_EXPORT is normally set by export.h, but that's not
+            # generated for Bazel.
+            "@bazel_tools//src/conditions:windows": [
+                "GLOG_EXPORT=",
+                "GLOG_DEPRECATED=__declspec(deprecated)",
+                "GLOG_NO_ABBREVIATED_SEVERITIES",
+            ],
+            "//conditions:default": [
+                "GLOG_DEPRECATED=__attribute__((deprecated))",
+                "GLOG_EXPORT=__attribute__((visibility(\\\"default\\\")))",
+            ],
+        }),
+        deps = gflags_deps + select({
+            "@bazel_tools//src/conditions:windows": [":strip_include_prefix_hack"],
+            "//conditions:default": [],
+        }),
+        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,
+            }) +
+            select({
+                ":clang-cl": clang_cl_only_copts,
+                "//conditions:default": [],
+            }),
         **kwargs
     )
 
-    native.genrule(
-        name = 'gen_sh',
-        outs = [
-            'gen.sh',
+    # 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 = [
+            "src/glog/log_severity.h",
+            ":logging_h",
+            ":raw_logging_h",
+            ":stl_logging_h",
+            ":vlog_is_on_h",
         ],
-        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 = 'config_h',
-        srcs = [
-            'src/config.h.cmake.in',
-        ],
-        outs = [
-            'glog_internal/config.h',
-        ],
-        cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $< > $@",
+    expand_template(
+        name = "config_h",
+        template = "src/config.h.cmake.in",
+        out = "glog_internal/config.h",
+        substitutions = {"#cmakedefine": "//cmakedefine"},
     )
 
-    [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',
+    common_config = {
+        "@ac_cv_cxx11_atomic@": "1",
+        "@ac_cv_cxx11_constexpr@": "1",
+        "@ac_cv_cxx11_chrono@": "1",
+        "@ac_cv_cxx11_nullptr_t@": "1",
+        "@ac_cv_cxx_using_operator@": "1",
+        "@ac_cv_have_inttypes_h@": "0",
+        "@ac_cv_have_u_int16_t@": "0",
+        "@ac_cv_have_glog_export@": "0",
+        "@ac_google_start_namespace@": "namespace google {",
+        "@ac_google_end_namespace@": "}",
+        "@ac_google_namespace@": "google",
+    }
+
+    posix_config = dict_union(common_config, {
+        "@ac_cv___attribute___noinline@": "__attribute__((noinline))",
+        "@ac_cv___attribute___noreturn@": "__attribute__((noreturn))",
+        "@ac_cv___attribute___printf_4_5@": "__attribute__((__format__(__printf__, 4, 5)))",
+        "@ac_cv_have___builtin_expect@": "1",
+        "@ac_cv_have___uint16@": "0",
+        "@ac_cv_have_libgflags@": "1" if with_gflags else "0",
+        "@ac_cv_have_mode_t@": "1",
+        "@ac_cv_have_ssize_t@": "1",
+        "@ac_cv_have_stdint_h@": "1",
+        "@ac_cv_have_systypes_h@": "1",
+        "@ac_cv_have_uint16_t@": "1",
+        "@ac_cv_have_unistd_h@": "1",
+    })
+
+    windows_config = dict_union(common_config, {
+        "@ac_cv___attribute___noinline@": "",
+        "@ac_cv___attribute___noreturn@": "__declspec(noreturn)",
+        "@ac_cv___attribute___printf_4_5@": "",
+        "@ac_cv_have___builtin_expect@": "0",
+        "@ac_cv_have___uint16@": "1",
+        "@ac_cv_have_libgflags@": "0",
+        "@ac_cv_have_mode_t@": "0",
+        "@ac_cv_have_ssize_t@": "0",
+        "@ac_cv_have_stdint_h@": "0",
+        "@ac_cv_have_systypes_h@": "0",
+        "@ac_cv_have_uint16_t@": "0",
+        "@ac_cv_have_unistd_h@": "0",
+    })
+
+    [
+        expand_template(
+            name = "%s_h" % f,
+            template = "src/glog/%s.h.in" % f,
+            out = "src/glog/%s.h" % f,
+            substitutions = select({
+                "@bazel_tools//src/conditions:windows": windows_config,
+                "//conditions:default": posix_config,
+            }),
+        )
+        for f in [
+            "vlog_is_on",
+            "stl_logging",
+            "raw_logging",
+            "logging",
         ]
     ]