-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
280 lines (250 loc) · 10.4 KB
/
build.py
File metadata and controls
280 lines (250 loc) · 10.4 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
import subprocess, os, platform, argparse
def print_step(title):
print()
print("**************************************************")
print("**** {}".format(title))
print("**************************************************")
print()
def log_command(command):
command_line = " ".join('"{}"'.format(arg) for arg in command)
print("Executing: {}".format(command_line))
def log_check_call(command, shell=False):
log_command(command)
subprocess.check_call(command, shell=shell)
def log_call(command):
log_command(command)
subprocess.call(command)
def log_check_output(command):
log_command(command)
return subprocess.check_output(command)
def get_latest_vs_install_path():
program_files_x86 = os.environ["ProgramFiles(x86)"]
# print("program_files_x86=" + program_files_x86)
visual_studio_vswhere_path = os.path.join(program_files_x86, "Microsoft Visual Studio/Installer/vswhere.exe")
# print("visual_studio_vswhere_path=" + visual_studio_vswhere_path)
latest_vs_install_path = log_check_output([visual_studio_vswhere_path, "-latest", "-products", "*", "-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64", "-property", "installationPath"]).decode().strip()
# print("visual_studio_install_path=" + latest_vs_install_path)
return latest_vs_install_path
def get_latest_vc_path():
latest_vs_install_path = get_latest_vs_install_path()
default_vc_version_file_path = os.path.join(latest_vs_install_path, "VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt")
# print("default_vc_version_file_path=" + default_vc_version_file_path)
with open(default_vc_version_file_path, "r") as file:
default_vc_version = file.read().strip()
latest_vc_path = os.path.join(latest_vs_install_path, "VC/Tools/MSVC", default_vc_version, "bin/HostX64/x64/cl.exe").replace("\\", "/")
# print("latest_vc_path=" + latest_vc_path)
return latest_vc_path
def get_latest_vcvars64_path():
latest_vs_install_path = get_latest_vs_install_path()
latest_vsvars64_path = os.path.join(latest_vs_install_path, "VC/Auxiliary/Build/vcvars64.bat")
# print("latest_vsvars64_path=" + latest_vsvars64_path)
return latest_vsvars64_path
def update_env_with_vcvars64(config):
latest_vsvars64_path = get_latest_vcvars64_path()
vcvars_env_file_path = os.path.join(config.build_dir, "vcvars_env.txt")
# print("vcvars_env_file_path=" + vcvars_env_file_path)
log_check_call([latest_vsvars64_path, "&&", "set", ">", vcvars_env_file_path], shell=True)
with open(vcvars_env_file_path, "r") as file:
lines = file.readlines()
for line in lines:
stripped_line = line.strip()
equal_pos = stripped_line.find("=")
if equal_pos > 0:
env_var_name = stripped_line[:equal_pos]
env_var_value = stripped_line[equal_pos+1:]
# print("ENV: {} = {}".format(env_var_name, env_var_value))
os.environ[env_var_name] = env_var_value
def get_default_pkg_mgr():
return "conan"
def get_default_os():
return platform.system()
def get_default_arch():
return "x64"
def get_default_build_type():
return "Debug"
def get_cmake_toolchain_settings(config):
settings = []
if config.os == "Windows" and config.generator != "ninja":
settings += [
"-A", "Win32" if config.arch == "x86" else config.arch,
]
if config.toolchain_file != "":
settings += [
"-DCMAKE_TOOLCHAIN_FILE=" + config.toolchain_file,
]
return settings
def get_default_toolchain_file(config):
if config.pkg_mgr != "vcpkg":
return ""
try:
if platform.system() == "Windows":
result = log_check_output(["where", "vcpkg"]).decode()
result = os.path.dirname(result)
else:
results = log_check_output(["whereis", "vcpkg"]).decode()
result = results.split(" ")[-1].strip()
result = os.path.dirname(result)
except:
print("Warning: Could not infer vcpkg path - will default to '.'")
result = "."
result = os.path.join(result, "scripts/buildsystems/vcpkg.cmake")
return result
def get_vcpkg_triplet(config):
vcpkg_arch = config.arch
vcpkg_os = config.os.lower()
return vcpkg_arch + "-" + vcpkg_os
def get_vcpkg_thirdparties(config):
packages = [
"catch2",
"fmt",
"sfml",
"nlohmann-json",
"sdl2",
"sdl2-image",
"sdl2-mixer",
"sdl2-ttf",
"range-v3",
"opencv",
"restinio",
"http-parser",
"spdlog",
]
if not "APPVEYOR" in os.environ:
packages += [
"boost",
]
if config.use_llvm_package:
packages += [
"llvm",
]
return packages
def get_cmake_configure_command(config):
command = [
"cmake",
"-DCMAKE_VERBOSE_MAKEFILE=TRUE",
"-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE",
"-DCMAKE_CONFIGURATION_TYPES=" + config.build_type,
"-DCMAKE_BUILD_TYPE=" + config.build_type,
"-DEXP_PKG_MGR=" + config.pkg_mgr,
]
command += get_cmake_toolchain_settings(config)
if config.pkg_mgr == "vcpkg":
command += [
"-DVCPKG_TARGET_TRIPLET=" + get_vcpkg_triplet(config),
]
if "APPVEYOR" in os.environ:
boost_root = "C:/Libraries/boost_1_69_0"
command += [
"-DBOOST_ROOT={}".format(boost_root),
"-DBOOST_INCLUDEDIR={}/boost".format(boost_root),
"-DBOOST_LIBRARYDIR={}/lib64-msvc-15.0".format(boost_root),
]
if config.generator == "ninja":
if config.os == "Windows":
latest_vc_path = get_latest_vc_path()
command += [
"-DCMAKE_CXX_COMPILER=" + latest_vc_path,
"-DCMAKE_C_COMPILER=" + latest_vc_path,
]
command += [
"-G", "Ninja",
]
command += [
config.this_dir,
]
return command
def get_cmake_build_command(config):
command = [
"cmake",
"--build", config.build_dir,
"--config", config.build_type,
]
return command
def get_package_manager_command(config):
if config.pkg_mgr == "vcpkg":
return [
"vcpkg",
"install",
"--triplet", get_vcpkg_triplet(config)
] + get_vcpkg_thirdparties(config)
if config.pkg_mgr == "conan":
return [
"conan",
"install",
"--build=missing",
"-s", "os={}".format(config.os),
"-s", "arch={}".format("x86_64" if config.arch == "x64" else config.arch),
"-s", "build_type={}".format(config.build_type),
config.this_dir
]
return []
def initialize(config):
print_step("Initializing")
config.this_dir = os.path.dirname(os.path.realpath(__file__))
config.build_dir = os.path.join(config.this_dir, "out", "build", "py-{}-{}-{}-{}-{}-{}".format(
config.pkg_mgr,
config.generator,
("win" if config.os == "Windows" else "lin"),
config.arch,
("vc" if config.os == "Windows" else "gcc"),
("dbg" if config.build_type == "Debug" else "rel")))
config.toolchain_file = get_default_toolchain_file(config)
print("Package Manager: {}".format(config.pkg_mgr))
print("Os: {}".format(config.os))
print("Arch: {}".format(config.arch))
print("Build type: {}".format(config.build_type))
print("This dir: {}".format(config.this_dir))
print("Build dir: {}".format(config.build_dir))
print("Generator: {}".format(config.generator))
print("Toolchain file: {}".format(config.toolchain_file))
if config.pkg_mgr == "vcpkg":
log_check_call(["vcpkg", "version"])
elif config.pkg_mgr == "conan":
log_check_call(["conan", "--version"])
log_call(["conan", "remote", "add", "bincrafters", "https://api.bintray.com/conan/bincrafters/public-conan"])
# log_call(["conan", "remote", "add", "conan-community", "https://api.bintray.com/conan/conan-community/conan"])
log_check_call(["cmake", "--version"])
if not os.path.exists(config.build_dir):
os.makedirs(config.build_dir)
os.chdir(config.build_dir)
if config.generator == "ninja" and config.os == "Windows":
update_env_with_vcvars64(config)
def acquire(config):
print_step("Acquiring Third-parties")
package_manager_command = get_package_manager_command(config)
log_check_call(package_manager_command)
def configure(config):
print_step("Configuring Project")
cmake_configure_command = get_cmake_configure_command(config)
log_check_call(cmake_configure_command)
def build(config):
print_step("Building Project")
cmake_build_command = get_cmake_build_command(config)
log_check_call(cmake_build_command)
def test(config):
print_step("Testing Project")
cmake_test_command = ["ctest", "-C", config.build_type, "--verbose"]
log_check_call(cmake_test_command)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--acquire", help="acquire the third-parties", action="store_true")
parser.add_argument("--configure", help="configure cmake", action="store_true")
parser.add_argument("--build", help="build with cmake", action="store_true")
parser.add_argument("--test", help="test with ctest", action="store_true")
parser.add_argument("--pkg_mgr", help="set the package manager to use", type=str, choices=["vcpkg", "conan"], default=get_default_pkg_mgr())
parser.add_argument("--os", help="set the os", type=str, choices=["Linux", "Windows"], default=get_default_os())
parser.add_argument("--arch", help="set the arch", type=str, choices=["x86", "x64"], default=get_default_arch())
parser.add_argument("--build_type", help="set the build type", type=str, choices=["Debug", "Release"], default=get_default_build_type())
parser.add_argument("--generator", help="set the generator to use", type=str, choices=["default", "ninja"], default="default")
parser.add_argument("--use_llvm_package", help="use llvm package (not used by default, because very long to build)", action="store_true")
config = parser.parse_args()
initialize(config)
if config.acquire:
acquire(config)
if config.configure:
configure(config)
if config.build:
build(config)
if config.test:
test(config)