1# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2from __future__ import absolute_import
3from __future__ import division
4from __future__ import print_function
5from __future__ import unicode_literals
6
7rocksdb_target_header_template = \
8    """# This file \100generated by:
9#$ python3 buckifier/buckify_rocksdb.py{extra_argv}
10# --> DO NOT EDIT MANUALLY <--
11# This file is a Facebook-specific integration for buck builds, so can
12# only be validated by Facebook employees.
13#
14load("@fbcode_macros//build_defs:auto_headers.bzl", "AutoHeaders")
15load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
16load(":defs.bzl", "test_binary")
17
18REPO_PATH = package_name() + "/"
19
20ROCKSDB_COMPILER_FLAGS_0 = [
21    "-fno-builtin-memcmp",
22    # Needed to compile in fbcode
23    "-Wno-expansion-to-defined",
24    # Added missing flags from output of build_detect_platform
25    "-Wnarrowing",
26    "-DROCKSDB_NO_DYNAMIC_EXTENSION",
27]
28
29ROCKSDB_EXTERNAL_DEPS = [
30    ("bzip2", None, "bz2"),
31    ("snappy", None, "snappy"),
32    ("zlib", None, "z"),
33    ("gflags", None, "gflags"),
34    ("lz4", None, "lz4"),
35    ("zstd", None, "zstd"),
36]
37
38ROCKSDB_OS_DEPS_0 = [
39    (
40        "linux",
41        ["third-party//numa:numa", "third-party//liburing:uring", "third-party//tbb:tbb"],
42    ),
43    (
44        "macos",
45        ["third-party//tbb:tbb"],
46    ),
47]
48
49ROCKSDB_OS_PREPROCESSOR_FLAGS_0 = [
50    (
51        "linux",
52        [
53            "-DOS_LINUX",
54            "-DROCKSDB_FALLOCATE_PRESENT",
55            "-DROCKSDB_MALLOC_USABLE_SIZE",
56            "-DROCKSDB_PTHREAD_ADAPTIVE_MUTEX",
57            "-DROCKSDB_RANGESYNC_PRESENT",
58            "-DROCKSDB_SCHED_GETCPU_PRESENT",
59            "-DROCKSDB_IOURING_PRESENT",
60            "-DHAVE_SSE42",
61            "-DLIBURING",
62            "-DNUMA",
63            "-DROCKSDB_PLATFORM_POSIX",
64            "-DROCKSDB_LIB_IO_POSIX",
65            "-DTBB",
66        ],
67    ),
68    (
69        "macos",
70        [
71            "-DOS_MACOSX",
72            "-DROCKSDB_PLATFORM_POSIX",
73            "-DROCKSDB_LIB_IO_POSIX",
74            "-DTBB",
75        ],
76    ),
77    (
78        "windows",
79        [ "-DOS_WIN", "-DWIN32", "-D_MBCS", "-DWIN64", "-DNOMINMAX" ]
80    ),
81]
82
83ROCKSDB_PREPROCESSOR_FLAGS = [
84    "-DROCKSDB_SUPPORT_THREAD_LOCAL",
85
86    # Flags to enable libs we include
87    "-DSNAPPY",
88    "-DZLIB",
89    "-DBZIP2",
90    "-DLZ4",
91    "-DZSTD",
92    "-DZSTD_STATIC_LINKING_ONLY",
93    "-DGFLAGS=gflags",
94
95    # Added missing flags from output of build_detect_platform
96    "-DROCKSDB_BACKTRACE",
97]
98
99# Directories with files for #include
100ROCKSDB_INCLUDE_PATHS = [
101    "",
102    "include",
103]
104
105ROCKSDB_ARCH_PREPROCESSOR_FLAGS = {{
106    "x86_64": [
107        "-DHAVE_PCLMUL",
108    ],
109}}
110
111build_mode = read_config("fbcode", "build_mode")
112
113is_opt_mode = build_mode.startswith("opt")
114
115# -DNDEBUG is added by default in opt mode in fbcode. But adding it twice
116# doesn't harm and avoid forgetting to add it.
117ROCKSDB_COMPILER_FLAGS = ROCKSDB_COMPILER_FLAGS_0 + (["-DNDEBUG"] if is_opt_mode else [])
118
119sanitizer = read_config("fbcode", "sanitizer")
120
121# Do not enable jemalloc if sanitizer presents. RocksDB will further detect
122# whether the binary is linked with jemalloc at runtime.
123ROCKSDB_OS_PREPROCESSOR_FLAGS = ROCKSDB_OS_PREPROCESSOR_FLAGS_0 + ([(
124    "linux",
125    ["-DROCKSDB_JEMALLOC"],
126)] if sanitizer == "" else [])
127
128ROCKSDB_OS_DEPS = ROCKSDB_OS_DEPS_0 + ([(
129    "linux",
130    ["third-party//jemalloc:headers"],
131)] if sanitizer == "" else [])
132
133ROCKSDB_LIB_DEPS = [
134    ":rocksdb_lib",
135    ":rocksdb_test_lib",
136] if not is_opt_mode else [":rocksdb_lib"]
137"""
138
139
140library_template = """
141cpp_library(
142    name = "{name}",
143    srcs = [{srcs}],
144    {headers_attr_prefix}headers = {headers},
145    arch_preprocessor_flags = ROCKSDB_ARCH_PREPROCESSOR_FLAGS,
146    compiler_flags = ROCKSDB_COMPILER_FLAGS,
147    os_deps = ROCKSDB_OS_DEPS,
148    os_preprocessor_flags = ROCKSDB_OS_PREPROCESSOR_FLAGS,
149    preprocessor_flags = ROCKSDB_PREPROCESSOR_FLAGS,
150    include_paths = ROCKSDB_INCLUDE_PATHS,
151    deps = [{deps}],
152    external_deps = ROCKSDB_EXTERNAL_DEPS{extra_external_deps},
153    link_whole = {link_whole},
154)
155"""
156
157rocksdb_library_template = """
158cpp_library(
159    name = "{name}",
160    srcs = [{srcs}],
161    {headers_attr_prefix}headers = {headers},
162    arch_preprocessor_flags = ROCKSDB_ARCH_PREPROCESSOR_FLAGS,
163    compiler_flags = ROCKSDB_COMPILER_FLAGS,
164    os_deps = ROCKSDB_OS_DEPS,
165    os_preprocessor_flags = ROCKSDB_OS_PREPROCESSOR_FLAGS,
166    preprocessor_flags = ROCKSDB_PREPROCESSOR_FLAGS,
167    include_paths = ROCKSDB_INCLUDE_PATHS,
168    deps = ROCKSDB_LIB_DEPS,
169    external_deps = ROCKSDB_EXTERNAL_DEPS,
170)
171"""
172
173binary_template = """
174cpp_binary(
175    name = "{name}",
176    srcs = [{srcs}],
177    arch_preprocessor_flags = ROCKSDB_ARCH_PREPROCESSOR_FLAGS,
178    compiler_flags = ROCKSDB_COMPILER_FLAGS,
179    preprocessor_flags = ROCKSDB_PREPROCESSOR_FLAGS,
180    include_paths = ROCKSDB_INCLUDE_PATHS,
181    deps = [{deps}],
182    external_deps = ROCKSDB_EXTERNAL_DEPS,
183)
184"""
185
186test_cfg_template = """    [
187        "%s",
188        "%s",
189        "%s",
190        %s,
191        %s,
192    ],
193"""
194
195unittests_template = """
196# [test_name, test_src, test_type, extra_deps, extra_compiler_flags]
197ROCKS_TESTS = [
198{tests}]
199
200# Generate a test rule for each entry in ROCKS_TESTS
201# Do not build the tests in opt mode, since SyncPoint and other test code
202# will not be included.
203[
204    cpp_unittest(
205        name = test_name,
206        srcs = [test_cc],
207        arch_preprocessor_flags = ROCKSDB_ARCH_PREPROCESSOR_FLAGS,
208        os_preprocessor_flags = ROCKSDB_OS_PREPROCESSOR_FLAGS,
209        compiler_flags = ROCKSDB_COMPILER_FLAGS + extra_compiler_flags,
210        preprocessor_flags = ROCKSDB_PREPROCESSOR_FLAGS,
211        include_paths = ROCKSDB_INCLUDE_PATHS,
212        deps = [":rocksdb_test_lib"] + extra_deps,
213        external_deps = ROCKSDB_EXTERNAL_DEPS + [
214            ("googletest", None, "gtest"),
215        ],
216    )
217    for test_name, test_cc, parallelism, extra_deps, extra_compiler_flags in ROCKS_TESTS
218    if not is_opt_mode
219]
220"""
221