Search code examples
bazelbazel-rules

Can you create a config_setting for an environment variable specified with action_env?


I'm trying to select between the development and distribution provisioning profiles for my iOS application built with Bazel.

Right now I'm hacking this with a config_setting based on the compilation mode:

config_setting(
    name = "isFastBuild",
    values = {"compilation_mode": "fastbuild"}
)

config_setting(
    name = "isDebugBuild",
    values = {"compilation_mode": "dbg"}
)

config_setting(
    name = "isOptimizedBuild",
    values = {"compilation_mode": "opt"}
)

...

    provisioning_profile = select({
        ":isFastBuild": ":DevelopmentProfile",
        ":isDebugBuild": ":DevelopmentProfile",
        ":isOptimizedBuild": ":DistributionProfile",
    }),

But ideally I could just pass an environment variable that would make this more explicit since the compilation mode doesn't really imply whether it's a release build or not.

Is there a way I can use an environment variable (or some other feature) to accomplish this more explicitly?


Solution

  • There are a number of things you can do:

    • Make a constraint_setting with appropriate constraint_values for each of your profiles. Then make a platform for each release profile that sets the appropriate constraint value, and use the correct platform with --platforms when you want to build.
    • Use a build_setting instead of constraint_setting. Making it a flag will enable you to set it's value on the command line. You can use build settings in select() statements by wrapping it in a config_setting.
    • If you want to use an environment variable instead of a command line option, you can make a repository rule that creates a BUILD file that contains a simple starlark map from the environment variables to their values. You can optionally then use those environment variable values to set the initial value of build settings, or choose what constraint value to include in your platform. The advantage of using build_settings/constraint values over a simple starlark object is that you'll be able to use transitions to change their values later. Android currently does this.