Skip to main content

command_alias

name

def name(
*,
name: str,
default_target_platform: None | str = ...,
target_compatible_with: list[str] = ...,
compatible_with: list[str] = ...,
exec_compatible_with: list[str] = ...,
visibility: list[str] = ...,
within_view: list[str] = ...,
metadata: OpaqueMetadata = ...,
tests: list[str] = ...,
modifiers: OpaqueMetadata = ...,
_apple_platforms: dict[str, str] = ...,
_exec_os_type: str = ...,
_target_os_type: str = ...,
args: list[str] = ...,
contacts: list[str] = ...,
default_host_platform: None | str = ...,
env: dict[str, str] = ...,
exe: None | str = ...,
labels: list[str] = ...,
licenses: list[str] = ...,
platform_exe: dict[str, str] = ...,
resources: list[str] = ...,
run_using_single_arg: bool = ...,
) -> None

The command_alias rule enables you to wrap build rules that create binaries and to pre-apply command-line arguments and environment variables.

Parameters

  • name: name of the target

  • default_target_platform: specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: a list of visibility patterns restricting what targets can depend on this one

  • within_view: a list of visibility patterns restricting what this target can depend on

  • metadata: a key-value map of metadata associated with this target

  • tests: a list of targets that provide tests for this one

  • modifiers: an array of modifiers associated with this target

  • args: A string of arguments that is passed to the executable specified by exe at startup. These arguments support a subset of Buck's string parameter macros . Only the $(location ...) and $(exe ...) macros are supported currently.

  • env: A map of environment variables that will be passed to the executable represented by exe on startup. Environment variables support the same macros as arguments.

  • exe: A build target for a rule that outputs an executable, such as an sh_binary(), or an executable source file.

  • platform_exe: A mapping from platforms to build target. enables you to override exe per host platform.

    If present, exe will be used as a fallback on host platforms that are not specified in platform_exe.

    It is possible to omit exe when providing platform_exe. In that case, the build will fail if the command is invoked on a platform not specified in the mapping.

    Valid platforms are all values of the Platform enum :

    • FREEBSD
    • LINUX
    • MACOS
    • WINDOWS
  • run_using_single_arg: Ensure that the command alias can be run as a single argument (instead of $(exe) or RunInfo potentially expanding to multiple arguments).

Details

Example uses include running a command written in a scripting language with a specific interpreter, and transparently wrapping sub-commands of a binary.

You can reference a command_alias target in the cmd parameter of a genrule() by using the exe macro:



$(exe //path/to:target)

Examples:


# Combining an interpreter and a script

cxx_binary(
name = "node-js",
srcs = [
# ...
],
headers = [
# ...
],
)

export_file(
name = "scripts"
)

command_alias(
name = "server",
exe = ":node-js",
args = [
"$(location :scripts)/start-server.js",
],
)


# Exposing sub commands

export_file(
name = "yarn",
src = "yarn.sh",
)

command_alias(
name = "add",
exe = ":yarn",
args = ["add"],
)

command_alias(
name = "install",
exe = ":yarn",
args = ["install"],
)

command_alias(
name = "run",
exe = ":yarn",
args = ["run"],
)


# Platform specific commands

export_file(
name = "node-windows",
src = "windows/node.exe",
)

export_file(
name = "node-linux",
src = "linux/node",
)

export_file(
name = "node-macos",
src = "macos/node",
)

command_alias(
name = "node",
platform_exe = {
"windows": ":node-windows",
"linux": ":node-linux",
"macos": ":node-macos",
},
)