genrule
A genrule()
is used to generate files from a shell command. It must produce a single output file or folder.
Function Signature
def genrule(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_build_only_native_code: bool = select({"prelude//android/constraints:build_only_native_code": True, "DEFAULT": False}),
_exec_os_type: str = "prelude//os_lookup/targets:os_lookup",
_genrule_toolchain: str = "gh_facebook_buck2_shims_meta//:genrule",
always_print_stderr: bool = False,
bash: None | str = None,
cacheable: None | bool = None,
cmd: None | str = None,
cmd_exe: None | str = None,
constraint_overrides: list[None | str] = [],
contacts: list[str] = [],
default_host_platform: None | str = None,
default_outs: None | list[str] = None,
enable_sandbox: None | bool = None,
env: dict[str, str] = {},
environment_expansion_separator: None | str = None,
executable: None | bool = None,
executable_outs: None | list[str] = None,
labels: list[str] = [],
licenses: list[str] = [],
metadata_env_var: None | str = None,
metadata_path: None | str = None,
need_android_tools: bool = False,
no_outputs_cleanup: bool = False,
out: None | str = None,
outs: None | dict[str, list[str]] = None,
platform_override: None | str = None,
remote: None | bool = None,
remote_execution_dependencies: list[dict[str, str]] = [],
srcs: list[str] | dict[str, str] = [],
type: None | str = None,
uses_experimental_content_based_path_hashing: bool = False,
weight: None | int = None,
) -> None
Parameters
-
name
: (required)name of the target
-
default_target_platform
: (defaults to:None
)specifies the default target platform, used when no platforms are specified on the command line
-
target_compatible_with
: (defaults to:[]
)a list of constraints that are required to be satisfied for this target to be compatible with a configuration
-
compatible_with
: (defaults to:[]
)a list of constraints that are required to be satisfied for this target to be compatible with a configuration
-
exec_compatible_with
: (defaults to:[]
)a list of constraints that are required to be satisfied for this target to be compatible with an execution platform
-
visibility
: (defaults to:[]
)a list of visibility patterns restricting what targets can depend on this one
-
within_view
: (defaults to:["PUBLIC"]
)a list of visibility patterns restricting what this target can depend on
-
metadata
: (defaults to:{}
)a key-value map of metadata associated with this target
-
tests
: (defaults to:[]
)a list of targets that provide tests for this one
-
modifiers
: (defaults to:[]
)an array of modifiers associated with this target
-
bash
: (defaults to:None
)A platform-specific version of the shell command parameter
cmd
. It runs on Linux and UNIX systems—including OSX—on whichbash
is installed. It has a higher priority thancmd
. Thebash
argument is run with/usr/bin/env bash -c
. It has access to the same set of macros and variables as thecmd
argument. -
cmd
: (defaults to:None
)The shell command to run to generate the output file. It is the fallback for
bash
andcmd_exe
arguments. The following environment variables are populated by Buck and available to the shell command. They are accessed using the syntax:${<variable>}
Example:
${SRCS}
${SRCS}
A string expansion of the
srcs
argument delimited by theenvironment_expansion_separator
argument where each element ofsrcs
will be translated into a relative path.${SRCDIR}
The relative path to a directory to which sources are copied prior to running the command.
${OUT}
The output file or directory for the
genrule()
. This variable will have whatever value is specified by theout
argument if not using named outputs. If using named outputs, this variable will be the output directory.The value should be a valid filepath. The semantics of the shell command determine whether this filepath is treated as a file or a directory. If the filepath is a directory, then the shell command needs to create it if not using named outputs. Otherwise, it will be automatically created. All outputs (directories and files) must be readable, writable, and (in the case of directories) executable by the current user.
The file or directory specified by this variable must always be written by this command. If not, the execution of this rule will be considered a failure, halting the build process.
${TMP}
A temporary directory which can be used for intermediate results and will not be bundled into the output.
-
cmd_exe
: (defaults to:None
)A platform-specific version of the shell command parameter
cmd
. It runs on Windows and has a higher priority thancmd
. Thecmd_exe
argument is run withcmd.exe /v:off /c
. It has access to the same set of macros and variables as thecmd
argument. -
default_outs
: (defaults to:None
)Default output which must be present if the
outs
arg is present. Otherwise does not apply.If a rule with
outs
is consumed without an output label, the default output is returned. The default output does not need to be present in any of the named outputs defined inouts
.Note that a maximum of one value may be present in this list. For example:
default_outs = [ "output_one", ]
is valid, whereas
default_outs = [ "output_one", "output_two", ]
is not.
-
enable_sandbox
: (defaults to:None
)Whether this target should be executed in a sandbox or not.
-
env
: (defaults to:{}
)A map of variables to be set in the environment where the shell command is run.
-
environment_expansion_separator
: (defaults to:None
)The delimiter between paths in environment variables, such as SRCS, that can contain multiple paths. It can be useful to specify this parameter if the paths could contain spaces.
-
executable
: (defaults to:None
)Whether the output of the genrule is itself executable. Marking an output as executable makes
buck run
and$(exe ...)
macro expansion work with this target. -
executable_outs
: (defaults to:None
)Only valid if the
outs
arg is present. Dictates which of those named outputs are marked as executable. -
out
: (defaults to:None
)The name of the output file or directory. The complete path to this argument is provided to the shell command through the
OUT
environment variable. Only one ofout
orouts
may be present. -
outs
: (defaults to:None
)Mapping defining
named outputs
to output paths relative to the rule's output directory. Only one ofout
orouts
may be present.Example:
genrule(
name = "named_outputs",
outs = {
"output1": [
"out1.txt",
],
"output2": [
"out2.txt",
],
},
default_outs = [ "out1.txt" ],
cmd = "echo something> $OUT/out1.txt && echo another> $OUT/out2.txt",
)Note that a maximum of one value may be present in the list in this map. For example:
outs = {
"output1": [
"out1.txt",
],
},is valid, whereas
outs = {
"output1": [
"out1.txt",
"out2.txt",
],
},is not.
-
remote
: (defaults to:None
)Opts this genrule in to remote execution. Note that it is only safe to execute a genrule remotely if it is completely hermetic and completely and correctly describes its dependencies. Defaults to false. This parameter is unstable. It is subject to removal, default reversal, and other arbitrary changes in the future.
-
srcs
: (defaults to:[]
)Either a list or a map of the source files which Buck makes available to the shell command at the path in the
SRCDIR
environment variable. If you specify a list, the source files are the names in the list. If you specify a map, the source files are made available as the names in the keys of the map, where the values of the map are the original source file names. -
type
: (defaults to:None
)Specifies the type of this genrule. This is used for logging and is particularly useful for grouping genrules that share an underlying logical "type".
For example, if you have the following
cxx_genrule
defined in the root directory of your Buck project
cxx_genrule(
name = 'cxx_gen',
type = 'epilog',
cmd = 'touch finish.txt; cp finish.txt $OUT',
out = 'finish.txt'
)then the following
buck query
command
buck query "attrfilter( type, 'epilog', '//...' )"returns
//:cxx_gen -
weight
: (defaults to:None
)How many local slots these genrule should take when executing locally.
Examples
This genrule() uses a Python script to derive a new
AndroidManifest.xml
from an
AndroidManifest.xml
in the source tree.
Note you don't need to prepend execution commands with
python
: Buck knows how to execute different
kinds of binaries using $(exe)
command.
genrule(
name = 'generate_manifest',
srcs = [
'AndroidManifest.xml',
],
bash = '$(exe //python/android:basic_to_full) ' '$SRCDIR/AndroidManifest.xml > $OUT',
cmd_exe = '$(exe //python/android:basic_to_full) ' '%SRCDIR%\AndroidManifest.xml > %OUT%',
out = 'AndroidManifest.xml',
)
genrule(
name = 'generate_manifest_with_named_outputs',
srcs = [
'AndroidManifest.xml',
],
bash = '$(exe //python/android:basic_to_full) ' '$SRCDIR/AndroidManifest.xml > $OUT/AndroidManifest.xml',
cmd_exe = '$(exe //python/android:basic_to_full) ' '%SRCDIR%\AndroidManifest.xml > %OUT%\AndroidManifest.xml',
outs = {
"manifest": [ "AndroidManifest.xml" ],
},
default_outs = [ "AndroidManifest.xml" ],
)
For named outputs, build with any of the following:
buck build //:generate_manifest_with_named_outputs
buck build //:generate_manifest_with_named_outputs[manifest]
Consume in srcs
with:
export_file(
name = "magic1",
src = ":generate_manifest_with_named_outputs",
out = "some_dir_to_copy_to/AndroidManifest.xml",
)
export_file(
name = "magic2",
src = ":generate_manifest_with_named_outputs[manifest]",
out = "some_dir_to_copy_to/AndroidManifest.xml",
)
Note that magic1
consumes generate_manifest_with_named_outputs
's default
output. magic2
consumes generate_manifest_with_named_outputs
's named
output "manifest," which happen to be pointing to the same output as the default output in this
case, but they do not have to point to the same output.