-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlib_utils_external.py
More file actions
228 lines (200 loc) · 8.05 KB
/
Copy pathlib_utils_external.py
File metadata and controls
228 lines (200 loc) · 8.05 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
#!/usr/bin/env python3
from SCons.Script.SConscript import SConsEnvironment
from SCons.Util import WhereIs
import SCons
import os, subprocess, shutil
# Get the name of the cmake build directory
def get_cmake_build_dir_name(env: SConsEnvironment) -> str:
if env.get("threads", True) and env["platform"] == "web":
return f'{env["platform"]}_{env["arch"]}_threads'
return f'cmake_build_out/{env["platform"]}_{env["arch"]}'
# Get a path to the build folder of the cmake library
def get_cmake_build_dir(env: SConsEnvironment, lib_path: str) -> str:
abs_scons_root = os.path.dirname(os.path.abspath(__file__))
# CMake doesn't seem to be able to work with the cache at all...
#
# scons_cache_path = os.environ.get("SCONS_CACHE")
# if scons_cache_path:
# return os.path.join(abs_scons_root, scons_cache_path, "cmake", lib_path, get_cmake_build_dir_name(env))
# else:
return os.path.join(abs_scons_root, lib_path, get_cmake_build_dir_name(env))
# Get a path to the output folder of the cmake library
def get_cmake_output_lib_dir(env: SConsEnvironment, lib_path: str) -> str:
return os.path.join(get_cmake_build_dir(env, lib_path), "RelWithDebInfo" if env["dev_build"] else "Release")
def contains_flag(list: list, flag_name: str) -> bool:
if any((flag_name in x for x in list)):
return True
else:
return False
def print_subprocess_result(result, prefix: str):
if result.stdout:
print(f"{prefix} output: {result.stdout}")
if result.stderr:
print(f"{prefix} errors: {result.stderr}")
def apply_git_patches(env: SConsEnvironment, patches_to_apply: list, working_dir: str):
for patch in patches_to_apply:
print()
try:
result = subprocess.run(
[
"git",
"apply",
f"--directory={working_dir}",
"--ignore-space-change",
"--ignore-whitespace",
"--reverse",
"--check",
patch,
],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
print(f"Already applied patch: '{patch}'")
continue
except subprocess.CalledProcessError as e:
print(f"Trying to apply a patch: '{patch}'")
try:
result = subprocess.run(
["git", "apply", f"--directory={working_dir}", "--ignore-space-change", "--ignore-whitespace", patch],
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
print_subprocess_result(result, "git")
print(f"Successfully applied patch: '{patch}'")
except subprocess.CalledProcessError as e:
print_subprocess_result(e, "git")
print("Please fix the patches, disable them, or try to git reset!")
return 1
print()
return 0
def cmake_build_project(
env: SConsEnvironment, lib_path: str, extra_args: list, extra_c_compiler_flags: dict = {}, vs_proj_version: str = ""
):
print()
arch = env["arch"]
platform = env["platform"]
platform_args = []
build_args = []
compiler_flags = extra_c_compiler_flags.get("c_flags", []).copy()
linker_flags = extra_c_compiler_flags.get("linker_flags", []).copy()
if platform == "windows":
msvc_runtime_type = "/MT" if env["use_static_cpp"] else "/MD"
arch_map = {"arm32": "ARM", "arm64": "ARM64", "x86_32": "Win32", "x86_64": "x64"}
scons_msbuild_map = {
"14.5": "Visual Studio 18 2026",
"14.3": "Visual Studio 17 2022",
"14.2": "Visual Studio 16 2019",
"14.1": "Visual Studio 15 2017",
}
vs_proj = vs_proj_version
if len(vs_proj_version) == 0:
vs_proj = scons_msbuild_map[env["MSVC_VERSION"]]
print(f'Selected Visual Studio version for the CMake project: "{vs_proj}"')
platform_args += [
"-G",
vs_proj,
"-A",
arch_map[arch],
# CMAKE_MSVC_RUNTIME_LIBRARY does not work with the Visual Studio project
# "-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded" + ("" if env["use_static_cpp"] else "DLL")]
f"-DCMAKE_C_FLAGS_DEBUG={msvc_runtime_type} /Zi /Ob0 /Od /RTC1",
f"-DCMAKE_C_FLAGS_MINSIZEREL={msvc_runtime_type} /O1 /Ob1 /DNDEBUG",
f"-DCMAKE_C_FLAGS_RELEASE={msvc_runtime_type} /O2 /Ob2 /DNDEBUG",
f"-DCMAKE_C_FLAGS_RELWITHDEBINFO={msvc_runtime_type} /Zi /O2 /Ob1 /DNDEBUG",
]
elif platform == "linux":
platform_args += [
"-G",
"Ninja Multi-Config",
]
elif platform == "macos":
platform_args += [
"-G",
"Ninja Multi-Config",
"-DCMAKE_SYSTEM_NAME=Darwin",
]
elif platform == "ios":
platform_args += [
"-G",
"Ninja Multi-Config",
"-DCMAKE_SYSTEM_NAME=iOS",
]
elif platform == "android":
arch_map = {"arm32": "armeabi-v7a", "arm64": "arm64-v8a", "x86_32": "x86", "x86_64": "x86_64"}
platform_args += [
"-G",
"Ninja Multi-Config",
f"-DANDROID_ABI={arch_map[arch]}",
]
if not contains_flag(extra_args, "-DCMAKE_TOOLCHAIN_FILE"):
platform_args += [
f'-DCMAKE_TOOLCHAIN_FILE={os.getenv("ANDROID_HOME")}/ndk/28.1.13356709/build/cmake/android.toolchain.cmake'
]
if not contains_flag(extra_args, "-DANDROID_PLATFORM"):
platform_args += [f'-DANDROID_PLATFORM={env.get("android_api_level", 21)}']
elif platform == "web":
platform_args += [
"-G",
"Ninja Multi-Config",
f'-DCMAKE_TOOLCHAIN_FILE={os.path.dirname(WhereIs("emcc"))}/cmake/Modules/Platform/Emscripten.cmake',
]
if env.get("threads", True):
compiler_flags += ["-sUSE_PTHREADS=1"]
linker_flags += ["-sUSE_PTHREADS=1"]
build_args += ["--config", "RelWithDebInfo" if env["dev_build"] else "Release"]
if len(compiler_flags):
platform_args += [
f'-DCMAKE_C_FLAGS={";".join(compiler_flags)}',
f'-DCMAKE_CXX_FLAGS={";".join(compiler_flags)}',
]
if len(linker_flags):
platform_args += [f'-DCMAKE_EXE_LINKER_FLAGS={";".join(linker_flags)}']
curdir = os.curdir
os.chdir(lib_path)
try:
build_dir = get_cmake_build_dir(env, lib_path)
def config():
proc_args = ["cmake", f"-B{build_dir}"] + platform_args + extra_args
print()
print("CMake configuration is run with the following arguments:", " ".join(proc_args))
print()
result = subprocess.run(
proc_args,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
print_subprocess_result(result, "cmake config")
try:
config()
except subprocess.CalledProcessError as e:
print_subprocess_result(e, "cmake config")
print(f"Attempt to clean up the build directory and reconfigure it...\n")
shutil.rmtree(build_dir)
config()
proc_args = ["cmake", "--build", build_dir] + build_args
print()
print("CMake build is run with arguments:", " ".join(proc_args))
print()
result = subprocess.run(
proc_args,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
print_subprocess_result(result, "cmake build")
print(f"Successfully build: {lib_path}")
except subprocess.CalledProcessError as e:
print_subprocess_result(e, "cmake")
print(f"cmake can't build {lib_path}. Please fix the errors!")
return 1
finally:
os.chdir(curdir)
print()
return 0