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 else: 39 assert False, "FIXME: Implement on non-win" 40 41 print('Done.') 42 if sys.platform == 'win32': 43 # CMake doesn't like backslashes in commandline args. 44 abs_out_dir = os.path.abspath(out_dir).replace(os.path.sep, '/') 45 print('Pass -DLLVM_WINSYSROOT=' + abs_out_dir + ' to cmake.') 46 else: 47 print('Pass -DCMAKE_SYSROOT=' + abs_out_dir + ' to cmake.') 48 49 50def main(): 51 parser = argparse.ArgumentParser(description=__doc__) 52 53 subparsers = parser.add_subparsers(dest='command', required=True) 54 55 makefake = subparsers.add_parser('make-fake', 56 help='Create a sysroot that symlinks to local directories.') 57 makefake.add_argument('--out-dir', required=True) 58 59 args = parser.parse_args() 60 61 assert args.command == 'make-fake' 62 make_fake_sysroot(args.out_dir) 63 64 65if __name__ == '__main__': 66 main() 67