Helper rules for Apple platforms

On this page:

apple_genrule

Genrule which provides Apple specific environment and make variables.

This mirrors the native genrule except that it provides a different set of make variables. This rule will only run on a Mac.

Example of use:

load("@build_bazel_apple_support//rules:apple_genrule.bzl", "apple_genrule")

apple_genrule(
    name = "world",
    outs = ["hi"],
    cmd = "touch $@",
)

This rule also does location expansion, much like the native genrule. For example, $(location hi) may be used to refer to the output in the above example.

The set of make variables that are supported for this rule:

  • OUTS: The outs list. If you have only one output file, you can also use $@.
  • SRCS: The srcs list (or more precisely, the pathnames of the files corresponding to labels in the srcs list). If you have only one source file, you can also use $<.
  • <: srcs, if it's a single file.
  • @: outs, if it's a single file.
  • @D: The output directory. If there is only one filename in outs, this expands to the directory containing that file. If there are multiple filenames, this variable instead expands to the package's root directory in the genfiles tree, even if all the generated files belong to the same subdirectory.

The following environment variables are defined when the rule is executed:

  • DEVELOPER_DIR: The base developer directory as defined on Apple architectures, most commonly used in invoking Apple tools such as xcrun.
  • SDKROOT: The base SDK directory as defined on Apple architectures, most commonly used in invoking Apple tools such as xcrun.

NOTE: DEVELOPER_DIR and SDKROOT are environment variables and not make variables. To refer to them in cmd you must use environment variable syntax (i.e. using $$). Example: cmd = "xcrun --sdkroot $$SDKROOT clang...

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcsA list of inputs for this rule, such as source files to process.List of labelsoptional[]
outsA list of files generated by this rule. If the executable flag is set, outs must contain exactly one label.List of labels; nonconfigurablerequired
cmdThe command to run. Subject the variable substitution.Stringrequired
executableDeclare output to be executable. Setting this flag to 1 means the output is an executable file and can be run using the run command. The genrule must produce exactly one output in this case.BooleanoptionalFalse
messageA progress message to be reported as the rule runs.Stringoptional""
no_sandboxIf the sandbox should be disabled when the action is run.BooleanoptionalFalse
toolsA list of tool dependencies for this rule, they will be available when the action is run.List of labelsoptional[]

toolchain_substitution

Register a subsitution, from a variable to a file, for use in arguments in a target's attributes. Useful for passing custom tools to a compile or link.

Example:

load("@build_bazel_apple_support//rules:toolchain_substitution.bzl", "toolchain_substitution")

toolchain_substitution(
    name = "resource_rules",
    src = "resource_rules.plist",
    var_name = "RULES",
)

ios_application(
    ...
    codesignopts = ["--resource-rules=$(RULES)"],
    codesign_inputs = [":resource_rules"],
    toolchains = [":resource_rules"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcSource file to substituteLabelrequired
var_nameName of the variable to substitute, ex: FOOStringrequired

universal_binary

This rule produces a multi-architecture (“fat”) binary targeting Apple macOS platforms regardless of the architecture of the macOS host platform. The lipo tool is used to combine built binaries of multiple architectures. For non-macOS platforms, this simply just creates a symbolic link of the input binary.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
binaryTarget to generate a ‘fat’ binary from.Labelrequired