Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly resolve macOS SDK paths #2478

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Unreleased changes template.
* Bazel 6 support is dropped and Bazel 7.4.1 is the minimum supported
version, per our Bazel support matrix. Earlier versions are not
tested by CI, so functionality cannot be guaranteed.
* Use `xcrun xcodebuild --showsdks` to find XCode root

{#v0-0-0-fixed}
### Fixed
Expand Down
46 changes: 38 additions & 8 deletions python/private/pypi/whl_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,35 @@ def _get_xcode_location_cflags(rctx):
# This is a full xcode installation somewhere like /Applications/Xcode13.0.app/Contents/Developer
# so we need to change the path to to the macos specific tools which are in a different relative
# path than xcode installed command line tools.
xcode_root = "{}/Platforms/MacOSX.platform/Developer".format(xcode_root)
xcode_sdks_json = repo_utils.execute_checked(
rctx,
op = "LocateXCodeSDKs",
arguments = [
repo_utils.which_checked(rctx, "xcrun"),
"xcodebuild",
"-showsdks",
"-json",
],
environment = {
"DEVELOPER_DIR": xcode_root,
}).stdout
xcode_sdks = json.decode(xcode_sdks_json)
potential_sdks = [
sdk
for sdk in xcode_sdks
if "productName" in sdk and
sdk["productName"] == "macOS" and
"darwinos" not in sdk["canonicalName"]
]

# Now we'll get two entries here (one for internal and another one for public)
# It shouldn't matter which one we pick.
xcode_sdk_path = potential_sdks[0]["sdkPath"]
else:
xcode_sdk_path = "{}/SDKs/MacOSX.sdk".format(xcode_root)

return [
"-isysroot {}/SDKs/MacOSX.sdk".format(xcode_root),
"-isysroot {}".format(xcode_sdk_path),
]

def _get_toolchain_unix_cflags(rctx, python_interpreter, logger = None):
Expand Down Expand Up @@ -158,19 +184,23 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
Dictionary of environment variable suitable to pass to rctx.execute.
"""

# Gather any available CPPFLAGS values
cppflags = []
cppflags.extend(_get_xcode_location_cflags(rctx))
cppflags.extend(_get_toolchain_unix_cflags(rctx, python_interpreter, logger = logger))

env = {
"PYTHONPATH": pypi_repo_utils.construct_pythonpath(
rctx,
entries = rctx.attr._python_path_entries,
),
_CPPFLAGS: " ".join(cppflags),
}

# Gather any available CPPFLAGS values
#
# We may want to build in an environment without a cc toolchain.
# In those cases, we're limited to --download-only, but we should respect that here.
is_wheel = rctx.attr.filename and rctx.attr.filename.endswith(".whl")
if not (rctx.attr.download_only or is_wheel):
cppflags = []
cppflags.extend(_get_xcode_location_cflags(rctx))
cppflags.extend(_get_toolchain_unix_cflags(rctx, python_interpreter))
env[_CPPFLAGS] = " ".join(cppflags)
return env

def _whl_library_impl(rctx):
Expand Down