Skip to content

Commit

Permalink
Fix fetching SDKROOT with command line tools instead of Xcode
Browse files Browse the repository at this point in the history
Also suppresses superfluous xcrun warnings and improves comments.
Fixes hedronvision/bazel-compile-commands-extractor#68
  • Loading branch information
tiponada4l authored and cpsauer committed Jul 11, 2022
1 parent 8916828 commit 429bd26
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions refresh.template.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,19 @@ def _get_files(compile_action):
@functools.lru_cache(maxsize=None)
def _get_apple_SDKROOT(SDK_name: str):
"""Get path to xcode-select'd root for the given OS."""
# We're manually building the path because something like `xcodebuild -sdk iphoneos` requires different capitalization and more parsing, and this is a hack anyway until Bazel fixes https://github.com/bazelbuild/bazel/issues/12852
return f'{_get_apple_DEVELOPER_DIR()}/Platforms/{SDK_name}.platform/Developer/SDKs/{SDK_name}.sdk'
# Unless xcode-select has been invoked (like for a beta) we'd expect '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk'
# Traditionally stored in SDKROOT environment variable, but not provided.
SDKROOT_maybe_versioned = subprocess.check_output(
('xcrun', '--show-sdk-path', '-sdk', SDK_name.lower()),
stderr = subprocess.DEVNULL,
encoding=locale.getpreferredencoding()
).rstrip()
# Unless xcode-select has been invoked (like for a beta) we'd expect, e.g., '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS<version>.sdk' or '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
version = subprocess.check_output(
('xcrun', '--show-sdk-version', '-sdk', SDK_name.lower()),
stderr = subprocess.DEVNULL,
encoding=locale.getpreferredencoding()
).rstrip()
return SDKROOT_maybe_versioned.replace(version, '') # Strip version and use unversioned SDK symlink so the compile commands are still valid after an SDK update.
# Traditionally stored in SDKROOT environment variable, but not provided by Bazel. See https://github.com/bazelbuild/bazel/issues/12852


def _get_apple_platform(compile_args: typing.List[str]):
Expand All @@ -532,15 +541,19 @@ def _get_apple_platform(compile_args: typing.List[str]):
def _get_apple_DEVELOPER_DIR():
"""Get path to xcode-select'd developer directory."""
return subprocess.check_output(('xcode-select', '--print-path'), encoding=locale.getpreferredencoding()).rstrip()
# Unless xcode-select has been invoked (like for a beta) we'd expect '/Applications/Xcode.app/Contents/Developer' from xcode-select -p
# Traditionally stored in DEVELOPER_DIR environment variable, but not provided.
# Unless xcode-select has been invoked (like for a beta) we'd expect, e.g., '/Applications/Xcode.app/Contents/Developer' or '/Library/Developer/CommandLineTools'
# Traditionally stored in DEVELOPER_DIR environment variable, but not provided by Bazel. See https://github.com/bazelbuild/bazel/issues/12852


@functools.lru_cache(maxsize=None)
def _get_apple_active_clang():
"""Get path to xcode-select'd clang version."""
return subprocess.check_output(('xcrun', '--find', 'clang'), encoding=locale.getpreferredencoding()).rstrip()
# Unless xcode-select has been invoked (like for a beta) we'd expect '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang' from xcrun -f clang
return subprocess.check_output(
('xcrun', '--find', 'clang'),
stderr = subprocess.DEVNULL, # Suppress superfluous error messages like "Requested but did not find extension point with identifier..."
encoding=locale.getpreferredencoding()
).rstrip()
# Unless xcode-select has been invoked (like for a beta) we'd expect, e.g., '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang' or whatever the equivalent is for command line tools.


def _apple_platform_patch(compile_args: typing.List[str]):
Expand Down

0 comments on commit 429bd26

Please sign in to comment.