Skip to main content

sh_binary

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] = ...,
_target_os_type: str = ...,
append_script_extension: bool = ...,
contacts: list[str] = ...,
copy_resources: bool = ...,
default_host_platform: None | str = ...,
deps: list[str] = ...,
labels: list[str] = ...,
licenses: list[str] = ...,
main: str,
resources: list[str] = ...,
) -> None

An sh_binary() is used to execute a shell script.

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

  • append_script_extension: By default, sh_binary ensures that the script has an appropriate extension (e.g. .sh or .bat), appending one itself if necessary. Setting this to False prevents that behavior and makes the caller responsible for ensuring an existing appropriate extension.

  • copy_resources: By default, sh_binary attempts to use symbolic links for the resources. This can be changed so, that copies are made instead.

  • main: Either the path to the script (relative to the build file), or a build target. This file must be executable in order to be run.

  • resources: A list of files or build rules that this rule requires in order to run. These could be things such as random data files.

    When the script runs, the $BUCK_DEFAULT_RUNTIME_RESOURCES environment variable specifies the directory that contains these resources. This directory's location is determined entirely by Buck; the script should not assume the directory's location.

    The resources are also made available in a tree structure that mirrors their locations in the source and buck-out trees. The environment variable $BUCK_PROJECT_ROOT specifies a directory that contains all the resources, laid out in their locations relative to the original buck project root.

Details

Examples:

This sh_binary() just cats a sample data file back at the user.


# $REPO/BUCK
sh_binary(
name = "script",
main = "script.sh",
resources = [
"data.dat",
],
)


# Sample data file with data we need at runtime
$ echo "I'm a datafile" > data.dat

# Create a simple script that prints out the resource
$ cat > script.sh
#!/bin/sh
cat $BUCK_DEFAULT_RUNTIME_RESOURCES/data.dat

# Make sure the script is executable
$ chmod u+x script.sh

# Run the script, and see that it prints out the resource we provided
$ buck run //:script
Jobs completed: 4. Time elapsed: 0.2s.
BUILD SUCCEEDED
I'm a datafile