gflags.bzl 3.4 KB

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