|
| 1 | +#!/usr/bin/env python |
| 2 | +# utils/rth - Resilience test helper |
| 3 | +# |
| 4 | +# This source file is part of the Swift.org open source project |
| 5 | +# |
| 6 | +# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 7 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 8 | +# |
| 9 | +# See http://swift.org/LICENSE.txt for license information |
| 10 | +# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 11 | + |
| 12 | +import subprocess |
| 13 | +import argparse |
| 14 | +import shutil |
| 15 | +import os |
| 16 | +import shlex |
| 17 | +import pipes |
| 18 | + |
| 19 | +VERBOSE = True |
| 20 | + |
| 21 | +def verbose_print_command(command): |
| 22 | + if VERBOSE: |
| 23 | + print " ".join(pipes.quote(c) for c in command) |
| 24 | + |
| 25 | +class ResilienceTest(object): |
| 26 | + def __init__(self, target_build_swift, target_run, tmp_dir, test_dir, |
| 27 | + test_src, additional_compile_flags_library): |
| 28 | + self.target_build_swift = shlex.split(target_build_swift) |
| 29 | + self.target_run = shlex.split(target_run) |
| 30 | + self.tmp_dir = tmp_dir |
| 31 | + self.test_dir = test_dir |
| 32 | + self.test_src = test_src |
| 33 | + self.additional_compile_flags_library = \ |
| 34 | + shlex.split(additional_compile_flags_library) |
| 35 | + |
| 36 | + self.before_dir = os.path.join(self.tmp_dir, 'before') |
| 37 | + self.after_dir = os.path.join(self.tmp_dir, 'after') |
| 38 | + self.config_dir_map = {'BEFORE': self.before_dir, |
| 39 | + 'AFTER': self.after_dir} |
| 40 | + |
| 41 | + self.lib_src_name = os.path.basename(self.test_src)[5:] |
| 42 | + self.lib_obj_name = self.lib_src_name[:-6] + '.o' |
| 43 | + self.lib_src = os.path.join(self.test_dir, 'Inputs', self.lib_src_name) |
| 44 | + |
| 45 | + def run(self): |
| 46 | + self.set_up() |
| 47 | + self.compile_library() |
| 48 | + self.compile_main() |
| 49 | + self.link() |
| 50 | + self.execute() |
| 51 | + return 0 |
| 52 | + |
| 53 | + def set_up(self): |
| 54 | + shutil.rmtree(self.tmp_dir) |
| 55 | + os.makedirs(self.after_dir) |
| 56 | + os.makedirs(self.before_dir) |
| 57 | + |
| 58 | + def compile_library(self): |
| 59 | + for config in self.config_dir_map: |
| 60 | + for emit_flag in ['-emit-library', '-emit-module']: |
| 61 | + output_obj = os.path.join(self.config_dir_map[config], |
| 62 | + self.lib_obj_name) |
| 63 | + compiler_flags = [emit_flag, '-Xfrontend', |
| 64 | + '-enable-resilience', '-D', config, '-c', |
| 65 | + self.lib_src, '-o', output_obj] |
| 66 | + command = self.target_build_swift + \ |
| 67 | + self.additional_compile_flags_library + compiler_flags |
| 68 | + verbose_print_command(command) |
| 69 | + returncode = subprocess.call(command) |
| 70 | + assert returncode == 0, str(command) |
| 71 | + |
| 72 | + def compile_main(self): |
| 73 | + for config in self.config_dir_map: |
| 74 | + output_obj = os.path.join(self.config_dir_map[config], 'main.o') |
| 75 | + compiler_flags = ['-D', config, '-c', self.test_src, '-I', |
| 76 | + self.config_dir_map[config], '-o', output_obj] |
| 77 | + command = self.target_build_swift + compiler_flags |
| 78 | + verbose_print_command(command) |
| 79 | + returncode = subprocess.call(command) |
| 80 | + assert returncode == 0, str(command) |
| 81 | + |
| 82 | + def link(self): |
| 83 | + for config1 in self.config_dir_map: |
| 84 | + for config2 in self.config_dir_map: |
| 85 | + config1_lower = config1.lower() |
| 86 | + config2_lower = config2.lower() |
| 87 | + output_obj = os.path.join(self.tmp_dir, |
| 88 | + config1_lower + '_' + config2_lower) |
| 89 | + compiler_flags = [ |
| 90 | + os.path.join(self.config_dir_map[config1], |
| 91 | + self.lib_obj_name), |
| 92 | + os.path.join(self.config_dir_map[config2], |
| 93 | + 'main.o'), |
| 94 | + '-o', output_obj |
| 95 | + ] |
| 96 | + command = self.target_build_swift + compiler_flags |
| 97 | + verbose_print_command(command) |
| 98 | + returncode = subprocess.call(command) |
| 99 | + assert returncode == 0, str(command) |
| 100 | + |
| 101 | + def execute(self): |
| 102 | + for config1 in self.config_dir_map: |
| 103 | + for config2 in self.config_dir_map: |
| 104 | + config1_lower = config1.lower() |
| 105 | + config2_lower = config2.lower() |
| 106 | + output_obj = os.path.join(self.tmp_dir, |
| 107 | + config1_lower + '_' + config2_lower) |
| 108 | + command = self.target_run + [output_obj] |
| 109 | + verbose_print_command(command) |
| 110 | + returncode = subprocess.call(command) |
| 111 | + assert returncode == 0, str(command) |
| 112 | + |
| 113 | +def main(): |
| 114 | + parser = argparse.ArgumentParser(description='Resilience test helper') |
| 115 | + parser.add_argument('--target-build-swift', required=True) |
| 116 | + parser.add_argument('--target-run', required=True) |
| 117 | + parser.add_argument('--t', required=True) |
| 118 | + parser.add_argument('--S', required=True) |
| 119 | + parser.add_argument('--s', required=True) |
| 120 | + parser.add_argument('--additional-compile-flags-library', default='') |
| 121 | + |
| 122 | + args = parser.parse_args() |
| 123 | + |
| 124 | + resilience_test = ResilienceTest(args.target_build_swift, args.target_run, |
| 125 | + args.t, args.S, args.s, |
| 126 | + args.additional_compile_flags_library) |
| 127 | + |
| 128 | + return resilience_test.run() |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + exit(main()) |
0 commit comments