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") |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 9 | load("@build_bazel_rules_nodejs//:index.bzl", "js_library") |
| 10 | load("@npm//@bazel/typescript:index.bzl", "ts_project") |
| 11 | load("@rules_cc//cc:defs.bzl", "cc_library") |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 12 | |
| 13 | flatc_path = "@com_github_google_flatbuffers//:flatc" |
| 14 | |
| 15 | DEFAULT_INCLUDE_PATHS = [ |
| 16 | "./", |
| 17 | "$(GENDIR)", |
| 18 | "$(BINDIR)", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | "$(execpath @com_github_google_flatbuffers//:flatc).runfiles/com_github_google_flatbuffers", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 20 | ] |
| 21 | |
| 22 | DEFAULT_FLATC_ARGS = [ |
| 23 | "--gen-object-api", |
| 24 | "--gen-compare", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | "--keep-prefix", |
milind upadhyay | 328d339 | 2020-11-25 19:34:00 -0800 | [diff] [blame] | 26 | "--cpp-std", |
| 27 | "c++17", |
Austin Schuh | f13042b | 2020-11-25 23:11:41 -0800 | [diff] [blame] | 28 | "--require-explicit-ids", |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 29 | "--gen-mutable", |
| 30 | "--reflect-names", |
| 31 | "--cpp-ptr-type 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 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 43 | DEFAULT_FLATC_TS_ARGS = [ |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 44 | "--gen-object-api", |
| 45 | "--gen-mutable", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 46 | "--reflect-names", |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 47 | "--gen-name-strings", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 48 | "--ts-flat-files", |
| 49 | "--keep-prefix", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 50 | ] |
| 51 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 52 | def flatbuffer_library_public( |
| 53 | name, |
| 54 | srcs, |
| 55 | outs, |
| 56 | language_flag, |
| 57 | out_prefix = "", |
| 58 | includes = [], |
| 59 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 60 | flatc_args = DEFAULT_FLATC_ARGS, |
| 61 | reflection_name = "", |
| 62 | reflection_visibility = None, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 63 | compatible_with = None, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 64 | restricted_to = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 65 | target_compatible_with = None, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 66 | output_to_bindir = False): |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 67 | """Generates code files for reading/writing the given flatbuffers in the |
| 68 | requested language using the public compiler. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 69 | |
| 70 | Args: |
| 71 | name: Rule name. |
| 72 | srcs: Source .fbs files. Sent in order to the compiler. |
| 73 | outs: Output files from flatc. |
| 74 | language_flag: Target language flag. One of [-c, -j, -js]. |
| 75 | out_prefix: Prepend this path to the front of all generated files except on |
| 76 | single source targets. Usually is a directory name. |
| 77 | includes: Optional, list of filegroups of schemas that the srcs depend on. |
| 78 | include_paths: Optional, list of paths the includes files can be found in. |
| 79 | flatc_args: Optional, list of additional arguments to pass to flatc. |
| 80 | reflection_name: Optional, if set this will generate the flatbuffer |
| 81 | reflection binaries for the schemas. |
| 82 | reflection_visibility: The visibility of the generated reflection Fileset. |
| 83 | output_to_bindir: Passed to genrule for output to bin directory. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 84 | compatible_with: Optional, The list of environments this rule can be |
| 85 | built for, in addition to default-supported environments. |
| 86 | restricted_to: Optional, The list of environments this rule can be built |
| 87 | for, instead of default-supported environments. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 88 | target_compatible_with: Optional, The list of target platform constraints |
| 89 | to use. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 90 | output_to_bindir: Passed to genrule for output to bin directory. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | This rule creates a filegroup(name) with all generated source files, and |
| 94 | optionally a Fileset([reflection_name]) with all generated reflection |
| 95 | binaries. |
| 96 | """ |
| 97 | include_paths_cmd = ["-I %s" % (s) for s in include_paths] |
| 98 | |
| 99 | # '$(@D)' when given a single source target will give the appropriate |
| 100 | # directory. Appending 'out_prefix' is only necessary when given a build |
| 101 | # target with multiple sources. |
| 102 | output_directory = ( |
| 103 | ("-o $(@D)/%s" % (out_prefix)) if len(srcs) > 1 else ("-o $(@D)") |
| 104 | ) |
| 105 | genrule_cmd = " ".join([ |
| 106 | "SRCS=($(SRCS));", |
| 107 | "for f in $${SRCS[@]:0:%s}; do" % len(srcs), |
| 108 | "$(location %s)" % (flatc_path), |
| 109 | " ".join(include_paths_cmd), |
| 110 | " ".join(flatc_args), |
| 111 | language_flag, |
| 112 | output_directory, |
| 113 | "$$f;", |
| 114 | "done", |
| 115 | ]) |
| 116 | native.genrule( |
| 117 | name = name, |
| 118 | srcs = srcs + includes, |
| 119 | outs = outs, |
| 120 | output_to_bindir = output_to_bindir, |
| 121 | tools = [flatc_path], |
| 122 | cmd = genrule_cmd, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 123 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 124 | target_compatible_with = target_compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 125 | restricted_to = restricted_to, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 126 | message = "Generating flatbuffer files for %s:" % (name), |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 127 | ) |
| 128 | if reflection_name: |
| 129 | reflection_genrule_cmd = " ".join([ |
| 130 | "SRCS=($(SRCS));", |
| 131 | "for f in $${SRCS[@]:0:%s}; do" % len(srcs), |
| 132 | "$(location %s)" % (flatc_path), |
| 133 | "-b --schema", |
| 134 | " ".join(flatc_args), |
| 135 | " ".join(include_paths_cmd), |
| 136 | language_flag, |
| 137 | output_directory, |
| 138 | "$$f;", |
| 139 | "done", |
| 140 | ]) |
| 141 | reflection_outs = [ |
| 142 | (out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1]) |
| 143 | for s in srcs |
| 144 | ] |
| 145 | native.genrule( |
| 146 | name = "%s_srcs" % reflection_name, |
| 147 | srcs = srcs + includes, |
| 148 | outs = reflection_outs, |
| 149 | output_to_bindir = output_to_bindir, |
| 150 | tools = [flatc_path], |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 151 | compatible_with = compatible_with, |
| 152 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 153 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 154 | cmd = reflection_genrule_cmd, |
| 155 | message = "Generating flatbuffer reflection binary for %s:" % (name), |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 156 | visibility = reflection_visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 157 | ) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | native.filegroup( |
| 159 | name = "%s_out" % reflection_name, |
| 160 | srcs = reflection_outs, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 161 | visibility = reflection_visibility, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 162 | compatible_with = compatible_with, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 163 | restricted_to = restricted_to, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 164 | ) |
| 165 | |
| 166 | def flatbuffer_cc_library( |
| 167 | name, |
| 168 | srcs, |
| 169 | srcs_filegroup_name = "", |
| 170 | out_prefix = "", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 171 | deps = [], |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 172 | includes = [], |
| 173 | include_paths = DEFAULT_INCLUDE_PATHS, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 174 | cc_include_paths = [], |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 175 | flatc_args = DEFAULT_FLATC_ARGS, |
| 176 | visibility = None, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 177 | compatible_with = None, |
| 178 | restricted_to = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 179 | target_compatible_with = None, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 180 | srcs_filegroup_visibility = None, |
| 181 | gen_reflections = False): |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 182 | """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] | 183 | |
| 184 | Args: |
| 185 | name: Rule name. |
| 186 | srcs: Source .fbs files. Sent in order to the compiler. |
| 187 | srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this |
| 188 | filegroup into the `includes` parameter of any other |
| 189 | flatbuffer_cc_library that depends on this one's schemas. |
| 190 | out_prefix: Prepend this path to the front of all generated files. Usually |
| 191 | is a directory name. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 192 | deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified |
| 193 | alongside includes. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 194 | includes: Optional, list of filegroups of schemas that the srcs depend on. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 195 | Use of this is discouraged, and may be deprecated. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 196 | 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^] | 197 | 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] | 198 | flatc_args: Optional list of additional arguments to pass to flatc |
| 199 | (e.g. --gen-mutable). |
| 200 | visibility: The visibility of the generated cc_library. By default, use the |
| 201 | default visibility of the project. |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 202 | target_compatible_with: Optional, the list of constraints the target |
| 203 | platform must satisfy for this target to be considered compatible. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 204 | srcs_filegroup_visibility: The visibility of the generated srcs filegroup. |
| 205 | By default, use the value of the visibility parameter above. |
| 206 | gen_reflections: Optional, if true this will generate the flatbuffer |
| 207 | reflection binaries for the schemas. |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 208 | compatible_with: Optional, The list of environments this rule can be built |
| 209 | for, in addition to default-supported environments. |
| 210 | restricted_to: Optional, The list of environments this rule can be built |
| 211 | for, instead of default-supported environments. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 212 | target_compatible_with: Optional, The list of target platform constraints |
| 213 | to use. |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 214 | |
| 215 | This produces: |
| 216 | filegroup([name]_srcs): all generated .h files. |
| 217 | filegroup(srcs_filegroup_name if specified, or [name]_includes if not): |
| 218 | Other flatbuffer_cc_library's can pass this in for their `includes` |
| 219 | parameter, if they depend on the schemas in this library. |
| 220 | Fileset([name]_reflection): (Optional) all generated reflection binaries. |
| 221 | cc_library([name]): library with sources and flatbuffers deps. |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 222 | """ |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 223 | output_headers = [ |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 224 | (out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1].split(":")[-1]) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 225 | for s in srcs |
| 226 | ] |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 227 | if deps and includes: |
| 228 | # There is no inherent reason we couldn't support both, but this discourages |
| 229 | # use of includes without good reason. |
| 230 | fail("Cannot specify both deps and include in flatbuffer_cc_library.") |
| 231 | if deps: |
| 232 | includes = [d + "_includes" for d in deps] |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 233 | reflection_name = "%s_reflection" % name if gen_reflections else "" |
| 234 | |
| 235 | srcs_lib = "%s_srcs" % (name) |
| 236 | flatbuffer_library_public( |
| 237 | name = srcs_lib, |
| 238 | srcs = srcs, |
| 239 | outs = output_headers, |
| 240 | language_flag = "-c", |
| 241 | out_prefix = out_prefix, |
| 242 | includes = includes, |
| 243 | include_paths = include_paths, |
| 244 | flatc_args = flatc_args, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 245 | compatible_with = compatible_with, |
| 246 | restricted_to = restricted_to, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 247 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 248 | reflection_name = reflection_name, |
| 249 | reflection_visibility = visibility, |
| 250 | ) |
| 251 | native.cc_library( |
| 252 | name = name, |
| 253 | hdrs = [ |
| 254 | ":" + srcs_lib, |
| 255 | ], |
| 256 | srcs = [ |
| 257 | ":" + srcs_lib, |
| 258 | ], |
| 259 | features = [ |
| 260 | "-parse_headers", |
| 261 | ], |
| 262 | deps = [ |
| 263 | "@com_github_google_flatbuffers//:runtime_cc", |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 264 | "@com_github_google_flatbuffers//:flatbuffers", |
| 265 | ] + deps, |
| 266 | includes = cc_include_paths, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 267 | compatible_with = compatible_with, |
| 268 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 269 | target_compatible_with = target_compatible_with, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 270 | linkstatic = 1, |
| 271 | visibility = visibility, |
| 272 | ) |
| 273 | |
| 274 | # A filegroup for the `srcs`. That is, all the schema files for this |
| 275 | # Flatbuffer set. |
| 276 | native.filegroup( |
| 277 | name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name), |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 278 | srcs = srcs + includes, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 279 | compatible_with = compatible_with, |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 280 | restricted_to = restricted_to, |
| 281 | visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility, |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 282 | ) |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 283 | |
Brian Silverman | 2876027 | 2020-02-02 13:21:51 -0800 | [diff] [blame] | 284 | def flatbuffer_py_library( |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 285 | name, |
| 286 | srcs, |
| 287 | namespace, |
| 288 | tables, |
| 289 | compatible_with = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 290 | target_compatible_with = None, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 291 | includes = [], |
| 292 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 293 | flatc_args = DEFAULT_FLATC_ARGS, |
| 294 | visibility = None, |
| 295 | srcs_filegroup_visibility = None): |
| 296 | """Generates a py_library rule for a given flatbuffer definition. |
| 297 | |
| 298 | Args: |
| 299 | name: Name of the generated py_library rule. |
| 300 | srcs: Source .fbs file(s). |
| 301 | namespace: Namespace of the specified flatbuffer schema. Until |
| 302 | we make the rules sophisticated enough to figure out what |
| 303 | python files will be generated from a given schema, the user |
| 304 | has to manually specify this. |
| 305 | tables: List of table names--currently, we don't do anything to |
| 306 | automatically figure out how to handle the fact that a separate |
| 307 | python file will be generated for every table definition, and that |
| 308 | we can't know what files will be output until after the file has |
| 309 | been parsed. As such, we just force the user to manually specify |
| 310 | things. |
| 311 | """ |
Austin Schuh | a4f69d6 | 2020-02-28 13:58:14 -0800 | [diff] [blame] | 312 | python_files = ["%s/%s.py" % (namespace.replace(".", "/"), table) for table in tables] |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 313 | |
| 314 | srcs_lib = "%s_srcs" % (name) |
| 315 | flatbuffer_library_public( |
| 316 | name = srcs_lib, |
| 317 | srcs = srcs, |
| 318 | outs = python_files, |
| 319 | language_flag = "--python", |
| 320 | includes = includes, |
| 321 | include_paths = include_paths, |
| 322 | flatc_args = flatc_args, |
| 323 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 324 | target_compatible_with = target_compatible_with, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 325 | ) |
| 326 | native.py_library( |
| 327 | name = name, |
| 328 | srcs = python_files, |
| 329 | visibility = visibility, |
| 330 | compatible_with = compatible_with, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 331 | target_compatible_with = target_compatible_with, |
James Kuszmaul | f385c46 | 2019-12-24 09:37:34 -0800 | [diff] [blame] | 332 | imports = ["."], |
| 333 | deps = ["@com_github_google_flatbuffers//:flatpy"], |
| 334 | ) |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 335 | |
Alex Perry | 0d0aae3 | 2022-02-09 21:10:17 -0800 | [diff] [blame] | 336 | def flatbuffer_go_library( |
| 337 | name, |
| 338 | srcs, |
| 339 | importpath, |
| 340 | compatible_with = None, |
| 341 | target_compatible_with = None, |
| 342 | includes = [], |
| 343 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 344 | flatc_args = DEFAULT_FLATC_GO_ARGS, |
| 345 | visibility = None, |
| 346 | srcs_filegroup_visibility = None): |
| 347 | srcs_lib = "%s_srcs" % (name) |
| 348 | outs = ["%s_generated.go" % (s.replace(".fbs", "").split("/")[-1]) for s in srcs] |
| 349 | flatc_args = flatc_args + ["--go-namespace", importpath.split("/")[-1]] |
| 350 | |
| 351 | flatbuffer_library_public( |
| 352 | name = srcs_lib, |
| 353 | srcs = srcs, |
| 354 | outs = outs, |
| 355 | language_flag = "--go", |
| 356 | includes = includes, |
| 357 | include_paths = include_paths, |
| 358 | flatc_args = flatc_args, |
| 359 | compatible_with = compatible_with, |
| 360 | target_compatible_with = target_compatible_with, |
| 361 | ) |
| 362 | go_library( |
| 363 | name = name, |
| 364 | srcs = outs, |
| 365 | deps = ["@com_github_google_flatbuffers//go"], |
| 366 | importpath = importpath, |
| 367 | visibility = visibility, |
| 368 | compatible_with = compatible_with, |
| 369 | target_compatible_with = target_compatible_with, |
| 370 | ) |
| 371 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 372 | def flatbuffer_ts_library( |
| 373 | name, |
| 374 | srcs, |
| 375 | compatible_with = None, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 376 | target_compatible_with = None, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 377 | deps = [], |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 378 | include_paths = DEFAULT_INCLUDE_PATHS, |
| 379 | flatc_args = DEFAULT_FLATC_TS_ARGS, |
| 380 | visibility = None, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 381 | restricted_to = None, |
| 382 | include_reflection = True): |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 383 | """Generates a ts_library rule for a given flatbuffer definition. |
| 384 | |
| 385 | Args: |
| 386 | name: Name of the generated ts_library rule. |
| 387 | srcs: Source .fbs file(s). |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 388 | deps: Other flatbuffer_ts_library's to depend on. Note that currently |
| 389 | you must specify all your transitive dependencies manually. |
| 390 | include_paths: Optional, list of paths the includes files can be found in. |
| 391 | flatc_args: Optional list of additional arguments to pass to flatc |
| 392 | (e.g. --gen-mutable). |
| 393 | visibility: The visibility of the generated cc_library. By default, use the |
| 394 | default visibility of the project. |
| 395 | compatible_with: Optional, The list of environments this rule can be built |
| 396 | for, in addition to default-supported environments. |
| 397 | restricted_to: Optional, The list of environments this rule can be built |
| 398 | for, instead of default-supported environments. |
| 399 | target_compatible_with: Optional, The list of target platform constraints |
| 400 | to use. |
| 401 | include_reflection: Optional, Whether to depend on the flatbuffer |
| 402 | reflection library automatically. Only really relevant for the |
| 403 | target that builds the reflection library itself. |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 404 | """ |
| 405 | srcs_lib = "%s_srcs" % (name) |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 406 | |
| 407 | # frc971-specific modification: Add a genrule that overwrites the imports for any flatbuffer |
| 408 | # types (mostly just for reflection) because they need to point to external/, not to |
| 409 | # third_party/. |
| 410 | # TODO(james): There absolutely are better ways to do this, but this was the quick and dirty |
| 411 | # one.... |
| 412 | pre_outs = ["%s_pregenerated.ts" % (s.replace(".fbs", "").split("/")[-1]) for s in srcs] |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 413 | 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^] | 414 | includes = [d + "_includes" for d in deps] |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 415 | flatbuffer_library_public( |
| 416 | name = srcs_lib, |
| 417 | srcs = srcs, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 418 | outs = pre_outs, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 419 | language_flag = "--ts", |
| 420 | includes = includes, |
| 421 | include_paths = include_paths, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 422 | flatc_args = flatc_args + ["--filename-suffix _pregenerated"], |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 423 | compatible_with = compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 424 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 425 | target_compatible_with = target_compatible_with, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 426 | ) |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 427 | genrule_cmd = " ".join([ |
| 428 | "SRCS=($(SRCS));", |
| 429 | "OUTS=($(OUTS));", |
| 430 | "for i in $${!SRCS[@]}; do", |
| 431 | "sed 's/third_party\\/flatbuffers/external\\/com_github_google_flatbuffers/' $${SRCS[i]} > $${OUTS[i]};", |
| 432 | "sed -i 's/_pregenerated/_generated/' $${OUTS[i]};", |
| 433 | "done", |
| 434 | ]) |
| 435 | native.genrule( |
| 436 | name = name + "_reimporter", |
| 437 | srcs = pre_outs, |
| 438 | outs = outs, |
| 439 | cmd = genrule_cmd, |
| 440 | ) |
| 441 | ts_project( |
| 442 | name = name + "_ts", |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 443 | srcs = outs, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 444 | declaration = True, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 445 | visibility = visibility, |
| 446 | compatible_with = compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 447 | restricted_to = restricted_to, |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 448 | target_compatible_with = target_compatible_with, |
James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 449 | tsconfig = { |
| 450 | "compilerOptions": { |
| 451 | "declaration": True, |
| 452 | "lib": [ |
| 453 | "ES2015", |
| 454 | "ES2020.BigInt", |
| 455 | "DOM", |
| 456 | ], |
| 457 | "module": "es2015", |
| 458 | "moduleResolution": "node", |
| 459 | "strict": True, |
| 460 | "types": ["node"], |
| 461 | }, |
| 462 | }, |
| 463 | deps = deps + ["@com_github_google_flatbuffers//ts:flatbuffers"] + (["@com_github_google_flatbuffers//reflection:reflection_ts_fbs"] if include_reflection else []), |
| 464 | ) |
| 465 | js_library( |
| 466 | name = name, |
| 467 | visibility = visibility, |
| 468 | compatible_with = compatible_with, |
| 469 | restricted_to = restricted_to, |
| 470 | target_compatible_with = target_compatible_with, |
| 471 | deps = [name + "_ts"], |
| 472 | ) |
| 473 | native.filegroup( |
| 474 | name = "%s_includes" % (name), |
| 475 | srcs = srcs + includes, |
| 476 | compatible_with = compatible_with, |
| 477 | restricted_to = restricted_to, |
| 478 | visibility = visibility, |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 479 | ) |