Search code examples
bazel

How to know the compilation mode in a genrule


I'm using bazel to build my android project. I need to access an environment variable DEBUG(self-defined) to determine what value of BuildConfig.DEBUG should be, but I can't find any description about this in Bazel's doc. Does Bazel support this? Or what can I do to reach my intent?

Thanks very much for any help!

PS: I'm using the genrule rule to generate my BuildConfig.java, but the value of BuildConfig.DEBUG should be determined by the environment variable DEBUG:

genrule(
    name = "build-config-genrule",
    outs = [ "BuildConfig.java" ],
    cmd  = "echo 'package com.qzone;" +
           "public class BuildConfig {" +
           "public static final boolean DEBUG = ???;" +
           "}' > $(@)"
)

Solution

  • You can use the $(COMPILATION_MODE) Make Variable in the genrule.cmd:

    COMPILATION_MODE: "fastbuild", "dbg", or "opt".

    See Make Variable substitution.

    EDIT: Important to mention that COMPILATION_MODE reflects the value of the -c / --compilation_mode flag, but there's no way in general to specify values on the command line that you could access in genrule.cmd.