diff --git a/cpython-unix/build-cpython.sh b/cpython-unix/build-cpython.sh index 7c969d30..23ed5dca 100755 --- a/cpython-unix/build-cpython.sh +++ b/cpython-unix/build-cpython.sh @@ -587,6 +587,7 @@ fi # that a) it works on as many machines as possible b) doesn't leak details # about the build environment, which is non-portable. cat > ${ROOT}/hack_sysconfig.py << EOF +import json import os import sys import sysconfig @@ -628,6 +629,41 @@ def replace_in_all(search, replace): replace_in_file(SYSCONFIGDATA, search, replace) +def format_sysconfigdata(): + """Reformat the sysconfigdata file to avoid implicit string concatenations. + + In some Python versions, the sysconfigdata file contains implicit string + concatenations that extend over multiple lines, which make string replacement + much harder. This function reformats the file to avoid this issue. + + See: https://github.com/python/cpython/blob/a03efb533a58fd13fb0cc7f4a5c02c8406a407bd/Mac/BuildScript/build-installer.py#L1360C1-L1385C15. + """ + with open(SYSCONFIGDATA, "rb") as fh: + data = fh.read() + + globals_dict = {} + locals_dict = {} + exec(data, globals_dict, locals_dict) + build_time_vars = locals_dict['build_time_vars'] + + with open(SYSCONFIGDATA, "wb") as fh: + fh.write(b'# system configuration generated and used by the sysconfig module\n') + fh.write(('build_time_vars = %s' % json.dumps(build_time_vars, indent=4)).encode("utf-8")) + fh.close() + + +# Format sysconfig to ensure that string replacements take effect. +format_sysconfigdata() + +# Remove the Xcode path from the compiler flags. +# +# CPython itself will drop this from `sysconfig.get_config_var("CFLAGS")` and +# similar calls, but _not_ if `CFLAGS` is set in the environment (regardless of +# the `CFLAGS` value). It will almost always be wrong, so we drop it unconditionally. +xcode_path = os.getenv("APPLE_SDK_PATH") +if xcode_path: + replace_in_all("-isysroot %s" % xcode_path, "") + # -fdebug-default-version is Clang only. Strip so compiling works on GCC. replace_in_all("-fdebug-default-version=4", "") diff --git a/cpython-unix/build.py b/cpython-unix/build.py index 1c1963bb..9bfc93ef 100755 --- a/cpython-unix/build.py +++ b/cpython-unix/build.py @@ -186,6 +186,8 @@ def add_target_env(env, build_platform, target_triple, build_env): if not os.path.exists(sdk_path): raise Exception("macOS SDK path %s does not exist" % sdk_path) + env["APPLE_SDK_PATH"] = sdk_path + # Grab the version from the SDK so we can put it in PYTHON.json. sdk_settings_path = pathlib.Path(sdk_path) / "SDKSettings.json" with sdk_settings_path.open("rb") as fh: