blob: d674fb43e5223c4f5a08d9be7e5fa5ddc5c1a125 [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001# Implement a macro glog_library() that the BUILD file can load.
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
9
10def glog_library(namespace='google', with_gflags=1, **kwargs):
11 if native.repository_name() != '@':
12 gendir = '$(GENDIR)/external/' + native.repository_name().lstrip('@')
13 else:
14 gendir = '$(GENDIR)'
15
16 native.cc_library(
17 name = 'glog',
18 visibility = [ '//visibility:public' ],
19 srcs = [
20 ':config_h',
21 'src/base/commandlineflags.h',
22 'src/base/googleinit.h',
23 'src/base/mutex.h',
24 'src/demangle.cc',
25 'src/demangle.h',
26 'src/logging.cc',
27 'src/raw_logging.cc',
28 'src/signalhandler.cc',
29 'src/stacktrace.h',
30 'src/stacktrace_generic-inl.h',
31 'src/stacktrace_libunwind-inl.h',
32 'src/stacktrace_powerpc-inl.h',
33 'src/stacktrace_windows-inl.h',
34 'src/stacktrace_x86-inl.h',
35 'src/stacktrace_x86_64-inl.h',
36 'src/symbolize.cc',
37 'src/symbolize.h',
38 'src/utilities.cc',
39 'src/utilities.h',
40 'src/vlog_is_on.cc',
41 ],
42 hdrs = [
43 ':logging_h',
44 ':raw_logging_h',
45 ':stl_logging_h',
46 ':vlog_is_on_h',
47 'src/glog/log_severity.h',
48 ],
49 strip_include_prefix = 'src',
50 copts = [
51 # Disable warnings that exists in glog.
52 '-Wno-sign-compare',
53 '-Wno-unused-function',
54 '-Wno-unused-local-typedefs',
55 '-Wno-unused-variable',
Austin Schuh10358f22019-01-21 20:25:11 -080056 '-Wno-format-nonliteral',
Austin Schuh906616c2019-01-21 20:25:11 -080057 "-DGLOG_BAZEL_BUILD",
58 # Inject a C++ namespace.
59 "-DGOOGLE_NAMESPACE='%s'" % namespace,
60 # Allows src/base/mutex.h to include pthread.h.
61 '-DHAVE_PTHREAD',
62 # Allows src/logging.cc to determine the host name.
63 '-DHAVE_SYS_UTSNAME_H',
64 # For src/utilities.cc.
65 '-DHAVE_SYS_SYSCALL_H',
66 '-DHAVE_SYS_TIME_H',
67 '-DHAVE_STDINT_H',
68 '-DHAVE_STRING_H',
69 # Enable dumping stacktrace upon sigaction.
70 '-DHAVE_SIGACTION',
Austin Schuhe8bfc552019-12-03 23:48:23 -080071 '-DHAVE_EXECINFO_H',
Austin Schuh906616c2019-01-21 20:25:11 -080072 # For logging.cc.
73 '-DHAVE_PREAD',
74 '-DHAVE___ATTRIBUTE__',
75
76 # Include generated header files.
77 '-I%s/glog_internal' % gendir,
78 ] + ([
79 # Use gflags to parse CLI arguments.
80 '-DHAVE_LIB_GFLAGS',
81 ] if with_gflags else []),
82 deps = [
83 '@com_github_gflags_gflags//:gflags',
84 ] if with_gflags else [],
85 **kwargs
86 )
87
88 native.genrule(
89 name = 'gen_sh',
90 outs = [
91 'gen.sh',
92 ],
93 cmd = r'''\
94#!/bin/sh
95cat > $@ <<"EOF"
96sed -e 's/@ac_cv_cxx_using_operator@/1/g' \
97 -e 's/@ac_cv_have_unistd_h@/1/g' \
98 -e 's/@ac_cv_have_stdint_h@/1/g' \
99 -e 's/@ac_cv_have_systypes_h@/1/g' \
100 -e 's/@ac_cv_have_libgflags@/{}/g' \
101 -e 's/@ac_cv_have_uint16_t@/1/g' \
102 -e 's/@ac_cv_have___builtin_expect@/1/g' \
103 -e 's/@ac_cv_have_.*@/0/g' \
104 -e 's/@ac_google_start_namespace@/namespace google {{/g' \
105 -e 's/@ac_google_end_namespace@/}}/g' \
106 -e 's/@ac_google_namespace@/google/g' \
107 -e 's/@ac_cv___attribute___noinline@/__attribute__((noinline))/g' \
108 -e 's/@ac_cv___attribute___noreturn@/__attribute__((noreturn))/g' \
109 -e 's/@ac_cv___attribute___printf_4_5@/__attribute__((__format__ (__printf__, 4, 5)))/g'
110EOF
111'''.format(int(with_gflags)),
112 )
113
114 native.genrule(
115 name = 'config_h',
116 srcs = [
117 'src/config.h.cmake.in',
118 ],
119 outs = [
120 'glog_internal/config.h',
121 ],
122 cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $< > $@",
123 )
124
125 [native.genrule(
126 name = '%s_h' % f,
127 srcs = [
128 'src/glog/%s.h.in' % f,
129 ],
130 outs = [
131 'src/glog/%s.h' % f,
132 ],
133 cmd = '$(location :gen_sh) < $< > $@',
134 tools = [':gen_sh'],
135 ) for f in [
136 'vlog_is_on',
137 'stl_logging',
138 'raw_logging',
139 'logging',
140 ]
141 ]