|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | + |
| 5 | +ROOT = os.path.normpath( |
| 6 | + os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, |
| 7 | + os.path.pardir)) |
| 8 | +r_package = os.path.join(ROOT, 'R-package') |
| 9 | + |
| 10 | + |
| 11 | +class DirectoryExcursion: |
| 12 | + def __init__(self, path: os.PathLike): |
| 13 | + self.path = path |
| 14 | + self.curdir = os.path.normpath(os.path.abspath(os.path.curdir)) |
| 15 | + |
| 16 | + def __enter__(self): |
| 17 | + os.chdir(self.path) |
| 18 | + |
| 19 | + def __exit__(self, *args): |
| 20 | + os.chdir(self.curdir) |
| 21 | + |
| 22 | + |
| 23 | +def get_mingw_bin(): |
| 24 | + return os.path.join('c:/rtools40/mingw64/', 'bin') |
| 25 | + |
| 26 | + |
| 27 | +def test_with_autotools(args): |
| 28 | + with DirectoryExcursion(r_package): |
| 29 | + if args.compiler == 'mingw': |
| 30 | + mingw_bin = get_mingw_bin() |
| 31 | + CXX = os.path.join(mingw_bin, 'g++.exe') |
| 32 | + CC = os.path.join(mingw_bin, 'gcc.exe') |
| 33 | + cmd = ['R.exe', 'CMD', 'INSTALL', str(os.path.curdir)] |
| 34 | + env = os.environ.copy() |
| 35 | + env.update({'CC': CC, 'CXX': CXX}) |
| 36 | + subprocess.check_call(cmd, env=env) |
| 37 | + elif args.compiler == 'msvc': |
| 38 | + cmd = ['R.exe', 'CMD', 'INSTALL', str(os.path.curdir)] |
| 39 | + env = os.environ.copy() |
| 40 | + # autotool favor mingw by default. |
| 41 | + env.update({'CC': 'cl.exe', 'CXX': 'cl.exe'}) |
| 42 | + subprocess.check_call(cmd, env=env) |
| 43 | + else: |
| 44 | + raise ValueError('Wrong compiler') |
| 45 | + subprocess.check_call([ |
| 46 | + 'R.exe', '-q', '-e', |
| 47 | + "library(testthat); setwd('tests'); source('testthat.R')" |
| 48 | + ]) |
| 49 | + |
| 50 | + |
| 51 | +def test_with_cmake(args): |
| 52 | + os.mkdir('build') |
| 53 | + with DirectoryExcursion('build'): |
| 54 | + if args.compiler == 'mingw': |
| 55 | + mingw_bin = get_mingw_bin() |
| 56 | + CXX = os.path.join(mingw_bin, 'g++.exe') |
| 57 | + CC = os.path.join(mingw_bin, 'gcc.exe') |
| 58 | + env = os.environ.copy() |
| 59 | + env.update({'CC': CC, 'CXX': CXX}) |
| 60 | + subprocess.check_call([ |
| 61 | + 'cmake', os.path.pardir, '-DUSE_OPENMP=ON', '-DR_LIB=ON', |
| 62 | + '-DCMAKE_CONFIGURATION_TYPES=Release', '-G', 'Unix Makefiles', |
| 63 | + ], |
| 64 | + env=env) |
| 65 | + subprocess.check_call(['make', '-j', 'install']) |
| 66 | + elif args.compiler == 'msvc': |
| 67 | + subprocess.check_call([ |
| 68 | + 'cmake', os.path.pardir, '-DUSE_OPENMP=ON', '-DR_LIB=ON', |
| 69 | + '-DCMAKE_CONFIGURATION_TYPES=Release', '-A', 'x64' |
| 70 | + ]) |
| 71 | + subprocess.check_call([ |
| 72 | + 'cmake', '--build', os.path.curdir, '--target', 'install', |
| 73 | + '--config', 'Release' |
| 74 | + ]) |
| 75 | + else: |
| 76 | + raise ValueError('Wrong compiler') |
| 77 | + with DirectoryExcursion(r_package): |
| 78 | + subprocess.check_call([ |
| 79 | + 'R.exe', '-q', '-e', |
| 80 | + "library(testthat); setwd('tests'); source('testthat.R')" |
| 81 | + ]) |
| 82 | + |
| 83 | + |
| 84 | +def main(args): |
| 85 | + if args.build_tool == 'autotools': |
| 86 | + test_with_autotools(args) |
| 87 | + else: |
| 88 | + test_with_cmake(args) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + parser = argparse.ArgumentParser() |
| 93 | + parser.add_argument('--compiler', |
| 94 | + type=str, |
| 95 | + choices=['mingw', 'msvc'], |
| 96 | + help='Compiler used for compiling CXX code.') |
| 97 | + parser.add_argument( |
| 98 | + '--build-tool', |
| 99 | + type=str, |
| 100 | + choices=['cmake', 'autotools'], |
| 101 | + help='Build tool for compiling CXX code and install R package.') |
| 102 | + args = parser.parse_args() |
| 103 | + main(args) |
0 commit comments