1*13fbcb42Sjoergimport argparse
2*13fbcb42Sjoergimport os
3*13fbcb42Sjoergimport sys
4*13fbcb42Sjoerg
5*13fbcb42Sjoergfrom subprocess import call, check_call, CalledProcessError
6*13fbcb42Sjoergfrom time import sleep
7*13fbcb42Sjoergfrom typing import List, Tuple
8*13fbcb42Sjoerg
9*13fbcb42Sjoerg
10*13fbcb42Sjoergdef main():
11*13fbcb42Sjoerg    settings, rest = parse_arguments()
12*13fbcb42Sjoerg    if settings.wait:
13*13fbcb42Sjoerg        wait()
14*13fbcb42Sjoerg    if settings.build_llvm or settings.build_llvm_only:
15*13fbcb42Sjoerg        build_llvm()
16*13fbcb42Sjoerg    if settings.build_llvm_only:
17*13fbcb42Sjoerg        return
18*13fbcb42Sjoerg    sys.exit(test(rest))
19*13fbcb42Sjoerg
20*13fbcb42Sjoerg
21*13fbcb42Sjoergdef wait():
22*13fbcb42Sjoerg    # It is an easy on CPU way of keeping the docker container running
23*13fbcb42Sjoerg    # while the user has a terminal session in that container.
24*13fbcb42Sjoerg    while True:
25*13fbcb42Sjoerg        sleep(3600)
26*13fbcb42Sjoerg
27*13fbcb42Sjoerg
28*13fbcb42Sjoergdef parse_arguments() -> Tuple[argparse.Namespace, List[str]]:
29*13fbcb42Sjoerg    parser = argparse.ArgumentParser()
30*13fbcb42Sjoerg    parser.add_argument('--wait', action='store_true')
31*13fbcb42Sjoerg    parser.add_argument('--build-llvm', action='store_true')
32*13fbcb42Sjoerg    parser.add_argument('--build-llvm-only', action='store_true')
33*13fbcb42Sjoerg    return parser.parse_known_args()
34*13fbcb42Sjoerg
35*13fbcb42Sjoerg
36*13fbcb42Sjoergdef build_llvm():
37*13fbcb42Sjoerg    os.chdir('/build')
38*13fbcb42Sjoerg    try:
39*13fbcb42Sjoerg        if is_cmake_needed():
40*13fbcb42Sjoerg            cmake()
41*13fbcb42Sjoerg        ninja()
42*13fbcb42Sjoerg    except CalledProcessError:
43*13fbcb42Sjoerg        print("Build failed!")
44*13fbcb42Sjoerg        sys.exit(1)
45*13fbcb42Sjoerg
46*13fbcb42Sjoerg
47*13fbcb42Sjoergdef is_cmake_needed():
48*13fbcb42Sjoerg    return "build.ninja" not in os.listdir()
49*13fbcb42Sjoerg
50*13fbcb42Sjoerg
51*13fbcb42SjoergCMAKE_COMMAND = "cmake -G Ninja -DCMAKE_BUILD_TYPE=Release " \
52*13fbcb42Sjoerg    "-DCMAKE_INSTALL_PREFIX=/analyzer -DLLVM_TARGETS_TO_BUILD=X86 " \
53*13fbcb42Sjoerg    "-DLLVM_ENABLE_PROJECTS=\"clang;openmp\" -DLLVM_BUILD_RUNTIME=OFF " \
54*13fbcb42Sjoerg    "-DLLVM_ENABLE_TERMINFO=OFF -DCLANG_ENABLE_ARCMT=OFF " \
55*13fbcb42Sjoerg    "-DCLANG_ENABLE_STATIC_ANALYZER=ON"
56*13fbcb42Sjoerg
57*13fbcb42Sjoerg
58*13fbcb42Sjoergdef cmake():
59*13fbcb42Sjoerg    check_call(CMAKE_COMMAND + ' /llvm-project/llvm', shell=True)
60*13fbcb42Sjoerg
61*13fbcb42Sjoerg
62*13fbcb42Sjoergdef ninja():
63*13fbcb42Sjoerg    check_call("ninja install", shell=True)
64*13fbcb42Sjoerg
65*13fbcb42Sjoerg
66*13fbcb42Sjoergdef test(args: List[str]) -> int:
67*13fbcb42Sjoerg    os.chdir("/projects")
68*13fbcb42Sjoerg    return call("/scripts/SATest.py " + " ".join(args), shell=True)
69*13fbcb42Sjoerg
70*13fbcb42Sjoerg
71*13fbcb42Sjoergif __name__ == '__main__':
72*13fbcb42Sjoerg    main()
73