Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | # Description: |
| 2 | # BUILD rules for generating flatbuffer files in various languages. |
| 3 | |
| 4 | """ |
| 5 | Rules for building C++ flatbuffers with Bazel. |
| 6 | """ |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 7 | |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 8 | load("@io_bazel_rules_go//go:def.bzl", "go_library") |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 9 | load("@rules_rust//rust:defs.bzl", "rust_library") |
| 10 | load("@rules_rust//rust:rust_common.bzl", "CrateInfo") |
Philipp Schrader | 175a93c | 2023-02-19 13:13:40 -0800 | [diff] [blame] | 11 | load("@aspect_rules_ts//ts:defs.bzl", "ts_project") |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 12 | load("@rules_cc//cc:defs.bzl", "cc_library") |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 13 | |
| 14 | flatc_path = "@com_github_google_flatbuffers//:flatc" |
| 15 | |
| 16 | DEFAULT_INCLUDE_PATHS = [ |
| 17 | "./", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 18 | ] |
| 19 | |
| 20 | DEFAULT_FLATC_ARGS = [ |
| 21 | "--gen-object-api", |
| 22 | "--gen-compare", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | "--keep-prefix", |
James Kuszmaul | 79794f2 | 2023-11-06 13:35:59 -0800 | [diff] [blame] | 24 | "--bfbs-builtins", |
milind upadhyay | 328d339 | 2020-11-25 19:34:00 -0800 | [diff] [blame] | 25 | "--cpp-std", |
| 26 | "c++17", |
Austin Schuh | f13042b | 2020-11-25 23:11:41 -0800 | [diff] [blame] | 27 | "--require-explicit-ids", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 28 | "--gen-mutable", |
| 29 | "--reflect-names", |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 30 | "--cpp-ptr-type", |
| 31 | "flatbuffers::unique_ptr", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | "--force-empty", |
Austin Schuh | 872723c | 2019-12-25 14:38:09 -0800 | [diff] [blame] | 33 | "--scoped-enums", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | "--gen-name-strings", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 35 | ] |
| 36 | |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 37 | DEFAULT_FLATC_GO_ARGS = [ |
| 38 | "--gen-onefile", |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 39 | "--gen-object-api", |
Philipp Schrader | 2d4ad41 | 2022-02-25 15:56:20 -0800 | [diff] [blame] | 40 | "--require-explicit-ids", |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 41 | ] |
| 42 | |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 43 | DEFAULT_FLATC_RUST_ARGS = [ |
| 44 | "--gen-object-api", |
| 45 | "--require-explicit-ids", |
Brian Silverman | 90221f8 | 2022-08-22 23:46:09 -0700 | [diff] [blame] | 46 | "--gen-name-strings", |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 47 | ] |
| 48 | |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 49 | """Contains information about a set of flatbuffers which have their code for |
| 50 | reading/writing generated in a single library-style rule. |
| 51 | |
| 52 | Fields: |
| 53 | srcs: [File], the .fbs source files |
| 54 | """ |
| 55 | FlatbufferLibraryInfo = provider() |
| 56 | |
| 57 | def _flatbuffer_library_compile_impl(ctx): |
| 58 | outs = [] |
| 59 | commands = [] |
| 60 | for src in ctx.files.srcs: |
| 61 | if ctx.attr.tables_for_filenames: |
| 62 | out_dir = None |
| 63 | for table in ctx.attr.tables_for_filenames: |
| 64 | out = ctx.actions.declare_file(ctx.attr.out_prefix + table + ctx.attr.output_suffix) |
| 65 | this_out_dir = "/".join(out.dirname.split("/")[:-(len(ctx.attr.out_prefix.split("/")) - 1)]) |
| 66 | if out_dir: |
| 67 | if this_out_dir != out_dir: |
| 68 | fail("Trying to write to multiple directories") |
| 69 | else: |
| 70 | out_dir = this_out_dir |
| 71 | outs.append(out) |
| 72 | else: |
| 73 | out = ctx.actions.declare_file(ctx.attr.out_prefix + src.basename.replace(".fbs", "") + ctx.attr.output_suffix) |
| 74 | outs.append(out) |
| 75 | out_dir = out.dirname |
| 76 | arguments = [ctx.executable._flatc.path] |
| 77 | for path in ctx.attr.include_paths: |
| 78 | for subpath in ["", ctx.bin_dir.path + "/"]: |
| 79 | arguments.append("-I") |
| 80 | arguments.append(subpath + path) |
| 81 | arguments.append("-I") |
| 82 | arguments.append("%s.runfiles/com_github_google_flatbuffers" % ctx.executable._flatc.path) |
| 83 | arguments.extend(ctx.attr.flatc_args) |
| 84 | arguments.extend(ctx.attr.language_flags) |
| 85 | arguments.extend([ |
| 86 | "-o", |
| 87 | out_dir, |
| 88 | ]) |
| 89 | arguments.append(src.path) |
| 90 | commands.append(arguments) |
| 91 | ctx.actions.run_shell( |
| 92 | outputs = outs, |
| 93 | inputs = ctx.files.srcs + ctx.files.includes, |
| 94 | tools = [ctx.executable._flatc], |
| 95 | command = " && ".join([" ".join(arguments) for arguments in commands]), |
| 96 | mnemonic = "Flatc", |
| 97 | progress_message = "Generating flatbuffer files for %{input}:", |
| 98 | ) |
Philipp Schrader | 46f4004 | 2024-01-24 17:16:03 -0800 | [diff] [blame] | 99 | return [DefaultInfo(files = depset(outs)), FlatbufferLibraryInfo(srcs = ctx.files.srcs)] |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 100 | |
| 101 | _flatbuffer_library_compile = rule( |
| 102 | implementation = _flatbuffer_library_compile_impl, |
| 103 | attrs = { |
| 104 | "srcs": attr.label_list(mandatory = True, allow_files = True), |
| 105 | "output_suffix": attr.string(mandatory = True), |
| 106 | "tables_for_filenames": attr.string_list(mandatory = False), |
| 107 | "language_flags": attr.string_list(mandatory = True), |
| 108 | "includes": attr.label_list(default = [], allow_files = True), |
| 109 | "include_paths": attr.string_list(default = []), |
| 110 | "flatc_args": attr.string_list(default = []), |
| 111 | "out_prefix": attr.string(default = ""), |
| 112 | "_flatc": attr.label(executable = True, cfg = "exec", default = Label(flatc_path)), |
| 113 | }, |
| 114 | ) |
| 115 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 116 | def flatbuffer_library_public( |
| 117 | name, |
| 118 | srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 119 | output_suffix, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 120 | language_flag, |
| 121 | out_prefix = "", |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 122 | tables_for_filenames = None, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 123 | includes = [], |
| 124 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 125 | flatc_args = DEFAULT_FLATC_ARGS, |
| 126 | reflection_name = "", |
| 127 | reflection_visibility = None, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 128 | compatible_with = None, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 129 | restricted_to = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 130 | target_compatible_with = None, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 131 | output_to_bindir = False, |
| 132 | visibility = None): |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 133 | """Generates code files for reading/writing the given flatbuffers in the |
| 134 | requested language using the public compiler. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 135 | |
| 136 | Args: |
| 137 | name: Rule name. |
| 138 | srcs: Source .fbs files. Sent in order to the compiler. |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 139 | output_suffix: Suffix for output files from flatc. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 140 | language_flag: Target language flag. One of [-c, -j, -js]. |
| 141 | out_prefix: Prepend this path to the front of all generated files except on |
| 142 | single source targets. Usually is a directory name. |
| 143 | includes: Optional, list of filegroups of schemas that the srcs depend on. |
| 144 | include_paths: Optional, list of paths the includes files can be found in. |
| 145 | flatc_args: Optional, list of additional arguments to pass to flatc. |
| 146 | reflection_name: Optional, if set this will generate the flatbuffer |
| 147 | reflection binaries for the schemas. |
| 148 | reflection_visibility: The visibility of the generated reflection Fileset. |
| 149 | output_to_bindir: Passed to genrule for output to bin directory. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 150 | compatible_with: Optional, The list of environments this rule can be |
| 151 | built for, in addition to default-supported environments. |
| 152 | restricted_to: Optional, The list of environments this rule can be built |
| 153 | for, instead of default-supported environments. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 154 | target_compatible_with: Optional, The list of target platform constraints |
| 155 | to use. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 156 | output_to_bindir: Passed to genrule for output to bin directory. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 157 | |
| 158 | |
| 159 | This rule creates a filegroup(name) with all generated source files, and |
| 160 | optionally a Fileset([reflection_name]) with all generated reflection |
| 161 | binaries. |
| 162 | """ |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 163 | _flatbuffer_library_compile( |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 164 | name = name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 165 | srcs = srcs, |
| 166 | output_suffix = output_suffix, |
| 167 | language_flags = [language_flag], |
| 168 | includes = includes, |
| 169 | include_paths = include_paths, |
| 170 | flatc_args = flatc_args, |
| 171 | out_prefix = out_prefix, |
| 172 | tables_for_filenames = tables_for_filenames, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 173 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 174 | target_compatible_with = target_compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 175 | restricted_to = restricted_to, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 176 | visibility = visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 177 | ) |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 178 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 179 | if reflection_name: |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 180 | _flatbuffer_library_compile( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | name = "%s_out" % reflection_name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 182 | srcs = srcs, |
| 183 | output_suffix = ".bfbs", |
| 184 | language_flags = ["-b", "--schema"], |
| 185 | includes = includes, |
| 186 | include_paths = include_paths, |
| 187 | flatc_args = flatc_args, |
| 188 | out_prefix = out_prefix, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 189 | compatible_with = compatible_with, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 190 | target_compatible_with = target_compatible_with, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 191 | restricted_to = restricted_to, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 192 | visibility = reflection_visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 193 | ) |
| 194 | |
| 195 | def flatbuffer_cc_library( |
| 196 | name, |
| 197 | srcs, |
| 198 | srcs_filegroup_name = "", |
| 199 | out_prefix = "", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 200 | deps = [], |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 201 | includes = [], |
| 202 | include_paths = DEFAULT_INCLUDE_PATHS, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 203 | cc_include_paths = [], |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 204 | flatc_args = DEFAULT_FLATC_ARGS, |
| 205 | visibility = None, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 206 | compatible_with = None, |
| 207 | restricted_to = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 208 | target_compatible_with = None, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 209 | srcs_filegroup_visibility = None, |
| 210 | gen_reflections = False): |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 211 | """A cc_library with the generated reader/writers for the given flatbuffer definitions. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 212 | |
| 213 | Args: |
| 214 | name: Rule name. |
| 215 | srcs: Source .fbs files. Sent in order to the compiler. |
| 216 | srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this |
| 217 | filegroup into the `includes` parameter of any other |
| 218 | flatbuffer_cc_library that depends on this one's schemas. |
| 219 | out_prefix: Prepend this path to the front of all generated files. Usually |
| 220 | is a directory name. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 221 | deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified |
| 222 | alongside includes. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 223 | includes: Optional, list of filegroups of schemas that the srcs depend on. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 224 | Use of this is discouraged, and may be deprecated. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 225 | include_paths: Optional, list of paths the includes files can be found in. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 226 | cc_include_paths: Optional, list of paths to add to the cc_library includes attribute. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 227 | flatc_args: Optional list of additional arguments to pass to flatc |
| 228 | (e.g. --gen-mutable). |
| 229 | visibility: The visibility of the generated cc_library. By default, use the |
| 230 | default visibility of the project. |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 231 | target_compatible_with: Optional, the list of constraints the target |
| 232 | platform must satisfy for this target to be considered compatible. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 233 | srcs_filegroup_visibility: The visibility of the generated srcs filegroup. |
| 234 | By default, use the value of the visibility parameter above. |
| 235 | gen_reflections: Optional, if true this will generate the flatbuffer |
| 236 | reflection binaries for the schemas. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 237 | compatible_with: Optional, The list of environments this rule can be built |
| 238 | for, in addition to default-supported environments. |
| 239 | restricted_to: Optional, The list of environments this rule can be built |
| 240 | for, instead of default-supported environments. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 241 | target_compatible_with: Optional, The list of target platform constraints |
| 242 | to use. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 243 | |
| 244 | This produces: |
| 245 | filegroup([name]_srcs): all generated .h files. |
| 246 | filegroup(srcs_filegroup_name if specified, or [name]_includes if not): |
| 247 | Other flatbuffer_cc_library's can pass this in for their `includes` |
| 248 | parameter, if they depend on the schemas in this library. |
| 249 | Fileset([name]_reflection): (Optional) all generated reflection binaries. |
| 250 | cc_library([name]): library with sources and flatbuffers deps. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 251 | """ |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 252 | if deps and includes: |
| 253 | # There is no inherent reason we couldn't support both, but this discourages |
| 254 | # use of includes without good reason. |
| 255 | fail("Cannot specify both deps and include in flatbuffer_cc_library.") |
| 256 | if deps: |
| 257 | includes = [d + "_includes" for d in deps] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 258 | reflection_name = "%s_reflection" % name if gen_reflections else "" |
| 259 | |
| 260 | srcs_lib = "%s_srcs" % (name) |
| 261 | flatbuffer_library_public( |
| 262 | name = srcs_lib, |
| 263 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 264 | output_suffix = "_generated.h", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 265 | language_flag = "-c", |
| 266 | out_prefix = out_prefix, |
| 267 | includes = includes, |
| 268 | include_paths = include_paths, |
| 269 | flatc_args = flatc_args, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 270 | compatible_with = compatible_with, |
| 271 | restricted_to = restricted_to, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 272 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 273 | reflection_name = reflection_name, |
| 274 | reflection_visibility = visibility, |
| 275 | ) |
| 276 | native.cc_library( |
| 277 | name = name, |
| 278 | hdrs = [ |
| 279 | ":" + srcs_lib, |
| 280 | ], |
| 281 | srcs = [ |
| 282 | ":" + srcs_lib, |
| 283 | ], |
| 284 | features = [ |
| 285 | "-parse_headers", |
| 286 | ], |
| 287 | deps = [ |
| 288 | "@com_github_google_flatbuffers//:runtime_cc", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 289 | "@com_github_google_flatbuffers//:flatbuffers", |
| 290 | ] + deps, |
| 291 | includes = cc_include_paths, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 292 | compatible_with = compatible_with, |
| 293 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 294 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 295 | linkstatic = 1, |
| 296 | visibility = visibility, |
| 297 | ) |
| 298 | |
| 299 | # A filegroup for the `srcs`. That is, all the schema files for this |
| 300 | # Flatbuffer set. |
| 301 | native.filegroup( |
| 302 | name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name), |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 303 | srcs = srcs + includes, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 304 | compatible_with = compatible_with, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 305 | restricted_to = restricted_to, |
| 306 | visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 307 | ) |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 308 | |
Brian Silverman | 2876027 | 2020-02-02 13:21:51 -0800 | [diff] [blame] | 309 | def flatbuffer_py_library( |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 310 | name, |
| 311 | srcs, |
| 312 | namespace, |
| 313 | tables, |
| 314 | compatible_with = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 315 | target_compatible_with = None, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 316 | includes = [], |
| 317 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 318 | flatc_args = DEFAULT_FLATC_ARGS, |
| 319 | visibility = None, |
| 320 | srcs_filegroup_visibility = None): |
| 321 | """Generates a py_library rule for a given flatbuffer definition. |
| 322 | |
| 323 | Args: |
| 324 | name: Name of the generated py_library rule. |
| 325 | srcs: Source .fbs file(s). |
| 326 | namespace: Namespace of the specified flatbuffer schema. Until |
| 327 | we make the rules sophisticated enough to figure out what |
| 328 | python files will be generated from a given schema, the user |
| 329 | has to manually specify this. |
| 330 | tables: List of table names--currently, we don't do anything to |
| 331 | automatically figure out how to handle the fact that a separate |
| 332 | python file will be generated for every table definition, and that |
| 333 | we can't know what files will be output until after the file has |
| 334 | been parsed. As such, we just force the user to manually specify |
| 335 | things. |
| 336 | """ |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 337 | |
| 338 | srcs_lib = "%s_srcs" % (name) |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 339 | if not tables: |
| 340 | fail("Must specify the list of tables") |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 341 | flatbuffer_library_public( |
| 342 | name = srcs_lib, |
| 343 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 344 | output_suffix = ".py", |
| 345 | out_prefix = namespace.replace(".", "/") + "/", |
| 346 | tables_for_filenames = tables, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 347 | language_flag = "--python", |
| 348 | includes = includes, |
| 349 | include_paths = include_paths, |
| 350 | flatc_args = flatc_args, |
| 351 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 352 | target_compatible_with = target_compatible_with, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 353 | visibility = ["//visibility:private"], |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 354 | ) |
| 355 | native.py_library( |
| 356 | name = name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 357 | srcs = [srcs_lib], |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 358 | visibility = visibility, |
| 359 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 360 | target_compatible_with = target_compatible_with, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 361 | imports = ["."], |
| 362 | deps = ["@com_github_google_flatbuffers//:flatpy"], |
| 363 | ) |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 364 | |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 365 | def flatbuffer_go_library( |
| 366 | name, |
| 367 | srcs, |
| 368 | importpath, |
| 369 | compatible_with = None, |
| 370 | target_compatible_with = None, |
| 371 | includes = [], |
| 372 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 373 | flatc_args = DEFAULT_FLATC_GO_ARGS, |
| 374 | visibility = None, |
| 375 | srcs_filegroup_visibility = None): |
| 376 | srcs_lib = "%s_srcs" % (name) |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 377 | flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]] |
| 378 | |
| 379 | flatbuffer_library_public( |
| 380 | name = srcs_lib, |
| 381 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 382 | output_suffix = "_generated.go", |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 383 | language_flag = "--go", |
| 384 | includes = includes, |
| 385 | include_paths = include_paths, |
| 386 | flatc_args = flatc_args, |
| 387 | compatible_with = compatible_with, |
| 388 | target_compatible_with = target_compatible_with, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 389 | visibility = ["//visibility:private"], |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 390 | ) |
| 391 | go_library( |
| 392 | name = name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 393 | srcs = [srcs_lib], |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 394 | deps = ["@com_github_google_flatbuffers//go"], |
| 395 | importpath = importpath, |
| 396 | visibility = visibility, |
| 397 | compatible_with = compatible_with, |
| 398 | target_compatible_with = target_compatible_with, |
| 399 | ) |
| 400 | |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame] | 401 | def _flatbuffer_rust_lib_gen_impl(ctx): |
| 402 | # TODO(Brian): I think this needs changes to properly handle multiple .fbs files in a rule. |
| 403 | uses = [] |
| 404 | for (dep, dep_srcs) in zip(ctx.attr.deps, ctx.attr.dep_srcs): |
| 405 | for dep_src in dep_srcs[FlatbufferLibraryInfo].srcs: |
| 406 | uses.append((dep[CrateInfo].name, dep_src.basename.replace(".fbs", "_generated"))) |
| 407 | lib_rs_content = "\n".join( |
| 408 | [ |
| 409 | "// Automatically generated by the Flatbuffers Bazel rules. Do not modify", |
| 410 | "#![allow(unused_imports)]", |
| 411 | ] + ["use %s as %s;" % (crate, use_as) for (crate, use_as) in uses] + |
| 412 | ["include!(\"%s\");" % src.basename for src in ctx.files.srcs_lib], |
| 413 | ) |
| 414 | output = ctx.actions.declare_file(ctx.attr.name + "_lib.rs") |
| 415 | ctx.actions.write( |
| 416 | output = output, |
| 417 | content = lib_rs_content, |
| 418 | ) |
| 419 | return [DefaultInfo(files = depset([output]))] |
| 420 | |
| 421 | """Generates a lib.rs for a flatbuffer_rust_library. |
| 422 | |
| 423 | flatc generates individual .rs files for us. It can also generate a top-level mod.rs to be included |
| 424 | in a crate, but that is laid out to include all flatbuffers files in a project. That's not a good |
| 425 | fit for Bazel rules and monorepos, so we generate an alternative that imports all dependencies under |
| 426 | their expected names.""" |
| 427 | _flatbuffer_rust_lib_gen = rule( |
| 428 | implementation = _flatbuffer_rust_lib_gen_impl, |
| 429 | attrs = { |
| 430 | "srcs_lib": attr.label(mandatory = True, doc = "The generated srcs for this rule"), |
| 431 | "dep_srcs": attr.label_list(mandatory = True, providers = [FlatbufferLibraryInfo], doc = "The _srcs rules for all our direct dependencies"), |
| 432 | "deps": attr.label_list(mandatory = True, providers = [CrateInfo]), |
| 433 | }, |
| 434 | ) |
| 435 | |
| 436 | def flatbuffer_rust_library( |
| 437 | name, |
| 438 | srcs, |
| 439 | compatible_with = None, |
| 440 | target_compatible_with = None, |
| 441 | deps = [], |
| 442 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 443 | flatc_args = DEFAULT_FLATC_RUST_ARGS, |
| 444 | include_reflection = True, |
| 445 | crate_name = None, |
| 446 | visibility = None, |
| 447 | srcs_filegroup_visibility = None): |
| 448 | includes = [d + "_includes" for d in deps] |
| 449 | srcs_lib = "%s_srcs" % (name) |
| 450 | lib_gen = "%s_lib_gen" % (name) |
| 451 | deps = list(deps) |
| 452 | if include_reflection: |
| 453 | deps.append("@com_github_google_flatbuffers//reflection:reflection_rust_fbs") |
| 454 | |
| 455 | flatbuffer_library_public( |
| 456 | name = srcs_lib, |
| 457 | srcs = srcs, |
| 458 | language_flag = "--rust", |
| 459 | output_suffix = "_generated.rs", |
| 460 | includes = includes, |
| 461 | include_paths = include_paths, |
| 462 | flatc_args = flatc_args, |
| 463 | compatible_with = compatible_with, |
| 464 | target_compatible_with = target_compatible_with, |
| 465 | visibility = visibility, |
| 466 | ) |
| 467 | _flatbuffer_rust_lib_gen( |
| 468 | name = lib_gen, |
| 469 | deps = deps, |
| 470 | dep_srcs = [dep + "_srcs" for dep in deps], |
| 471 | srcs_lib = srcs_lib, |
| 472 | visibility = ["//visibility:private"], |
| 473 | compatible_with = compatible_with, |
| 474 | target_compatible_with = target_compatible_with, |
| 475 | ) |
| 476 | rust_library( |
| 477 | name = name, |
| 478 | srcs = [srcs_lib, lib_gen], |
| 479 | crate_root = lib_gen, |
| 480 | crate_name = crate_name, |
| 481 | deps = ["@com_github_google_flatbuffers//rust"] + deps, |
| 482 | edition = "2018", |
| 483 | visibility = visibility, |
| 484 | compatible_with = compatible_with, |
| 485 | target_compatible_with = target_compatible_with, |
| 486 | ) |