|
| 1 | +#!/usr/bin/env python |
| 2 | +# check-incremental - Check if incremental compilation works -*- python -*- |
| 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 | +# ---------------------------------------------------------------------------- |
| 13 | +# |
| 14 | +# This is a wrapper for the swift compiler. |
| 15 | +# It invokes the compiler multiple times and checks if the output object file |
| 16 | +# is only written a single time. |
| 17 | +# The main purpose of the check is to ensure that the compiler is |
| 18 | +# deterministic. |
| 19 | +# |
| 20 | +# ---------------------------------------------------------------------------- |
| 21 | + |
| 22 | +import subprocess |
| 23 | +import os |
| 24 | +import sys |
| 25 | +import time |
| 26 | + |
| 27 | + |
| 28 | +def main(): |
| 29 | + verbose = False |
| 30 | + num_iterations = 4 |
| 31 | + |
| 32 | + write_obj_file = False |
| 33 | + next_arg_is_output = False |
| 34 | + compare_time = True |
| 35 | + output_file = None |
| 36 | + |
| 37 | + for arg in sys.argv: |
| 38 | + if next_arg_is_output: |
| 39 | + output_file = arg |
| 40 | + next_arg_is_output = False |
| 41 | + elif arg == '-c': |
| 42 | + write_obj_file = True |
| 43 | + elif arg == '-disable-incremental-llvm-codegen': |
| 44 | + compare_time = False |
| 45 | + elif arg == '-o': |
| 46 | + next_arg_is_output = True |
| 47 | + |
| 48 | + if not write_obj_file or output_file is None: |
| 49 | + return |
| 50 | + |
| 51 | + new_args = sys.argv[1:] |
| 52 | + subprocess.check_call(new_args) |
| 53 | + |
| 54 | + if verbose: |
| 55 | + print "Reference compilation of " + output_file + ":" |
| 56 | + |
| 57 | + reference_md5 = subprocess.check_output(["md5", "-q", output_file]) |
| 58 | + reference_time = os.path.getmtime(output_file) |
| 59 | + |
| 60 | + if verbose: |
| 61 | + print " time = {}".format(reference_time) |
| 62 | + print " md5 = " + reference_md5 |
| 63 | + |
| 64 | + subprocess.check_call(["cp", output_file, output_file + ".ref.o"]) |
| 65 | + |
| 66 | + for iteration in range(0, num_iterations): |
| 67 | + |
| 68 | + if verbose: |
| 69 | + print "Iteration {}:".format(iteration) |
| 70 | + |
| 71 | + subprocess.check_call(new_args) |
| 72 | + |
| 73 | + second_md5 = subprocess.check_output(["md5", "-q", output_file]) |
| 74 | + second_time = os.path.getmtime(output_file) |
| 75 | + |
| 76 | + if verbose: |
| 77 | + print " time = {}".format(second_time) |
| 78 | + print " md5 = " + second_md5 |
| 79 | + |
| 80 | + # This is the most important check: is the output file exactly the same. |
| 81 | + if reference_md5 != second_md5: |
| 82 | + sys.exit("non-determinism when generating: " + output_file) |
| 83 | + |
| 84 | + # This is the bonus check: does the compiler not re-write the output file. |
| 85 | + # (For compilations < 1sec this check may succeed even if the file was |
| 86 | + # overwritten). |
| 87 | + if compare_time and reference_time != second_time: |
| 88 | + sys.exit("file re-written: " + output_file) |
| 89 | + |
| 90 | + |
| 91 | +main() |
| 92 | + |
0 commit comments