aboutsummaryrefslogtreecommitdiff
path: root/tools/win_build.py
blob: 6cb079b42c0daee29c454bcda72ae61a46e3fadb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import argparse
import os
import os.path
import shutil
import subprocess
import sys

parser = argparse.ArgumentParser()
parser.add_argument('command', choices=[
                    'configure', 'build', 'test'], nargs='?', default='test', help='specify command to execute')
parser.add_argument('--skip-install-packages', action="store_true",
                    help='skip using vcpkg to install package')
parser.add_argument('-a', '--arch', choices=['x86', 'x64'],
                    default='x64', help='specify target cpu architecture')
parser.add_argument('-c', '--config', choices=['Debug', 'Release'],
                    default='Debug', help='specify build configuration')
parser.add_argument('-d', '--work-dir', default='build',
                    help='specify working directory for building')
args = parser.parse_args()


project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
work_dir = os.path.join(project_root, args.work_dir)

vcpkg_root = os.path.join(project_root, 'vcpkg')

cmake_toolchain_path = os.path.join(
    vcpkg_root, 'scripts/buildsystems/vcpkg.cmake')

# this is not used when generator is Visual Studio


# def init_vc_environment(arch):
#     arch_bat_map = {
#         'x86': 'vcvarsamd64_x86',
#         'x64': 'vcvars64'
#     }
#     vars = subprocess.check_output(['C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\{}'.format(
#         arch_bat_map[arch]), '&&', 'set'], shell=True, text=True)
#     for var in vars.splitlines():
#         k, _, v = map(str.strip, var.strip().partition('='))
#         if k.startswith('?'):
#             continue
#         os.environ[k] = v

def install_packages():
    subprocess.check_call('vcpkg\\vcpkg.exe install gtest:{arch}-windows fmt:{arch}-windows ms-gsl:{arch}-windows'.format(arch=args.arch),
                          stdout=sys.stdout, stderr=sys.stderr)


def configure():
    generater_vs_arch_map = {
        'x86': 'Win32',
        'x64': 'x64'
    }

    # -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    subprocess.check_call('cmake -S . -B {build_dir} -G "Visual Studio 16 2019" -A {arch} -T host=x64 -DCMAKE_TOOLCHAIN_FILE={toolchain}'
                          .format(build_dir=work_dir, arch=generater_vs_arch_map[args.arch], toolchain=cmake_toolchain_path),
                          stdout=sys.stdout, stderr=sys.stderr)


def build():
    subprocess.check_call('cmake --build {build_dir} --target ALL_BUILD --config {config}'.format(build_dir=work_dir, config=args.config),
                          stdout=sys.stdout, stderr=sys.stderr)


def test():
    os.chdir(work_dir)
    subprocess.check_call('ctest')
    os.chdir(project_root)


os.chdir(project_root)

if not args.skip_install_packages:
    install_packages()

configure()

if args.command == 'build' or args.command == 'test':
    build()
    if args.command == 'test':
        test()