blob: df34e4e89b2c549e0ae6d6995119766c63f7461f [file] [log] [blame]
Austin Schuh8fec4f42018-10-29 21:52:32 -07001# ------------------------------------------------------------------------------
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({
Brian Silverman16a923c2018-10-31 19:40:51 -070052 "//:x64_windows": [
Austin Schuh8fec4f42018-10-29 21:52:32 -070053 "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",
Austin Schuh87dc7bb2018-10-29 21:53:23 -070076 "-Wno-format-nonliteral",
Austin Schuh8fec4f42018-10-29 21:52:32 -070077 ] + select({
Brian Silverman16a923c2018-10-31 19:40:51 -070078 "//:x64_windows": [
Austin Schuh8fec4f42018-10-29 21:52:32 -070079 "-DOS_WINDOWS",
80 ],
81 "//conditions:default": [
82 "-DHAVE_UNISTD_H",
83 "-DHAVE_FNMATCH_H",
84 "-DHAVE_PTHREAD",
85 ],
86 })
87 linkopts = []
88 if threads:
89 linkopts += select({
Brian Silverman16a923c2018-10-31 19:40:51 -070090 "//:x64_windows": [],
Austin Schuh8fec4f42018-10-29 21:52:32 -070091 "//conditions:default": ["-lpthread"],
92 })
93 else:
94 name += "_nothreads"
95 copts += ["-DNO_THREADS"]
96 native.cc_library(
97 name = name,
98 hdrs = hdrs,
99 srcs = srcs,
100 copts = copts,
101 linkopts = linkopts,
102 visibility = ["//visibility:public"],
103 include_prefix = 'gflags'
104 )