@@ -43,116 +43,36 @@ PYTHON_FILE_EXTENSIONS = [
4343
4444def create_binary_semantics_struct (
4545 * ,
46- create_executable ,
47- get_cc_details_for_binary ,
4846 get_central_uncachable_version_file ,
49- get_coverage_deps ,
50- get_debugger_deps ,
51- get_extra_common_runfiles_for_binary ,
52- get_extra_providers ,
53- get_extra_write_build_data_env ,
54- get_interpreter_path ,
55- get_imports ,
5647 get_native_deps_dso_name ,
57- get_native_deps_user_link_flags ,
58- get_stamp_flag ,
59- maybe_precompile ,
6048 should_build_native_deps_dso ,
61- should_create_init_files ,
6249 should_include_build_data ):
6350 """Helper to ensure a semantics struct has all necessary fields.
6451
6552 Call this instead of a raw call to `struct(...)`; it'll help ensure all
6653 the necessary functions are being correctly provided.
6754
6855 Args:
69- create_executable: Callable; creates a binary's executable output. See
70- py_executable.bzl#py_executable_base_impl for details.
71- get_cc_details_for_binary: Callable that returns a `CcDetails` struct; see
72- `create_cc_detail_struct`.
7356 get_central_uncachable_version_file: Callable that returns an optional
7457 Artifact; this artifact is special: it is never cached and is a copy
7558 of `ctx.version_file`; see py_builtins.copy_without_caching
76- get_coverage_deps: Callable that returns a list of Targets for making
77- coverage work; only called if coverage is enabled.
78- get_debugger_deps: Callable that returns a list of Targets that provide
79- custom debugger support; only called for target-configuration.
80- get_extra_common_runfiles_for_binary: Callable that returns a runfiles
81- object of extra runfiles a binary should include.
82- get_extra_providers: Callable that returns extra providers; see
83- py_executable.bzl#_create_providers for details.
84- get_extra_write_build_data_env: Callable that returns a dict[str, str]
85- of additional environment variable to pass to build data generation.
86- get_interpreter_path: Callable that returns an optional string, which is
87- the path to the Python interpreter to use for running the binary.
88- get_imports: Callable that returns a list of the target's import
89- paths (from the `imports` attribute, so just the target's own import
90- path strings, not from dependencies).
9159 get_native_deps_dso_name: Callable that returns a string, which is the
9260 basename (with extension) of the native deps DSO library.
93- get_native_deps_user_link_flags: Callable that returns a list of strings,
94- which are any extra linker flags to pass onto the native deps DSO
95- linking action.
96- get_stamp_flag: Callable that returns bool of if the --stamp flag was
97- enabled or not.
98- maybe_precompile: Callable that may optional precompile the input `.py`
99- sources and returns the full set of desired outputs derived from
100- the source files (e.g., both py and pyc, only one of them, etc).
10161 should_build_native_deps_dso: Callable that returns bool; True if
10262 building a native deps DSO is supported, False if not.
103- should_create_init_files: Callable that returns bool; True if
104- `__init__.py` files should be generated, False if not.
10563 should_include_build_data: Callable that returns bool; True if
10664 build data should be generated, False if not.
10765 Returns:
10866 A "BinarySemantics" struct.
10967 """
11068 return struct (
11169 # keep-sorted
112- create_executable = create_executable ,
113- get_cc_details_for_binary = get_cc_details_for_binary ,
11470 get_central_uncachable_version_file = get_central_uncachable_version_file ,
115- get_coverage_deps = get_coverage_deps ,
116- get_debugger_deps = get_debugger_deps ,
117- get_extra_common_runfiles_for_binary = get_extra_common_runfiles_for_binary ,
118- get_extra_providers = get_extra_providers ,
119- get_extra_write_build_data_env = get_extra_write_build_data_env ,
120- get_imports = get_imports ,
121- get_interpreter_path = get_interpreter_path ,
12271 get_native_deps_dso_name = get_native_deps_dso_name ,
123- get_native_deps_user_link_flags = get_native_deps_user_link_flags ,
124- get_stamp_flag = get_stamp_flag ,
125- maybe_precompile = maybe_precompile ,
12672 should_build_native_deps_dso = should_build_native_deps_dso ,
127- should_create_init_files = should_create_init_files ,
12873 should_include_build_data = should_include_build_data ,
12974 )
13075
131- def create_library_semantics_struct (
132- * ,
133- get_cc_info_for_library ,
134- get_imports ,
135- maybe_precompile ):
136- """Create a `LibrarySemantics` struct.
137-
138- Call this instead of a raw call to `struct(...)`; it'll help ensure all
139- the necessary functions are being correctly provided.
140-
141- Args:
142- get_cc_info_for_library: Callable that returns a CcInfo for the library;
143- see py_library_impl for arg details.
144- get_imports: Callable; see create_binary_semantics_struct.
145- maybe_precompile: Callable; see create_binary_semantics_struct.
146- Returns:
147- a `LibrarySemantics` struct.
148- """
149- return struct (
150- # keep sorted
151- get_cc_info_for_library = get_cc_info_for_library ,
152- get_imports = get_imports ,
153- maybe_precompile = maybe_precompile ,
154- )
155-
15676def create_cc_details_struct (
15777 * ,
15878 cc_info_for_propagating ,
@@ -241,12 +161,8 @@ def collect_cc_info(ctx, extra_deps = []):
241161 Returns:
242162 CcInfo provider of merged information.
243163 """
244- deps = ctx .attr .deps
245- if extra_deps :
246- deps = list (deps )
247- deps .extend (extra_deps )
248164 cc_infos = []
249- for dep in deps :
165+ for dep in collect_deps ( ctx , extra_deps ) :
250166 if CcInfo in dep :
251167 cc_infos .append (dep [CcInfo ])
252168
@@ -255,29 +171,28 @@ def collect_cc_info(ctx, extra_deps = []):
255171
256172 return cc_common .merge_cc_infos (cc_infos = cc_infos )
257173
258- def collect_imports (ctx , semantics ):
174+ def collect_imports (ctx , extra_deps = [] ):
259175 """Collect the direct and transitive `imports` strings.
260176
261177 Args:
262178 ctx: {type}`ctx` the current target ctx
263- semantics: semantics object for fetching direct imports.
179+ extra_deps: list of Target to also collect imports from .
264180
265181 Returns:
266182 {type}`depset[str]` of import paths
267183 """
184+
268185 transitive = []
269- for dep in ctx . attr . deps :
186+ for dep in collect_deps ( ctx , extra_deps ) :
270187 if PyInfo in dep :
271188 transitive .append (dep [PyInfo ].imports )
272189 if BuiltinPyInfo != None and BuiltinPyInfo in dep :
273190 transitive .append (dep [BuiltinPyInfo ].imports )
274- return depset (direct = semantics . get_imports (ctx ), transitive = transitive )
191+ return depset (direct = get_imports (ctx ), transitive = transitive )
275192
276193def get_imports (ctx ):
277194 """Gets the imports from a rule's `imports` attribute.
278195
279- See create_binary_semantics_struct for details about this function.
280-
281196 Args:
282197 ctx: Rule ctx.
283198
@@ -562,3 +477,19 @@ def runfiles_root_path(ctx, short_path):
562477 return short_path [3 :]
563478 else :
564479 return "{}/{}" .format (ctx .workspace_name , short_path )
480+
481+ def collect_deps (ctx , extra_deps = []):
482+ """Collect the dependencies from the rule's context.
483+
484+ Args:
485+ ctx: rule ctx
486+ extra_deps: list of Target to also collect dependencies from.
487+
488+ Returns:
489+ list of Target
490+ """
491+ deps = ctx .attr .deps
492+ if extra_deps :
493+ deps = list (deps )
494+ deps .extend (extra_deps )
495+ return deps
0 commit comments