Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32import argparse3import plistlib4import pathlib5import tarfile6import os7import contextlib9@contextlib.contextmanager10def cd(path):11"""Context manager that restores PWD even if an exception was raised."""12old_pwd = os.getcwd()13os.chdir(str(path))14try:15yield16finally:17os.chdir(old_pwd)19def run():20parser = argparse.ArgumentParser(21description=__doc__, formatter_class=argparse.RawTextHelpFormatter)23parser.add_argument('xcode_app', metavar='XCODEAPP', type=pathlib.Path)24parser.add_argument("-o", metavar='OUTSDKTAR', dest='out_sdkt', type=pathlib.Path, required=False)26args = parser.parse_args()28xcode_app = args.xcode_app.resolve()29assert xcode_app.is_dir(), "The supplied Xcode.app path '{}' either does not exist or is not a directory".format(xcode_app)31xcode_app_plist = xcode_app.joinpath("Contents/version.plist")32with xcode_app_plist.open('rb') as fp:33pl = plistlib.load(fp)34xcode_version = pl['CFBundleShortVersionString']35xcode_build_id = pl['ProductBuildVersion']36print("Found Xcode (version: {xcode_version}, build id: {xcode_build_id})".format(xcode_version=xcode_version, xcode_build_id=xcode_build_id))38sdk_dir = xcode_app.joinpath("Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk")39sdk_plist = sdk_dir.joinpath("System/Library/CoreServices/SystemVersion.plist")40with sdk_plist.open('rb') as fp:41pl = plistlib.load(fp)42sdk_version = pl['ProductVersion']43sdk_build_id = pl['ProductBuildVersion']44print("Found MacOSX SDK (version: {sdk_version}, build id: {sdk_build_id})".format(sdk_version=sdk_version, sdk_build_id=sdk_build_id))46out_name = "Xcode-{xcode_version}-{xcode_build_id}-extracted-SDK-with-libcxx-headers".format(xcode_version=xcode_version, xcode_build_id=xcode_build_id)48out_sdkt_path = args.out_sdkt or pathlib.Path("./{}.tar".format(out_name))50def tarfp_add_with_base_change(tarfp, dir_to_add, alt_base_dir):51"""Add all files in dir_to_add to tarfp, but prepend alt_base_dir to the files'52names54e.g. if the only file under /root/bazdir is /root/bazdir/qux, invoking:56tarfp_add_with_base_change(tarfp, "foo/bar", "/root/bazdir")58would result in the following members being added to tarfp:60foo/bar/ -> corresponding to /root/bazdir61foo/bar/qux -> corresponding to /root/bazdir/qux63"""64def change_tarinfo_base(tarinfo):65if tarinfo.name and tarinfo.name.endswith((".swiftmodule", ".modulemap")):66return None67if tarinfo.name and tarinfo.name.startswith("./"):68tarinfo.name = str(pathlib.Path(alt_base_dir, tarinfo.name))69if tarinfo.linkname and tarinfo.linkname.startswith("./"):70tarinfo.linkname = str(pathlib.Path(alt_base_dir, tarinfo.linkname))71# make metadata deterministic72tarinfo.mtime = 073tarinfo.uid, tarinfo.uname = 0, ''74tarinfo.gid, tarinfo.gname = 0, ''75# don't use isdir() as there are also executable files present76tarinfo.mode = 0o0755 if tarinfo.mode & 0o0100 else 0o064477return tarinfo78with cd(dir_to_add):79# recursion already adds entries in sorted order80tarfp.add("./usr/include", recursive=True, filter=change_tarinfo_base)81tarfp.add("./usr/lib", recursive=True, filter=change_tarinfo_base)82tarfp.add("./System/Library/Frameworks", recursive=True, filter=change_tarinfo_base)84print("Creating output .tar file...")85with out_sdkt_path.open("wb") as fp:86with tarfile.open(mode="w", fileobj=fp, format=tarfile.PAX_FORMAT) as tarfp:87print("Adding MacOSX SDK {} files...".format(sdk_version))88tarfp_add_with_base_change(tarfp, sdk_dir, out_name)89print("Done! Find the resulting tarball at:")90print(out_sdkt_path.resolve())92if __name__ == '__main__':93run()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.