|
6 | 6 | * `PYTHON_LIB_PATH`: Location of python libraries.
|
7 | 7 | """
|
8 | 8 |
|
| 9 | +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") |
| 10 | + |
9 | 11 | _BAZEL_SH = "BAZEL_SH"
|
10 | 12 | _PYTHON_BIN_PATH = "PYTHON_BIN_PATH"
|
11 | 13 | _PYTHON_CONFIG_BIN_PATH = "PYTHON_CONFIG_BIN_PATH"
|
@@ -441,3 +443,62 @@ Args:
|
441 | 443 | will configure for that Python version instead of the installed
|
442 | 444 | binary. This configuration takes precedence over python_version.
|
443 | 445 | """
|
| 446 | + |
| 447 | +def _parse_my_own_version_from_module_dot_bazel(ctx): |
| 448 | + lines = ctx.read(Label("//:MODULE.bazel")).split("\n") |
| 449 | + for line in lines: |
| 450 | + parts = line.split("\"") |
| 451 | + if parts[0] == " version = ": |
| 452 | + return parts[1] |
| 453 | + _fail("Failed to parse my own version from `MODULE.bazel`! " + |
| 454 | + "This should never happen!") |
| 455 | + |
| 456 | +def _extension_impl(ctx): |
| 457 | + toolchain = None |
| 458 | + for module in ctx.modules: |
| 459 | + if module.is_root: |
| 460 | + if not module.tags.toolchain: |
| 461 | + pass |
| 462 | + elif len(module.tags.toolchain) == 1: |
| 463 | + toolchain = module.tags.toolchain[0] |
| 464 | + else: |
| 465 | + _fail("The root module may not specify multiple `toolchain` " + |
| 466 | + "tags. Found %r `toolchain` tags specified." % ( |
| 467 | + len(module.tags.toolchain), |
| 468 | + )) |
| 469 | + elif module.tags.toolchain: |
| 470 | + _fail("A non-root module may not specify any `toolchain` tags. " + |
| 471 | + "Found %r `toolchain` tags specified by module %r." % ( |
| 472 | + len(module.tags.toolchain), |
| 473 | + module.name, |
| 474 | + )) |
| 475 | + if toolchain == None: |
| 476 | + python_configure( |
| 477 | + name = "local_config_python", |
| 478 | + ) |
| 479 | + else: |
| 480 | + python_configure( |
| 481 | + name = "local_config_python", |
| 482 | + python_version = toolchain.python_version, |
| 483 | + python_interpreter_target = toolchain.python_interpreter_target, |
| 484 | + ) |
| 485 | + |
| 486 | + version = _parse_my_own_version_from_module_dot_bazel(ctx) |
| 487 | + http_archive( |
| 488 | + name = "pybind11", |
| 489 | + build_file = "//:pybind11.BUILD", |
| 490 | + strip_prefix = "pybind11-%s" % version, |
| 491 | + urls = ["https://github.com/pybind/pybind11/archive/v%s.zip" % version], |
| 492 | + ) |
| 493 | + |
| 494 | +extension = module_extension( |
| 495 | + implementation = _extension_impl, |
| 496 | + tag_classes = { |
| 497 | + "toolchain": tag_class( |
| 498 | + attrs = { |
| 499 | + "python_version": attr.string(default = ""), |
| 500 | + "python_interpreter_target": attr.label(), |
| 501 | + }, |
| 502 | + ), |
| 503 | + }, |
| 504 | +) |
0 commit comments