glog.bzl 4.25 KB
Newer Older
Zhongming Qu's avatar
Zhongming Qu committed
1
# Implement a macro glog_library() that the BUILD file can load.
2 3 4

# By default, glog is built with gflags support.  You can change this behavior
# by using glog_library(with_gflags=0)
Zhongming Qu's avatar
Zhongming Qu committed
5 6 7 8 9
#
# 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

10 11 12 13 14 15
def glog_library(namespace='google', with_gflags=1):
    if native.repository_name() != '@':
        gendir = '$(GENDIR)/external/' + native.repository_name().lstrip('@')
    else:
        gendir = '$(GENDIR)'

Zhongming Qu's avatar
Zhongming Qu committed
16 17 18 19
    native.cc_library(
        name = 'glog',
        visibility = [ '//visibility:public' ],
        srcs = [
20
            ':config_h',
Zhongming Qu's avatar
Zhongming Qu committed
21 22
            'src/base/commandlineflags.h',
            'src/base/googleinit.h',
23
            'src/base/mutex.h',
Zhongming Qu's avatar
Zhongming Qu committed
24
            'src/demangle.cc',
25
            'src/demangle.h',
Zhongming Qu's avatar
Zhongming Qu committed
26 27 28
            'src/logging.cc',
            'src/raw_logging.cc',
            'src/signalhandler.cc',
29 30 31 32 33 34 35
            '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',
Zhongming Qu's avatar
Zhongming Qu committed
36
            'src/symbolize.cc',
37
            'src/symbolize.h',
Zhongming Qu's avatar
Zhongming Qu committed
38
            'src/utilities.cc',
39
            'src/utilities.h',
Zhongming Qu's avatar
Zhongming Qu committed
40 41 42
            'src/vlog_is_on.cc',
        ],
        hdrs = [
43 44 45 46
            ':logging_h',
            ':raw_logging_h',
            ':stl_logging_h',
            ':vlog_is_on_h',
Zhongming Qu's avatar
Zhongming Qu committed
47 48
            'src/glog/log_severity.h',
        ],
49
        strip_include_prefix = 'src',
Zhongming Qu's avatar
Zhongming Qu committed
50
        copts = [
51
            # Disable warnings that exists in glog.
Zhongming Qu's avatar
Zhongming Qu committed
52
            '-Wno-sign-compare',
53
            '-Wno-unused-function',
Zhongming Qu's avatar
Zhongming Qu committed
54
            '-Wno-unused-local-typedefs',
55
            '-Wno-unused-variable',
56
            "-DGLOG_BAZEL_BUILD",
57 58
            # Inject a C++ namespace.
            "-DGOOGLE_NAMESPACE='%s'" % namespace,
Zhongming Qu's avatar
Zhongming Qu committed
59 60 61 62
            # Allows src/base/mutex.h to include pthread.h.
            '-DHAVE_PTHREAD',
            # Allows src/logging.cc to determine the host name.
            '-DHAVE_SYS_UTSNAME_H',
63
            # For src/utilities.cc.
Zhongming Qu's avatar
Zhongming Qu committed
64 65 66 67
            '-DHAVE_SYS_SYSCALL_H',
            '-DHAVE_SYS_TIME_H',
            '-DHAVE_STDINT_H',
            '-DHAVE_STRING_H',
68 69 70
            # Enable dumping stacktrace upon sigaction.
            '-DHAVE_SIGACTION',
            # For logging.cc.
Zhongming Qu's avatar
Zhongming Qu committed
71
            '-DHAVE_PREAD',
72 73 74

            # Include generated header files.
            '-I%s/glog_internal' % gendir,
Zhongming Qu's avatar
Zhongming Qu committed
75
        ] + [
76 77 78 79 80
            # Use gflags to parse CLI arguments.
            '-DHAVE_LIB_GFLAGS',
        ] if with_gflags else [],
        deps = [
            '@com_github_gflags_gflags//:gflags',
Zhongming Qu's avatar
Zhongming Qu committed
81 82 83 84 85 86 87 88
        ] if with_gflags else [],
    )

    native.genrule(
        name = 'gen_sh',
        outs = [
            'gen.sh',
        ],
89 90
        cmd = r'''\
#!/bin/sh
Zhongming Qu's avatar
Zhongming Qu committed
91
cat > $@ <<"EOF"
92 93
sed -e 's/@ac_cv_cxx_using_operator@/1/g' \
    -e 's/@ac_cv_have_unistd_h@/1/g' \
Zhongming Qu's avatar
Zhongming Qu committed
94 95 96 97 98 99 100 101 102 103 104 105
    -e 's/@ac_cv_have_stdint_h@/1/g' \
    -e 's/@ac_cv_have_systypes_h@/1/g' \
    -e 's/@ac_cv_have_libgflags_h@/1/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'
106 107 108
EOF
''',
    )
Zhongming Qu's avatar
Zhongming Qu committed
109 110 111 112 113 114 115

    native.genrule(
        name = 'config_h',
        srcs = [
            'src/config.h.cmake.in',
        ],
        outs = [
116
            'glog_internal/config.h',
Zhongming Qu's avatar
Zhongming Qu committed
117
        ],
118
        cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $< > $@",
Zhongming Qu's avatar
Zhongming Qu committed
119 120
    )

121 122
    [native.genrule(
        name = '%s_h' % f,
Zhongming Qu's avatar
Zhongming Qu committed
123
        srcs = [
124
            'src/glog/%s.h.in' % f,
Zhongming Qu's avatar
Zhongming Qu committed
125 126
        ],
        outs = [
127
            'src/glog/%s.h' % f,
Zhongming Qu's avatar
Zhongming Qu committed
128
        ],
129
        cmd = '$(location :gen_sh) < $< > $@',
Zhongming Qu's avatar
Zhongming Qu committed
130
        tools = [':gen_sh'],
131 132 133 134 135 136 137
    ) for f in [
            'vlog_is_on',
            'stl_logging',
            'raw_logging',
            'logging',
        ]
    ]