1*890232f2SAndroid Build Coastguard Worker# Description: 2*890232f2SAndroid Build Coastguard Worker# BUILD rules for generating flatbuffer files in various languages. 3*890232f2SAndroid Build Coastguard Worker 4*890232f2SAndroid Build Coastguard Worker""" 5*890232f2SAndroid Build Coastguard WorkerRules for building C++ flatbuffers with Bazel. 6*890232f2SAndroid Build Coastguard Worker""" 7*890232f2SAndroid Build Coastguard Worker 8*890232f2SAndroid Build Coastguard Workerload("@rules_cc//cc:defs.bzl", "cc_library") 9*890232f2SAndroid Build Coastguard Worker 10*890232f2SAndroid Build Coastguard Workerflatc_path = "@com_github_google_flatbuffers//:flatc" 11*890232f2SAndroid Build Coastguard Worker 12*890232f2SAndroid Build Coastguard WorkerDEFAULT_INCLUDE_PATHS = [ 13*890232f2SAndroid Build Coastguard Worker "./", 14*890232f2SAndroid Build Coastguard Worker "$(GENDIR)", 15*890232f2SAndroid Build Coastguard Worker "$(BINDIR)", 16*890232f2SAndroid Build Coastguard Worker "$(execpath @com_github_google_flatbuffers//:flatc).runfiles/com_github_google_flatbuffers", 17*890232f2SAndroid Build Coastguard Worker] 18*890232f2SAndroid Build Coastguard Worker 19*890232f2SAndroid Build Coastguard WorkerDEFAULT_FLATC_ARGS = [ 20*890232f2SAndroid Build Coastguard Worker "--gen-object-api", 21*890232f2SAndroid Build Coastguard Worker "--gen-compare", 22*890232f2SAndroid Build Coastguard Worker "--no-includes", 23*890232f2SAndroid Build Coastguard Worker "--gen-mutable", 24*890232f2SAndroid Build Coastguard Worker "--reflect-names", 25*890232f2SAndroid Build Coastguard Worker "--cpp-ptr-type flatbuffers::unique_ptr", 26*890232f2SAndroid Build Coastguard Worker] 27*890232f2SAndroid Build Coastguard Worker 28*890232f2SAndroid Build Coastguard Workerdef flatbuffer_library_public( 29*890232f2SAndroid Build Coastguard Worker name, 30*890232f2SAndroid Build Coastguard Worker srcs, 31*890232f2SAndroid Build Coastguard Worker outs, 32*890232f2SAndroid Build Coastguard Worker language_flag, 33*890232f2SAndroid Build Coastguard Worker out_prefix = "", 34*890232f2SAndroid Build Coastguard Worker includes = [], 35*890232f2SAndroid Build Coastguard Worker include_paths = DEFAULT_INCLUDE_PATHS, 36*890232f2SAndroid Build Coastguard Worker flatc_args = DEFAULT_FLATC_ARGS, 37*890232f2SAndroid Build Coastguard Worker reflection_name = "", 38*890232f2SAndroid Build Coastguard Worker reflection_visibility = None, 39*890232f2SAndroid Build Coastguard Worker compatible_with = None, 40*890232f2SAndroid Build Coastguard Worker restricted_to = None, 41*890232f2SAndroid Build Coastguard Worker target_compatible_with = None, 42*890232f2SAndroid Build Coastguard Worker output_to_bindir = False): 43*890232f2SAndroid Build Coastguard Worker """Generates code files for reading/writing the given flatbuffers in the requested language using the public compiler. 44*890232f2SAndroid Build Coastguard Worker 45*890232f2SAndroid Build Coastguard Worker Args: 46*890232f2SAndroid Build Coastguard Worker name: Rule name. 47*890232f2SAndroid Build Coastguard Worker srcs: Source .fbs files. Sent in order to the compiler. 48*890232f2SAndroid Build Coastguard Worker outs: Output files from flatc. 49*890232f2SAndroid Build Coastguard Worker language_flag: Target language flag. One of [-c, -j, -js]. 50*890232f2SAndroid Build Coastguard Worker out_prefix: Prepend this path to the front of all generated files except on 51*890232f2SAndroid Build Coastguard Worker single source targets. Usually is a directory name. 52*890232f2SAndroid Build Coastguard Worker includes: Optional, list of filegroups of schemas that the srcs depend on. 53*890232f2SAndroid Build Coastguard Worker include_paths: Optional, list of paths the includes files can be found in. 54*890232f2SAndroid Build Coastguard Worker flatc_args: Optional, list of additional arguments to pass to flatc. 55*890232f2SAndroid Build Coastguard Worker reflection_name: Optional, if set this will generate the flatbuffer 56*890232f2SAndroid Build Coastguard Worker reflection binaries for the schemas. 57*890232f2SAndroid Build Coastguard Worker reflection_visibility: The visibility of the generated reflection Fileset. 58*890232f2SAndroid Build Coastguard Worker output_to_bindir: Passed to genrule for output to bin directory. 59*890232f2SAndroid Build Coastguard Worker compatible_with: Optional, The list of environments this rule can be 60*890232f2SAndroid Build Coastguard Worker built for, in addition to default-supported environments. 61*890232f2SAndroid Build Coastguard Worker restricted_to: Optional, The list of environments this rule can be built 62*890232f2SAndroid Build Coastguard Worker for, instead of default-supported environments. 63*890232f2SAndroid Build Coastguard Worker target_compatible_with: Optional, The list of target platform constraints 64*890232f2SAndroid Build Coastguard Worker to use. 65*890232f2SAndroid Build Coastguard Worker output_to_bindir: Passed to genrule for output to bin directory. 66*890232f2SAndroid Build Coastguard Worker 67*890232f2SAndroid Build Coastguard Worker 68*890232f2SAndroid Build Coastguard Worker This rule creates a filegroup(name) with all generated source files, and 69*890232f2SAndroid Build Coastguard Worker optionally a Fileset([reflection_name]) with all generated reflection 70*890232f2SAndroid Build Coastguard Worker binaries. 71*890232f2SAndroid Build Coastguard Worker """ 72*890232f2SAndroid Build Coastguard Worker include_paths_cmd = ["-I %s" % (s) for s in include_paths] 73*890232f2SAndroid Build Coastguard Worker 74*890232f2SAndroid Build Coastguard Worker # '$(@D)' when given a single source target will give the appropriate 75*890232f2SAndroid Build Coastguard Worker # directory. Appending 'out_prefix' is only necessary when given a build 76*890232f2SAndroid Build Coastguard Worker # target with multiple sources. 77*890232f2SAndroid Build Coastguard Worker output_directory = ( 78*890232f2SAndroid Build Coastguard Worker ("-o $(@D)/%s" % (out_prefix)) if len(srcs) > 1 else ("-o $(@D)") 79*890232f2SAndroid Build Coastguard Worker ) 80*890232f2SAndroid Build Coastguard Worker genrule_cmd = " ".join([ 81*890232f2SAndroid Build Coastguard Worker "SRCS=($(SRCS));", 82*890232f2SAndroid Build Coastguard Worker "for f in $${SRCS[@]:0:%s}; do" % len(srcs), 83*890232f2SAndroid Build Coastguard Worker "$(location %s)" % (flatc_path), 84*890232f2SAndroid Build Coastguard Worker " ".join(include_paths_cmd), 85*890232f2SAndroid Build Coastguard Worker " ".join(flatc_args), 86*890232f2SAndroid Build Coastguard Worker language_flag, 87*890232f2SAndroid Build Coastguard Worker output_directory, 88*890232f2SAndroid Build Coastguard Worker "$$f;", 89*890232f2SAndroid Build Coastguard Worker "done", 90*890232f2SAndroid Build Coastguard Worker ]) 91*890232f2SAndroid Build Coastguard Worker native.genrule( 92*890232f2SAndroid Build Coastguard Worker name = name, 93*890232f2SAndroid Build Coastguard Worker srcs = srcs + includes, 94*890232f2SAndroid Build Coastguard Worker outs = outs, 95*890232f2SAndroid Build Coastguard Worker output_to_bindir = output_to_bindir, 96*890232f2SAndroid Build Coastguard Worker tools = [flatc_path], 97*890232f2SAndroid Build Coastguard Worker cmd = genrule_cmd, 98*890232f2SAndroid Build Coastguard Worker compatible_with = compatible_with, 99*890232f2SAndroid Build Coastguard Worker target_compatible_with = target_compatible_with, 100*890232f2SAndroid Build Coastguard Worker restricted_to = restricted_to, 101*890232f2SAndroid Build Coastguard Worker message = "Generating flatbuffer files for %s:" % (name), 102*890232f2SAndroid Build Coastguard Worker ) 103*890232f2SAndroid Build Coastguard Worker if reflection_name: 104*890232f2SAndroid Build Coastguard Worker reflection_genrule_cmd = " ".join([ 105*890232f2SAndroid Build Coastguard Worker "SRCS=($(SRCS));", 106*890232f2SAndroid Build Coastguard Worker "for f in $${SRCS[@]:0:%s}; do" % len(srcs), 107*890232f2SAndroid Build Coastguard Worker "$(location %s)" % (flatc_path), 108*890232f2SAndroid Build Coastguard Worker "-b --schema", 109*890232f2SAndroid Build Coastguard Worker " ".join(flatc_args), 110*890232f2SAndroid Build Coastguard Worker " ".join(include_paths_cmd), 111*890232f2SAndroid Build Coastguard Worker language_flag, 112*890232f2SAndroid Build Coastguard Worker output_directory, 113*890232f2SAndroid Build Coastguard Worker "$$f;", 114*890232f2SAndroid Build Coastguard Worker "done", 115*890232f2SAndroid Build Coastguard Worker ]) 116*890232f2SAndroid Build Coastguard Worker reflection_outs = [ 117*890232f2SAndroid Build Coastguard Worker (out_prefix + "%s.bfbs") % (s.replace(".fbs", "").split("/")[-1]) 118*890232f2SAndroid Build Coastguard Worker for s in srcs 119*890232f2SAndroid Build Coastguard Worker ] 120*890232f2SAndroid Build Coastguard Worker native.genrule( 121*890232f2SAndroid Build Coastguard Worker name = "%s_srcs" % reflection_name, 122*890232f2SAndroid Build Coastguard Worker srcs = srcs + includes, 123*890232f2SAndroid Build Coastguard Worker outs = reflection_outs, 124*890232f2SAndroid Build Coastguard Worker output_to_bindir = output_to_bindir, 125*890232f2SAndroid Build Coastguard Worker tools = [flatc_path], 126*890232f2SAndroid Build Coastguard Worker compatible_with = compatible_with, 127*890232f2SAndroid Build Coastguard Worker restricted_to = restricted_to, 128*890232f2SAndroid Build Coastguard Worker target_compatible_with = target_compatible_with, 129*890232f2SAndroid Build Coastguard Worker cmd = reflection_genrule_cmd, 130*890232f2SAndroid Build Coastguard Worker message = "Generating flatbuffer reflection binary for %s:" % (name), 131*890232f2SAndroid Build Coastguard Worker visibility = reflection_visibility, 132*890232f2SAndroid Build Coastguard Worker ) 133*890232f2SAndroid Build Coastguard Worker native.filegroup( 134*890232f2SAndroid Build Coastguard Worker name = "%s_out" % reflection_name, 135*890232f2SAndroid Build Coastguard Worker srcs = reflection_outs, 136*890232f2SAndroid Build Coastguard Worker visibility = reflection_visibility, 137*890232f2SAndroid Build Coastguard Worker compatible_with = compatible_with, 138*890232f2SAndroid Build Coastguard Worker restricted_to = restricted_to, 139*890232f2SAndroid Build Coastguard Worker ) 140*890232f2SAndroid Build Coastguard Worker 141*890232f2SAndroid Build Coastguard Workerdef flatbuffer_cc_library( 142*890232f2SAndroid Build Coastguard Worker name, 143*890232f2SAndroid Build Coastguard Worker srcs, 144*890232f2SAndroid Build Coastguard Worker srcs_filegroup_name = "", 145*890232f2SAndroid Build Coastguard Worker out_prefix = "", 146*890232f2SAndroid Build Coastguard Worker deps = [], 147*890232f2SAndroid Build Coastguard Worker includes = [], 148*890232f2SAndroid Build Coastguard Worker include_paths = DEFAULT_INCLUDE_PATHS, 149*890232f2SAndroid Build Coastguard Worker cc_include_paths = [], 150*890232f2SAndroid Build Coastguard Worker flatc_args = DEFAULT_FLATC_ARGS, 151*890232f2SAndroid Build Coastguard Worker visibility = None, 152*890232f2SAndroid Build Coastguard Worker compatible_with = None, 153*890232f2SAndroid Build Coastguard Worker restricted_to = None, 154*890232f2SAndroid Build Coastguard Worker target_compatible_with = None, 155*890232f2SAndroid Build Coastguard Worker srcs_filegroup_visibility = None, 156*890232f2SAndroid Build Coastguard Worker gen_reflections = False): 157*890232f2SAndroid Build Coastguard Worker """A cc_library with the generated reader/writers for the given flatbuffer definitions. 158*890232f2SAndroid Build Coastguard Worker 159*890232f2SAndroid Build Coastguard Worker Args: 160*890232f2SAndroid Build Coastguard Worker name: Rule name. 161*890232f2SAndroid Build Coastguard Worker srcs: Source .fbs files. Sent in order to the compiler. 162*890232f2SAndroid Build Coastguard Worker srcs_filegroup_name: Name of the output filegroup that holds srcs. Pass this 163*890232f2SAndroid Build Coastguard Worker filegroup into the `includes` parameter of any other 164*890232f2SAndroid Build Coastguard Worker flatbuffer_cc_library that depends on this one's schemas. 165*890232f2SAndroid Build Coastguard Worker out_prefix: Prepend this path to the front of all generated files. Usually 166*890232f2SAndroid Build Coastguard Worker is a directory name. 167*890232f2SAndroid Build Coastguard Worker deps: Optional, list of other flatbuffer_cc_library's to depend on. Cannot be specified 168*890232f2SAndroid Build Coastguard Worker alongside includes. 169*890232f2SAndroid Build Coastguard Worker includes: Optional, list of filegroups of schemas that the srcs depend on. 170*890232f2SAndroid Build Coastguard Worker Use of this is discouraged, and may be deprecated. 171*890232f2SAndroid Build Coastguard Worker include_paths: Optional, list of paths the includes files can be found in. 172*890232f2SAndroid Build Coastguard Worker cc_include_paths: Optional, list of paths to add to the cc_library includes attribute. 173*890232f2SAndroid Build Coastguard Worker flatc_args: Optional list of additional arguments to pass to flatc 174*890232f2SAndroid Build Coastguard Worker (e.g. --gen-mutable). 175*890232f2SAndroid Build Coastguard Worker visibility: The visibility of the generated cc_library. By default, use the 176*890232f2SAndroid Build Coastguard Worker default visibility of the project. 177*890232f2SAndroid Build Coastguard Worker srcs_filegroup_visibility: The visibility of the generated srcs filegroup. 178*890232f2SAndroid Build Coastguard Worker By default, use the value of the visibility parameter above. 179*890232f2SAndroid Build Coastguard Worker gen_reflections: Optional, if true this will generate the flatbuffer 180*890232f2SAndroid Build Coastguard Worker reflection binaries for the schemas. 181*890232f2SAndroid Build Coastguard Worker compatible_with: Optional, The list of environments this rule can be built 182*890232f2SAndroid Build Coastguard Worker for, in addition to default-supported environments. 183*890232f2SAndroid Build Coastguard Worker restricted_to: Optional, The list of environments this rule can be built 184*890232f2SAndroid Build Coastguard Worker for, instead of default-supported environments. 185*890232f2SAndroid Build Coastguard Worker target_compatible_with: Optional, The list of target platform constraints 186*890232f2SAndroid Build Coastguard Worker to use. 187*890232f2SAndroid Build Coastguard Worker 188*890232f2SAndroid Build Coastguard Worker This produces: 189*890232f2SAndroid Build Coastguard Worker filegroup([name]_srcs): all generated .h files. 190*890232f2SAndroid Build Coastguard Worker filegroup(srcs_filegroup_name if specified, or [name]_includes if not): 191*890232f2SAndroid Build Coastguard Worker Other flatbuffer_cc_library's can pass this in for their `includes` 192*890232f2SAndroid Build Coastguard Worker parameter, if they depend on the schemas in this library. 193*890232f2SAndroid Build Coastguard Worker Fileset([name]_reflection): (Optional) all generated reflection binaries. 194*890232f2SAndroid Build Coastguard Worker cc_library([name]): library with sources and flatbuffers deps. 195*890232f2SAndroid Build Coastguard Worker """ 196*890232f2SAndroid Build Coastguard Worker output_headers = [ 197*890232f2SAndroid Build Coastguard Worker (out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1].split(":")[-1]) 198*890232f2SAndroid Build Coastguard Worker for s in srcs 199*890232f2SAndroid Build Coastguard Worker ] 200*890232f2SAndroid Build Coastguard Worker if deps and includes: 201*890232f2SAndroid Build Coastguard Worker # There is no inherent reason we couldn't support both, but this discourages 202*890232f2SAndroid Build Coastguard Worker # use of includes without good reason. 203*890232f2SAndroid Build Coastguard Worker fail("Cannot specify both deps and include in flatbuffer_cc_library.") 204*890232f2SAndroid Build Coastguard Worker if deps: 205*890232f2SAndroid Build Coastguard Worker includes = [d + "_includes" for d in deps] 206*890232f2SAndroid Build Coastguard Worker reflection_name = "%s_reflection" % name if gen_reflections else "" 207*890232f2SAndroid Build Coastguard Worker 208*890232f2SAndroid Build Coastguard Worker srcs_lib = "%s_srcs" % (name) 209*890232f2SAndroid Build Coastguard Worker flatbuffer_library_public( 210*890232f2SAndroid Build Coastguard Worker name = srcs_lib, 211*890232f2SAndroid Build Coastguard Worker srcs = srcs, 212*890232f2SAndroid Build Coastguard Worker outs = output_headers, 213*890232f2SAndroid Build Coastguard Worker language_flag = "-c", 214*890232f2SAndroid Build Coastguard Worker out_prefix = out_prefix, 215*890232f2SAndroid Build Coastguard Worker includes = includes, 216*890232f2SAndroid Build Coastguard Worker include_paths = include_paths, 217*890232f2SAndroid Build Coastguard Worker flatc_args = flatc_args, 218*890232f2SAndroid Build Coastguard Worker compatible_with = compatible_with, 219*890232f2SAndroid Build Coastguard Worker restricted_to = restricted_to, 220*890232f2SAndroid Build Coastguard Worker target_compatible_with = target_compatible_with, 221*890232f2SAndroid Build Coastguard Worker reflection_name = reflection_name, 222*890232f2SAndroid Build Coastguard Worker reflection_visibility = visibility, 223*890232f2SAndroid Build Coastguard Worker ) 224*890232f2SAndroid Build Coastguard Worker cc_library( 225*890232f2SAndroid Build Coastguard Worker name = name, 226*890232f2SAndroid Build Coastguard Worker hdrs = [ 227*890232f2SAndroid Build Coastguard Worker ":" + srcs_lib, 228*890232f2SAndroid Build Coastguard Worker ], 229*890232f2SAndroid Build Coastguard Worker srcs = [ 230*890232f2SAndroid Build Coastguard Worker ":" + srcs_lib, 231*890232f2SAndroid Build Coastguard Worker ], 232*890232f2SAndroid Build Coastguard Worker features = [ 233*890232f2SAndroid Build Coastguard Worker "-parse_headers", 234*890232f2SAndroid Build Coastguard Worker ], 235*890232f2SAndroid Build Coastguard Worker deps = [ 236*890232f2SAndroid Build Coastguard Worker "@com_github_google_flatbuffers//:runtime_cc", 237*890232f2SAndroid Build Coastguard Worker "@com_github_google_flatbuffers//:flatbuffers", 238*890232f2SAndroid Build Coastguard Worker ] + deps, 239*890232f2SAndroid Build Coastguard Worker includes = cc_include_paths, 240*890232f2SAndroid Build Coastguard Worker compatible_with = compatible_with, 241*890232f2SAndroid Build Coastguard Worker restricted_to = restricted_to, 242*890232f2SAndroid Build Coastguard Worker target_compatible_with = target_compatible_with, 243*890232f2SAndroid Build Coastguard Worker linkstatic = 1, 244*890232f2SAndroid Build Coastguard Worker visibility = visibility, 245*890232f2SAndroid Build Coastguard Worker ) 246*890232f2SAndroid Build Coastguard Worker 247*890232f2SAndroid Build Coastguard Worker # A filegroup for the `srcs`. That is, all the schema files for this 248*890232f2SAndroid Build Coastguard Worker # Flatbuffer set. 249*890232f2SAndroid Build Coastguard Worker native.filegroup( 250*890232f2SAndroid Build Coastguard Worker name = srcs_filegroup_name if srcs_filegroup_name else "%s_includes" % (name), 251*890232f2SAndroid Build Coastguard Worker srcs = srcs + includes, 252*890232f2SAndroid Build Coastguard Worker compatible_with = compatible_with, 253*890232f2SAndroid Build Coastguard Worker restricted_to = restricted_to, 254*890232f2SAndroid Build Coastguard Worker visibility = srcs_filegroup_visibility if srcs_filegroup_visibility != None else visibility, 255*890232f2SAndroid Build Coastguard Worker ) 256