Skip to content

Commit

Permalink
bazel: use expand_template instead of genrules
Browse files Browse the repository at this point in the history
This lets us drop the dependency on the preprocessed headers in
src/windows/glog/.

For some reason, this lets Bazel realize we've failed to declare a
dependency on dirent.h, so I added that too.
  • Loading branch information
drigz committed Mar 31, 2021
1 parent d66e593 commit e7282cd
Showing 1 changed file with 85 additions and 58 deletions.
143 changes: 85 additions & 58 deletions bazel/glog.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@
#
# Known issue: the namespace parameter is not supported on Win32.

def expand_template_impl(ctx):
ctx.actions.expand_template(
template = ctx.file.template,
output = ctx.outputs.out,
substitutions = {
k: ctx.expand_location(v, ctx.attr.data)
for k, v in ctx.attr.substitutions.items()
},
is_executable = ctx.attr.is_executable,
)

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),
"is_executable": attr.bool(default = False, mandatory = False),
"data": attr.label_list(allow_files = 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().lstrip("@")
Expand Down Expand Up @@ -78,6 +106,7 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
windows_only_srcs = [
"src/glog/log_severity.h",
"src/windows/config.h",
"src/windows/dirent.h",
"src/windows/port.cc",
"src/windows/port.h",
]
Expand Down Expand Up @@ -112,29 +141,17 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
"@bazel_tools//src/conditions:windows": windows_only_srcs,
"//conditions:default": [":config_h"],
}),
hdrs = select({
"@bazel_tools//src/conditions:windows": [
"src/windows/glog/logging.h",
"src/windows/glog/log_severity.h",
"src/windows/glog/raw_logging.h",
"src/windows/glog/stl_logging.h",
"src/windows/glog/vlog_is_on.h",
],
"//conditions:default": [
hdrs = [
"src/glog/log_severity.h",
":logging_h",
":raw_logging_h",
":stl_logging_h",
":vlog_is_on_h",
],
}),
strip_include_prefix = select({
"@bazel_tools//src/conditions:windows": "/src/windows",
"//conditions:default": "src",
}),
strip_include_prefix = "src",
defines = select({
# We need to override the default GOOGLE_GLOG_DLL_DECL from
# src/windows/glog/*.h to match src/windows/config.h.
# src/glog/*.h to match src/windows/config.h.
"@bazel_tools//src/conditions:windows": ["GOOGLE_GLOG_DLL_DECL=__declspec(dllexport)"],
"//conditions:default": [],
}),
Expand All @@ -157,57 +174,67 @@ def glog_library(namespace = "google", with_gflags = 1, **kwargs):
# the dependencies without strip_include_prefix.
native.cc_library(
name = "strip_include_prefix_hack",
hdrs = native.glob(["src/windows/*.h"]),
hdrs = [
"src/glog/log_severity.h",
":logging_h",
":raw_logging_h",
":stl_logging_h",
":vlog_is_on_h",
],
)

native.genrule(
expand_template(
name = "config_h",
srcs = [
"src/config.h.cmake.in",
],
outs = [
"glog_internal/config.h",
],
cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $< > $@",
template = "src/config.h.cmake.in",
out = "glog_internal/config.h",
substitutions = {"#cmakedefine": "//cmakedefine"},
)

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)),
)
common_config = {
"@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_have_unistd_h@": "1",
"@ac_cv_have_stdint_h@": "1",
"@ac_cv_have_systypes_h@": "1",
"@ac_cv_have_uint16_t@": "1",
"@ac_cv_have___uint16@": "0",
"@ac_cv_have___builtin_expect@": "1",
"@ac_cv_have_libgflags@": "1" if with_gflags else "0",
"@ac_cv___attribute___noinline@": "__attribute__((noinline))",
"@ac_cv___attribute___noreturn@": "__attribute__((noreturn))",
"@ac_cv___attribute___printf_4_5@": "__attribute__((__format__(__printf__, 4, 5)))",
})

windows_config = dict_union(common_config, {
"@ac_cv_have_unistd_h@": "0",
"@ac_cv_have_stdint_h@": "0",
"@ac_cv_have_systypes_h@": "0",
"@ac_cv_have_uint16_t@": "0",
"@ac_cv_have___uint16@": "1",
"@ac_cv_have___builtin_expect@": "0",
"@ac_cv_have_libgflags@": "0",
"@ac_cv___attribute___noinline@": "",
"@ac_cv___attribute___noreturn@": "__declspec(noreturn)",
"@ac_cv___attribute___printf_4_5@": "",
})

[
native.genrule(
expand_template(
name = "%s_h" % f,
srcs = [
"src/glog/%s.h.in" % f,
],
outs = [
"src/glog/%s.h" % f,
],
cmd = "$(location :gen_sh) < $< > $@",
tools = [":gen_sh"],
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",
Expand Down

0 comments on commit e7282cd

Please sign in to comment.