14
14
15
15
""
16
16
17
- load ("//python:repositories.bzl" , "get_interpreter_dirname" , " is_standalone_interpreter" )
17
+ load ("//python:repositories.bzl" , "is_standalone_interpreter" )
18
18
load ("//python:versions.bzl" , "WINDOWS_NAME" )
19
19
load ("//python/pip_install:repositories.bzl" , "all_requirements" )
20
20
load ("//python/pip_install:requirements_parser.bzl" , parse_requirements = "parse" )
@@ -43,15 +43,11 @@ def _construct_pypath(rctx):
43
43
Returns: String of the PYTHONPATH.
44
44
"""
45
45
46
- # Get the root directory of these rules
47
- rules_root = rctx .path (Label ("//:BUILD.bazel" )).dirname
48
- thirdparty_roots = [
49
- # Includes all the external dependencies from repositories.bzl
50
- rctx .path (Label ("@" + repo + "//:BUILD.bazel" )).dirname
51
- for repo in all_requirements
52
- ]
53
46
separator = ":" if not "windows" in rctx .os .name .lower () else ";"
54
- pypath = separator .join ([str (p ) for p in [rules_root ] + thirdparty_roots ])
47
+ pypath = separator .join ([
48
+ str (rctx .path (entry ).dirname )
49
+ for entry in rctx .attr ._python_path_entries
50
+ ])
55
51
return pypath
56
52
57
53
def _get_python_interpreter_attr (rctx ):
@@ -123,7 +119,7 @@ def _get_xcode_location_cflags(rctx):
123
119
"-isysroot {}/SDKs/MacOSX.sdk" .format (xcode_root ),
124
120
]
125
121
126
- def _get_toolchain_unix_cflags (rctx ):
122
+ def _get_toolchain_unix_cflags (rctx , python_interpreter ):
127
123
"""Gather cflags from a standalone toolchain for unix systems.
128
124
129
125
Pip won't be able to compile c extensions from sdists with the pre built python distributions from indygreg
@@ -135,19 +131,19 @@ def _get_toolchain_unix_cflags(rctx):
135
131
return []
136
132
137
133
# Only update the location when using a standalone toolchain.
138
- if not is_standalone_interpreter (rctx , rctx . attr . python_interpreter_target ):
134
+ if not is_standalone_interpreter (rctx , python_interpreter ):
139
135
return []
140
136
141
137
er = rctx .execute ([
142
- rctx . path ( rctx . attr . python_interpreter_target ). realpath ,
138
+ python_interpreter ,
143
139
"-c" ,
144
140
"import sys; print(f'{sys.version_info[0]}.{sys.version_info[1]}', end='')" ,
145
141
])
146
142
if er .return_code != 0 :
147
143
fail ("could not get python version from interpreter (status {}): {}" .format (er .return_code , er .stderr ))
148
144
_python_version = er .stdout
149
145
include_path = "{}/include/python{}" .format (
150
- get_interpreter_dirname ( rctx , rctx . attr . python_interpreter_target ) ,
146
+ python_interpreter . dirname ,
151
147
_python_version ,
152
148
)
153
149
@@ -218,19 +214,20 @@ def _parse_optional_attrs(rctx, args):
218
214
219
215
return args
220
216
221
- def _create_repository_execution_environment (rctx ):
217
+ def _create_repository_execution_environment (rctx , python_interpreter ):
222
218
"""Create a environment dictionary for processes we spawn with rctx.execute.
223
219
224
220
Args:
225
- rctx: The repository context.
221
+ rctx (repository_ctx): The repository context.
222
+ python_interpreter (path): The resolved python interpreter.
226
223
Returns:
227
224
Dictionary of environment variable suitable to pass to rctx.execute.
228
225
"""
229
226
230
227
# Gather any available CPPFLAGS values
231
228
cppflags = []
232
229
cppflags .extend (_get_xcode_location_cflags (rctx ))
233
- cppflags .extend (_get_toolchain_unix_cflags (rctx ))
230
+ cppflags .extend (_get_toolchain_unix_cflags (rctx , python_interpreter ))
234
231
235
232
env = {
236
233
"PYTHONPATH" : _construct_pypath (rctx ),
@@ -630,7 +627,7 @@ def _whl_library_impl(rctx):
630
627
result = rctx .execute (
631
628
args ,
632
629
# Manually construct the PYTHONPATH since we cannot use the toolchain here
633
- environment = _create_repository_execution_environment (rctx ),
630
+ environment = _create_repository_execution_environment (rctx , python_interpreter ),
634
631
quiet = rctx .attr .quiet ,
635
632
timeout = rctx .attr .timeout ,
636
633
)
@@ -720,6 +717,19 @@ whl_library_attrs = {
720
717
mandatory = True ,
721
718
doc = "Python requirement string describing the package to make available" ,
722
719
),
720
+ "_python_path_entries" : attr .label_list (
721
+ # Get the root directory of these rules and keep them as a default attribute
722
+ # in order to avoid unnecessary repository fetching restarts.
723
+ #
724
+ # This is very similar to what was done in https://github.com/bazelbuild/rules_go/pull/3478
725
+ default = [
726
+ Label ("//:BUILD.bazel" ),
727
+ ] + [
728
+ # Includes all the external dependencies from repositories.bzl
729
+ Label ("@" + repo + "//:BUILD.bazel" )
730
+ for repo in all_requirements
731
+ ],
732
+ ),
723
733
}
724
734
725
735
whl_library_attrs .update (** common_attrs )
0 commit comments