Skip to main content

sh_binary

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

Function Signature

def sh_binary(
*,
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] = {},
_target_os_type: str = "prelude//os_lookup/targets:os_lookup",
append_script_extension: bool = True,
contacts: list[str] = [],
copy_resources: bool = False,
default_host_platform: None | str = None,
deps: list[str] = [],
labels: list[str] = [],
licenses: list[str] = [],
main: str,
resources: list[str] = [],
) -> 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

  • append_script_extension: (defaults to: True)

    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.

  • contacts: (defaults to: [])

    A list of organizational contacts for this rule. These could be individuals who you would contact in the event of a failure or other issue with the rule.

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
  • copy_resources: (defaults to: False)

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

  • labels: (defaults to: [])

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query().

  • licenses: (defaults to: [])

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query

  • main: (required)

    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: (defaults to: [])

    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.

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