Build with Bazel.
[platform/upstream/glog.git] / bazel / glog.bzl
1 # Implement a macro glog_library() that the BUILD file can load.
2 # By default, glog is built with gflags support.  You can change this behavior by using
3 # glog_library(with_gflags=0)
4 #
5 # This file is inspired by the following sample BUILD files:
6 #       https://github.com/google/glog/issues/61
7 #       https://github.com/google/glog/files/393474/BUILD.txt
8
9 def glog_library(with_gflags=1):
10     native.cc_library(
11         name = 'glog',
12         visibility = [ '//visibility:public' ],
13         srcs = [
14             'src/base/commandlineflags.h',
15             'src/base/googleinit.h',
16             'src/demangle.cc',
17             'src/logging.cc',
18             'src/raw_logging.cc',
19             'src/signalhandler.cc',
20             'src/symbolize.cc',
21             'src/utilities.cc',
22             'src/vlog_is_on.cc',
23         ],
24         hdrs = [
25             'src/base/mutex.h',
26             'src/demangle.h',
27             'src/stacktrace.h',
28             'src/symbolize.h',
29             'src/utilities.h',
30             'src/glog/log_severity.h',
31         ],
32         includes = [
33             'src',
34         ],
35         copts = [
36             # Disable warnings that exists in glog
37             '-Wno-sign-compare',
38             '-Wno-unused-local-typedefs',
39             ## Inject google namespace as 'google'
40             "-D_START_GOOGLE_NAMESPACE_='namespace google {'",
41             "-D_END_GOOGLE_NAMESPACE_='}'",
42             "-DGOOGLE_NAMESPACE='google'",
43             # Allows src/base/mutex.h to include pthread.h.
44             '-DHAVE_PTHREAD',
45             # Allows src/logging.cc to determine the host name.
46             '-DHAVE_SYS_UTSNAME_H',
47             # System header files enabler for src/utilities.cc
48             # Enable system calls from syscall.h
49             '-DHAVE_SYS_SYSCALL_H',
50             # Enable system calls from sys/time.h
51             '-DHAVE_SYS_TIME_H',
52             '-DHAVE_STDINT_H',
53             '-DHAVE_STRING_H',
54             # For logging.cc
55             '-DHAVE_PREAD',
56         ] + [
57             '-DHAVE_LIB_GFLAGS'
58         ] if with_gflags else [],
59         deps = [ ':internal_headers' ] + \
60                 [ '//external:gflags' ] if with_gflags else [],
61     )
62
63     internal_headers = [
64         ':config_h',
65         ':logging_h',
66         ':raw_logging_h',
67         ':stl_logging_h',
68         ':vlog_is_on_h',
69     ]
70
71     if PACKAGE_NAME:
72         native.cc_library(
73             name = 'internal_headers',
74             hdrs = internal_headers,
75             includes = [
76                         PACKAGE_NAME,
77             ],
78         )
79     else:
80         native.cc_library(
81             name = 'internal_headers',
82             hdrs = internal_headers,
83         )
84
85     native.genrule(
86         name = 'gen_sh',
87         outs = [
88             'gen.sh',
89         ],
90         cmd = '''
91 #! /bin/sh
92 cat > $@ <<"EOF"
93 sed -e 's/@ac_cv_have_unistd_h@/1/g' \
94     -e 's/@ac_cv_have_stdint_h@/1/g' \
95     -e 's/@ac_cv_have_systypes_h@/1/g' \
96     -e 's/@ac_cv_have_libgflags_h@/1/g' \
97     -e 's/@ac_cv_have_uint16_t@/1/g' \
98     -e 's/@ac_cv_have___builtin_expect@/1/g' \
99     -e 's/@ac_cv_have_.*@/0/g' \
100     -e 's/@ac_google_start_namespace@/namespace google {/g' \
101     -e 's/@ac_google_end_namespace@/}/g' \
102     -e 's/@ac_google_namespace@/google/g' \
103     -e 's/@ac_cv___attribute___noinline@/__attribute__((noinline))/g' \
104     -e 's/@ac_cv___attribute___noreturn@/__attribute__((noreturn))/g' \
105     -e 's/@ac_cv___attribute___printf_4_5@/__attribute__((__format__ (__printf__, 4, 5)))/g'
106 EOF''')
107
108     native.genrule(
109         name = 'config_h',
110         srcs = [
111             'src/config.h.cmake.in',
112         ],
113         outs = [
114             '/'.join([PACKAGE_NAME, 'config.h']) if PACKAGE_NAME else 'config.h',
115         ],
116         cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $(<) > $(@)",
117     )
118
119     native.genrule(
120         name = 'logging_h',
121         srcs = [
122             'src/glog/logging.h.in',
123         ],
124         outs = [
125             '/'.join([PACKAGE_NAME, 'glog/logging.h']) if PACKAGE_NAME else 'glog/logging.h',
126         ],
127         cmd = '$(location :gen_sh) < $(<) > $(@)',
128         tools = [':gen_sh'],
129     )
130
131     native.genrule(
132         name = 'raw_logging_h',
133         srcs = [
134             'src/glog/raw_logging.h.in',
135         ],
136         outs = [
137             '/'.join([PACKAGE_NAME, 'glog/raw_logging.h']) if PACKAGE_NAME else 'glog/raw_logging.h',
138         ],
139         cmd = '$(location :gen_sh) < $(<) > $(@)',
140         tools = [':gen_sh'],
141     )
142
143     native.genrule(
144         name = 'stl_logging_h',
145         srcs = [
146             'src/glog/stl_logging.h.in',
147         ],
148         outs = [
149             '/'.join([PACKAGE_NAME, 'glog/stl_logging.h']) if PACKAGE_NAME else 'glog/stl_logging.h',
150         ],
151         cmd = '$(location :gen_sh) < $(<) > $(@)',
152         tools = [':gen_sh'],
153     )
154
155     native.genrule(
156         name = 'vlog_is_on_h',
157         srcs = [
158             'src/glog/vlog_is_on.h.in',
159         ],
160         outs = [
161             '/'.join([PACKAGE_NAME, 'glog/vlog_is_on.h']) if PACKAGE_NAME else 'glog/vlog_is_on.h',
162         ],
163         cmd = '$(location :gen_sh) < $(<) > $(@)',
164         tools = [':gen_sh'],
165     )