workspace_defs.bzl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (c) 2009-2021, Google LLC
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of Google LLC nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. """Repository rule for using Python 3.x headers from the system."""
  26. _build_file = """
  27. load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
  28. cc_library(
  29. name = "python_headers",
  30. hdrs = glob(["python/**/*.h"]),
  31. includes = ["python"],
  32. visibility = ["//visibility:public"],
  33. )
  34. py_runtime(
  35. name = "py3_runtime",
  36. interpreter_path = "%s",
  37. python_version = "PY3",
  38. )
  39. py_runtime_pair(
  40. name = "runtime_pair",
  41. py3_runtime = ":py3_runtime",
  42. )
  43. toolchain(
  44. name = "python_toolchain",
  45. toolchain = ":runtime_pair",
  46. toolchain_type = "@rules_python//python:toolchain_type",
  47. )
  48. """
  49. def _get_config_var(repository_ctx, name):
  50. py_program = "import sysconfig; print(sysconfig.get_config_var('%s'), end='')"
  51. result = repository_ctx.execute(["python3", "-c", py_program % (name)])
  52. if result.return_code != 0:
  53. fail("No python3 executable available on the system")
  54. return result.stdout
  55. def _python_headers_impl(repository_ctx):
  56. path = _get_config_var(repository_ctx, "INCLUDEPY")
  57. repository_ctx.symlink(path, "python")
  58. python3 = repository_ctx.which("python3")
  59. repository_ctx.file("BUILD.bazel", _build_file % python3)
  60. # The system_python() repository rule exposes Python headers from the system.
  61. #
  62. # In WORKSPACE:
  63. # system_python(
  64. # name = "system_python_repo",
  65. # )
  66. #
  67. # This repository exposes a single rule that you can depend on from BUILD:
  68. # cc_library(
  69. # name = "foobar",
  70. # srcs = ["foobar.cc"],
  71. # deps = ["@system_python_repo//:python_headers"],
  72. # )
  73. #
  74. # The headers should correspond to the version of python obtained by running
  75. # the `python3` command on the system.
  76. system_python = repository_rule(
  77. implementation = _python_headers_impl,
  78. local = True,
  79. )