Bazel: using macros to generate build rules from l

2019-08-23 16:19发布

问题:

I'm new to Bazel and got a question regarding Bazel macros. I'm looking for the best way to structure our build.

Is it possible to iterate over a list containing the specifics to the build rules? For example I have a list containing srcs,deps,hdrs,name etc. This list is combined into one larger list containing all modules i want to build creating one component.

If possible can someone give a short example how this would look in code?

Thanks for your time

回答1:

ok i got it:

Content of Build.bazel:

load(":macro.bzl","buildmacro")
load(":SrcList.bzl","SrcLists","CommonDependencies")

[buildmacro(
    current_module_name = Module[0][0],
    current_module_srcs=Module[1],
    current_module_hdrs=Module[2],
    current_module_deps=Module[3] + CommonDependencies,
 )for Module in SrcLists]

Content of macro.bzl:

def buildmacro(current_module_name,current_module_srcs,current_module_hdrs,current_module_deps):         
    native.cc_library(
        name = current_module_name,     
        deps = current_module_deps, 
        srcs = current_module_srcs,
        hdrs = current_module_hdrs,         

       linkstatic = 1,
       visibility = ["//visibility:public"],
    )

Example of the SrcLists-file:

listofcode  = [["nameofrule"]["srcfiles"]["headers"]["deps"]...]
listofcode2 = ...
SrcLists = [listofcode] + [listofcode2] ...

execute bazel build :all