|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Builds demumble for Mac, Linux, Windows. Must run on a Mac. |
| 4 | +# Needs a chromium checkout at ~/src/chrome/src that was synced with |
| 5 | +# target_os=['win'] to get the Windows toolchain. You must run |
| 6 | +# `build/linux/sysroot_scripts/install-sysroot.py --arch amd64` once to |
| 7 | +# get the linux toolchain. |
| 8 | + |
| 9 | +# Also needs a GN build of llvm at ~/src/llvm-project/out/gn for llvm-strip |
| 10 | +# for stripping the Linux binary. |
| 11 | + |
| 12 | +# Doesn't run tests, so make sure to run `./demumble_test.py` on all 3 platforms |
| 13 | +# before running this script. |
| 14 | + |
| 15 | +# After this script finishes successfully, the cwd will contain |
| 16 | +# demumble-mac.zip, demumble-linux.zip, demumble-windows.zip which each contain |
| 17 | +# a demumble binary built for that OS, ready for releasing (assuming the script |
| 18 | +# was run on the release branch). |
| 19 | + |
| 20 | +# https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/CrossCompiling has |
| 21 | +# some documentation on cross builds with cmake. |
| 22 | + |
| 23 | +import contextlib |
| 24 | +import json |
| 25 | +import glob |
| 26 | +import os |
| 27 | +import subprocess |
| 28 | +import sys |
| 29 | + |
| 30 | +crsrc = os.path.join(os.path.expanduser('~'), 'src/chrome/src') |
| 31 | +if len(sys.argv) > 1: |
| 32 | + crsrc = os.path.abspath(sys.argv[1]) |
| 33 | +clangcl = crsrc + '/third_party/llvm-build/Release+Asserts/bin/clang-cl' |
| 34 | +clangxx = crsrc + '/third_party/llvm-build/Release+Asserts/bin/clang++' |
| 35 | +lldlink = crsrc + '/third_party/llvm-build/Release+Asserts/bin/lld-link' |
| 36 | + |
| 37 | +linux_strip = os.path.join(os.path.expanduser('~'), |
| 38 | + 'src/llvm-project/out/gn/bin/llvm-strip') |
| 39 | + |
| 40 | +cmake = '/Applications/CMake.app/Contents/bin/cmake' |
| 41 | +call_cmake = [cmake, '-GNinja', '..', '-DCMAKE_BUILD_TYPE=Release'] |
| 42 | + |
| 43 | + |
| 44 | +@contextlib.contextmanager |
| 45 | +def buildir(newdir): |
| 46 | + """Creates newdir if it doesn't exist yet and temporarily sets cwd to it.""" |
| 47 | + newdir = os.path.join(os.path.dirname(__file__), newdir) |
| 48 | + if not os.path.isdir(newdir): |
| 49 | + os.mkdir(newdir) # Intentionally not deleted. |
| 50 | + prevdir = os.getcwd() |
| 51 | + os.chdir(newdir) |
| 52 | + try: |
| 53 | + yield |
| 54 | + finally: |
| 55 | + os.chdir(prevdir) |
| 56 | + |
| 57 | +subprocess.check_call(['rm', '-rf', 'buildlinux', 'buildmac', 'buildwin']) |
| 58 | +devnull = open(os.devnull,"w") |
| 59 | + |
| 60 | +# Linux. |
| 61 | +linux_sysroot = crsrc + '/build/linux/debian_jessie_amd64-sysroot' |
| 62 | +cflags = [ '--sysroot', linux_sysroot, '--target=x86_64-linux-gnu', ] |
| 63 | +ldflags = ['-fuse-ld=lld'] + cflags |
| 64 | +with buildir('buildlinux'): |
| 65 | + print 'building linux' |
| 66 | + subprocess.check_call(call_cmake + [ |
| 67 | + '-DCMAKE_CXX_COMPILER=' + clangxx, |
| 68 | + '-DCMAKE_CXX_FLAGS=' + ' '.join(cflags), |
| 69 | + '-DCMAKE_EXE_LINKER_FLAGS=' + ' '.join(ldflags), |
| 70 | + '-DCMAKE_SYSTEM_NAME=Linux', |
| 71 | + ], stdout=devnull) |
| 72 | + subprocess.check_call(['ninja', 'demumble']) |
| 73 | + # FIXME: https://chromium-review.googlesource.com/c/chromium/src/+/1214943 |
| 74 | + # has a way to build eu-strip on macOS, which is arguably a smaller dep |
| 75 | + # than llvm-strip. |
| 76 | + subprocess.check_call([linux_strip, 'demumble']) |
| 77 | + subprocess.check_call(['zip', '-q9', 'demumble-linux.zip', 'demumble']) |
| 78 | + subprocess.check_call(['mv', 'demumble-linux.zip', '..']) |
| 79 | + |
| 80 | +# Mac. |
| 81 | +with buildir('buildmac'): |
| 82 | + print 'building mac' |
| 83 | + subprocess.check_call(call_cmake + [ |
| 84 | + '-DCMAKE_CXX_COMPILER=' + clangxx, |
| 85 | + ], stdout=devnull) |
| 86 | + subprocess.check_call(['ninja', 'demumble']) |
| 87 | + subprocess.check_call(['strip', 'demumble']) |
| 88 | + subprocess.check_call(['zip', '-q9', 'demumble-mac.zip', 'demumble']) |
| 89 | + subprocess.check_call(['mv', 'demumble-mac.zip', '..']) |
| 90 | + |
| 91 | +# Win. |
| 92 | +win_sysroot = glob.glob( |
| 93 | + crsrc + '/third_party/depot_tools/win_toolchain/vs_files/*')[0] |
| 94 | +win_bindir = win_sysroot + '/win_sdk/bin' |
| 95 | +# This json file looks like http://codepad.org/kmfgf0UL |
| 96 | +winenv = json.load(open(win_bindir + '/SetEnv.x64.json'))['env'] |
| 97 | +for k in ['INCLUDE', 'LIB']: |
| 98 | + winenv[k] = [os.path.join(*([win_bindir] + e)) for e in winenv[k]] |
| 99 | +win_include = ['-imsvc' + i for i in winenv['INCLUDE']] |
| 100 | +win_lib = ['/libpath:' + i for i in winenv['LIB']] |
| 101 | +cflags = ['--target=x86_64-pc-windows'] + win_include |
| 102 | +with buildir('buildwin'): |
| 103 | + print 'building windows' |
| 104 | + subprocess.check_call(call_cmake + [ |
| 105 | + '-DCMAKE_CXX_COMPILER=' + clangcl, |
| 106 | + '-DCMAKE_CXX_FLAGS=' + ' '.join(cflags), |
| 107 | + # Without /manifest:no, cmake creates a default manifest file -- and |
| 108 | + # explicitly calls mt.exe (which we don't have in a cross build). |
| 109 | + # This also removes a dependency on rc.exe -- without this we'd also |
| 110 | + # have to set CMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY. |
| 111 | + '-DCMAKE_EXE_LINKER_FLAGS=' + ' '.join(['/manifest:no'] + win_lib), |
| 112 | + '-DCMAKE_LINKER=' + lldlink, |
| 113 | + '-DCMAKE_SYSTEM_NAME=Windows', |
| 114 | + ], stdout=devnull) |
| 115 | + subprocess.check_call(['ninja', 'demumble']) |
| 116 | + # No stripping on Windows. |
| 117 | + subprocess.check_call(['zip', '-q9', 'demumble-win.zip', 'demumble.exe']) |
| 118 | + subprocess.check_call(['mv', 'demumble-win.zip', '..']) |
| 119 | + |
| 120 | +# Copy over mac binary and run tests. |
| 121 | +print 'running tests (on mac)' |
| 122 | +subprocess.check_call(['cp', 'buildmac/demumble', '.']) |
| 123 | +subprocess.check_call(['./demumble_test.py']) |
| 124 | + |
| 125 | +# Show zip files. |
| 126 | +subprocess.check_call('ls -hl *.zip', shell=True) |
| 127 | +subprocess.check_call(['./demumble', '--version']) |
0 commit comments