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