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