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") |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 11 | load("@build_bazel_rules_nodejs//:index.bzl", "js_library") |
| 12 | load("@npm//@bazel/typescript:index.bzl", "ts_project") |
| 13 | load("@rules_cc//cc:defs.bzl", "cc_library") |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 14 | |
| 15 | flatc_path = "@com_github_google_flatbuffers//:flatc" |
| 16 | |
| 17 | DEFAULT_INCLUDE_PATHS = [ |
| 18 | "./", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 19 | ] |
| 20 | |
| 21 | DEFAULT_FLATC_ARGS = [ |
| 22 | "--gen-object-api", |
| 23 | "--gen-compare", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | "--keep-prefix", |
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", |
| 46 | ] |
| 47 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 48 | DEFAULT_FLATC_TS_ARGS = [ |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 49 | "--gen-object-api", |
| 50 | "--gen-mutable", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 51 | "--reflect-names", |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 52 | "--gen-name-strings", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 53 | "--ts-flat-files", |
| 54 | "--keep-prefix", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 55 | ] |
| 56 | |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 57 | """Contains information about a set of flatbuffers which have their code for |
| 58 | reading/writing generated in a single library-style rule. |
| 59 | |
| 60 | Fields: |
| 61 | srcs: [File], the .fbs source files |
| 62 | """ |
| 63 | FlatbufferLibraryInfo = provider() |
| 64 | |
| 65 | def _flatbuffer_library_compile_impl(ctx): |
| 66 | outs = [] |
| 67 | commands = [] |
| 68 | for src in ctx.files.srcs: |
| 69 | if ctx.attr.tables_for_filenames: |
| 70 | out_dir = None |
| 71 | for table in ctx.attr.tables_for_filenames: |
| 72 | out = ctx.actions.declare_file(ctx.attr.out_prefix + table + ctx.attr.output_suffix) |
| 73 | this_out_dir = "/".join(out.dirname.split("/")[:-(len(ctx.attr.out_prefix.split("/")) - 1)]) |
| 74 | if out_dir: |
| 75 | if this_out_dir != out_dir: |
| 76 | fail("Trying to write to multiple directories") |
| 77 | else: |
| 78 | out_dir = this_out_dir |
| 79 | outs.append(out) |
| 80 | else: |
| 81 | out = ctx.actions.declare_file(ctx.attr.out_prefix + src.basename.replace(".fbs", "") + ctx.attr.output_suffix) |
| 82 | outs.append(out) |
| 83 | out_dir = out.dirname |
| 84 | arguments = [ctx.executable._flatc.path] |
| 85 | for path in ctx.attr.include_paths: |
| 86 | for subpath in ["", ctx.bin_dir.path + "/"]: |
| 87 | arguments.append("-I") |
| 88 | arguments.append(subpath + path) |
| 89 | arguments.append("-I") |
| 90 | arguments.append("%s.runfiles/com_github_google_flatbuffers" % ctx.executable._flatc.path) |
| 91 | arguments.extend(ctx.attr.flatc_args) |
| 92 | arguments.extend(ctx.attr.language_flags) |
| 93 | arguments.extend([ |
| 94 | "-o", |
| 95 | out_dir, |
| 96 | ]) |
| 97 | arguments.append(src.path) |
| 98 | commands.append(arguments) |
| 99 | ctx.actions.run_shell( |
| 100 | outputs = outs, |
| 101 | inputs = ctx.files.srcs + ctx.files.includes, |
| 102 | tools = [ctx.executable._flatc], |
| 103 | command = " && ".join([" ".join(arguments) for arguments in commands]), |
| 104 | mnemonic = "Flatc", |
| 105 | progress_message = "Generating flatbuffer files for %{input}:", |
| 106 | ) |
| 107 | return [DefaultInfo(files = depset(outs), runfiles = ctx.runfiles(files = outs)), FlatbufferLibraryInfo(srcs = ctx.files.srcs)] |
| 108 | |
| 109 | _flatbuffer_library_compile = rule( |
| 110 | implementation = _flatbuffer_library_compile_impl, |
| 111 | attrs = { |
| 112 | "srcs": attr.label_list(mandatory = True, allow_files = True), |
| 113 | "output_suffix": attr.string(mandatory = True), |
| 114 | "tables_for_filenames": attr.string_list(mandatory = False), |
| 115 | "language_flags": attr.string_list(mandatory = True), |
| 116 | "includes": attr.label_list(default = [], allow_files = True), |
| 117 | "include_paths": attr.string_list(default = []), |
| 118 | "flatc_args": attr.string_list(default = []), |
| 119 | "out_prefix": attr.string(default = ""), |
| 120 | "_flatc": attr.label(executable = True, cfg = "exec", default = Label(flatc_path)), |
| 121 | }, |
| 122 | ) |
| 123 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 124 | def flatbuffer_library_public( |
| 125 | name, |
| 126 | srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 127 | output_suffix, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 128 | language_flag, |
| 129 | out_prefix = "", |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 130 | tables_for_filenames = None, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 131 | includes = [], |
| 132 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 133 | flatc_args = DEFAULT_FLATC_ARGS, |
| 134 | reflection_name = "", |
| 135 | reflection_visibility = None, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | compatible_with = None, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 137 | restricted_to = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 138 | target_compatible_with = None, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 139 | output_to_bindir = False, |
| 140 | visibility = None): |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 141 | """Generates code files for reading/writing the given flatbuffers in the |
| 142 | requested language using the public compiler. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 143 | |
| 144 | Args: |
| 145 | name: Rule name. |
| 146 | srcs: Source .fbs files. Sent in order to the compiler. |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 147 | output_suffix: Suffix for output files from flatc. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 148 | language_flag: Target language flag. One of [-c, -j, -js]. |
| 149 | out_prefix: Prepend this path to the front of all generated files except on |
| 150 | single source targets. Usually is a directory name. |
| 151 | includes: Optional, list of filegroups of schemas that the srcs depend on. |
| 152 | include_paths: Optional, list of paths the includes files can be found in. |
| 153 | flatc_args: Optional, list of additional arguments to pass to flatc. |
| 154 | reflection_name: Optional, if set this will generate the flatbuffer |
| 155 | reflection binaries for the schemas. |
| 156 | reflection_visibility: The visibility of the generated reflection Fileset. |
| 157 | output_to_bindir: Passed to genrule for output to bin directory. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 158 | compatible_with: Optional, The list of environments this rule can be |
| 159 | built for, in addition to default-supported environments. |
| 160 | restricted_to: Optional, The list of environments this rule can be built |
| 161 | for, instead of default-supported environments. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 162 | target_compatible_with: Optional, The list of target platform constraints |
| 163 | to use. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 164 | output_to_bindir: Passed to genrule for output to bin directory. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 165 | |
| 166 | |
| 167 | This rule creates a filegroup(name) with all generated source files, and |
| 168 | optionally a Fileset([reflection_name]) with all generated reflection |
| 169 | binaries. |
| 170 | """ |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 171 | _flatbuffer_library_compile( |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 172 | name = name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 173 | srcs = srcs, |
| 174 | output_suffix = output_suffix, |
| 175 | language_flags = [language_flag], |
| 176 | includes = includes, |
| 177 | include_paths = include_paths, |
| 178 | flatc_args = flatc_args, |
| 179 | out_prefix = out_prefix, |
| 180 | tables_for_filenames = tables_for_filenames, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 182 | target_compatible_with = target_compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 183 | restricted_to = restricted_to, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 184 | visibility = visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 185 | ) |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 186 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 187 | if reflection_name: |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 188 | _flatbuffer_library_compile( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 189 | name = "%s_out" % reflection_name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 190 | srcs = srcs, |
| 191 | output_suffix = ".bfbs", |
| 192 | language_flags = ["-b", "--schema"], |
| 193 | includes = includes, |
| 194 | include_paths = include_paths, |
| 195 | flatc_args = flatc_args, |
| 196 | out_prefix = out_prefix, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | compatible_with = compatible_with, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 198 | target_compatible_with = target_compatible_with, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 199 | restricted_to = restricted_to, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 200 | visibility = reflection_visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 201 | ) |
| 202 | |
| 203 | def flatbuffer_cc_library( |
| 204 | name, |
| 205 | srcs, |
| 206 | srcs_filegroup_name = "", |
| 207 | out_prefix = "", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 208 | deps = [], |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 209 | includes = [], |
| 210 | include_paths = DEFAULT_INCLUDE_PATHS, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 211 | cc_include_paths = [], |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 212 | flatc_args = DEFAULT_FLATC_ARGS, |
| 213 | visibility = None, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 214 | compatible_with = None, |
| 215 | restricted_to = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 216 | target_compatible_with = None, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 217 | srcs_filegroup_visibility = None, |
| 218 | gen_reflections = False): |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 219 | """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] | 220 | |
| 221 | Args: |
| 222 | name: Rule name. |
| 223 | srcs: Source .fbs files. Sent in order to the compiler. |
| 224 | srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this |
| 225 | filegroup into the `includes` parameter of any other |
| 226 | flatbuffer_cc_library that depends on this one's schemas. |
| 227 | out_prefix: Prepend this path to the front of all generated files. Usually |
| 228 | is a directory name. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 229 | deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified |
| 230 | alongside includes. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 231 | includes: Optional, list of filegroups of schemas that the srcs depend on. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 232 | Use of this is discouraged, and may be deprecated. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 233 | 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] | 234 | 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] | 235 | flatc_args: Optional list of additional arguments to pass to flatc |
| 236 | (e.g. --gen-mutable). |
| 237 | visibility: The visibility of the generated cc_library. By default, use the |
| 238 | default visibility of the project. |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 239 | target_compatible_with: Optional, the list of constraints the target |
| 240 | platform must satisfy for this target to be considered compatible. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 241 | srcs_filegroup_visibility: The visibility of the generated srcs filegroup. |
| 242 | By default, use the value of the visibility parameter above. |
| 243 | gen_reflections: Optional, if true this will generate the flatbuffer |
| 244 | reflection binaries for the schemas. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 245 | compatible_with: Optional, The list of environments this rule can be built |
| 246 | for, in addition to default-supported environments. |
| 247 | restricted_to: Optional, The list of environments this rule can be built |
| 248 | for, instead of default-supported environments. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 249 | target_compatible_with: Optional, The list of target platform constraints |
| 250 | to use. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 251 | |
| 252 | This produces: |
| 253 | filegroup([name]_srcs): all generated .h files. |
| 254 | filegroup(srcs_filegroup_name if specified, or [name]_includes if not): |
| 255 | Other flatbuffer_cc_library's can pass this in for their `includes` |
| 256 | parameter, if they depend on the schemas in this library. |
| 257 | Fileset([name]_reflection): (Optional) all generated reflection binaries. |
| 258 | cc_library([name]): library with sources and flatbuffers deps. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 259 | """ |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 260 | if deps and includes: |
| 261 | # There is no inherent reason we couldn't support both, but this discourages |
| 262 | # use of includes without good reason. |
| 263 | fail("Cannot specify both deps and include in flatbuffer_cc_library.") |
| 264 | if deps: |
| 265 | includes = [d + "_includes" for d in deps] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 266 | reflection_name = "%s_reflection" % name if gen_reflections else "" |
| 267 | |
| 268 | srcs_lib = "%s_srcs" % (name) |
| 269 | flatbuffer_library_public( |
| 270 | name = srcs_lib, |
| 271 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 272 | output_suffix = "_generated.h", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 273 | language_flag = "-c", |
| 274 | out_prefix = out_prefix, |
| 275 | includes = includes, |
| 276 | include_paths = include_paths, |
| 277 | flatc_args = flatc_args, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 278 | compatible_with = compatible_with, |
| 279 | restricted_to = restricted_to, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 280 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 281 | reflection_name = reflection_name, |
| 282 | reflection_visibility = visibility, |
| 283 | ) |
| 284 | native.cc_library( |
| 285 | name = name, |
| 286 | hdrs = [ |
| 287 | ":" + srcs_lib, |
| 288 | ], |
| 289 | srcs = [ |
| 290 | ":" + srcs_lib, |
| 291 | ], |
| 292 | features = [ |
| 293 | "-parse_headers", |
| 294 | ], |
| 295 | deps = [ |
| 296 | "@com_github_google_flatbuffers//:runtime_cc", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 297 | "@com_github_google_flatbuffers//:flatbuffers", |
| 298 | ] + deps, |
| 299 | includes = cc_include_paths, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 300 | compatible_with = compatible_with, |
| 301 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 302 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 303 | linkstatic = 1, |
| 304 | visibility = visibility, |
| 305 | ) |
| 306 | |
| 307 | # A filegroup for the `srcs`. That is, all the schema files for this |
| 308 | # Flatbuffer set. |
| 309 | native.filegroup( |
| 310 | name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name), |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 311 | srcs = srcs + includes, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 312 | compatible_with = compatible_with, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 313 | restricted_to = restricted_to, |
| 314 | visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 315 | ) |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 316 | |
Brian Silverman | 2876027 | 2020-02-02 13:21:51 -0800 | [diff] [blame] | 317 | def flatbuffer_py_library( |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 318 | name, |
| 319 | srcs, |
| 320 | namespace, |
| 321 | tables, |
| 322 | compatible_with = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 323 | target_compatible_with = None, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 324 | includes = [], |
| 325 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 326 | flatc_args = DEFAULT_FLATC_ARGS, |
| 327 | visibility = None, |
| 328 | srcs_filegroup_visibility = None): |
| 329 | """Generates a py_library rule for a given flatbuffer definition. |
| 330 | |
| 331 | Args: |
| 332 | name: Name of the generated py_library rule. |
| 333 | srcs: Source .fbs file(s). |
| 334 | namespace: Namespace of the specified flatbuffer schema. Until |
| 335 | we make the rules sophisticated enough to figure out what |
| 336 | python files will be generated from a given schema, the user |
| 337 | has to manually specify this. |
| 338 | tables: List of table names--currently, we don't do anything to |
| 339 | automatically figure out how to handle the fact that a separate |
| 340 | python file will be generated for every table definition, and that |
| 341 | we can't know what files will be output until after the file has |
| 342 | been parsed. As such, we just force the user to manually specify |
| 343 | things. |
| 344 | """ |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 345 | |
| 346 | srcs_lib = "%s_srcs" % (name) |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 347 | if not tables: |
| 348 | fail("Must specify the list of tables") |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 349 | flatbuffer_library_public( |
| 350 | name = srcs_lib, |
| 351 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 352 | output_suffix = ".py", |
| 353 | out_prefix = namespace.replace(".", "/") + "/", |
| 354 | tables_for_filenames = tables, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 355 | language_flag = "--python", |
| 356 | includes = includes, |
| 357 | include_paths = include_paths, |
| 358 | flatc_args = flatc_args, |
| 359 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 360 | target_compatible_with = target_compatible_with, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 361 | visibility = ["//visibility:private"], |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 362 | ) |
| 363 | native.py_library( |
| 364 | name = name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 365 | srcs = [srcs_lib], |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 366 | visibility = visibility, |
| 367 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 368 | target_compatible_with = target_compatible_with, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 369 | imports = ["."], |
| 370 | deps = ["@com_github_google_flatbuffers//:flatpy"], |
| 371 | ) |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 372 | |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 373 | def flatbuffer_go_library( |
| 374 | name, |
| 375 | srcs, |
| 376 | importpath, |
| 377 | compatible_with = None, |
| 378 | target_compatible_with = None, |
| 379 | includes = [], |
| 380 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 381 | flatc_args = DEFAULT_FLATC_GO_ARGS, |
| 382 | visibility = None, |
| 383 | srcs_filegroup_visibility = None): |
| 384 | srcs_lib = "%s_srcs" % (name) |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 385 | flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]] |
| 386 | |
| 387 | flatbuffer_library_public( |
| 388 | name = srcs_lib, |
| 389 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 390 | output_suffix = "_generated.go", |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 391 | language_flag = "--go", |
| 392 | includes = includes, |
| 393 | include_paths = include_paths, |
| 394 | flatc_args = flatc_args, |
| 395 | compatible_with = compatible_with, |
| 396 | target_compatible_with = target_compatible_with, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 397 | visibility = ["//visibility:private"], |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 398 | ) |
| 399 | go_library( |
| 400 | name = name, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 401 | srcs = [srcs_lib], |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 402 | deps = ["@com_github_google_flatbuffers//go"], |
| 403 | importpath = importpath, |
| 404 | visibility = visibility, |
| 405 | compatible_with = compatible_with, |
| 406 | target_compatible_with = target_compatible_with, |
| 407 | ) |
| 408 | |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 409 | def _flatbuffer_rust_lib_gen_impl(ctx): |
| 410 | # TODO(Brian): I think this needs changes to properly handle multiple .fbs files in a rule. |
| 411 | uses = [] |
| 412 | for (dep, dep_srcs) in zip(ctx.attr.deps, ctx.attr.dep_srcs): |
| 413 | for dep_src in dep_srcs[FlatbufferLibraryInfo].srcs: |
| 414 | uses.append((dep[CrateInfo].name, dep_src.basename.replace(".fbs", "_generated"))) |
| 415 | lib_rs_content = "\n".join( |
| 416 | [ |
| 417 | "// Automatically generated by the Flatbuffers Bazel rules. Do not modify", |
| 418 | "#![allow(unused_imports)]", |
| 419 | ] + ["use %s as %s;" % (crate, use_as) for (crate, use_as) in uses] + |
| 420 | ["include!(\"%s\");" % src.basename for src in ctx.files.srcs_lib], |
| 421 | ) |
| 422 | output = ctx.actions.declare_file(ctx.attr.name + "_lib.rs") |
| 423 | ctx.actions.write( |
| 424 | output = output, |
| 425 | content = lib_rs_content, |
| 426 | ) |
| 427 | return [DefaultInfo(files = depset([output]))] |
| 428 | |
| 429 | """Generates a lib.rs for a flatbuffer_rust_library. |
| 430 | |
| 431 | flatc generates individual .rs files for us. It can also generate a top-level mod.rs to be included |
| 432 | in a crate, but that is laid out to include all flatbuffers files in a project. That's not a good |
| 433 | fit for Bazel rules and monorepos, so we generate an alternative that imports all dependencies under |
| 434 | their expected names.""" |
| 435 | _flatbuffer_rust_lib_gen = rule( |
| 436 | implementation = _flatbuffer_rust_lib_gen_impl, |
| 437 | attrs = { |
| 438 | "srcs_lib": attr.label(mandatory = True, doc = "The generated srcs for this rule"), |
| 439 | "dep_srcs": attr.label_list(mandatory = True, providers = [FlatbufferLibraryInfo], doc = "The _srcs rules for all our direct dependencies"), |
| 440 | "deps": attr.label_list(mandatory = True, providers = [CrateInfo]), |
| 441 | }, |
| 442 | ) |
| 443 | |
| 444 | def flatbuffer_rust_library( |
| 445 | name, |
| 446 | srcs, |
| 447 | compatible_with = None, |
| 448 | target_compatible_with = None, |
| 449 | deps = [], |
| 450 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 451 | flatc_args = DEFAULT_FLATC_RUST_ARGS, |
| 452 | include_reflection = True, |
| 453 | crate_name = None, |
| 454 | visibility = None, |
| 455 | srcs_filegroup_visibility = None): |
| 456 | includes = [d + "_includes" for d in deps] |
| 457 | srcs_lib = "%s_srcs" % (name) |
| 458 | lib_gen = "%s_lib_gen" % (name) |
| 459 | deps = list(deps) |
| 460 | if include_reflection: |
| 461 | deps.append("@com_github_google_flatbuffers//reflection:reflection_rust_fbs") |
| 462 | |
| 463 | flatbuffer_library_public( |
| 464 | name = srcs_lib, |
| 465 | srcs = srcs, |
| 466 | language_flag = "--rust", |
| 467 | output_suffix = "_generated.rs", |
| 468 | includes = includes, |
| 469 | include_paths = include_paths, |
| 470 | flatc_args = flatc_args, |
| 471 | compatible_with = compatible_with, |
| 472 | target_compatible_with = target_compatible_with, |
| 473 | visibility = visibility, |
| 474 | ) |
| 475 | _flatbuffer_rust_lib_gen( |
| 476 | name = lib_gen, |
| 477 | deps = deps, |
| 478 | dep_srcs = [dep + "_srcs" for dep in deps], |
| 479 | srcs_lib = srcs_lib, |
| 480 | visibility = ["//visibility:private"], |
| 481 | compatible_with = compatible_with, |
| 482 | target_compatible_with = target_compatible_with, |
| 483 | ) |
| 484 | rust_library( |
| 485 | name = name, |
| 486 | srcs = [srcs_lib, lib_gen], |
| 487 | crate_root = lib_gen, |
| 488 | crate_name = crate_name, |
| 489 | deps = ["@com_github_google_flatbuffers//rust"] + deps, |
| 490 | edition = "2018", |
| 491 | visibility = visibility, |
| 492 | compatible_with = compatible_with, |
| 493 | target_compatible_with = target_compatible_with, |
| 494 | ) |
| 495 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 496 | def flatbuffer_ts_library( |
| 497 | name, |
| 498 | srcs, |
| 499 | compatible_with = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 500 | target_compatible_with = None, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 501 | deps = [], |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 502 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 503 | flatc_args = DEFAULT_FLATC_TS_ARGS, |
| 504 | visibility = None, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 505 | restricted_to = None, |
James Kuszmaul | 136aa2b | 2022-04-02 14:50:56 -0700 | [diff] [blame] | 506 | include_reflection = True, |
| 507 | package_name = None): |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 508 | """Generates a ts_library rule for a given flatbuffer definition. |
| 509 | |
| 510 | Args: |
| 511 | name: Name of the generated ts_library rule. |
| 512 | srcs: Source .fbs file(s). |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 513 | deps: Other flatbuffer_ts_library's to depend on. Note that currently |
| 514 | you must specify all your transitive dependencies manually. |
| 515 | include_paths: Optional, list of paths the includes files can be found in. |
| 516 | flatc_args: Optional list of additional arguments to pass to flatc |
| 517 | (e.g. --gen-mutable). |
| 518 | visibility: The visibility of the generated cc_library. By default, use the |
| 519 | default visibility of the project. |
| 520 | compatible_with: Optional, The list of environments this rule can be built |
| 521 | for, in addition to default-supported environments. |
| 522 | restricted_to: Optional, The list of environments this rule can be built |
| 523 | for, instead of default-supported environments. |
| 524 | target_compatible_with: Optional, The list of target platform constraints |
| 525 | to use. |
| 526 | include_reflection: Optional, Whether to depend on the flatbuffer |
| 527 | reflection library automatically. Only really relevant for the |
| 528 | target that builds the reflection library itself. |
James Kuszmaul | 136aa2b | 2022-04-02 14:50:56 -0700 | [diff] [blame] | 529 | package_name: Optional, Package name to use for the generated code. |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 530 | """ |
| 531 | srcs_lib = "%s_srcs" % (name) |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 532 | |
| 533 | # frc971-specific modification: Add a genrule that overwrites the imports for any flatbuffer |
| 534 | # types (mostly just for reflection) because they need to point to external/, not to |
| 535 | # third_party/. |
| 536 | # TODO(james): There absolutely are better ways to do this, but this was the quick and dirty |
| 537 | # one.... |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 538 | outs = ["%s_generated.ts" % (s.replace(".fbs", "").split("/")[-1]) for s in srcs] |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 539 | includes = [d + "_includes" for d in deps] |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 540 | flatbuffer_library_public( |
| 541 | name = srcs_lib, |
| 542 | srcs = srcs, |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 543 | output_suffix = "_pregenerated.ts", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 544 | language_flag = "--ts", |
| 545 | includes = includes, |
| 546 | include_paths = include_paths, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 547 | flatc_args = flatc_args + ["--filename-suffix _pregenerated"], |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 548 | compatible_with = compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 549 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 550 | target_compatible_with = target_compatible_with, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 551 | ) |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 552 | genrule_cmd = " ".join([ |
| 553 | "SRCS=($(SRCS));", |
| 554 | "OUTS=($(OUTS));", |
| 555 | "for i in $${!SRCS[@]}; do", |
James Kuszmaul | 136aa2b | 2022-04-02 14:50:56 -0700 | [diff] [blame] | 556 | "sed \"s/'.*reflection\\/reflection_pregenerated/'flatbuffers_reflection\\/reflection_generated/\" $${SRCS[i]} > $${OUTS[i]};", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 557 | "sed -i 's/_pregenerated/_generated/' $${OUTS[i]};", |
| 558 | "done", |
| 559 | ]) |
| 560 | native.genrule( |
Brian Silverman | dae15a1 | 2022-07-23 12:55:20 -0700 | [diff] [blame^] | 561 | name = name + "_reimporter.ts", |
| 562 | srcs = [srcs_lib], |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 563 | outs = outs, |
| 564 | cmd = genrule_cmd, |
| 565 | ) |
| 566 | ts_project( |
| 567 | name = name + "_ts", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 568 | srcs = outs, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 569 | declaration = True, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 570 | visibility = visibility, |
| 571 | compatible_with = compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 572 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 573 | target_compatible_with = target_compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 574 | tsconfig = { |
| 575 | "compilerOptions": { |
| 576 | "declaration": True, |
| 577 | "lib": [ |
| 578 | "ES2015", |
| 579 | "ES2020.BigInt", |
| 580 | "DOM", |
| 581 | ], |
| 582 | "module": "es2015", |
| 583 | "moduleResolution": "node", |
| 584 | "strict": True, |
| 585 | "types": ["node"], |
| 586 | }, |
| 587 | }, |
| 588 | deps = deps + ["@com_github_google_flatbuffers//ts:flatbuffers"] + (["@com_github_google_flatbuffers//reflection:reflection_ts_fbs"] if include_reflection else []), |
| 589 | ) |
| 590 | js_library( |
| 591 | name = name, |
| 592 | visibility = visibility, |
| 593 | compatible_with = compatible_with, |
| 594 | restricted_to = restricted_to, |
| 595 | target_compatible_with = target_compatible_with, |
| 596 | deps = [name + "_ts"], |
James Kuszmaul | 136aa2b | 2022-04-02 14:50:56 -0700 | [diff] [blame] | 597 | package_name = package_name, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame] | 598 | ) |
| 599 | native.filegroup( |
| 600 | name = "%s_includes" % (name), |
| 601 | srcs = srcs + includes, |
| 602 | compatible_with = compatible_with, |
| 603 | restricted_to = restricted_to, |
| 604 | visibility = visibility, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 605 | ) |