2016-03-04 03:04:20 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-03-06 20:16:49 +01:00
|
|
|
"""Test that the toolchain can build executables.
|
|
|
|
|
|
|
|
Multiple build tools and languages are supported. If an emulator is available,
|
|
|
|
its ability to run the generated executables is also tested.
|
|
|
|
"""
|
2016-03-04 03:04:20 +01:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
2016-04-13 03:32:13 +02:00
|
|
|
def test_none_build_system(build_dir, language, source, linker_flags, exe_suffix):
|
2016-03-07 03:38:38 +01:00
|
|
|
build_cmd = list()
|
2016-03-04 03:04:20 +01:00
|
|
|
if language == 'C':
|
|
|
|
compiler = os.getenv('CC', 'cc')
|
|
|
|
elif language == 'C++':
|
2016-03-07 03:38:38 +01:00
|
|
|
compiler = os.getenv('CXX', 'c++')
|
2016-03-04 03:04:20 +01:00
|
|
|
else:
|
|
|
|
print('Unknown language: ' + language)
|
|
|
|
return 1
|
2016-03-07 03:38:38 +01:00
|
|
|
build_cmd.append(compiler)
|
|
|
|
if linker_flags:
|
|
|
|
build_cmd.extend(linker_flags)
|
|
|
|
build_cmd.append(source)
|
|
|
|
|
2016-04-13 03:32:13 +02:00
|
|
|
build_cmd.append('-o')
|
|
|
|
build_cmd.append('a.out' + exe_suffix)
|
|
|
|
|
2016-03-04 03:04:20 +01:00
|
|
|
print('Building ' + source + ' by calling ' + compiler + '...')
|
2016-03-07 03:38:38 +01:00
|
|
|
print(' '.join(build_cmd))
|
2016-03-06 20:16:49 +01:00
|
|
|
sys.stdout.flush()
|
2016-03-07 03:38:38 +01:00
|
|
|
|
|
|
|
return subprocess.call(build_cmd)
|
2016-03-04 03:04:20 +01:00
|
|
|
|
|
|
|
|
2016-03-13 03:26:41 +01:00
|
|
|
def test_cmake_build_system(build_dir, language, source, emulator, linker_flags,
|
|
|
|
exe_suffix):
|
2016-03-04 03:04:20 +01:00
|
|
|
shutil.copy(source, build_dir)
|
2016-03-07 03:38:38 +01:00
|
|
|
print('Building ' + source + ' with CMake...')
|
|
|
|
|
2016-03-04 03:04:20 +01:00
|
|
|
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')
|
2016-03-06 20:16:49 +01:00
|
|
|
if emulator:
|
|
|
|
fp.write('enable_testing()\n')
|
2016-03-13 03:26:41 +01:00
|
|
|
fp.write('add_test(NAME emulator-in-cmake COMMAND a.out)\n')
|
2016-03-07 03:38:38 +01:00
|
|
|
|
2016-03-04 03:04:20 +01:00
|
|
|
os.mkdir('build')
|
|
|
|
os.chdir('build')
|
2016-03-07 03:38:38 +01:00
|
|
|
|
|
|
|
cmake_configuration_cmd = ['cmake', '..']
|
|
|
|
if linker_flags:
|
|
|
|
cmake_configuration_cmd.insert(1,
|
|
|
|
'-DCMAKE_EXE_LINKER_FLAGS="{0}"'.format(' '.join(linker_flags)))
|
|
|
|
print(' '.join(cmake_configuration_cmd))
|
2016-03-06 20:16:49 +01:00
|
|
|
sys.stdout.flush()
|
2016-03-07 03:38:38 +01:00
|
|
|
if subprocess.call(cmake_configuration_cmd):
|
2016-03-04 03:04:20 +01:00
|
|
|
return 1
|
2016-03-07 03:38:38 +01:00
|
|
|
if subprocess.call(['make', 'VERBOSE=1']):
|
2016-03-04 03:04:20 +01:00
|
|
|
return 1
|
2016-03-06 20:16:49 +01:00
|
|
|
if emulator:
|
|
|
|
if subprocess.call(['ctest']):
|
|
|
|
return 1
|
2016-03-13 03:26:41 +01:00
|
|
|
shutil.copy('a.out' + exe_suffix, build_dir)
|
2016-03-04 03:04:20 +01:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2016-03-13 03:26:41 +01:00
|
|
|
def test_source(source, language, build_system, emulator, linker_flags,
|
2016-04-14 01:50:00 +02:00
|
|
|
exe_suffix, debug):
|
2016-03-04 03:04:20 +01:00
|
|
|
result = 0
|
|
|
|
cwd = os.getcwd()
|
|
|
|
build_dir = tempfile.mkdtemp()
|
|
|
|
os.chdir(build_dir)
|
|
|
|
|
|
|
|
if build_system == 'None':
|
2016-04-13 03:32:13 +02:00
|
|
|
result += test_none_build_system(build_dir, language, source,
|
|
|
|
linker_flags, exe_suffix)
|
2016-03-04 03:04:20 +01:00
|
|
|
elif build_system == 'CMake':
|
2016-03-13 03:26:41 +01:00
|
|
|
result += test_cmake_build_system(build_dir, language, source, emulator,
|
|
|
|
linker_flags, exe_suffix)
|
2016-03-04 03:04:20 +01:00
|
|
|
else:
|
|
|
|
print('Unknown build system: ' + build_system)
|
|
|
|
result += 1
|
|
|
|
|
|
|
|
if emulator:
|
|
|
|
cmd = emulator
|
2016-03-13 03:26:41 +01:00
|
|
|
cmd += ' ' + os.path.join(build_dir, 'a.out' + exe_suffix)
|
2016-03-04 03:04:20 +01:00
|
|
|
print('Running ' + cmd + '...')
|
2016-03-06 20:16:49 +01:00
|
|
|
sys.stdout.flush()
|
2016-03-04 03:04:20 +01:00
|
|
|
result += subprocess.call(cmd, shell=True)
|
|
|
|
|
|
|
|
os.chdir(cwd)
|
2016-04-14 01:50:00 +02:00
|
|
|
if not debug:
|
|
|
|
print('Deleting temporary build directory ' + build_dir)
|
|
|
|
shutil.rmtree(build_dir)
|
|
|
|
else:
|
|
|
|
print('Keeping temporary build directory ' + build_dir)
|
|
|
|
|
2016-03-06 20:16:49 +01:00
|
|
|
sys.stdout.flush()
|
2016-03-04 03:04:20 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-03-13 03:26:41 +01:00
|
|
|
def test_build_system(test_dir, language, build_system, emulator, linker_flags,
|
2016-04-14 01:50:00 +02:00
|
|
|
exe_suffix, debug):
|
2016-03-06 20:16:49 +01:00
|
|
|
print('\n\n--------------------------------------------------------')
|
|
|
|
print('Testing ' + build_system + ' build system with the ' +
|
|
|
|
language + ' language\n')
|
|
|
|
sys.stdout.flush()
|
2016-03-04 03:04:20 +01:00
|
|
|
result = 0
|
|
|
|
for source in glob.glob(os.path.join(test_dir, language, '*')):
|
2016-03-13 03:26:41 +01:00
|
|
|
result += test_source(source, language, build_system, emulator,
|
2016-04-14 01:50:00 +02:00
|
|
|
linker_flags, exe_suffix, debug)
|
2016-03-04 03:04:20 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2016-03-13 03:26:41 +01:00
|
|
|
def test_language(test_dir, language, build_systems, emulator, linker_flags,
|
2016-04-14 01:50:00 +02:00
|
|
|
exe_suffix, debug):
|
2016-03-04 03:04:20 +01:00
|
|
|
result = 0
|
|
|
|
for build_system in build_systems:
|
2016-03-07 03:38:38 +01:00
|
|
|
result += test_build_system(test_dir,
|
|
|
|
language,
|
|
|
|
build_system,
|
|
|
|
emulator,
|
2016-03-13 03:26:41 +01:00
|
|
|
linker_flags,
|
2016-04-14 01:50:00 +02:00
|
|
|
exe_suffix,
|
|
|
|
debug)
|
2016-03-04 03:04:20 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def run_tests(test_dir, languages=('C', 'C++'), build_systems=('None', 'CMake'),
|
2016-04-14 01:50:00 +02:00
|
|
|
emulator=None, linker_flags=None, exe_suffix='', debug=False):
|
2016-03-04 03:04:20 +01:00
|
|
|
"""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:
|
2016-03-07 03:38:38 +01:00
|
|
|
result += test_language(test_dir,
|
|
|
|
language,
|
|
|
|
build_systems,
|
|
|
|
emulator,
|
2016-03-13 03:26:41 +01:00
|
|
|
linker_flags,
|
2016-04-14 01:50:00 +02:00
|
|
|
exe_suffix,
|
|
|
|
debug)
|
2016-03-04 03:04:20 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Test the cross-compiler toolchain.')
|
2016-03-07 03:38:38 +01:00
|
|
|
parser.add_argument('--languages', '-l', nargs='+', default=['C', 'C++'],
|
2016-03-04 03:04:20 +01:00
|
|
|
help='Languages to test. Options: C C++')
|
2016-03-07 03:38:38 +01:00
|
|
|
parser.add_argument('--build-systems', '-b', nargs='+', default=['None', 'CMake'],
|
2016-03-04 03:04:20 +01:00
|
|
|
help='Build systems to test. Options: None CMake')
|
|
|
|
parser.add_argument('--emulator', '-e',
|
|
|
|
help='Emulator used to test generated executables')
|
2016-03-07 03:38:38 +01:00
|
|
|
parser.add_argument('--linker-flags', '-w', nargs='+',
|
|
|
|
help='Extra compilation linker flags')
|
2016-03-13 03:26:41 +01:00
|
|
|
parser.add_argument('--exe-suffix', '-s', default='',
|
|
|
|
help='Suffix for generated executables')
|
2016-04-14 01:50:00 +02:00
|
|
|
parser.add_argument('--debug', '-d', action='store_true',
|
|
|
|
help='Do not remove temporary build directory')
|
2016-03-04 03:04:20 +01:00
|
|
|
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,
|
2016-03-07 03:38:38 +01:00
|
|
|
emulator=args.emulator,
|
2016-03-13 03:26:41 +01:00
|
|
|
linker_flags=args.linker_flags,
|
2016-04-14 01:50:00 +02:00
|
|
|
exe_suffix=args.exe_suffix,
|
|
|
|
debug=args.debug) != 0)
|