-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmakegen.py
More file actions
109 lines (88 loc) · 3.76 KB
/
cmakegen.py
File metadata and controls
109 lines (88 loc) · 3.76 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import argparse
import os
import platform
import sys
import subprocess
from abc import ABC, abstractmethod
def run_cmd(command):
subprocess.run(command, shell=True)
def change_working_dir_to(path):
os.chdir(path)
class IBuilder(ABC):
'''
Abstract class to generate a project for some IDEs.
'''
def __init__(self, generator, build_type, build_tests, build_samples, enable_coverage):
self.build_dir = 'build'
self.generator = generator
self.build_type = build_type
self.build_tests = build_tests
self.build_samples = build_samples
self.enable_coverage = enable_coverage
@abstractmethod
def create_build_dir(self):
pass
@abstractmethod
def generate_project(self):
pass
def get_cmake_generate_command(self):
return 'cmake -G "{}" .. -DCMAKE_BUILD_TYPE={} -DBUILD_TESTS={} -DBUILD_SAMPLES={} -DENABLE_COVERAGE={}'.format(
self.generator, self.build_type, self.build_tests, self.build_samples, self.enable_coverage)
class XcodeBuilder(IBuilder):
'''
Class to generate a project for Xcode IDE.
'''
def __init__(self, build_type, build_tests, build_samples, enable_coverage):
super().__init__('Xcode', build_type, build_tests, build_samples, enable_coverage)
def create_build_dir(self):
run_cmd('mkdir -p {}'.format(self.build_dir))
change_working_dir_to(self.build_dir)
def generate_project(self):
run_cmd(super().get_cmake_generate_command())
class VisualStudioBuilder(IBuilder):
'''
Class to generate a project for Visual Studio IDE.
'''
def __init__(self, build_type, build_tests, build_samples, enable_coverage):
super().__init__('Visual Studio 17 2022', build_type, build_tests, build_samples, enable_coverage)
self.platform = 'x64'
def create_build_dir(self):
run_cmd('if not exist {0} mkdir {0} && pushd {0}'.format(self.build_dir))
change_working_dir_to(self.build_dir)
def generate_project(self):
run_cmd('{} -A {}'.format(super().get_cmake_generate_command(), self.platform))
class UnixBuilder(IBuilder):
'''
Class to generate a project for QtCreator IDE.
'''
def __init__(self, build_type, build_tests, build_samples, enable_coverage):
super().__init__('CodeBlocks - Unix Makefiles', build_type, build_tests, build_samples, enable_coverage)
def create_build_dir(self):
run_cmd('mkdir -p {}'.format(self.build_dir))
change_working_dir_to(self.build_dir)
def generate_project(self):
run_cmd(super().get_cmake_generate_command())
def create_builder(build_type, build_tests, build_samples, enable_coverage):
'''
Factory method that makes a project builder for the specific platform.
'''
platform_name = platform.system()
if platform_name == 'Darwin':
return XcodeBuilder(build_type, build_tests, build_samples, enable_coverage)
elif platform_name == 'Windows':
return VisualStudioBuilder(build_type, build_tests, build_samples, enable_coverage)
elif platform_name == 'Linux':
return UnixBuilder(build_type, build_tests, build_samples, enable_coverage)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--build-type', '-b', dest='build_type', default='Debug')
parser.add_argument('--build-tests', '-t', action='store_true')
parser.add_argument('--build-samples', '-s', action='store_true')
parser.add_argument('--enable-coverage', '-c', action='store_true')
args = parser.parse_args()
builder = create_builder(args.build_type, args.build_tests, args.build_samples, args.enable_coverage)
builder.create_build_dir()
builder.generate_project()
return 0
if __name__ == '__main__':
sys.exit(main())