|
14 | 14 |
|
15 | 15 | ""
|
16 | 16 |
|
| 17 | +load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") |
17 | 18 | load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
|
18 | 19 | load("//python/private:envsubst.bzl", "envsubst")
|
19 | 20 | load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
|
20 | 21 | load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
|
21 | 22 | load(":attrs.bzl", "ATTRS", "use_isolated")
|
22 | 23 | load(":deps.bzl", "all_repo_names", "record_files")
|
23 | 24 | load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel")
|
| 25 | +load(":parse_requirements.bzl", "host_platform") |
24 | 26 | load(":parse_whl_name.bzl", "parse_whl_name")
|
25 | 27 | load(":patch_whl.bzl", "patch_whl")
|
26 | 28 | load(":pypi_repo_utils.bzl", "pypi_repo_utils")
|
| 29 | +load(":whl_metadata.bzl", "whl_metadata") |
27 | 30 | load(":whl_target_platforms.bzl", "whl_target_platforms")
|
28 | 31 |
|
29 | 32 | _CPPFLAGS = "CPPFLAGS"
|
@@ -340,79 +343,147 @@ def _whl_library_impl(rctx):
|
340 | 343 | timeout = rctx.attr.timeout,
|
341 | 344 | )
|
342 | 345 |
|
343 |
| - target_platforms = rctx.attr.experimental_target_platforms or [] |
344 |
| - if target_platforms: |
345 |
| - parsed_whl = parse_whl_name(whl_path.basename) |
346 |
| - |
347 |
| - # NOTE @aignas 2023-12-04: if the wheel is a platform specific wheel, we |
348 |
| - # only include deps for that target platform |
349 |
| - if parsed_whl.platform_tag != "any": |
350 |
| - target_platforms = [ |
351 |
| - p.target_platform |
352 |
| - for p in whl_target_platforms( |
353 |
| - platform_tag = parsed_whl.platform_tag, |
354 |
| - abi_tag = parsed_whl.abi_tag.strip("tm"), |
355 |
| - ) |
356 |
| - ] |
357 |
| - |
358 |
| - pypi_repo_utils.execute_checked( |
359 |
| - rctx, |
360 |
| - op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path), |
361 |
| - python = python_interpreter, |
362 |
| - arguments = args + [ |
363 |
| - "--whl-file", |
364 |
| - whl_path, |
365 |
| - ] + ["--platform={}".format(p) for p in target_platforms], |
366 |
| - srcs = rctx.attr._python_srcs, |
367 |
| - environment = environment, |
368 |
| - quiet = rctx.attr.quiet, |
369 |
| - timeout = rctx.attr.timeout, |
370 |
| - logger = logger, |
371 |
| - ) |
| 346 | + if rp_config.enable_pipstar: |
| 347 | + pypi_repo_utils.execute_checked( |
| 348 | + rctx, |
| 349 | + op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path), |
| 350 | + python = python_interpreter, |
| 351 | + arguments = args + [ |
| 352 | + "--whl-file", |
| 353 | + whl_path, |
| 354 | + "--enable-pipstar", |
| 355 | + ], |
| 356 | + srcs = rctx.attr._python_srcs, |
| 357 | + environment = environment, |
| 358 | + quiet = rctx.attr.quiet, |
| 359 | + timeout = rctx.attr.timeout, |
| 360 | + logger = logger, |
| 361 | + ) |
372 | 362 |
|
373 |
| - metadata = json.decode(rctx.read("metadata.json")) |
374 |
| - rctx.delete("metadata.json") |
| 363 | + metadata = json.decode(rctx.read("metadata.json")) |
| 364 | + rctx.delete("metadata.json") |
| 365 | + python_version = metadata["python_version"] |
375 | 366 |
|
376 |
| - # NOTE @aignas 2024-06-22: this has to live on until we stop supporting |
377 |
| - # passing `twine` as a `:pkg` library via the `WORKSPACE` builds. |
378 |
| - # |
379 |
| - # See ../../packaging.bzl line 190 |
380 |
| - entry_points = {} |
381 |
| - for item in metadata["entry_points"]: |
382 |
| - name = item["name"] |
383 |
| - module = item["module"] |
384 |
| - attribute = item["attribute"] |
385 |
| - |
386 |
| - # There is an extreme edge-case with entry_points that end with `.py` |
387 |
| - # See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174 |
388 |
| - entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name |
389 |
| - entry_point_target_name = ( |
390 |
| - _WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py |
| 367 | + # NOTE @aignas 2024-06-22: this has to live on until we stop supporting |
| 368 | + # passing `twine` as a `:pkg` library via the `WORKSPACE` builds. |
| 369 | + # |
| 370 | + # See ../../packaging.bzl line 190 |
| 371 | + entry_points = {} |
| 372 | + for item in metadata["entry_points"]: |
| 373 | + name = item["name"] |
| 374 | + module = item["module"] |
| 375 | + attribute = item["attribute"] |
| 376 | + |
| 377 | + # There is an extreme edge-case with entry_points that end with `.py` |
| 378 | + # See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174 |
| 379 | + entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name |
| 380 | + entry_point_target_name = ( |
| 381 | + _WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py |
| 382 | + ) |
| 383 | + entry_point_script_name = entry_point_target_name + ".py" |
| 384 | + |
| 385 | + rctx.file( |
| 386 | + entry_point_script_name, |
| 387 | + _generate_entry_point_contents(module, attribute), |
| 388 | + ) |
| 389 | + entry_points[entry_point_without_py] = entry_point_script_name |
| 390 | + |
| 391 | + metadata = whl_metadata( |
| 392 | + install_dir = whl_path.dirname.get_child("site-packages"), |
| 393 | + read_fn = rctx.read, |
| 394 | + logger = logger, |
391 | 395 | )
|
392 |
| - entry_point_script_name = entry_point_target_name + ".py" |
393 | 396 |
|
394 |
| - rctx.file( |
395 |
| - entry_point_script_name, |
396 |
| - _generate_entry_point_contents(module, attribute), |
| 397 | + build_file_contents = generate_whl_library_build_bazel( |
| 398 | + name = whl_path.basename, |
| 399 | + dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix), |
| 400 | + entry_points = entry_points, |
| 401 | + metadata_name = metadata.name, |
| 402 | + metadata_version = metadata.version, |
| 403 | + default_python_version = python_version, |
| 404 | + requires_dist = metadata.requires_dist, |
| 405 | + target_platforms = rctx.attr.experimental_target_platforms or [host_platform(rctx)], |
| 406 | + # TODO @aignas 2025-04-14: load through the hub: |
| 407 | + annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))), |
| 408 | + data_exclude = rctx.attr.pip_data_exclude, |
| 409 | + group_deps = rctx.attr.group_deps, |
| 410 | + group_name = rctx.attr.group_name, |
397 | 411 | )
|
398 |
| - entry_points[entry_point_without_py] = entry_point_script_name |
399 |
| - |
400 |
| - build_file_contents = generate_whl_library_build_bazel( |
401 |
| - name = whl_path.basename, |
402 |
| - dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix), |
403 |
| - entry_points = entry_points, |
404 |
| - # TODO @aignas 2025-04-14: load through the hub: |
405 |
| - dependencies = metadata["deps"], |
406 |
| - dependencies_by_platform = metadata["deps_by_platform"], |
407 |
| - annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))), |
408 |
| - data_exclude = rctx.attr.pip_data_exclude, |
409 |
| - group_deps = rctx.attr.group_deps, |
410 |
| - group_name = rctx.attr.group_name, |
411 |
| - tags = [ |
412 |
| - "pypi_name={}".format(metadata["name"]), |
413 |
| - "pypi_version={}".format(metadata["version"]), |
414 |
| - ], |
415 |
| - ) |
| 412 | + else: |
| 413 | + target_platforms = rctx.attr.experimental_target_platforms or [] |
| 414 | + if target_platforms: |
| 415 | + parsed_whl = parse_whl_name(whl_path.basename) |
| 416 | + |
| 417 | + # NOTE @aignas 2023-12-04: if the wheel is a platform specific wheel, we |
| 418 | + # only include deps for that target platform |
| 419 | + if parsed_whl.platform_tag != "any": |
| 420 | + target_platforms = [ |
| 421 | + p.target_platform |
| 422 | + for p in whl_target_platforms( |
| 423 | + platform_tag = parsed_whl.platform_tag, |
| 424 | + abi_tag = parsed_whl.abi_tag.strip("tm"), |
| 425 | + ) |
| 426 | + ] |
| 427 | + |
| 428 | + pypi_repo_utils.execute_checked( |
| 429 | + rctx, |
| 430 | + op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path), |
| 431 | + python = python_interpreter, |
| 432 | + arguments = args + [ |
| 433 | + "--whl-file", |
| 434 | + whl_path, |
| 435 | + ] + ["--platform={}".format(p) for p in target_platforms], |
| 436 | + srcs = rctx.attr._python_srcs, |
| 437 | + environment = environment, |
| 438 | + quiet = rctx.attr.quiet, |
| 439 | + timeout = rctx.attr.timeout, |
| 440 | + logger = logger, |
| 441 | + ) |
| 442 | + |
| 443 | + metadata = json.decode(rctx.read("metadata.json")) |
| 444 | + rctx.delete("metadata.json") |
| 445 | + |
| 446 | + # NOTE @aignas 2024-06-22: this has to live on until we stop supporting |
| 447 | + # passing `twine` as a `:pkg` library via the `WORKSPACE` builds. |
| 448 | + # |
| 449 | + # See ../../packaging.bzl line 190 |
| 450 | + entry_points = {} |
| 451 | + for item in metadata["entry_points"]: |
| 452 | + name = item["name"] |
| 453 | + module = item["module"] |
| 454 | + attribute = item["attribute"] |
| 455 | + |
| 456 | + # There is an extreme edge-case with entry_points that end with `.py` |
| 457 | + # See: https://github.com/bazelbuild/bazel/blob/09c621e4cf5b968f4c6cdf905ab142d5961f9ddc/src/test/java/com/google/devtools/build/lib/rules/python/PyBinaryConfiguredTargetTest.java#L174 |
| 458 | + entry_point_without_py = name[:-3] + "_py" if name.endswith(".py") else name |
| 459 | + entry_point_target_name = ( |
| 460 | + _WHEEL_ENTRY_POINT_PREFIX + "_" + entry_point_without_py |
| 461 | + ) |
| 462 | + entry_point_script_name = entry_point_target_name + ".py" |
| 463 | + |
| 464 | + rctx.file( |
| 465 | + entry_point_script_name, |
| 466 | + _generate_entry_point_contents(module, attribute), |
| 467 | + ) |
| 468 | + entry_points[entry_point_without_py] = entry_point_script_name |
| 469 | + |
| 470 | + build_file_contents = generate_whl_library_build_bazel( |
| 471 | + name = whl_path.basename, |
| 472 | + dep_template = rctx.attr.dep_template or "@{}{{name}}//:{{target}}".format(rctx.attr.repo_prefix), |
| 473 | + entry_points = entry_points, |
| 474 | + # TODO @aignas 2025-04-14: load through the hub: |
| 475 | + dependencies = metadata["deps"], |
| 476 | + dependencies_by_platform = metadata["deps_by_platform"], |
| 477 | + annotation = None if not rctx.attr.annotation else struct(**json.decode(rctx.read(rctx.attr.annotation))), |
| 478 | + data_exclude = rctx.attr.pip_data_exclude, |
| 479 | + group_deps = rctx.attr.group_deps, |
| 480 | + group_name = rctx.attr.group_name, |
| 481 | + tags = [ |
| 482 | + "pypi_name={}".format(metadata["name"]), |
| 483 | + "pypi_version={}".format(metadata["version"]), |
| 484 | + ], |
| 485 | + ) |
| 486 | + |
416 | 487 | rctx.file("BUILD.bazel", build_file_contents)
|
417 | 488 |
|
418 | 489 | return
|
|
0 commit comments