Buck: load()
Support Ukraine. Help Provide Humanitarian Aid to Ukraine.

load()

The load() function is used to include definitions (macros and constants) from another file.

The load() function executes a file of build-file-style code in the context of the current build file, so code in the included file may reference Buck functions, such as java_library, java_test, etc., as well as load() itself!

The motivation behind load() is to avoid copy-and-pasting code across multiple build files. Often, loaded files will contain data declarations (as shown in the example below) or definitions of macros for creating more complex build rules.

Arguments

  • label (required) #

    The label identifying file containing macros and constants. It's exactly the same as build target syntax but all macro files are assumed to be implicitly exported using buck.export_file with its original name, so a macro file defs from package directory is accessible using //package:defs syntax.

  • **symbols (required) #

    Names of macros and constants to import from path. In load() function keyword arguments have special meaning and can be used to change names of imported symbols. For example, load("//tools/build_rules:build_defs", my_rule="some_rule") will export some_rule from build_defs and make it available under the name my_rule. This can be useful to avoid name collisions with local definitions.

Examples

Suppose the file core/DEFS contains the following:
JARS_TO_EXCLUDE_FROM_DX = [
  'third_party/guava/guava-14.0.1.jar',
  'third_party/jackson/jackson-core-2.9.7.jar',
  'third_party/jackson/jackson-databind-2.9.7.jar',
  'third_party/jackson/jackson-datatype-guava-2.9.7.jar',
]
Then another build file could include the array using load(). This eliminates the need to copy-and-paste definitions across build files:
load('//core:DEFS', 'JARS_TO_EXCLUDE_FROM_DX')

android_binary(
  name = 'example',
  # ...
  no_dx = JARS_TO_EXCLUDE_FROM_DX,
)
Alternatively, to make an imported symbol available under a different name, keywords can be used instead:
load('//core:DEFS', no_dx='JARS_TO_EXCLUDE_FROM_DX')

android_binary(
  name = 'example',
  # ...
  no_dx = no_dx,
)