OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright 2013 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 # Downloads, builds (with instrumentation) and installs shared libraries. |
| 7 |
| 8 import argparse |
| 9 import os |
| 10 import shutil |
| 11 import subprocess |
| 12 import sys |
| 13 |
| 14 # Should be a dict from 'sanitizer type' to 'compiler flag'. |
| 15 SUPPORTED_SANITIZERS = {'asan': 'address'} |
| 16 |
| 17 |
| 18 class ScopedChangeDirectory(object): |
| 19 """Changes current working directory and restores it back automatically.""" |
| 20 def __init__(self, path): |
| 21 self.path = path |
| 22 self.old_path = '' |
| 23 |
| 24 def __enter__(self): |
| 25 self.old_path = os.getcwd() |
| 26 os.chdir(self.path) |
| 27 |
| 28 def __exit__(self, exc_type, exc_value, traceback): |
| 29 os.chdir(self.old_path) |
| 30 |
| 31 |
| 32 def get_script_absolute_path(): |
| 33 return os.path.dirname(os.path.abspath(__file__)) |
| 34 |
| 35 |
| 36 def get_library_build_dependencies(library): |
| 37 command = 'apt-get -s build-dep %s | grep Inst | cut -d " " -f 2' % library |
| 38 command_result = subprocess.Popen(command, stdout=subprocess.PIPE, |
| 39 shell=True) |
| 40 build_dependencies = [l.strip() for l in command_result.stdout] |
| 41 return build_dependencies |
| 42 |
| 43 |
| 44 def download_build_install(parsed_arguments): |
| 45 sanitizer_flag = SUPPORTED_SANITIZERS[parsed_arguments.sanitizer_type] |
| 46 |
| 47 environment = os.environ.copy() |
| 48 environment['CFLAGS'] = '-fsanitize=%s -g -fPIC -w' % sanitizer_flag |
| 49 environment['CXXFLAGS'] = '-fsanitize=%s -g -fPIC -w' % sanitizer_flag |
| 50 # We use XORIGIN as RPATH and after building library replace it to $ORIGIN |
| 51 # The reason: this flag goes through configure script and makefiles |
| 52 # differently for different libraries. So the dollar sign '$' should be |
| 53 # differently escaped. Instead of having problems with that it just |
| 54 # uses XORIGIN to build library and after that replaces it to $ORIGIN |
| 55 # directly in .so file. |
| 56 environment['LDFLAGS'] = '-Wl,-z,origin -Wl,-R,XORIGIN/.' |
| 57 |
| 58 library_directory = '%s/%s' % (parsed_arguments.intermediate_directory, |
| 59 parsed_arguments.library) |
| 60 |
| 61 install_prefix = '%s/%s/instrumented_libraries/%s' % ( |
| 62 get_script_absolute_path(), |
| 63 parsed_arguments.product_directory, |
| 64 parsed_arguments.sanitizer_type) |
| 65 |
| 66 if not os.path.exists(library_directory): |
| 67 os.makedirs(library_directory) |
| 68 |
| 69 with ScopedChangeDirectory(library_directory), \ |
| 70 open(os.devnull, 'w') as dev_null: |
| 71 if subprocess.call('apt-get source %s' % parsed_arguments.library, |
| 72 stdout=dev_null, stderr=dev_null, shell=True): |
| 73 raise Exception('Failed to download package %s' % parsed_arguments.library
) |
| 74 # There should be only one subdirectory after downloading a package. |
| 75 subdirectories = [d for d in os.listdir('.') if os.path.isdir(d)] |
| 76 if len(subdirectories) > 1: |
| 77 raise Exception('There was not one directory after downloading ' \ |
| 78 'a package %s' % parsed_arguments.library) |
| 79 with ScopedChangeDirectory(subdirectories[0]): |
| 80 # Now we are in the package directory. |
| 81 configure_command = './configure %s --prefix=%s' % ( |
| 82 parsed_arguments.custom_configure_flags, install_prefix) |
| 83 if subprocess.call(configure_command, stdout=dev_null, stderr=dev_null, |
| 84 env=environment, shell=True): |
| 85 raise Exception("Failed to configure %s" % parsed_arguments.library) |
| 86 if subprocess.call('make -j%s' % parsed_arguments.jobs, |
| 87 stdout=dev_null, stderr=dev_null, shell=True): |
| 88 raise Exception("Failed to make %s" % parsed_arguments.library) |
| 89 if subprocess.call('make -j%s install' % parsed_arguments.jobs, |
| 90 stdout=dev_null, stderr=dev_null, shell=True): |
| 91 raise Exception("Failed to install %s" % parsed_arguments.library) |
| 92 |
| 93 # Touch a txt file to indicate library is installed. |
| 94 open('%s/%s.txt' % (install_prefix, parsed_arguments.library), 'w').close() |
| 95 |
| 96 # Remove downloaded package and generated temporary build files. |
| 97 shutil.rmtree(library_directory) |
| 98 |
| 99 |
| 100 def main(): |
| 101 argument_parser = argparse.ArgumentParser( |
| 102 description = 'Download, build and install instrumented library') |
| 103 |
| 104 argument_parser.add_argument('-j', '--jobs', type=int, default=1) |
| 105 argument_parser.add_argument('-l', '--library', required=True) |
| 106 argument_parser.add_argument('-i', '--product-directory', default='.', |
| 107 help='Relative path to the directory with chrome binaries') |
| 108 argument_parser.add_argument('-m', '--intermediate-directory', default='.', |
| 109 help='Relative path to the directory for temporary build files') |
| 110 argument_parser.add_argument('-c', '--custom-configure-flags', default='') |
| 111 argument_parser.add_argument('-s', '--sanitizer-type', required=True, |
| 112 choices=SUPPORTED_SANITIZERS.keys()) |
| 113 |
| 114 parsed_arguments = argument_parser.parse_args() |
| 115 # Ensure current working directory is this script directory |
| 116 os.chdir(get_script_absolute_path()) |
| 117 # Ensure all build dependencies are installed |
| 118 build_dependencies = get_library_build_dependencies(parsed_arguments.library) |
| 119 if len(build_dependencies): |
| 120 print >> sys.stderr, 'Please, install build-dependencies for %s' % \ |
| 121 parsed_arguments.library |
| 122 print >> sys.stderr, 'One-liner for APT:' |
| 123 print >> sys.stderr, 'sudo apt-get -y --no-remove build-dep %s' % \ |
| 124 parsed_arguments.library |
| 125 sys.exit(1) |
| 126 |
| 127 download_build_install(parsed_arguments) |
| 128 |
| 129 |
| 130 if __name__ == '__main__': |
| 131 main() |
OLD | NEW |