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 import optparse |
| 7 import os |
| 8 import re |
| 9 import shutil |
| 10 import subprocess |
| 11 import sys |
| 12 |
| 13 |
| 14 # Where things are in relation to this script. |
| 15 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 16 SRC_DIR = os.path.dirname(SCRIPT_DIR) |
| 17 NACL_DIR = os.path.join(SRC_DIR, 'native_client') |
| 18 |
| 19 # Pathing to the two command_buffer directories (relative to native_client). |
| 20 NACL_CMD_BUFFER_DIR = os.path.join('src', 'shared', |
| 21 'ppapi_proxy', 'command_buffer') |
| 22 GPU_CMD_BUFFER_DIR = os.path.join('..', 'gpu', 'command_buffer') |
| 23 |
| 24 |
| 25 def RelativePath(path, base): |
| 26 """Find the relative path. |
| 27 |
| 28 Arguments: |
| 29 path: path we want a relative path to. |
| 30 base: path we want a relative path from. |
| 31 Returns: |
| 32 The relative path from base to path. |
| 33 """ |
| 34 path = os.path.abspath(path) |
| 35 base = os.path.abspath(base) |
| 36 path_parts = path.split(os.sep) |
| 37 base_parts = base.split(os.sep) |
| 38 while path_parts and base_parts and path_parts[0] == base_parts[0]: |
| 39 path_parts = path_parts[1:] |
| 40 base_parts = base_parts[1:] |
| 41 rel_parts = ['..'] * len(base_parts) + path_parts |
| 42 return os.sep.join(rel_parts) |
| 43 |
| 44 |
| 45 def PrintInputs(platforms): |
| 46 """Print all the transitive inputs required to build the IRT. |
| 47 |
| 48 Arguments: |
| 49 platforms: list of platform names to build for. |
| 50 """ |
| 51 inputs = set() |
| 52 for platform in platforms: |
| 53 # Invoke scons to get dependency tree. |
| 54 cmd = [ |
| 55 sys.executable, 'scons.py', '-n', '--tree=all', |
| 56 '--mode=nacl', 'platform=' + platform, |
| 57 'scons-out/nacl_irt-' + platform + '/staging/irt.nexe', |
| 58 ] |
| 59 p = subprocess.Popen(cmd, cwd=NACL_DIR, |
| 60 stdout=subprocess.PIPE, |
| 61 stderr=subprocess.PIPE) |
| 62 (p_stdout, p_stderr) = p.communicate() |
| 63 if p.returncode != 0: |
| 64 sys.exit(2) |
| 65 # Extract unique inputs. |
| 66 for line in p_stdout.splitlines(): |
| 67 m = re.match('^[ -+|]*\+\-(.+)', line) |
| 68 if not m: |
| 69 continue |
| 70 filename = m.group(1) |
| 71 if '[' in filename: |
| 72 continue |
| 73 if filename.startswith('scons-out'): |
| 74 continue |
| 75 if filename.endswith('.nexe'): |
| 76 continue |
| 77 # Apply the underlay of gpu/command_buffer (to match scons). |
| 78 if filename.startswith(NACL_CMD_BUFFER_DIR + os.sep): |
| 79 filename = GPU_CMD_BUFFER_DIR + filename[len(NACL_CMD_BUFFER_DIR):] |
| 80 inputs.add(filename) |
| 81 # Check that everything exists and make it script relative. |
| 82 rel_inputs = set() |
| 83 for f in inputs: |
| 84 nf = os.path.join(NACL_DIR, f) |
| 85 if not os.path.exists(nf): |
| 86 raise Exception('missing input file "%s"' % nf) |
| 87 rel_inputs.add(RelativePath(nf, SCRIPT_DIR)) |
| 88 # Print it sorted. |
| 89 rel_inputs = sorted(list(rel_inputs)) |
| 90 for f in rel_inputs: |
| 91 print f |
| 92 |
| 93 |
| 94 def BuildIRT(platforms, out_dir): |
| 95 """Build the IRT for several platforms. |
| 96 |
| 97 Arguments: |
| 98 platforms: list of platform names to build for. |
| 99 out_dir: directory to output the IRT to. |
| 100 """ |
| 101 # Clean. |
| 102 scons_out = os.path.join(NACL_DIR, 'scons-out') |
| 103 if os.path.exists(scons_out): |
| 104 shutil.rmtree(scons_out) |
| 105 # Build for each platform. |
| 106 for platform in platforms: |
| 107 cmd = [ |
| 108 sys.executable, 'scons.py', '--verbose', '-j8', |
| 109 '--mode=nacl', 'platform=' + platform, |
| 110 'scons-out/nacl_irt-' + platform + '/staging/irt.nexe', |
| 111 ] |
| 112 print 'Running: ' + ' '.join(cmd) |
| 113 p = subprocess.Popen(cmd, cwd=NACL_DIR) |
| 114 p.wait() |
| 115 if p.returncode != 0: |
| 116 sys.exit(3) |
| 117 # Copy out each platform after stripping. |
| 118 for platform in platforms: |
| 119 uplatform = platform.replace('-', '_') |
| 120 platform2 = {'x86-32': 'i686', 'x86-64': 'x86_64'}.get(platform, platform) |
| 121 cplatform = { |
| 122 'win32': 'win', |
| 123 'cygwin': 'win', |
| 124 'darwin': 'mac', |
| 125 }.get(sys.platform, 'linux') |
| 126 nexe = os.path.join(out_dir, 'nacl_irt_' + uplatform + '.nexe') |
| 127 cmd = [ |
| 128 '../native_client/toolchain/' + cplatform + '_x86_newlib/bin/' + |
| 129 platform2 + '-nacl-strip', |
| 130 '--strip-debug', |
| 131 '../native_client/scons-out/nacl_irt-' + platform + '/staging/irt.nexe', |
| 132 '-o', nexe |
| 133 ] |
| 134 print 'Running: ' + ' '.join(cmd) |
| 135 p = subprocess.Popen(cmd, cwd=SCRIPT_DIR) |
| 136 p.wait() |
| 137 if p.returncode != 0: |
| 138 sys.exit(4) |
| 139 |
| 140 |
| 141 def Main(argv): |
| 142 parser = optparse.OptionParser() |
| 143 parser.add_option('--inputs', dest='inputs', default=False, |
| 144 action='store_true', |
| 145 help='only emit the transitive inputs to the irt build') |
| 146 parser.add_option('--platform', dest='platforms', action='append', |
| 147 default=[], |
| 148 help='add a platform to build for (x86-32|x86-64)') |
| 149 parser.add_option('--outdir', dest='outdir', |
| 150 help='directory to out irt to') |
| 151 (options, args) = parser.parse_args(argv[1:]) |
| 152 if args or not options.platforms or ( |
| 153 not options.inputs and not options.outdir): |
| 154 parser.print_help() |
| 155 sys.exit(1) |
| 156 |
| 157 if options.inputs: |
| 158 PrintInputs(options.platforms) |
| 159 else: |
| 160 BuildIRT(options.platforms, options.outdir) |
| 161 |
| 162 |
| 163 if __name__ == '__main__': |
| 164 Main(sys.argv) |
OLD | NEW |