forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_runtime_repo_setup.bzl
More file actions
209 lines (191 loc) · 7.33 KB
/
local_runtime_repo_setup.bzl
File metadata and controls
209 lines (191 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Setup code called by the code generated by `local_runtime_repo`."""
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_cc//cc:cc_import.bzl", "cc_import")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//python:py_runtime.bzl", "py_runtime")
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
load("@rules_python//python/cc:py_cc_toolchain.bzl", "py_cc_toolchain")
load("@rules_python//python/private:py_exec_tools_toolchain.bzl", "py_exec_tools_toolchain")
_PYTHON_VERSION_FLAG = Label("@rules_python//python/config_settings:python_version")
def define_local_runtime_toolchain_impl(
name,
major,
minor,
micro,
abi_flags,
os,
implementation_name,
interpreter_path,
interface_library,
libraries,
defines,
abi3_interface_library,
abi3_libraries,
additional_dlls):
"""Defines a toolchain implementation for a local Python runtime.
Generates public targets:
* `python_runtimes`: The target toolchain type implementation
* `py_exec_tools_toolchain`: The exec tools toolchain type implementation
* `py_cc_toolchain`: The py cc toolchain type implementation
* `os`: A constraint (or alias to one) for the `target_compatible_with` this
toolchain is compatible with.
* `is_matching_python_version`: A `config_setting` for `target_settings`
this toolchain is compatible with.
Args:
name: `str` Only present to satisfy tooling
major: `str` The major Python version, e.g. `3` of `3.9.1`.
minor: `str` The minor Python version, e.g. `9` of `3.9.1`.
micro: `str` The micro Python version, e.g. "1" of `3.9.1`.
abi_flags: `str` The abi flags, as returned by `sys.abiflags`.
os: `str` A label to the OS constraint (e.g. `@platforms//os:linux`) for
this runtime.
implementation_name: `str` The implementation name, as returned by
`sys.implementation.name`.
interpreter_path: `str` Absolute path to the interpreter.
interface_library: `str` Path to the interface library.
e.g. "lib/python312.lib"
libraries: `list[str]` Path[s] to the python libraries.
e.g. ["lib/python312.dll"] or ["lib/python312.so"]
defines: `list[str]` List of additional defines.
abi3_interface_library: `str` Path to the interface library.
e.g. "lib/python3.lib"
abi3_libraries: `list[str]` Path[s] to the python libraries.
e.g. ["lib/python3.dll"] or ["lib/python3.so"]
additional_dlls: `list[str]` Path[s] to additional DLLs.
e.g. ["lib/msvcrt123.dll"]
"""
major_minor = "{}.{}".format(major, minor)
major_minor_micro = "{}.{}".format(major_minor, micro)
# To build Python C/C++ extension on Windows, we need to link to python import library pythonXY.lib
# See https://docs.python.org/3/extending/windows.html
# However not all python installations (such as manylinux) include shared or static libraries,
# so only create the import library when interface_library is set.
if interface_library:
cc_import(
name = "interface",
interface_library = interface_library,
system_provided = True,
)
if abi3_interface_library:
cc_import(
name = "abi3_interface",
interface_library = abi3_interface_library,
system_provided = True,
)
native.filegroup(
name = "includes",
srcs = native.glob(
include = ["include/**/*.h"],
exclude = ["include/numpy/**"], # numpy headers are handled separately
allow_empty = True, # A Python install may not have C headers
),
)
# header libraries.
cc_library(
name = "python_headers_abi3",
hdrs = [":includes"],
includes = ["include"],
defines = defines, # NOTE: Users should define Py_LIMITED_API=3
deps = select({
"@bazel_tools//src/conditions:windows": [":abi3_interface"],
"//conditions:default": [],
}),
)
cc_library(
name = "python_headers",
hdrs = [":includes"],
includes = ["include"],
defines = defines,
deps = select({
"@bazel_tools//src/conditions:windows": [":interface"],
"//conditions:default": [],
}),
)
# python libraries
cc_library(
name = "libpython_abi3",
hdrs = [":includes"],
defines = defines, # NOTE: Users should define Py_LIMITED_API=3
srcs = abi3_libraries + additional_dlls,
)
cc_library(
name = "libpython",
hdrs = [":includes"],
defines = defines,
srcs = libraries + additional_dlls,
)
# runtime configuration
py_runtime(
name = "py3_runtime",
interpreter_path = interpreter_path,
python_version = "PY3",
interpreter_version_info = {
"major": major,
"micro": micro,
"minor": minor,
},
implementation_name = implementation_name,
abi_flags = abi_flags,
pyc_tag = "{}-{}{}{}".format(implementation_name, major, minor, abi_flags),
)
py_runtime_pair(
name = "python_runtimes",
py2_runtime = None,
py3_runtime = ":py3_runtime",
visibility = ["//visibility:public"],
)
py_exec_tools_toolchain(
name = "py_exec_tools_toolchain",
visibility = ["//visibility:public"],
precompiler = "@rules_python//tools/precompiler:precompiler",
)
py_cc_toolchain(
name = "py_cc_toolchain",
headers = ":python_headers",
headers_abi3 = ":python_headers_abi3",
libs = ":libpython",
python_version = major_minor_micro,
visibility = ["//visibility:public"],
)
native.alias(
name = "os",
# Call Label() to force the string to evaluate in the context of
# rules_python, not the calling BUILD-file code. This is because
# the value is an `@platforms//foo` string, which @rules_python has
# visibility to, but the calling repo may not.
actual = Label(os),
visibility = ["//visibility:public"],
)
native.config_setting(
name = "_is_major_minor",
flag_values = {
_PYTHON_VERSION_FLAG: major_minor,
},
)
native.config_setting(
name = "_is_major_minor_micro",
flag_values = {
_PYTHON_VERSION_FLAG: major_minor_micro,
},
)
selects.config_setting_group(
name = "is_matching_python_version",
match_any = [
":_is_major_minor",
":_is_major_minor_micro",
],
visibility = ["//visibility:public"],
)