Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4823621

Browse files
committed
Improve our heuristic for selecting PG_SYSROOT on macOS.
In cases where Xcode is newer than the underlying macOS version, asking xcodebuild for the SDK path will produce a pointer to the SDK shipped with Xcode, which may end up building code that does not work on the underlying macOS version. It appears that in such cases, xcodebuild's answer also fails to match the default behavior of Apple's compiler: assuming one has installed Xcode's "command line tools", there will be an SDK for the OS's own version in /Library/Developer/CommandLineTools, and the compiler will default to using that. This is all pretty poorly documented, but experimentation suggests that "xcrun --show-sdk-path" gives the sysroot path that the compiler is actually using, at least in some cases. Hence, try that first, but revert to xcodebuild if xcrun fails (in very old Xcode, it is missing or lacks the --show-sdk-path switch). Also, "xcrun --show-sdk-path" may give a path that is valid but lacks any OS version identifier. We don't really want that, since most of the motivation for wiring -isysroot into the build flags at all is to ensure that all parts of a PG installation are built against the same SDK, even when considering extensions built later and/or on a different machine. Insist on finding "N.N" in the directory name before accepting the result. (Adding "--sdk macosx" to the xcrun call seems to produce the same answer as xcodebuild, but usually more quickly because it's cached, so we also try that as a fallback.) The core reason why we don't want to use Xcode's default SDK in cases like this is that Apple's technology for introducing new syscalls does not play nice with Autoconf: for example, configure will think that preadv/pwritev exist when using a Big Sur SDK, even when building on an older macOS version where they don't exist. It'd be nice to have a better solution to that problem, but this patch doesn't attempt to fix that. Per report from Sergey Shinderuk. Back-patch to all supported versions. Discussion: https://postgr.es/m/ed3b8e5d-0da8-6ebd-fd1c-e0ac80a4b204@postgrespro.ru
1 parent f9900df commit 4823621

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/template/darwin

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,27 @@
33
# Note: Darwin is the original code name for macOS, also known as OS X.
44
# We still use "darwin" as the port name, partly because config.guess does.
55

6-
# Select where system include files should be sought.
6+
# Select where system include files should be sought, if user didn't say.
77
if test x"$PG_SYSROOT" = x"" ; then
8-
PG_SYSROOT=`xcodebuild -version -sdk macosx Path 2>/dev/null`
8+
# This is far more complicated than it ought to be. We first ask
9+
# "xcrun --show-sdk-path", which seems to match the default -isysroot
10+
# setting of Apple's compilers. However, that may produce no result or
11+
# a result that is not version-specific (i.e., just ".../SDKs/MacOSX.sdk").
12+
# Using a version-specific sysroot seems desirable, so if there are not
13+
# digits in the directory name, try "xcrun --sdk macosx --show-sdk-path";
14+
# and if that still doesn't work, fall back to asking xcodebuild,
15+
# which is often a good deal slower.
16+
PG_SYSROOT=`xcrun --show-sdk-path 2>/dev/null`
17+
if expr x"$PG_SYSROOT" : '.*[0-9]\.[0-9][^/]*$' >/dev/null ; then : okay
18+
else
19+
PG_SYSROOT=`xcrun --sdk macosx --show-sdk-path 2>/dev/null`
20+
if expr x"$PG_SYSROOT" : '.*[0-9]\.[0-9][^/]*$' >/dev/null ; then : okay
21+
else
22+
PG_SYSROOT=`xcodebuild -version -sdk macosx Path 2>/dev/null`
23+
fi
24+
fi
925
fi
10-
# Old xcodebuild versions may produce garbage, so validate the result.
26+
# Validate the result: if it doesn't point at a directory, ignore it.
1127
if test x"$PG_SYSROOT" != x"" ; then
1228
if test -d "$PG_SYSROOT" ; then
1329
CPPFLAGS="-isysroot $PG_SYSROOT $CPPFLAGS"

0 commit comments

Comments
 (0)