|
13 | 13 | # limitations under the License. |
14 | 14 | """Various things common to Bazel and Google rule implementations.""" |
15 | 15 |
|
16 | | -load("//python/private:py_info.bzl", "PyInfo") |
| 16 | +load("//python/private:py_info.bzl", "PyInfo", "PyInfoBuilder") |
17 | 17 | load("//python/private:reexports.bzl", "BuiltinPyInfo") |
18 | 18 | load(":cc_helper.bzl", "cc_helper") |
19 | 19 | load(":py_internal.bzl", "py_internal") |
@@ -282,7 +282,7 @@ def collect_imports(ctx, semantics): |
282 | 282 | if BuiltinPyInfo in dep |
283 | 283 | ]) |
284 | 284 |
|
285 | | -def collect_runfiles(ctx, files): |
| 285 | +def collect_runfiles(ctx, files = depset()): |
286 | 286 | """Collects the necessary files from the rule's context. |
287 | 287 |
|
288 | 288 | This presumes the ctx is for a py_binary, py_test, or py_library rule. |
@@ -364,84 +364,50 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports): |
364 | 364 | transitive sources collected from dependencies (the latter is only |
365 | 365 | necessary for deprecated extra actions support). |
366 | 366 | """ |
367 | | - uses_shared_libraries = False |
368 | | - has_py2_only_sources = ctx.attr.srcs_version in ("PY2", "PY2ONLY") |
369 | | - has_py3_only_sources = ctx.attr.srcs_version in ("PY3", "PY3ONLY") |
370 | | - transitive_sources_depsets = [] # list of depsets |
371 | | - transitive_sources_files = [] # list of Files |
372 | | - transitive_pyc_depsets = [direct_pyc_files] # list of depsets |
| 367 | + |
| 368 | + py_info = PyInfoBuilder() |
| 369 | + py_info.direct_pyc_files.add(direct_pyc_files) |
| 370 | + py_info.transitive_pyc_files.add(direct_pyc_files) |
| 371 | + py_info.imports.add(imports) |
| 372 | + py_info.merge_has_py2_only_sources(ctx.attr.srcs_version in ("PY2", "PY2ONLY")) |
| 373 | + py_info.merge_has_py3_only_sources(ctx.attr.srcs_version in ("PY3", "PY3ONLY")) |
| 374 | + |
373 | 375 | for target in ctx.attr.deps: |
374 | 376 | # PyInfo may not be present e.g. cc_library rules. |
375 | 377 | if PyInfo in target or BuiltinPyInfo in target: |
376 | | - info = _get_py_info(target) |
377 | | - transitive_sources_depsets.append(info.transitive_sources) |
378 | | - uses_shared_libraries = uses_shared_libraries or info.uses_shared_libraries |
379 | | - has_py2_only_sources = has_py2_only_sources or info.has_py2_only_sources |
380 | | - has_py3_only_sources = has_py3_only_sources or info.has_py3_only_sources |
381 | | - |
382 | | - # BuiltinPyInfo doesn't have this field. |
383 | | - if hasattr(info, "transitive_pyc_files"): |
384 | | - transitive_pyc_depsets.append(info.transitive_pyc_files) |
| 378 | + py_info.merge(_get_py_info(target)) |
385 | 379 | else: |
386 | 380 | # TODO(b/228692666): Remove this once non-PyInfo targets are no |
387 | 381 | # longer supported in `deps`. |
388 | 382 | files = target.files.to_list() |
389 | 383 | for f in files: |
390 | 384 | if f.extension == "py": |
391 | | - transitive_sources_files.append(f) |
392 | | - uses_shared_libraries = ( |
393 | | - uses_shared_libraries or |
394 | | - cc_helper.is_valid_shared_library_artifact(f) |
395 | | - ) |
396 | | - deps_transitive_sources = depset( |
397 | | - direct = transitive_sources_files, |
398 | | - transitive = transitive_sources_depsets, |
399 | | - ) |
| 385 | + py_info.transitive_sources.add(f) |
| 386 | + py_info.merge_uses_shared_libraries(cc_helper.is_valid_shared_library_artifact(f)) |
| 387 | + |
| 388 | + deps_transitive_sources = py_info.transitive_sources.build() |
| 389 | + py_info.transitive_sources.add(direct_sources) |
400 | 390 |
|
401 | 391 | # We only look at data to calculate uses_shared_libraries, if it's already |
402 | 392 | # true, then we don't need to waste time looping over it. |
403 | | - if not uses_shared_libraries: |
| 393 | + if not py_info.get_uses_shared_libraries(): |
404 | 394 | # Similar to the above, except we only calculate uses_shared_libraries |
405 | 395 | for target in ctx.attr.data: |
406 | 396 | # TODO(b/234730058): Remove checking for PyInfo in data once depot |
407 | 397 | # cleaned up. |
408 | 398 | if PyInfo in target or BuiltinPyInfo in target: |
409 | 399 | info = _get_py_info(target) |
410 | | - uses_shared_libraries = info.uses_shared_libraries |
| 400 | + py_info.merge_uses_shared_libraries(info.uses_shared_libraries) |
411 | 401 | else: |
412 | 402 | files = target.files.to_list() |
413 | 403 | for f in files: |
414 | | - uses_shared_libraries = cc_helper.is_valid_shared_library_artifact(f) |
415 | | - if uses_shared_libraries: |
| 404 | + py_info.merge_uses_shared_libraries(cc_helper.is_valid_shared_library_artifact(f)) |
| 405 | + if py_info.get_uses_shared_libraries(): |
416 | 406 | break |
417 | | - if uses_shared_libraries: |
| 407 | + if py_info.get_uses_shared_libraries(): |
418 | 408 | break |
419 | 409 |
|
420 | | - py_info_kwargs = dict( |
421 | | - transitive_sources = depset( |
422 | | - transitive = [deps_transitive_sources, direct_sources], |
423 | | - ), |
424 | | - imports = imports, |
425 | | - # NOTE: This isn't strictly correct, but with Python 2 gone, |
426 | | - # the srcs_version logic is largely defunct, so shouldn't matter in |
427 | | - # practice. |
428 | | - has_py2_only_sources = has_py2_only_sources, |
429 | | - has_py3_only_sources = has_py3_only_sources, |
430 | | - uses_shared_libraries = uses_shared_libraries, |
431 | | - direct_pyc_files = direct_pyc_files, |
432 | | - transitive_pyc_files = depset(transitive = transitive_pyc_depsets), |
433 | | - ) |
434 | | - |
435 | | - # TODO(b/203567235): Set `uses_shared_libraries` field, though the Bazel |
436 | | - # docs indicate it's unused in Bazel and may be removed. |
437 | | - py_info = PyInfo(**py_info_kwargs) |
438 | | - |
439 | | - # Remove args that BuiltinPyInfo doesn't support |
440 | | - py_info_kwargs.pop("direct_pyc_files") |
441 | | - py_info_kwargs.pop("transitive_pyc_files") |
442 | | - builtin_py_info = BuiltinPyInfo(**py_info_kwargs) |
443 | | - |
444 | | - return py_info, deps_transitive_sources, builtin_py_info |
| 410 | + return py_info.build(), deps_transitive_sources, py_info.build_builtin_py_info() |
445 | 411 |
|
446 | 412 | def _get_py_info(target): |
447 | 413 | return target[PyInfo] if PyInfo in target else target[BuiltinPyInfo] |
|
0 commit comments