test: Add a script to test the toolchain.

This commit is contained in:
Matt McCormick 2016-03-03 21:04:20 -05:00
parent 19dc7051ea
commit a856edc56c
3 changed files with 122 additions and 0 deletions

7
test/C++/hello.cxx Normal file
View File

@ -0,0 +1,7 @@
#include <iostream>
int main( int argc, char * argv[] )
{
std::cout << "Hello cross-compilation world!" << std::endl;
return 0;
}

7
test/C/hello.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello cross-compilation world!\n");
return 0;
}

108
test/run.py Executable file
View File

@ -0,0 +1,108 @@
#!/usr/bin/env python
"""Run the tests."""
import argparse
import glob
import os
import shutil
import subprocess
import sys
import tempfile
def test_no_build_system(build_dir, language, source):
if language == 'C':
compiler = os.getenv('CC', 'cc')
elif language == 'C++':
compiler = os.getenv('C++', 'c++')
else:
print('Unknown language: ' + language)
return 1
print('Building ' + source + ' by calling ' + compiler + '...')
return subprocess.call([compiler, source])
def test_cmake_build_system(build_dir, source):
shutil.copy(source, build_dir)
with open('CMakeLists.txt', 'w') as fp:
fp.write('cmake_minimum_required(VERSION 3.0)\n')
fp.write('project(test-compiler)\n')
fp.write('add_executable(a.out ' + os.path.basename(source) + ')\n')
os.mkdir('build')
os.chdir('build')
print('Building ' + source + ' with CMake...')
if subprocess.call(['cmake', '..']):
return 1
if subprocess.call(['make']):
return 1
shutil.copy('a.out', build_dir)
return 0
def test_source(source, language, build_system, emulator):
result = 0
cwd = os.getcwd()
build_dir = tempfile.mkdtemp()
os.chdir(build_dir)
if build_system == 'None':
result += test_no_build_system(build_dir, language, source)
elif build_system == 'CMake':
result += test_cmake_build_system(build_dir, source)
else:
print('Unknown build system: ' + build_system)
result += 1
if emulator:
cmd = emulator
cmd += ' ' + os.path.join(build_dir, 'a.out')
print('Running ' + cmd + '...')
result += subprocess.call(cmd, shell=True)
os.chdir(cwd)
shutil.rmtree(build_dir)
return result
def test_build_system(test_dir, language, build_system, emulator):
result = 0
for source in glob.glob(os.path.join(test_dir, language, '*')):
result += test_source(source, language, build_system, emulator)
return result
def test_language(test_dir, language, build_systems, emulator):
result = 0
for build_system in build_systems:
result += test_build_system(test_dir, language, build_system, emulator)
return result
def run_tests(test_dir, languages=('C', 'C++'), build_systems=('None', 'CMake'),
emulator=None):
"""Run the tests found in test_dir where each directory corresponds to an
entry in languages. Every source within a language directory is built. The
output executable is also run with the emulator if provided."""
result = 0
for language in languages:
result += test_language(test_dir, language, build_systems, emulator)
return result
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Test the cross-compiler toolchain.')
parser.add_argument('--languages', nargs='+', default=['C', 'C++'],
help='Languages to test. Options: C C++')
parser.add_argument('--build-systems', nargs='+', default=['None', 'CMake'],
help='Build systems to test. Options: None CMake')
parser.add_argument('--emulator', '-e',
help='Emulator used to test generated executables')
args = parser.parse_args()
test_dir = os.path.dirname(os.path.abspath(__file__))
sys.exit(run_tests(test_dir,
languages=args.languages,
build_systems=args.build_systems,
emulator=args.emulator) != 0)