//python/private:py_binary_rule.bzl
Rule implementation of py_binary for Bazel.
- rule py_binary(name, srcs, data=[], deps=[], distribs=[], env={}, imports=[], legacy_create_init=-1, licenses=[], main=None, precompile='inherit', precompile_invalidation_mode='auto', precompile_optimize_level=0, precompile_source_retention='inherit', pyc_collection='inherit', python_version='PY3', srcs_version='PY2AND3', stamp=-1)
- Provides:
- Attributes:
A unique name for this target.
mandatory
The list of Python source files that are processed to create the target. This includes all your checked-in code and may include generated source files. The
.pyfiles belong insrcsand library targets belong indeps. Other binary files that may be needed at run time belong indata.mandatory
data– (list[label]) (default [])The list of files need by this library at runtime. See comments about the
dataattribute typically defined by rules.There is no
py_embed_datalike there iscc_embed_dataandgo_embed_data. This is because Python has a concept of runtime resources.optional
deps– (list[label]) (default [])List of additional libraries to be linked in to the target. See comments about the
depsattribute typically defined by rules. These are typicallypy_libraryrules.Targets that only provide data files used at runtime belong in the
dataattribute.optional
distribs– (list[str]) (default [])optional
env– (dict[str,str]) (default {})Dictionary of strings; optional; values are subject to
$(location)and “Make variable” substitution.Specifies additional environment variables to set when the target is executed by
testorrun.optional
imports– (list[str]) (default [])List of import directories to be added to the PYTHONPATH.
Subject to “Make variable” substitution. These import directories will be added for this rule and all rules that depend on it (note: not the rules this rule depends on. Each directory will be added to
PYTHONPATHbypy_binaryrules that depend on this rule. The strings are repo-runfiles-root relative,Absolute paths (paths that start with
/) and paths that references a path above the execution root are not allowed and will result in an error.optional
legacy_create_init– (int) (default -1)Whether to implicitly create empty
__init__.pyfiles in the runfiles tree. These are created in every directory containing Python source code or shared libraries, and every parent directory of those directories, excluding the repo root directory. The default,-1(auto), means true unless--incompatible_default_to_explicit_init_pyis used. If false, the user is responsible for creating (possibly empty)__init__.pyfiles and adding them to thesrcsof Python targets as required.optional
licenses– (list[str]) (default [])optional
Optional; the name of the source file that is the main entry point of the application. This file must also be listed in
srcs. If left unspecified,name, with.pyappended, is used instead. Ifnamedoes not match any filename insrcs,mainmust be specified.optional
precompile– (str) (default “inherit”)Whether py source files for this target should be precompiled.
Values:
inherit: Allow the downstream binary decide if precompiled files are used.enabled: Compile Python source files at build time.disabled: Don’t compile Python source files at build time.
See also
The
--precompileflag, which can override this attribute in some cases and will affect all targets when building.The
pyc_collectionattribute for transitively enabling precompiling on a per-target basis.The Precompiling docs for a guide about using precompiling.
optional
precompile_invalidation_mode– (str) (default “auto”)How precompiled files should be verified to be up-to-date with their associated source files. Possible values are:
auto: The effective value will be automatically determined by other build settings.checked_hash: Use the pyc file if the hash of the source file matches the hash recorded in the pyc file. This is most useful when working with code that you may modify.unchecked_hash: Always use the pyc file; don’t check the pyc’s hash against the source file. This is most useful when the code won’t be modified.
For more information on pyc invalidation modes, see https://docs.python.org/3/library/py_compile.html#py_compile.PycInvalidationMode
optional
precompile_optimize_level– (int) (default 0)The optimization level for precompiled files.
For more information about optimization levels, see the
compile()function’soptimizearg docs at https://docs.python.org/3/library/functions.html#compileNOTE: The value
-1means “current interpreter”, which will be the interpreter used at build time when pycs are generated, not the interpreter used at runtime when the code actually runs.optional
precompile_source_retention– (str) (default “inherit”)Determines, when a source file is compiled, if the source file is kept in the resulting output or not. Valid values are:
inherit: Inherit the value from the--precompile_source_retentionflag.keep_source: Include the original Python source.omit_source: Don’t include the original py source.
optional
pyc_collection– (str) (default “inherit”)Determines whether pyc files from dependencies should be manually included.
Valid values are:
inherit: Inherit the value from--precompile.include_pyc: Add implicitly generated pyc files from dependencies. i.e. pyc files for targets that specifyprecompile="inherit".disabled: Don’t add implicitly generated pyc files. Note that pyc files may still come from dependencies that enable precompiling at the target level.
optional
python_version– (str) (default “PY3”)Defunct, unused, does nothing.
optional
srcs_version– (str) (default “PY2AND3”)Defunct, unused, does nothing.
optional
Whether to encode build information into the binary. Possible values:
stamp = 1: Always stamp the build information into the binary, even in--nostampbuilds. This setting should be avoided, since it potentially kills remote caching for the binary and any downstream actions that depend on it.stamp = 0: Always replace build information by constant values. This gives good build result caching.stamp = -1: Embedding of build information is controlled by the--[no]stampflag.
Stamped binaries are not rebuilt unless their dependencies change.
WARNING: Stamping can harm build performance by reducing cache hits and should be avoided if possible.
optional