ray.bzl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_library_public")
  2. load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
  3. load("@bazel_common//tools/maven:pom_file.bzl", "pom_file")
  4. load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
  5. COPTS_WITHOUT_LOG = select({
  6. "//:opt": ["-DBAZEL_OPT"],
  7. "//conditions:default": [],
  8. }) + select({
  9. "@platforms//os:windows": [
  10. # TODO(mehrdadn): (How to) support dynamic linking?
  11. "-DRAY_STATIC",
  12. ],
  13. "//conditions:default": [],
  14. }) + select({
  15. "//:clang-cl": [
  16. "-Wno-builtin-macro-redefined", # To get rid of warnings caused by deterministic build macros (e.g. #define __DATE__ "redacted")
  17. "-Wno-microsoft-unqualified-friend", # This shouldn't normally be enabled, but otherwise we get: google/protobuf/map_field.h: warning: unqualified friend declaration referring to type outside of the nearest enclosing namespace is a Microsoft extension; add a nested name specifier (for: friend class DynamicMessage)
  18. ],
  19. "//conditions:default": [],
  20. })
  21. COPTS = COPTS_WITHOUT_LOG
  22. PYX_COPTS = select({
  23. "//:msvc-cl": [],
  24. "//conditions:default": [
  25. # Ignore this warning since CPython and Cython have issue removing deprecated tp_print on MacOS
  26. "-Wno-deprecated-declarations",
  27. ],
  28. }) + select({
  29. "@platforms//os:windows": [
  30. "/FI" + "src/shims/windows/python-nondebug.h",
  31. ],
  32. "//conditions:default": [],
  33. })
  34. PYX_SRCS = [] + select({
  35. "@platforms//os:windows": [
  36. "src/shims/windows/python-nondebug.h",
  37. ],
  38. "//conditions:default": [],
  39. })
  40. def flatbuffer_py_library(name, srcs, outs, out_prefix, includes = [], include_paths = []):
  41. flatbuffer_library_public(
  42. name = name,
  43. srcs = srcs,
  44. outs = outs,
  45. language_flag = "-p",
  46. out_prefix = out_prefix,
  47. include_paths = include_paths,
  48. includes = includes,
  49. )
  50. def define_java_module(
  51. name,
  52. additional_srcs = [],
  53. exclude_srcs = [],
  54. additional_resources = [],
  55. define_test_lib = False,
  56. test_deps = [],
  57. **kwargs):
  58. lib_name = "io_ray_ray_" + name
  59. pom_file_targets = [lib_name]
  60. native.java_library(
  61. name = lib_name,
  62. srcs = additional_srcs + native.glob(
  63. [name + "/src/main/java/**/*.java"],
  64. exclude = exclude_srcs,
  65. ),
  66. resources = native.glob([name + "/src/main/resources/**"]) + additional_resources,
  67. **kwargs
  68. )
  69. if define_test_lib:
  70. test_lib_name = "io_ray_ray_" + name + "_test"
  71. pom_file_targets.append(test_lib_name)
  72. native.java_library(
  73. name = test_lib_name,
  74. srcs = native.glob([name + "/src/test/java/**/*.java"]),
  75. deps = test_deps,
  76. )
  77. pom_file(
  78. name = "io_ray_ray_" + name + "_pom",
  79. targets = pom_file_targets,
  80. template_file = name + "/pom_template.xml",
  81. substitutions = {
  82. "{auto_gen_header}": "<!-- This file is auto-generated by Bazel from pom_template.xml, do not modify it. -->",
  83. },
  84. )
  85. def copy_to_workspace(name, srcs, dstdir = ""):
  86. if dstdir.startswith("/") or dstdir.startswith("\\"):
  87. fail("Subdirectory must be a relative path: " + dstdir)
  88. src_locations = " ".join(["$(locations %s)" % (src,) for src in srcs])
  89. native.genrule(
  90. name = name,
  91. srcs = srcs,
  92. outs = [name + ".out"],
  93. cmd = r"""
  94. mkdir -p -- {dstdir}
  95. for f in {locations}; do
  96. rm -f -- {dstdir}$${{f##*/}}
  97. cp -f -- "$$f" {dstdir}
  98. done
  99. date > $@
  100. """.format(
  101. locations = src_locations,
  102. dstdir = "." + ("/" + dstdir.replace("\\", "/")).rstrip("/") + "/",
  103. ),
  104. local = 1,
  105. tags = ["no-cache"],
  106. )
  107. def native_java_binary(module_name, name, native_binary_name):
  108. """Copy native binary file to different path based on operating systems"""
  109. copy_file(
  110. name = name + "_darwin",
  111. src = native_binary_name,
  112. out = module_name + "/src/main/resources/native/darwin/" + name,
  113. )
  114. copy_file(
  115. name = name + "_linux",
  116. src = native_binary_name,
  117. out = module_name + "/src/main/resources/native/linux/" + name,
  118. )
  119. copy_file(
  120. name = name + "_windows",
  121. src = native_binary_name,
  122. out = module_name + "/src/main/resources/native/windows/" + name,
  123. )
  124. native.filegroup(
  125. name = name,
  126. srcs = select({
  127. "@platforms//os:osx": [name + "_darwin"],
  128. "@platforms//os:windows": [name + "_windows"],
  129. "//conditions:default": [name + "_linux"],
  130. }),
  131. visibility = ["//visibility:public"],
  132. )
  133. def native_java_library(module_name, name, native_library_name):
  134. """Copy native library file to different path based on operating systems"""
  135. copy_file(
  136. name = name + "_darwin",
  137. src = native_library_name,
  138. out = module_name + "/src/main/resources/native/darwin/lib{}.dylib".format(name),
  139. )
  140. copy_file(
  141. name = name + "_linux",
  142. src = native_library_name,
  143. out = module_name + "/src/main/resources/native/linux/lib{}.so".format(name),
  144. )
  145. native.filegroup(
  146. name = name,
  147. srcs = select({
  148. "@platforms//os:osx": [name + "_darwin"],
  149. "@platforms//os:windows": [],
  150. "//conditions:default": [name + "_linux"],
  151. }),
  152. visibility = ["//visibility:public"],
  153. )
  154. def ray_cc_library(name, strip_include_prefix = "/src", copts = [], **kwargs):
  155. cc_library(
  156. name = name,
  157. strip_include_prefix = strip_include_prefix,
  158. copts = COPTS + copts,
  159. visibility = ["//visibility:public"],
  160. **kwargs
  161. )
  162. def ray_cc_test(name, linkopts = [], copts = [], **kwargs):
  163. cc_test(
  164. name = name,
  165. copts = COPTS + copts,
  166. linkopts = linkopts + ["-pie"],
  167. **kwargs
  168. )
  169. def ray_cc_binary(name, linkopts = [], copts = [], **kwargs):
  170. cc_binary(
  171. name = name,
  172. copts = COPTS + copts,
  173. linkopts = linkopts + ["-pie"],
  174. **kwargs
  175. )
  176. def _filter_files_with_suffix_impl(ctx):
  177. suffix = ctx.attr.suffix
  178. filtered_files = [f for f in ctx.files.srcs if f.basename.endswith(suffix)]
  179. print(filtered_files)
  180. return [
  181. DefaultInfo(
  182. files = depset(filtered_files),
  183. ),
  184. ]
  185. filter_files_with_suffix = rule(
  186. implementation = _filter_files_with_suffix_impl,
  187. attrs = {
  188. "srcs": attr.label_list(allow_files = True),
  189. "suffix": attr.string(),
  190. },
  191. )