BUILD.rocksdb.bazel 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License in the LICENSE file at the
  6. # root of this repository, or online at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Additive BUILD file for the librocksdb-sys Rust crate."""
  16. load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
  17. load("@bazel_skylib//rules:select_file.bzl", "select_file")
  18. load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
  19. load("@rules_rust_bindgen//:defs.bzl", "rust_bindgen")
  20. # Derived from <https://github.com/rust-rocksdb/rust-rocksdb/blob/7f9cba4a819e76d8022733b4c82509aec6056938/librocksdb-sys/build.rs>
  21. cc_library(
  22. name = "snappy",
  23. srcs = [
  24. "snappy/snappy.cc",
  25. "snappy/snappy-c.cc",
  26. "snappy/snappy-sinksource.cc",
  27. ],
  28. hdrs = [
  29. "snappy-stubs-public.h",
  30. "snappy/snappy.h",
  31. "snappy/snappy-c.h",
  32. "snappy/snappy-internal.h",
  33. "snappy/snappy-sinksource.h",
  34. "snappy/snappy-stubs-internal.h",
  35. ],
  36. copts = ["-std=c++11"],
  37. includes = [
  38. "",
  39. "snappy",
  40. ],
  41. local_defines = ["NDEBUG=1"],
  42. )
  43. filegroup(
  44. name = "rocksdb_srcs",
  45. srcs = glob(["**"]),
  46. visibility = ["//visibility:public"],
  47. )
  48. cmake(
  49. name = "rocksdb",
  50. build_args = ["-j8"],
  51. copts = [
  52. # https://github.com/facebook/rocksdb/issues/13558
  53. "-Wno-nontrivial-memaccess",
  54. ] + select(
  55. {
  56. "@platforms//cpu:x86_64": [
  57. # rocksdb requires these flags to be enabled, all instances we
  58. # run on support them.
  59. "-msse4.2",
  60. "-mpclmul",
  61. ],
  62. "@platforms//cpu:aarch64": [],
  63. },
  64. no_match_error = "Building rocksdb for the specified CPU is not supported.",
  65. ),
  66. generate_args = [
  67. "-DWITH_SNAPPY=1",
  68. "-DWITH_LZ4=1",
  69. "-DWITH_ZSTD=1",
  70. "-DWITH_GFLAGS=OFF",
  71. "-DWITH_ALL_TESTS=OFF",
  72. "-DWITH_TESTS=OFF",
  73. "-DWITH_TOOLS=OFF",
  74. "-DUSE_RTTI=1",
  75. "-DROCKSDB_BUILD_SHARED=OFF",
  76. # Without this set, rocksdb enables `-march=native` which prevents us
  77. # from running across various different instances.
  78. "-DPORTABLE=1",
  79. # `cmake` tries _very_ hard to find libraries to link against, and it
  80. # generally prefers dynamic libraries in the sysroot, which is exactly
  81. # what we don't want because it breaks hermeticity.
  82. #
  83. # We set a number of options here to limit what `cmake` will search for
  84. # so we link against our static libraries.
  85. "-DCMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH=0",
  86. "-DCMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH=0",
  87. "-DCMAKE_FIND_USE_CMAKE_SYSTEM_PATH=0",
  88. # Uncomment this if you ever need to debug what library cmake is resolving.
  89. # "-DCMAKE_FIND_DEBUG_MODE=TRUE",
  90. ] + select(
  91. {
  92. "@platforms//os:macos": [
  93. "-DCMAKE_OSX_DEPLOYMENT_TARGET=14.0",
  94. "-DCMAKE_SYSTEM_NAME=Darwin",
  95. ],
  96. "@platforms//os:linux": ["-DCMAKE_SYSTEM_NAME=Linux"],
  97. },
  98. no_match_error = "Building rocksdb for the specified OS is not supported.",
  99. ) + select(
  100. {
  101. "@platforms//cpu:x86_64": ["-DCMAKE_SYSTEM_PROCESSOR=x86_64"],
  102. "@platforms//cpu:aarch64": ["-DCMAKE_SYSTEM_PROCESSOR=aarch64"],
  103. },
  104. no_match_error = "Building rocksdb for the specified CPU is not supported.",
  105. ),
  106. includes = ["include/rocksdb/c.h"],
  107. lib_source = ":rocksdb_srcs",
  108. out_static_libs = ["librocksdb.a"],
  109. targets = ["rocksdb"],
  110. visibility = ["//visibility:public"],
  111. working_directory = "rocksdb/",
  112. deps = [
  113. ":snappy",
  114. "@lz4",
  115. "@zstd",
  116. ],
  117. )
  118. select_file(
  119. name = "librocksdb_a",
  120. srcs = ":rocksdb",
  121. subpath = "librocksdb.a",
  122. )
  123. filegroup(
  124. name = "rocksdb_include",
  125. srcs = glob(
  126. include = ["rocksdb/include/rocksdb/**/*.h"],
  127. ),
  128. )
  129. # We need to expose the header files with the rocksdb static lib. The `cmake`
  130. # rules doesn't give us a way to do that, so we manually piece it together.
  131. cc_import(
  132. name = "librocksdb",
  133. hdrs = [":rocksdb_include"],
  134. static_library = ":librocksdb_a",
  135. )
  136. rust_bindgen(
  137. name = "bindings",
  138. bindgen_flags = [
  139. "--no-derive-debug",
  140. "--blocklist-type=max_align_t",
  141. "--ctypes-prefix=libc",
  142. ],
  143. cc_lib = ":librocksdb",
  144. header = "rocksdb/include/rocksdb/c.h",
  145. )
  146. # Place the generated artifacts into an OUT_DIR.
  147. #
  148. # TODO(parkmycar): <https://github.com/bazelbuild/rules_rust/issues/3184>
  149. copy_to_directory(
  150. name = "out_dir",
  151. srcs = [":bindings"],
  152. visibility = ["//visibility:public"],
  153. )
  154. # Licensed to the Apache Software Foundation (ASF) under one or more
  155. # contributor license agreements. See the NOTICE file distributed with
  156. # this work for additional information regarding copyright ownership.
  157. # The ASF licenses this file to You under the Apache License, Version 2.0
  158. # (the "License"); you may not use this file except in compliance with
  159. # the License. You may obtain a copy of the License at
  160. #
  161. # http://www.apache.org/licenses/LICENSE-2.0
  162. #
  163. # Unless required by applicable law or agreed to in writing, software
  164. # distributed under the License is distributed on an "AS IS" BASIS,
  165. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  166. # See the License for the specific language governing permissions and
  167. # limitations under the License.
  168. #
  169. # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  170. # Licensed under the Apache License, Version 2.0 (the "License");
  171. # you may not use this file except in compliance with the License.
  172. # You may obtain a copy of the License at
  173. #
  174. # http://www.apache.org/licenses/LICENSE-2.0
  175. #
  176. # Unless required by applicable law or agreed to in writing, software
  177. # distributed under the License is distributed on an "AS IS" BASIS,
  178. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  179. # See the License for the specific language governing permissions and
  180. # limitations under the License.