OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 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 """Adaptor script called through build/isolate.gypi. |
| 7 |
| 8 Creates a wrapping .isolate which 'includes' the original one, that can be |
| 9 consumed by tools/swarming_client/isolate.py. Path variables are determined |
| 10 based on the current working directory. The relative_cwd in the .isolated file |
| 11 is determined based on *the .isolate file that declare the 'command' variable to |
| 12 be used* so the wrapping .isolate doesn't affect this value. |
| 13 |
| 14 It packages all the dynamic libraries found in this wrapping .isolate. This is |
| 15 inefficient and non-deterministic. In the very near future, it will parse |
| 16 build.ninja, find back the root target and find all the dynamic libraries that |
| 17 are marked as a dependency to this target. |
| 18 """ |
| 19 |
| 20 import glob |
| 21 import os |
| 22 import posixpath |
| 23 import subprocess |
| 24 import sys |
| 25 |
| 26 TOOLS_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 27 SWARMING_CLIENT_DIR = os.path.join(TOOLS_DIR, 'swarming_client') |
| 28 SRC_DIR = os.path.dirname(TOOLS_DIR) |
| 29 |
| 30 sys.path.insert(0, SWARMING_CLIENT_DIR) |
| 31 |
| 32 import isolate_format |
| 33 |
| 34 |
| 35 |
| 36 # Location to grab binaries based on the OS. |
| 37 DYNAMIC_LIBRARIES = { |
| 38 'darwin': '*.dylib', |
| 39 'linux2': 'lib/*.so', |
| 40 'win32': '*.dll', |
| 41 } |
| 42 |
| 43 |
| 44 def create_wrapper(args, isolate_index, isolated_index): |
| 45 """Creates a wrapper .isolate that add dynamic libs. |
| 46 |
| 47 The original .isolate is not modified. |
| 48 """ |
| 49 cwd = os.getcwd() |
| 50 isolate = args[isolate_index] |
| 51 # The code assumes the .isolate file is always specified path-less in cwd. Fix |
| 52 # if this assumption doesn't hold true. |
| 53 assert os.path.basename(isolate) == isolate, isolate |
| 54 |
| 55 # This will look like ../out/Debug. This is based against cwd. Note that this |
| 56 # must equal the value provided as PRODUCT_DIR. |
| 57 build_dir = os.path.dirname(args[isolated_index]) |
| 58 |
| 59 # This will look like chrome/unit_tests.isolate. It is based against SRC_DIR. |
| 60 # It's used to calculate temp_isolate. |
| 61 src_isolate = os.path.relpath(os.path.join(cwd, isolate), SRC_DIR) |
| 62 |
| 63 # The wrapping .isolate. This will look like |
| 64 # ../out/Debug/gen/chrome/unit_tests.isolate. |
| 65 temp_isolate = os.path.join(build_dir, 'gen', src_isolate) |
| 66 temp_isolate_dir = os.path.dirname(temp_isolate) |
| 67 |
| 68 # Relative path between the new and old .isolate file. |
| 69 isolate_relpath = os.path.relpath( |
| 70 '.', temp_isolate_dir).replace(os.path.sep, '/') |
| 71 |
| 72 # This will look like [../out/Debug/lib/libuser_prefs.so]. |
| 73 dynamic_libs = [ |
| 74 i.replace(os.path.sep, '/') |
| 75 for i in glob.iglob( |
| 76 os.path.join(build_dir, DYNAMIC_LIBRARIES[sys.platform])) |
| 77 ] |
| 78 # And now like ['<(PRODUCT_DIR)/lib/flibuser_prefs.so']. |
| 79 rebased_libs = [ |
| 80 '<(PRODUCT_DIR)/%s' % i[len(build_dir)+1:] |
| 81 for i in dynamic_libs |
| 82 ] |
| 83 |
| 84 # Now do actual wrapping .isolate. |
| 85 out = { |
| 86 'includes': [ |
| 87 posixpath.join(isolate_relpath, isolate), |
| 88 ], |
| 89 'variables': { |
| 90 isolate_format.KEY_TRACKED: rebased_libs, |
| 91 }, |
| 92 } |
| 93 if not os.path.isdir(temp_isolate_dir): |
| 94 os.makedirs(temp_isolate_dir) |
| 95 comment = ( |
| 96 '# Warning: this file was AUTOGENERATED.\n' |
| 97 '# DO NO EDIT.\n') |
| 98 with open(temp_isolate, 'wb') as f: |
| 99 isolate_format.print_all(comment, out, f) |
| 100 if '--verbose' in args: |
| 101 print('Added %d dynamic libs' % len(dynamic_libs)) |
| 102 args[isolate_index] = temp_isolate |
| 103 |
| 104 |
| 105 def main(): |
| 106 args = sys.argv[1:] |
| 107 isolate = None |
| 108 isolated = None |
| 109 is_component = False |
| 110 for i, arg in enumerate(args): |
| 111 if arg == '--isolate': |
| 112 isolate = i + 1 |
| 113 if arg == '--isolated': |
| 114 isolated = i + 1 |
| 115 if arg == 'component=shared_library': |
| 116 is_component = True |
| 117 if isolate is None or isolated is None: |
| 118 print >> sys.stderr, 'Internal failure' |
| 119 return 1 |
| 120 |
| 121 if is_component: |
| 122 create_wrapper(args, isolate, isolated) |
| 123 |
| 124 swarming_client = os.path.join(SRC_DIR, 'tools', 'swarming_client') |
| 125 sys.stdout.flush() |
| 126 result = subprocess.call( |
| 127 [sys.executable, os.path.join(swarming_client, 'isolate.py')] + args) |
| 128 return result |
| 129 |
| 130 |
| 131 if __name__ == '__main__': |
| 132 sys.exit(main()) |
OLD | NEW |