load()function is used to include definitions (macros and constants) from another file.
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_test, etc., as well as
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.
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_filewith its original name, so a macro file
packagedirectory is accessible using
Names of macros and constants to import from
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
build_defsand make it available under the name
my_rule. This can be useful to avoid name collisions with local definitions.
ExamplesSuppose the file
core/DEFScontains the following:
JARS_TO_EXCLUDE_FROM_DX = [ 'third_party/guava/guava-14.0.1.jar', 'third_party/jackson/jackson-core-2.7.8.jar', 'third_party/jackson/jackson-databind-2.7.8.jar', 'third_party/jackson/jackson-datatype-guava-2.0.4.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, )