Path: blob/main/tools/determinism_checker.py
4128 views
# Copyright 2018 The Emscripten Authors. All rights reserved.1# Emscripten is available under two separate licenses, the MIT license and the2# University of Illinois/NCSA Open Source License. Both these licenses can be3# found in the LICENSE file.45"""Runs a build command many times to search for any nondeterminism.6"""78import os9import random10import subprocess11import time12from pathlib import Path131415def run():16subprocess.check_call(['emcc', 'src.cpp', '-O2'])17ret = {}18for relevant_file in os.listdir('.'):19ret[relevant_file] = Path(relevant_file).read_text()20return ret212223def write(data, subdir):24if not os.path.exists(subdir):25os.mkdir(subdir)26for relevant_file in data:27Path(os.path.join(subdir, relevant_file)).write_text(data[relevant_file])282930os.chdir('/tmp/emscripten_temp')31assert len(os.listdir('.')) == 0, 'temp dir should start out empty, after that, everything there looks important to us'32Path('src.cpp').write_text('''33#include <iostream>3435int main()36{37std::cout << "hello world" << std::endl << 77 << "." << std::endl;38return 0;39}40''')4142os.environ['EMCC_DEBUG'] = '1'43# os.environ['BINARYEN_PASS_DEBUG'] = '3'4445first = run()4647i = 048while 1:49print(i)50i += 151time.sleep(random.random() / (10 * 5)) # add some timing nondeterminism here, not that we need it, but whatever52curr = run()53if first != curr:54print('NONDETERMINISM!!!1')55write(first, 'first')56write(curr, 'second')57break585960