1#!/usr/bin/env python3 2 3"""Helps manage sysroots.""" 4 5import argparse 6import os 7import subprocess 8import sys 9 10 11def make_fake_sysroot(out_dir): 12 def cmdout(cmd): 13 return subprocess.check_output(cmd).decode(sys.stdout.encoding).strip() 14 15 if sys.platform == 'win32': 16 p = os.getenv('ProgramFiles(x86)', 'C:\\Program Files (x86)') 17 18 winsdk = os.getenv('WindowsSdkDir') 19 if not winsdk: 20 winsdk = os.path.join(p, 'Windows Kits', '10') 21 print('%WindowsSdkDir% not set. You might want to run this from') 22 print('a Visual Studio cmd prompt. Defaulting to', winsdk) 23 24 vswhere = os.path.join( 25 p, 'Microsoft Visual Studio', 'Installer', 'vswhere') 26 vcid = 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' 27 vsinstalldir = cmdout( 28 [vswhere, '-latest', '-products', '*', '-requires', vcid, 29 '-property', 'installationPath']) 30 31 def mkjunction(dst, src): 32 subprocess.check_call(['mklink', '/j', dst, src], shell=True) 33 os.mkdir(out_dir) 34 mkjunction(os.path.join(out_dir, 'VC'), 35 os.path.join(vsinstalldir, 'VC')) 36 os.mkdir(os.path.join(out_dir, 'Windows Kits')) 37 mkjunction(os.path.join(out_dir, 'Windows Kits', '10'), winsdk) 38 elif sys.platform == 'darwin': 39 # The SDKs used by default in compiler-rt/cmake/base-config-ix.cmake. 40 # COMPILER_RT_ENABLE_IOS defaults to on. 41 # COMPILER_RT_ENABLE_WATCHOS and COMPILER_RT_ENABLE_TV default to off. 42 # compiler-rt/cmake/config-ix.cmake sets DARWIN_EMBEDDED_PLATFORMS 43 # depending on these. 44 sdks = ['macosx', 'iphoneos', 'iphonesimulator'] 45 os.mkdir(out_dir) 46 for sdk in sdks: 47 sdkpath = cmdout(['xcrun', '-sdk', sdk, '-show-sdk-path']) 48 # sdkpath is something like /.../SDKs/MacOSX11.1.sdk, which is a 49 # symlink to MacOSX.sdk in the same directory. Resolve the symlink, 50 # to make the symlink in out_dir less likely to break when the SDK 51 # is updated (which will bump the number on xcrun's output, but not 52 # on the symlink destination). 53 sdkpath = os.path.realpath(sdkpath) 54 os.symlink(sdkpath, os.path.join(out_dir, os.path.basename(sdkpath))) 55 else: 56 os.symlink('/', out_dir) 57 58 print('Done. Pass these flags to cmake:') 59 abs_out_dir = os.path.abspath(out_dir) 60 if sys.platform == 'win32': 61 # CMake doesn't like backslashes in commandline args. 62 abs_out_dir = abs_out_dir.replace(os.path.sep, '/') 63 print(' -DLLVM_WINSYSROOT=' + abs_out_dir) 64 elif sys.platform == 'darwin': 65 flags = [ 66 '-DCMAKE_OSX_SYSROOT=' + os.path.join(abs_out_dir, 'MacOSX.sdk'), 67 68 # For find_darwin_sdk_dir() in 69 # compiler-rt/cmake/Modules/CompilerRTDarwinUtils.cmake 70 '-DDARWIN_macosx_CACHED_SYSROOT=' + 71 os.path.join(abs_out_dir, 'MacOSX.sdk'), 72 '-DDARWIN_iphoneos_CACHED_SYSROOT=' + 73 os.path.join(abs_out_dir, 'iPhoneOS.sdk'), 74 '-DDARWIN_iphonesimulator_CACHED_SYSROOT=' + 75 os.path.join(abs_out_dir, 'iPhoneSimulator.sdk'), 76 ] 77 print(' ' + ' '.join(flags)) 78 else: 79 print(' -DCMAKE_SYSROOT=' + abs_out_dir + ' to cmake.') 80 81 82def main(): 83 parser = argparse.ArgumentParser(description=__doc__) 84 85 subparsers = parser.add_subparsers(dest='command', required=True) 86 87 makefake = subparsers.add_parser('make-fake', 88 help='Create a sysroot that symlinks to local directories.') 89 makefake.add_argument('--out-dir', required=True) 90 91 args = parser.parse_args() 92 93 assert args.command == 'make-fake' 94 make_fake_sysroot(args.out_dir) 95 96 97if __name__ == '__main__': 98 main() 99