1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515
16- load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
16+ """Additive BUILD file for the librocksdb-sys Rust crate."""
17+
1718load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
1819load("@bazel_skylib//rules:select_file.bzl", "select_file")
1920load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
21+ load("@rules_rust//bindgen:defs.bzl", "rust_bindgen")
2022
21- """Additive BUILD file for the librocksdb-sys Rust crate."""
22-
23- # Copied from https://github.com/tensorflow/tensorflow/blob/bdd8bf316e4ab7d699127d192d30eb614a158462/third_party/snappy.BUILD
23+ # Derived from <https://github.com/rust-rocksdb/rust-rocksdb/blob/7f9cba4a819e76d8022733b4c82509aec6056938/librocksdb-sys/build.rs>
2424
2525cc_library(
2626 name = "snappy",
2727 srcs = [
2828 "snappy/snappy.cc",
29- "snappy/snappy.h",
30- "snappy/snappy-internal.h",
29+ "snappy/snappy-c.cc",
3130 "snappy/snappy-sinksource.cc",
32- "snappy/snappy-sinksource.h",
33- "snappy/snappy-stubs-internal.cc",
34- "snappy/snappy-stubs-internal.h",
35- ":config_h",
36- ":snappy_stubs_public_h",
3731 ],
3832 hdrs = [
33+ "snappy-stubs-public.h",
3934 "snappy/snappy.h",
35+ "snappy/snappy-c.h",
36+ "snappy/snappy-internal.h",
37+ "snappy/snappy-sinksource.h",
38+ "snappy/snappy-stubs-internal.h",
4039 ],
41- copts = [
42- "-DHAVE_CONFIG_H",
43- "-fno-exceptions",
44- "-Wno-sign-compare",
45- "-Wno-shift-negative-value",
46- "-Wno-implicit-function-declaration",
47- ],
48- defines = ["HAVE_SYS_UIO_H"],
49- includes = ["snappy/."],
50- )
51-
52- genrule(
53- name = "config_h",
54- outs = ["snappy/config.h"],
55- cmd = "\n".join([
56- "cat <<'EOF' >$@",
57- "#define HAVE_STDDEF_H 1",
58- "#define HAVE_STDINT_H 1",
59- "",
60- "#ifdef __has_builtin",
61- "# if !defined(HAVE_BUILTIN_EXPECT) && __has_builtin(__builtin_expect)",
62- "# define HAVE_BUILTIN_EXPECT 1",
63- "# endif",
64- "# if !defined(HAVE_BUILTIN_CTZ) && __has_builtin(__builtin_ctzll)",
65- "# define HAVE_BUILTIN_CTZ 1",
66- "# endif",
67- "#elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 4)",
68- "# ifndef HAVE_BUILTIN_EXPECT",
69- "# define HAVE_BUILTIN_EXPECT 1",
70- "# endif",
71- "# ifndef HAVE_BUILTIN_CTZ",
72- "# define HAVE_BUILTIN_CTZ 1",
73- "# endif",
74- "#endif",
75- "",
76- "#ifdef __has_include",
77- "# if !defined(HAVE_BYTESWAP_H) && __has_include(<byteswap.h>)",
78- "# define HAVE_BYTESWAP_H 1",
79- "# endif",
80- "# if !defined(HAVE_UNISTD_H) && __has_include(<unistd.h>)",
81- "# define HAVE_UNISTD_H 1",
82- "# endif",
83- "# if !defined(HAVE_SYS_ENDIAN_H) && __has_include(<sys/endian.h>)",
84- "# define HAVE_SYS_ENDIAN_H 1",
85- "# endif",
86- "# if !defined(HAVE_SYS_MMAN_H) && __has_include(<sys/mman.h>)",
87- "# define HAVE_SYS_MMAN_H 1",
88- "# endif",
89- "# if !defined(HAVE_SYS_UIO_H) && __has_include(<sys/uio.h>)",
90- "# define HAVE_SYS_UIO_H 1",
91- "# endif",
92- "#endif",
40+ copts = ["-std=c++11"],
41+ includes = [
9342 "",
94- "#ifndef SNAPPY_IS_BIG_ENDIAN",
95- "# ifdef __s390x__",
96- "# define SNAPPY_IS_BIG_ENDIAN 1",
97- "# elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__",
98- "# define SNAPPY_IS_BIG_ENDIAN 1",
99- "# endif",
100- "#endif",
101- "EOF",
102- ]),
103- )
104-
105- genrule(
106- name = "snappy_stubs_public_h",
107- srcs = ["snappy/snappy-stubs-public.h.in"],
108- outs = ["snappy/snappy-stubs-public.h"],
109- cmd = ("sed " +
110- "-e 's/$${\\(.*\\)_01}/\\1/g' " +
111- "-e 's/$${SNAPPY_MAJOR}/1/g' " +
112- "-e 's/$${SNAPPY_MINOR}/1/g' " +
113- "-e 's/$${SNAPPY_PATCHLEVEL}/4/g' " +
114- "$< >$@"),
43+ "snappy",
44+ ],
45+ local_defines = ["NDEBUG=1"],
11546)
11647
11748filegroup(
@@ -174,6 +105,7 @@ cmake(
174105 },
175106 no_match_error = "Building rocksdb for the specified CPU is not supported.",
176107 ),
108+ includes = ["include/rocksdb/c.h"],
177109 lib_source = ":rocksdb_srcs",
178110 out_static_libs = ["librocksdb.a"],
179111 targets = ["rocksdb"],
@@ -186,52 +118,44 @@ cmake(
186118 ],
187119)
188120
189- filegroup(
190- name = "out_dir",
191- srcs = [":rocksdb"],
192- visibility = ["//visibility:public"],
193- )
194-
195121select_file(
196- name = "librocksdb ",
197- srcs = ":out_dir ",
122+ name = "librocksdb_a ",
123+ srcs = ":rocksdb ",
198124 subpath = "librocksdb.a",
199125)
200126
201- copy_file(
202- name = "librocksdb_copy",
203- src = ":librocksdb",
204- out = "librocksdb.a",
205- allow_symlink = False,
206- )
207-
208- copy_to_directory(
209- name = "rocksdb_lib",
210- srcs = [":librocksdb_copy"],
211- visibility = ["//visibility:public"],
127+ filegroup(
128+ name = "rocksdb_include",
129+ srcs = glob(
130+ include = ["rocksdb/include/rocksdb/**/*.h"],
131+ ),
212132)
213133
214- # Copy the include folder so we can specify `ROCKSDB_INCLUDE_DIR`
215- #
216- # Note: We used to use `select_file` here but it generated symlinks that
217- # overlapped with others and spammed the logs with WARNINGs.
218- copy_to_directory(
219- name = "rocksdb_include",
220- srcs = [":out_dir"],
221- out = "include",
222- root_paths = ["rocksdb/include"],
223- visibility = ["//visibility:public"],
134+ # We need to expose the header files with the rocksdb static lib. The `cmake`
135+ # rules doesn't give us a way to do that, so we manually piece it together.
136+ cc_import(
137+ name = "librocksdb",
138+ hdrs = [":rocksdb_include"],
139+ static_library = ":librocksdb_a",
224140)
225141
226- select_file(
227- name = "libsnappy",
228- srcs = ":snappy",
229- subpath = "libsnappy.a",
142+ rust_bindgen(
143+ name = "bindings",
144+ bindgen_flags = [
145+ "--no-derive-debug",
146+ "--blocklist-type=max_align_t",
147+ "--ctypes-prefix=libc",
148+ ],
149+ cc_lib = ":librocksdb",
150+ header = "rocksdb/include/rocksdb/c.h",
230151)
231152
153+ # Place the generated artifacts into an OUT_DIR.
154+ #
155+ # TODO(parkmycar): <https://github.com/bazelbuild/rules_rust/issues/3184>
232156copy_to_directory(
233- name = "snappy_lib ",
234- srcs = [":libsnappy "],
157+ name = "out_dir ",
158+ srcs = [":bindings "],
235159 visibility = ["//visibility:public"],
236160)
237161
0 commit comments