xref: /aosp_15_r20/external/executorch/third-party/gflags_defs.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# ------------------------------------------------------------------------------
2# Add native rules to configure source files
3# Not tested for building on windows platforms
4def gflags_sources(namespace = ["google", "gflags"]):
5    common_preamble = "mkdir -p `dirname $OUT` && "
6
7    # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
8    native.genrule(
9        name = "gflags_declare_h",
10        srcs = ["gflags/src/gflags_declare.h.in"],
11        out = "gflags/gflags_declare.h",
12        cmd = (common_preamble + "awk '{ " +
13               "gsub(/@GFLAGS_NAMESPACE@/, \"" + namespace[0] + "\"); " +
14               "gsub(/@(HAVE_STDINT_H|HAVE_SYS_TYPES_H|HAVE_INTTYPES_H|GFLAGS_INTTYPES_FORMAT_C99)@/, \"1\"); " +
15               "gsub(/@([A-Z0-9_]+)@/, \"0\"); " +
16               "print; }' $SRCS > $OUT"),
17    )
18    gflags_ns_h_files = []
19    for ns in namespace[1:]:
20        gflags_ns_h_file = "gflags_{}.h".format(ns)
21
22        # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
23        native.genrule(
24            name = gflags_ns_h_file.replace(".", "_"),
25            srcs = ["gflags/src/gflags_ns.h.in"],
26            out = "gflags/" + gflags_ns_h_file,
27            cmd = (common_preamble + "awk '{ " +
28                   "gsub(/@ns@/, \"" + ns + "\"); " +
29                   "gsub(/@NS@/, \"" + ns.upper() + "\"); " +
30                   "print; }' $SRCS > $OUT"),
31        )
32        gflags_ns_h_files.append(gflags_ns_h_file)
33
34    # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
35    native.genrule(
36        name = "gflags_h",
37        srcs = ["gflags/src/gflags.h.in"],
38        out = "gflags/gflags.h",
39        cmd = (common_preamble + "awk '{ " +
40               "gsub(/@GFLAGS_ATTRIBUTE_UNUSED@/, \"\"); " +
41               "gsub(/@INCLUDE_GFLAGS_NS_H@/, \"" + "\n".join(["#include \\\"gflags/{}\\\"".format(hdr) for hdr in gflags_ns_h_files]) + "\"); " +
42               "print; }' $SRCS > $OUT"),
43    )
44
45    # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
46    native.genrule(
47        name = "gflags_completions_h",
48        srcs = ["gflags/src/gflags_completions.h.in"],
49        out = "gflags/gflags_completions.h",
50        cmd = common_preamble + "awk '{ gsub(/@GFLAGS_NAMESPACE@/, \"" + namespace[0] + "\"); print; }' $SRCS > $OUT",
51    )
52    headers = {
53        "config.h": "gflags/src/config.h",
54        "mutex.h": "gflags/src/mutex.h",
55        "util.h": "gflags/src/util.h",
56        "windows_port.h": "gflags/src/windows_port.h",
57    }
58    exported_headers = {
59        "gflags/gflags.h": ":gflags_h",
60        "gflags/gflags_completions.h": ":gflags_completions_h",
61        "gflags/gflags_declare.h": ":gflags_declare_h",
62    }
63    exported_headers.update({"gflags/" + hdr: ":" + hdr.replace(".", "_") for hdr in gflags_ns_h_files})
64    srcs = [
65        "gflags/src/gflags.cc",
66        "gflags/src/gflags_completions.cc",
67        "gflags/src/gflags_reporting.cc",
68    ]
69    return [exported_headers, headers, srcs]
70
71# ------------------------------------------------------------------------------
72# Add native rule to build gflags library
73def gflags_library(name, exported_headers = {}, headers = {}, srcs = [], threads = True, deps = [], enable_static_variant = None, **kwargs):
74    copts_common = [
75        "-DHAVE_STDINT_H",
76        "-DHAVE_SYS_TYPES_H",
77        "-DHAVE_INTTYPES_H",
78        "-DHAVE_SYS_STAT_H",
79        "-DHAVE_UNISTD_H",
80        "-DHAVE_STRTOLL",
81        "-DHAVE_STRTOQ",
82        "-DHAVE_RWLOCK",
83        "-DGFLAGS_INTTYPES_FORMAT_C99",
84        "-DGFLAGS_IS_A_DLL=0",
85        "-DGFLAGS_BAZEL_BUILD",  # to avoid defines.h include
86    ]
87
88    copts = copts_common + [
89        "-DHAVE_FNMATCH_H",
90        "-DHAVE_PTHREAD",
91    ]
92
93    pthread_deps = []
94    copts.append("-DNO_THREADS")
95
96    # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
97    native.cxx_library(
98        name = name,
99        deps = [":_" + name],
100        exported_headers = exported_headers,
101        header_namespace = "",
102        visibility = ["PUBLIC"],
103    )
104
105    # @lint-ignore BUCKLINT: native and fb_native are explicitly forbidden in fbcode.
106    native.cxx_library(
107        name = "_" + name,
108        srcs = srcs,
109        headers = headers,
110        # Without header_namespace = "", include requires <third-party/gflags/gflags.h>
111        # this change enables us to do `#include <gflags/gflags.h>
112        header_namespace = "",
113        soname = "lib{}.$(ext)".format(name),
114        exported_headers = exported_headers,
115        labels = [
116            "depslint_never_add",  # Depslint should not add deps on these
117        ],
118        preprocessor_flags = copts,
119        deps = deps + pthread_deps,
120        visibility = ["PUBLIC"],
121        **kwargs
122    )
123