@@ -165,11 +165,29 @@ def add_target_env(env, build_platform, target_triple, build_env):
165165 # non-system (e.g. Homebrew) executables from being used.
166166 env ["PATH" ] = "/usr/bin:/bin"
167167
168- sdk_path = os .environ ["APPLE_SDK_PATH" ]
168+ if "APPLE_SDK_PATH" in os .environ :
169+ sdk_path = os .environ ["APPLE_SDK_PATH" ]
170+ else :
171+ # macOS SDK has historically been in /usr courtesy of an
172+ # installer provided by Xcode. But with Catalina, the files
173+ # are now typically in
174+ # /Applications/Xcode.app/Contents/Developer/Platforms/.
175+ # The proper way to resolve this path is with xcrun, which
176+ # will give us the headers that Xcode is configured to use.
177+ res = subprocess .run (
178+ ["xcrun" , "--sdk" , sdk_platform , "--show-sdk-path" ],
179+ check = True ,
180+ capture_output = True ,
181+ encoding = "utf-8" ,
182+ )
183+
184+ sdk_path = res .stdout .strip ()
169185
170186 if not os .path .exists (sdk_path ):
171187 raise Exception ("macOS SDK path %s does not exist" % sdk_path )
172188
189+ env ["APPLE_SDK_PATH" ] = sdk_path
190+
173191 # Grab the version from the SDK so we can put it in PYTHON.json.
174192 sdk_settings_path = pathlib .Path (sdk_path ) / "SDKSettings.json"
175193 with sdk_settings_path .open ("rb" ) as fh :
@@ -801,11 +819,6 @@ def build_cpython(
801819 if "lto" in parsed_build_options :
802820 env ["CPYTHON_LTO" ] = "1"
803821
804- sdk_path = os .environ ["APPLE_SDK_PATH" ]
805- if not os .path .exists (sdk_path ):
806- raise Exception ("macOS SDK path %s does not exist" % sdk_path )
807- env ["APPLE_SDK_PATH" ] = sdk_path
808-
809822 add_target_env (env , host_platform , target_triple , build_env )
810823
811824 build_env .run ("build-cpython.sh" , environment = env )
0 commit comments