Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """A utility script to help building Syzygy-reordered Chrome binaries.""" | |
| 7 | |
| 8 import logging | |
| 9 import optparse | |
| 10 import os | |
| 11 import subprocess | |
| 12 import sys | |
| 13 | |
| 14 | |
| 15 _SRC_DIR = os.path.abspath( | |
| 16 os.path.join(os.path.dirname(__file__), '../../../..')) | |
|
robertshield
2011/11/28 01:16:38
seems like this should be an input option
Sigurður Ásgeirsson
2011/11/28 15:02:10
It's only used to calculate the path to the defaul
| |
| 17 | |
| 18 # The default relink executable to use to reorder binaries. | |
| 19 _DEFAULT_RELINKER = os.path.join(_SRC_DIR, | |
| 20 'third_party/syzygy/binaries/exe/relink.exe') | |
| 21 | |
| 22 # A list of tuples, where each tuple names a binary and its associated PDB file. | |
| 23 _BINARIES = [('chrome.dll', 'chrome_dll.pdb'),] | |
|
robertshield
2011/11/28 01:16:38
feels like this should be specified in a .gyp file
Sigurður Ásgeirsson
2011/11/28 15:02:10
This is true. However, I was modelling this from t
| |
| 24 | |
| 25 _LOGGER = logging.getLogger() | |
| 26 | |
| 27 # We use the same seed for all random reorderings to get a deterministic build. | |
| 28 _RANDOM_SEED = 1347344 | |
|
robertshield
2011/11/28 01:16:38
this is not a comment, but http://xkcd.com/221/
Sigurður Ásgeirsson
2011/11/28 15:02:10
:)
| |
| 29 | |
| 30 | |
| 31 def _Shell(*cmd, **kw): | |
| 32 """Shells out to "cmd". Returns a tuple of cmd's stdout, stderr.""" | |
| 33 _LOGGER.info('Running command "%s".', cmd) | |
| 34 prog = subprocess.Popen(cmd, **kw) | |
| 35 | |
| 36 stdout, stderr = prog.communicate() | |
| 37 if prog.returncode != 0: | |
| 38 raise RuntimeError('Command "%s" returned %d.' % (cmd, prog.returncode)) | |
| 39 | |
| 40 return stdout, stderr | |
| 41 | |
| 42 | |
| 43 def _ReorderBinary(relink_exe, binary, symbol, input_dir, output_dir): | |
| 44 """Reorders the binary found in input_dir, and writes the resultant | |
| 45 reordered binary and symbol files to output_dir. | |
| 46 | |
| 47 If a file named <binary>-order.json exists in the input directory, imposes | |
| 48 that order on the output binaries, otherwise orders them randomly. | |
| 49 """ | |
| 50 cmd = [relink_exe, | |
| 51 '--verbose', | |
| 52 '--input-dll=%s' % os.path.join(input_dir, binary), | |
| 53 '--input-pdb=%s' % os.path.join(input_dir, symbol), | |
| 54 '--output-dll=%s' % os.path.join(output_dir, binary), | |
| 55 '--output-pdb=%s' % os.path.join(output_dir, symbol),] | |
| 56 | |
| 57 # Check whether there's an order file available for the binary. | |
| 58 order_file = os.path.join(input_dir, '%s-order.json' % binary) | |
| 59 if os.path.exists(order_file): | |
| 60 # The ordering file exists, let's use that. | |
| 61 _LOGGER.info('Reordering "%s" according to "%s".', binary, order_file) | |
| 62 cmd.append('--order-file=%s' % order_file) | |
| 63 else: | |
| 64 # No ordering file, we randomize the output. | |
|
robertshield
2011/11/28 01:16:38
Is the reordered path soon expected to be the defa
Sigurður Ásgeirsson
2011/11/28 15:02:10
Good question - I figure once we turn this on for
| |
| 65 _LOGGER.info('Randomly reordering "%s"', binary) | |
| 66 cmd.append('--seed=%d' % _RANDOM_SEED) | |
| 67 | |
| 68 return _Shell(*cmd) | |
| 69 | |
| 70 | |
| 71 def main(options): | |
| 72 logging.basicConfig(level=logging.INFO) | |
| 73 | |
| 74 # Make sure the destination directory exists. | |
| 75 if not os.path.isdir(options.destination_dir): | |
| 76 _LOGGER.info('Creating destination directory "%s".', | |
| 77 options.destination_dir) | |
| 78 os.makedirs(options.destination_dir) | |
| 79 | |
| 80 for (binary, symbol) in _BINARIES: | |
| 81 # Reorder the binaries into the destination directory, one by one. | |
| 82 _ReorderBinary(options.relinker, | |
| 83 binary, | |
| 84 symbol, | |
| 85 options.output_dir, | |
| 86 options.destination_dir) | |
| 87 | |
| 88 | |
| 89 def _ParseOptions(): | |
| 90 option_parser = optparse.OptionParser() | |
| 91 option_parser.add_option('-o', '--output_dir', | |
| 92 help='Output directory where the original binaries are built.') | |
| 93 option_parser.add_option('--relinker', default=_DEFAULT_RELINKER, | |
| 94 help='Relinker exectuable to use, defaults to "%s"' % _DEFAULT_RELINKER) | |
| 95 option_parser.add_option('-d', '--destination_dir', | |
| 96 help='Destination directory for reordered files, defaults to ' | |
| 97 'the subdirectory "reordered" in the output_dir.') | |
| 98 options, args = option_parser.parse_args() | |
| 99 | |
| 100 if not options.output_dir: | |
| 101 option_parser.error('You must provide an output dir.') | |
| 102 | |
| 103 if not options.destination_dir: | |
| 104 options.destination_dir = os.path.join(options.output_dir, 'reordered') | |
| 105 | |
| 106 return options | |
| 107 | |
| 108 | |
| 109 if '__main__' == __name__: | |
| 110 sys.exit(main(_ParseOptions())) | |
| OLD | NEW |