@@ -30,7 +30,7 @@ _CPPFLAGS = "CPPFLAGS"
30
30
_COMMAND_LINE_TOOLS_PATH_SLUG = "commandlinetools"
31
31
_WHEEL_ENTRY_POINT_PREFIX = "rules_python_wheel_entry_point"
32
32
33
- def _get_xcode_location_cflags (rctx ):
33
+ def _get_xcode_location_cflags (rctx , logger = None ):
34
34
"""Query the xcode sdk location to update cflags
35
35
36
36
Figure out if this interpreter target comes from rules_python, and patch the xcode sdk location if so.
@@ -46,6 +46,7 @@ def _get_xcode_location_cflags(rctx):
46
46
rctx ,
47
47
op = "GetXcodeLocation" ,
48
48
arguments = [repo_utils .which_checked (rctx , "xcode-select" ), "--print-path" ],
49
+ logger = logger ,
49
50
)
50
51
if xcode_sdk_location .return_code != 0 :
51
52
return []
@@ -55,9 +56,37 @@ def _get_xcode_location_cflags(rctx):
55
56
# This is a full xcode installation somewhere like /Applications/Xcode13.0.app/Contents/Developer
56
57
# so we need to change the path to to the macos specific tools which are in a different relative
57
58
# path than xcode installed command line tools.
58
- xcode_root = "{}/Platforms/MacOSX.platform/Developer" .format (xcode_root )
59
+ xcode_sdks_json = repo_utils .execute_checked (
60
+ rctx ,
61
+ op = "LocateXCodeSDKs" ,
62
+ arguments = [
63
+ repo_utils .which_checked (rctx , "xcrun" ),
64
+ "xcodebuild" ,
65
+ "-showsdks" ,
66
+ "-json" ,
67
+ ],
68
+ environment = {
69
+ "DEVELOPER_DIR" : xcode_root ,
70
+ },
71
+ logger = logger ,
72
+ ).stdout
73
+ xcode_sdks = json .decode (xcode_sdks_json )
74
+ potential_sdks = [
75
+ sdk
76
+ for sdk in xcode_sdks
77
+ if "productName" in sdk and
78
+ sdk ["productName" ] == "macOS" and
79
+ "darwinos" not in sdk ["canonicalName" ]
80
+ ]
81
+
82
+ # Now we'll get two entries here (one for internal and another one for public)
83
+ # It shouldn't matter which one we pick.
84
+ xcode_sdk_path = potential_sdks [0 ]["sdkPath" ]
85
+ else :
86
+ xcode_sdk_path = "{}/SDKs/MacOSX.sdk" .format (xcode_root )
87
+
59
88
return [
60
- "-isysroot {}/SDKs/MacOSX.sdk " .format (xcode_root ),
89
+ "-isysroot {}" .format (xcode_sdk_path ),
61
90
]
62
91
63
92
def _get_toolchain_unix_cflags (rctx , python_interpreter , logger = None ):
@@ -84,6 +113,7 @@ def _get_toolchain_unix_cflags(rctx, python_interpreter, logger = None):
84
113
"import sys; print(f'{sys.version_info[0]}.{sys.version_info[1]}', end='')" ,
85
114
],
86
115
srcs = [],
116
+ logger = logger ,
87
117
)
88
118
_python_version = stdout
89
119
include_path = "{}/include/python{}" .format (
@@ -176,19 +206,23 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
176
206
Dictionary of environment variable suitable to pass to rctx.execute.
177
207
"""
178
208
179
- # Gather any available CPPFLAGS values
180
- cppflags = []
181
- cppflags .extend (_get_xcode_location_cflags (rctx ))
182
- cppflags .extend (_get_toolchain_unix_cflags (rctx , python_interpreter , logger = logger ))
183
-
184
209
env = {
185
210
"PYTHONPATH" : pypi_repo_utils .construct_pythonpath (
186
211
rctx ,
187
212
entries = rctx .attr ._python_path_entries ,
188
213
),
189
- _CPPFLAGS : " " .join (cppflags ),
190
214
}
191
215
216
+ # Gather any available CPPFLAGS values
217
+ #
218
+ # We may want to build in an environment without a cc toolchain.
219
+ # In those cases, we're limited to --download-only, but we should respect that here.
220
+ is_wheel = rctx .attr .filename and rctx .attr .filename .endswith (".whl" )
221
+ if not (rctx .attr .download_only or is_wheel ):
222
+ cppflags = []
223
+ cppflags .extend (_get_xcode_location_cflags (rctx , logger = logger ))
224
+ cppflags .extend (_get_toolchain_unix_cflags (rctx , python_interpreter , logger = logger ))
225
+ env [_CPPFLAGS ] = " " .join (cppflags )
192
226
return env
193
227
194
228
def _whl_library_impl (rctx ):
0 commit comments