• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ------------------------------------------------------------------------------
2# Add native rules to configure source files
3def gflags_sources(namespace=["google", "gflags"]):
4    native.genrule(
5        name = "gflags_declare_h",
6        srcs = ["src/gflags_declare.h.in"],
7        outs = ["gflags_declare.h"],
8        cmd  = ("awk '{ " +
9                "gsub(/@GFLAGS_NAMESPACE@/, \"" + namespace[0] + "\"); " +
10                "gsub(/@(HAVE_STDINT_H|HAVE_SYS_TYPES_H|HAVE_INTTYPES_H|GFLAGS_INTTYPES_FORMAT_C99)@/, \"1\"); " +
11                "gsub(/@([A-Z0-9_]+)@/, \"0\"); " +
12                "print; }' $(<) > $(@)")
13    )
14    gflags_ns_h_files = []
15    for ns in namespace[1:]:
16        gflags_ns_h_file = "gflags_{}.h".format(ns)
17        native.genrule(
18            name = gflags_ns_h_file.replace('.', '_'),
19            srcs = ["src/gflags_ns.h.in"],
20            outs = [gflags_ns_h_file],
21            cmd  = ("awk '{ " +
22                    "gsub(/@ns@/, \"" + ns + "\"); " +
23                    "gsub(/@NS@/, \"" + ns.upper() + "\"); " +
24                    "print; }' $(<) > $(@)")
25        )
26        gflags_ns_h_files.append(gflags_ns_h_file)
27    native.genrule(
28        name = "gflags_h",
29        srcs = ["src/gflags.h.in"],
30        outs = ["gflags.h"],
31        cmd  = ("awk '{ " +
32                "gsub(/@GFLAGS_ATTRIBUTE_UNUSED@/, \"\"); " +
33                "gsub(/@INCLUDE_GFLAGS_NS_H@/, \"" + '\n'.join(["#include \\\"gflags/{}\\\"".format(hdr) for hdr in gflags_ns_h_files]) + "\"); " +
34                "print; }' $(<) > $(@)")
35    )
36    native.genrule(
37        name = "gflags_completions_h",
38        srcs = ["src/gflags_completions.h.in"],
39        outs = ["gflags_completions.h"],
40        cmd  = "awk '{ gsub(/@GFLAGS_NAMESPACE@/, \"" + namespace[0] + "\"); print; }' $(<) > $(@)"
41    )
42    hdrs = [":gflags_h", ":gflags_declare_h", ":gflags_completions_h"]
43    hdrs.extend([':' + hdr.replace('.', '_') for hdr in gflags_ns_h_files])
44    srcs = [
45        "src/config.h",
46        "src/gflags.cc",
47        "src/gflags_completions.cc",
48        "src/gflags_reporting.cc",
49        "src/mutex.h",
50        "src/util.h",
51    ] + select({
52        "//:x64_windows": [
53            "src/windows_port.cc",
54            "src/windows_port.h",
55        ],
56        "//conditions:default": [],
57    })
58    return [hdrs, srcs]
59
60# ------------------------------------------------------------------------------
61# Add native rule to build gflags library
62def gflags_library(hdrs=[], srcs=[], threads=1):
63    name = "gflags"
64    copts = [
65        "-DGFLAGS_BAZEL_BUILD",
66        "-DGFLAGS_INTTYPES_FORMAT_C99",
67        "-DGFLAGS_IS_A_DLL=0",
68        # macros otherwise defined by CMake configured defines.h file
69        "-DHAVE_STDINT_H",
70        "-DHAVE_SYS_TYPES_H",
71        "-DHAVE_INTTYPES_H",
72        "-DHAVE_SYS_STAT_H",
73        "-DHAVE_STRTOLL",
74        "-DHAVE_STRTOQ",
75        "-DHAVE_RWLOCK",
76    ] + select({
77        "//:x64_windows": [
78            "-DOS_WINDOWS",
79        ],
80        "//conditions:default": [
81            "-DHAVE_UNISTD_H",
82            "-DHAVE_FNMATCH_H",
83            "-DHAVE_PTHREAD",
84        ],
85    })
86    linkopts = []
87    if threads:
88        linkopts += select({
89            "//:x64_windows": [],
90            "//conditions:default": ["-lpthread"],
91        })
92    else:
93        name += "_nothreads"
94        copts += ["-DNO_THREADS"]
95    native.cc_library(
96        name       = name,
97        hdrs       = hdrs,
98        srcs       = srcs,
99        copts      = copts,
100        linkopts   = linkopts,
101        visibility = ["//visibility:public"],
102        include_prefix = 'gflags'
103    )
104