Skip to content

Commit 70111b5

Browse files
committed
Correctly resolve macOS SDK paths
XCode has facilities for accurately telling us where SDKs are installed. This is important to use, particularly when there may be multiple SDKs or versions of XCode installed.
1 parent b5ed3e4 commit 70111b5

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

python/private/pypi/whl_library.bzl

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,31 @@ def _get_xcode_location_cflags(rctx):
5555
# This is a full xcode installation somewhere like /Applications/Xcode13.0.app/Contents/Developer
5656
# so we need to change the path to to the macos specific tools which are in a different relative
5757
# path than xcode installed command line tools.
58-
xcode_root = "{}/Platforms/MacOSX.platform/Developer".format(xcode_root)
58+
xcode_sdks_json = rctx.execute([
59+
"xcrun",
60+
"xcodebuild",
61+
"-showsdks",
62+
"-json",
63+
], environment = {
64+
"DEVELOPER_DIR": xcode_root,
65+
}).stdout
66+
xcode_sdks = json.decode(xcode_sdks_json)
67+
potential_sdks = [
68+
sdk
69+
for sdk in xcode_sdks
70+
if "productName" in sdk and
71+
sdk["productName"] == "macOS" and
72+
"darwinos" not in sdk["canonicalName"]
73+
]
74+
75+
# Now we'll get two entries here (one for internal and another one for public)
76+
# It shouldn't matter which one we pick.
77+
xcode_sdk_path = potential_sdks[0]["sdkPath"]
78+
else:
79+
xcode_sdk_path = "{}/SDKs/MacOSX.sdk".format(xcode_root)
80+
5981
return [
60-
"-isysroot {}/SDKs/MacOSX.sdk".format(xcode_root),
82+
"-isysroot {}".format(xcode_sdk_path),
6183
]
6284

6385
def _get_toolchain_unix_cflags(rctx, python_interpreter, logger = None):
@@ -158,19 +180,22 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
158180
Dictionary of environment variable suitable to pass to rctx.execute.
159181
"""
160182

161-
# Gather any available CPPFLAGS values
162-
cppflags = []
163-
cppflags.extend(_get_xcode_location_cflags(rctx))
164-
cppflags.extend(_get_toolchain_unix_cflags(rctx, python_interpreter, logger = logger))
165-
166183
env = {
167184
"PYTHONPATH": pypi_repo_utils.construct_pythonpath(
168185
rctx,
169186
entries = rctx.attr._python_path_entries,
170187
),
171-
_CPPFLAGS: " ".join(cppflags),
172188
}
173189

190+
# Gather any available CPPFLAGS values
191+
#
192+
# We may want to build in an environment without a cc toolchain.
193+
# In those cases, we're limited to --donwload-only, but we should respect that here.
194+
if not rctx.attr.download_only:
195+
cppflags = []
196+
cppflags.extend(_get_xcode_location_cflags(rctx))
197+
cppflags.extend(_get_toolchain_unix_cflags(rctx, python_interpreter))
198+
env[_CPPFLAGS] = " ".join(cppflags)
174199
return env
175200

176201
def _whl_library_impl(rctx):

0 commit comments

Comments
 (0)