github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/flatbuffers/build_defs.bzl (about) 1 """BUILD rules for generating flatbuffer files.""" 2 3 flatc_path = "@flatbuffers//:flatc" 4 5 DEFAULT_FLATC_ARGS = [ 6 "--no-union-value-namespacing", 7 "--gen-object-api", 8 ] 9 10 def flatbuffer_library_public( 11 name, 12 srcs, 13 outs, 14 language_flag, 15 out_prefix = "", 16 includes = [], 17 include_paths = [], 18 flatc_args = DEFAULT_FLATC_ARGS, 19 reflection_name = "", 20 reflection_visiblity = None, 21 output_to_bindir = False): 22 """Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler. 23 24 Outs: 25 filegroup(name): all generated source files. 26 Fileset([reflection_name]): (Optional) all generated reflection binaries. 27 28 Args: 29 name: Rule name. 30 srcs: Source .fbs files. Sent in order to the compiler. 31 outs: Output files from flatc. 32 language_flag: Target language flag. One of [-c, -j, -js]. 33 out_prefix: Prepend this path to the front of all generated files except on 34 single source targets. Usually is a directory name. 35 includes: Optional, list of filegroups of schemas that the srcs depend on. 36 include_paths: Optional, list of paths the includes files can be found in. 37 flatc_args: Optional, list of additional arguments to pass to flatc. 38 reflection_name: Optional, if set this will generate the flatbuffer 39 reflection binaries for the schemas. 40 reflection_visibility: The visibility of the generated reflection Fileset. 41 output_to_bindir: Passed to genrule for output to bin directory. 42 """ 43 include_paths_cmd = ["-I %s" % (s) for s in include_paths] 44 45 # '$(@D)' when given a single source target will give the appropriate 46 # directory. Appending 'out_prefix' is only necessary when given a build 47 # target with multiple sources. 48 output_directory = ( 49 ("-o $(@D)/%s" % (out_prefix)) if len(srcs) > 1 else ("-o $(@D)") 50 ) 51 genrule_cmd = " ".join([ 52 "for f in $(SRCS); do", 53 "$(location %s)" % (flatc_path), 54 " ".join(flatc_args), 55 " ".join(include_paths_cmd), 56 language_flag, 57 output_directory, 58 "$$f;", 59 "done", 60 ]) 61 native.genrule( 62 name = name, 63 srcs = srcs, 64 outs = outs, 65 output_to_bindir = output_to_bindir, 66 tools = includes + [flatc_path], 67 cmd = genrule_cmd, 68 message = "Generating flatbuffer files for %s:" % (name), 69 ) 70 if reflection_name: 71 reflection_genrule_cmd = " ".join([ 72 "for f in $(SRCS); do", 73 "$(location %s)" % (flatc_path), 74 "-b --schema", 75 " ".join(flatc_args), 76 " ".join(include_paths_cmd), 77 language_flag, 78 output_directory, 79 "$$f;", 80 "done", 81 ]) 82 reflection_outs = [ 83 (out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1]) 84 for s in srcs 85 ] 86 native.genrule( 87 name = "%s_srcs" % reflection_name, 88 srcs = srcs, 89 outs = reflection_outs, 90 output_to_bindir = output_to_bindir, 91 tools = includes + [flatc_path], 92 cmd = reflection_genrule_cmd, 93 message = "Generating flatbuffer reflection binary for %s:" % (name), 94 ) 95 # TODO(b/114456773): Make bazel rules proper and supported by flatbuffer 96 # Have to comment this since FilesetEntry is not supported in bazel 97 # skylark. 98 # native.Fileset( 99 # name = reflection_name, 100 # out = "%s_out" % reflection_name, 101 # entries = [ 102 # native.FilesetEntry(files = reflection_outs), 103 # ], 104 # visibility = reflection_visiblity, 105 # ) 106 107 def flatbuffer_cc_library( 108 name, 109 srcs, 110 srcs_filegroup_name = "", 111 out_prefix = "", 112 includes = [], 113 include_paths = [], 114 flatc_args = DEFAULT_FLATC_ARGS, 115 visibility = None, 116 srcs_filegroup_visibility = None, 117 gen_reflections = False): 118 '''A cc_library with the generated reader/writers for the given flatbuffer definitions. 119 120 Outs: 121 filegroup([name]_srcs): all generated .h files. 122 filegroup(srcs_filegroup_name if specified, or [name]_includes if not): 123 Other flatbuffer_cc_library's can pass this in for their `includes` 124 parameter, if they depend on the schemas in this library. 125 Fileset([name]_reflection): (Optional) all generated reflection binaries. 126 cc_library([name]): library with sources and flatbuffers deps. 127 128 Remarks: 129 ** Because the genrule used to call flatc does not have any trivial way of 130 computing the output list of files transitively generated by includes and 131 --gen-includes (the default) being defined for flatc, the --gen-includes 132 flag will not work as expected. The way around this is to add a dependency 133 to the flatbuffer_cc_library defined alongside the flatc included Fileset. 134 For example you might define: 135 136 flatbuffer_cc_library( 137 name = "my_fbs", 138 srcs = [ "schemas/foo.fbs" ], 139 includes = [ "//third_party/bazz:bazz_fbs_includes" ], 140 ) 141 142 In which foo.fbs includes a few files from the Fileset defined at 143 //third_party/bazz:bazz_fbs_includes. When compiling the library that 144 includes foo_generated.h, and therefore has my_fbs as a dependency, it 145 will fail to find any of the bazz *_generated.h files unless you also 146 add bazz's flatbuffer_cc_library to your own dependency list, e.g.: 147 148 cc_library( 149 name = "my_lib", 150 deps = [ 151 ":my_fbs", 152 "//third_party/bazz:bazz_fbs" 153 ], 154 ) 155 156 Happy dependent Flatbuffering! 157 158 Args: 159 name: Rule name. 160 srcs: Source .fbs files. Sent in order to the compiler. 161 srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this 162 filegroup into the `includes` parameter of any other 163 flatbuffer_cc_library that depends on this one's schemas. 164 out_prefix: Prepend this path to the front of all generated files. Usually 165 is a directory name. 166 includes: Optional, list of filegroups of schemas that the srcs depend on. 167 ** SEE REMARKS BELOW ** 168 include_paths: Optional, list of paths the includes files can be found in. 169 flatc_args: Optional list of additional arguments to pass to flatc 170 (e.g. --gen-mutable). 171 visibility: The visibility of the generated cc_library. By default, use the 172 default visibility of the project. 173 srcs_filegroup_visibility: The visibility of the generated srcs filegroup. 174 By default, use the value of the visibility parameter above. 175 gen_reflections: Optional, if true this will generate the flatbuffer 176 reflection binaries for the schemas. 177 ''' 178 output_headers = [ 179 (out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1]) 180 for s in srcs 181 ] 182 reflection_name = "%s_reflection" % name if gen_reflections else "" 183 184 flatbuffer_library_public( 185 name = "%s_srcs" % (name), 186 srcs = srcs, 187 outs = output_headers, 188 language_flag = "-c", 189 out_prefix = out_prefix, 190 includes = includes, 191 include_paths = include_paths, 192 flatc_args = flatc_args, 193 reflection_name = reflection_name, 194 reflection_visiblity = visibility, 195 ) 196 native.cc_library( 197 name = name, 198 hdrs = output_headers, 199 srcs = output_headers, 200 features = [ 201 "-parse_headers", 202 ], 203 deps = [ 204 "@flatbuffers//:runtime_cc", 205 ], 206 includes = ["."], 207 linkstatic = 1, 208 visibility = visibility, 209 ) 210 211 # A filegroup for the `srcs`. That is, all the schema files for this 212 # Flatbuffer set. 213 native.filegroup( 214 name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name), 215 srcs = srcs, 216 visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility, 217 )