Debian Codegraph @

third_party/upb/bazel/system_python.bzl

raw
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
26 """Repository rule for using Python 3.x headers from the system."""
27
28 _build_file = """
29 load("@bazel_tools//tools/python:toolchain.bzl", "py_runtime_pair")
30
31 cc_library(
32 name = "python_headers",
33 hdrs = glob(["python/**/*.h"]),
34 includes = ["python"],
35 visibility = ["//visibility:public"],
36 )
37
38 py_runtime(
39 name = "py3_runtime",
40 interpreter_path = "{}",
41 python_version = "PY3",
42 )
43
44 py_runtime_pair(
45 name = "runtime_pair",
46 py3_runtime = ":py3_runtime",
47 )
48
49 toolchain(
50 name = "python_toolchain",
51 toolchain = ":runtime_pair",
52 toolchain_type = "@rules_python//python:toolchain_type",
53 )
54 """
55
56 def _get_python_version(repository_ctx):
57 py_program = "import sys; print(str(sys.version_info.major) + str(sys.version_info.minor))"
58 result = repository_ctx.execute(["python3", "-c", py_program])
59 return (result.stdout).strip()
60
61 def _get_config_var(repository_ctx, name):
62 py_program = "import sysconfig; print(sysconfig.get_config_var('%s'), end='')"
63 result = repository_ctx.execute(["python3", "-c", py_program % (name)])
64 if result.return_code != 0:
65 return None
66 return result.stdout
67
68 def _python_headers_impl(repository_ctx):
69 path = _get_config_var(repository_ctx, "INCLUDEPY")
70 if not path:
71 # buildifier: disable=print
72 print("WARNING: no system python available, builds against system python will fail")
73 repository_ctx.file("BUILD.bazel", "")
74 repository_ctx.file("version.bzl", "SYSTEM_PYTHON_VERSION = None")
75 return
76 repository_ctx.symlink(path, "python")
77 python3 = repository_ctx.which("python3")
78 python_version = _get_python_version(repository_ctx)
79 repository_ctx.file("BUILD.bazel", _build_file.format(python3))
80 repository_ctx.file("version.bzl", "SYSTEM_PYTHON_VERSION = '{}'".format(python_version))
81
82 # The system_python() repository rule exposes Python headers from the system.
83 #
84 # In WORKSPACE:
85 # system_python(
86 # name = "system_python_repo",
87 # )
88 #
89 # This repository exposes a single rule that you can depend on from BUILD:
90 # cc_library(
91 # name = "foobar",
92 # srcs = ["foobar.cc"],
93 # deps = ["@system_python_repo//:python_headers"],
94 # )
95 #
96 # The headers should correspond to the version of python obtained by running
97 # the `python3` command on the system.
98 system_python = repository_rule(
99 implementation = _python_headers_impl,
100 local = True,
101 )