-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
411 lines (316 loc) · 16.7 KB
/
build.py
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""
Copyright (c) 2024 DevAM. All Rights Reserved.
SPDX-License-Identifier: GPL-2.0-only
"""
from io import BytesIO
import os
import platform
import shutil
import subprocess
import sys
import traceback
from typing import *
from time import time
from datetime import timedelta
from zipfile import ZipFile
import requests
from tqdm import tqdm
system: str = platform.system().lower()
architecture: str = platform.machine().lower()
base_build_directory: str = f"""{os.getcwd()}/build"""
lemonshark_src_directory: str = f"""{os.getcwd()}/liblemonshark/src"""
lemonshark_build_directory: str = f"""{base_build_directory}/liblemonshark/{system}/{architecture}"""
lemonshark_binary_base_directory: str = f"""{lemonshark_build_directory}/bin"""
lemonshark_examples_directory: str = f"""{os.getcwd()}/liblemonshark/examples"""
lemonshark_examples_build_directory: str = f"""{base_build_directory}/examples/{system}/{architecture}"""
lemonshark_examples_binary_base_directory: str = f"""{lemonshark_examples_build_directory}/bin"""
lemonshark_host_src_directory: str = f"""{os.getcwd()}/liblemonshark/lemonshark_host"""
lemonshark_host_build_directory: str = f"""{base_build_directory}/lemonshark_host/{system}/{architecture}"""
lemonshark_host_binary_base_directory: str = f"""{lemonshark_host_build_directory}/bin"""
lemonshark_major_version: int = 0
lemonshark_minor_version: int = 9
lemonshark_patch_version: int = 0
wireshark_git_url: str = "https://gitlab.com/wireshark/wireshark.git"
wireshark_major_version: int = 4
wireshark_minor_version: int = 4
wireshark_patch_version: int = 3
wireshark_suffix_version: str = ""
wireshark_base_directory: str = f"""{os.getcwd()}/wireshark"""
wireshark_build_directory: str = f"""{base_build_directory}/wireshark/{system}/{architecture}"""
wireshark_binary_base_directory: str = f"""{wireshark_build_directory}/run"""
wireshark_libs_directory: str = f"""{base_build_directory}/wireshark-win64-libs-{wireshark_major_version}.{wireshark_minor_version}"""
python_base_directory: str = f"""{os.getcwd()}/python"""
python_build_directory: str = f"""{base_build_directory}/python"""
dotnet_base_directory: str = f"""{os.getcwd()}/dotnet"""
dotnet_build_directory: str = f"""{base_build_directory}/dotnet"""
win_flex_bison_url: str = "https://github.com/lexxmark/winflexbison/releases/download/v2.5.25/win_flex_bison-2.5.25.zip"
strawberry_perl_url: str = "https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/download/SP_54001_64bit_UCRT/strawberry-perl-5.40.0.1-64bit-portable.zip"
def get_msbuild_path():
path: str = ""
if system == "windows":
command: str = f"""\"%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe" -latest -requires Microsoft.Component.MSBuild -find MSBuild\\**\\Bin\\MSBuild.exe"""
print(f"""call '{command}'""")
raw_path: bytes = subprocess.check_output(command, shell=True)
path: str = raw_path.decode("UTF-8")
path = path.replace("\n", "").replace("\r", "")
if " " in path:
path = f"""\"{path}\""""
return path
def copy_file(source_path: str, target_path: str):
try:
if os.path.exists(source_path):
print(f"""Copy '{source_path}' to '{target_path}'""")
with open(source_path, "r+b") as input_file:
content: bytes = input_file.read()
input_file.close()
os.makedirs(os.path.dirname(target_path), exist_ok=True)
with open(target_path, "w+b") as output_file:
output_file.write(content)
output_file.flush()
output_file.close()
except Exception:
print(f"""### Error: {traceback.format_exc()}""")
abort: str = input(f"Continue anyway? (y/n)")
if abort.lower() != "y":
sys.exit()
def copy_directory(
source_directory: str,
target_directories: Union[List[str], str],
allowed_file_extensions: Union[List[str], None] = None,
recursively=True):
if isinstance(target_directories, str):
target_directories = [target_directories]
if os.path.exists(source_directory):
for base_directory, sub_directories, file_names in os.walk(source_directory):
if not recursively:
if base_directory != source_directory:
continue
for file_name in file_names:
if allowed_file_extensions is None or any([file_name.endswith(extension)for extension in allowed_file_extensions]):
relative_base_directory_path = os.path.relpath(base_directory, source_directory)
source_path: str = os.path.join(base_directory, file_name)
for target_directory in target_directories:
target_path: str = os.path.join(target_directory, relative_base_directory_path, file_name)
copy_file(source_path, target_path)
def clear_directory(directory: str):
if os.path.exists(directory):
shutil.rmtree(directory, ignore_errors=True)
def clone_git(git_url: str, target_directory: str, version: Union[str, None]):
start_time: float = time()
currend_working_directory: str = os.getcwd()
print(f"""### Get {git_url} source""")
try:
if not os.path.exists(target_directory):
os.makedirs(target_directory, exist_ok=True)
command: str = f"""git clone {git_url} {target_directory}"""
print(f"""call '{command}'""")
os.system(command)
if version is not None:
os.chdir(target_directory)
os.system(f"""git fetch --all --tags""")
os.system(f"""git checkout tags/{version}""")
os.system(f"""git submodule update --init --recursive""")
except Exception:
print(f"""### Error: {traceback.format_exc()}""")
abort: str = input(f"Continue anyway? (y/n)")
if abort.lower() != "y":
sys.exit()
os.chdir(currend_working_directory)
duration: float = time() - start_time
print(f"""### Done in {timedelta(seconds=duration)} s""")
def get_wireshark_dependencies():
start_time: float = time()
currend_working_directory: str = os.getcwd()
print(f"""### Get wireshark dependencies""")
if system == "windows":
try:
os.environ["PLATFORM"] = "Win64"
os.environ["WIRESHARK_BASE_DIR"] = wireshark_base_directory
os.environ["WIRESHARK_LIB_DIR"] = wireshark_libs_directory
# win_flex_bison
if not os.path.exists(f"""{wireshark_libs_directory}/win_flex_bison"""):
print(f"""### Get win flex bison""")
with requests.get(win_flex_bison_url, stream=True) as win_flex_bison_request_response:
size = int(win_flex_bison_request_response.headers.get('content-length', 0))
with BytesIO() as file:
with tqdm(desc=win_flex_bison_url, total=size, unit='iB', unit_scale=True, unit_divisor=4096) as bar:
for data in win_flex_bison_request_response.iter_content(chunk_size=4096):
size = file.write(data)
bar.update(size)
with ZipFile(file) as win_flex_bison_zip_file:
win_flex_bison_zip_file.extractall(f"""{wireshark_libs_directory}/win_flex_bison""")
os.environ["PATH"] += f""";{wireshark_libs_directory}/win_flex_bison"""
# strawberry_perl
if not os.path.exists(f"""{wireshark_libs_directory}/strawberry_perl"""):
print(f"""### Get strawberry perl""")
with requests.get(strawberry_perl_url, stream=True) as strawberry_perl_request_response:
size = int(strawberry_perl_request_response.headers.get('content-length', 0))
with BytesIO() as file:
with tqdm(desc=strawberry_perl_url, total=size, unit='iB', unit_scale=True, unit_divisor=4096) as bar:
for data in strawberry_perl_request_response.iter_content(chunk_size=4096):
size = file.write(data)
bar.update(size)
with ZipFile(file) as strawberry_perl_zip_file:
strawberry_perl_zip_file.extractall(f"""{wireshark_libs_directory}/strawberry_perl""")
os.environ["PATH"] += f""";{wireshark_libs_directory}/strawberry_perl/perl/bin"""
except Exception:
print(f"""### Error: {traceback.format_exc()}""")
abort: str = input(f"Continue anyway? (y/n)")
if abort.lower() != "y":
sys.exit()
os.chdir(currend_working_directory)
duration: float = time() - start_time
print(f"""### Done in {timedelta(seconds=duration)} s""")
def integrate_lemonshark():
start_time: float = time()
currend_working_directory: str = os.getcwd()
print(f"""### Integrate lemonshark""")
try:
cmake_file_content: str = ""
cmake_file_content += f"""
# BEGIN LEMONSHARK
set(LEMONSHARK_MAJOR_VERSION {lemonshark_major_version})
set(LEMONSHARK_MINOR_VERSION {lemonshark_minor_version})
set(LEMONSHARK_PATCH_VERSION {lemonshark_patch_version})
set(LEMONSHARK_SRC_DIRECTORY {lemonshark_src_directory})
set(LEMONSHARK_BUILD_DIRECTORY {lemonshark_build_directory})
set(LEMONSHARK_BINARY_BASE_DIRECTORY {lemonshark_binary_base_directory})
set(LEMONSHARK_EXAMPLES_DIRECTORY {lemonshark_examples_directory})
set(LEMONSHARK_EXAMPLES_BUILD_DIRECTORY {lemonshark_examples_build_directory})
set(LEMONSHARK_EXAMPLES_BINARY_BASE_DIRECTORY {lemonshark_examples_binary_base_directory})
set(LEMONSHARK_HOST_SRC_DIRECTORY {lemonshark_host_src_directory})
set(LEMONSHARK_HOST_BUILD_DIRECTORY {lemonshark_host_build_directory})
set(LEMONSHARK_HOST_BINARY_BASE_DIRECTORY {lemonshark_host_binary_base_directory})
set(WIRESHARK_MAJOR_VERSION {wireshark_major_version})
set(WIRESHARK_MINOR_VERSION {wireshark_minor_version})
set(WIRESHARK_PATCH_VERSION {wireshark_patch_version})
set(WIRESHARK_BASE_DIRECTORY {wireshark_base_directory})
set(WIRESHARK_BINARY_BASE_DIRECTORY {wireshark_binary_base_directory})
set(PYTHON_BASE_DIRECTORY {python_base_directory})
set(PYTHON_BUILD_DIRECTORY {python_build_directory})
set(DOTNET_BASE_DIRECTORY {dotnet_base_directory})
set(DOTNET_BUILD_DIRECTORY {dotnet_build_directory})
set(LEMONSHARK_SYSTEM {system})
set(LEMONSHARK_ARCHITECTURE {architecture})
"""
if system == "windows":
cmake_file_content += f"""
set(ENV{{PLATFORM}} "Win64")
set(ENV{{WIRESHARK_BASE_DIR}} {wireshark_base_directory})
set(ENV{{WIRESHARK_LIB_DIR}} {wireshark_libs_directory})
"""
if "WIRESHARK_QT6_PREFIX_PATH" in os.environ and os.environ["WIRESHARK_QT6_PREFIX_PATH"] != "":
cmake_file_content += f"""
set(BUILD_wireshark ON)
"""
else:
cmake_file_content += f"""set(BUILD_wireshark OFF)\n"""
cmake_file_content = cmake_file_content.replace("\\", "/")
cmake_file_content = cmake_file_content.replace(" ", "")
cmake_file_content += f"""# END LEMONSHARK\n"""
if os.path.exists(f"""{wireshark_base_directory}/CMakeLists.backup"""):
with open(f"""{wireshark_base_directory}/CMakeLists.backup""", "r", encoding="UTF-8",) as cmake_file:
cmake_file_content += cmake_file.read()
cmake_file.close()
else:
with open(f"""{wireshark_base_directory}/CMakeLists.txt""", "r", encoding="UTF-8", ) as cmake_file:
cmake_file_content_backup: str = cmake_file.read()
cmake_file.close()
with open(f"""{wireshark_base_directory}/CMakeLists.backup""", "w", encoding="UTF-8", ) as cmake_file:
cmake_file.write(cmake_file_content_backup)
cmake_file.flush()
cmake_file.close()
cmake_file_content += cmake_file_content_backup
cmake_file_content += """
# BEGIN LEMONSHARK
add_subdirectory(${LEMONSHARK_SRC_DIRECTORY} ${LEMONSHARK_BUILD_DIRECTORY})
add_dependencies(liblemonshark epan)
add_dependencies(liblemonshark wsutil)
add_dependencies(liblemonshark wiretap)
add_subdirectory(${LEMONSHARK_EXAMPLES_DIRECTORY}/epan_packet ${LEMONSHARK_EXAMPLES_BUILD_DIRECTORY}/epan_packet)
add_dependencies(epan_packet_demo liblemonshark)
add_subdirectory(${LEMONSHARK_EXAMPLES_DIRECTORY}/field_description ${LEMONSHARK_EXAMPLES_BUILD_DIRECTORY}/field_description)
add_dependencies(field_description_demo liblemonshark)
add_subdirectory(${LEMONSHARK_EXAMPLES_DIRECTORY}/filter ${LEMONSHARK_EXAMPLES_BUILD_DIRECTORY}/filter)
add_dependencies(filter_demo liblemonshark)
add_subdirectory(${LEMONSHARK_EXAMPLES_DIRECTORY}/packet ${LEMONSHARK_EXAMPLES_BUILD_DIRECTORY}/packet)
add_dependencies(packet_demo liblemonshark)
add_subdirectory(${LEMONSHARK_HOST_SRC_DIRECTORY} ${LEMONSHARK_HOST_BUILD_DIRECTORY})
add_dependencies(lemonshark_host liblemonshark)
# END LEMONSHARK
""".replace(
" ", ""
)
with open(f"""{wireshark_base_directory}/CMakeLists.txt""", "w", encoding="UTF-8") as cmake_file:
cmake_file.write(cmake_file_content)
cmake_file.flush()
cmake_file.close()
except Exception:
print(f"""### Error: {traceback.format_exc()}""")
abort: str = input(f"Continue anyway? (y/n)")
if abort.lower() != "y":
sys.exit()
os.chdir(currend_working_directory)
duration: float = time() - start_time
print(f"""### Done in {timedelta(seconds=duration)} s""")
def generate_wireshark():
start_time: float = time()
currend_working_directory: str = os.getcwd()
print(f"""### Generate wireshark""")
try:
os.makedirs(wireshark_build_directory, exist_ok=True)
os.chdir(wireshark_build_directory)
cmake_command: str = f"""cmake {wireshark_base_directory} """
if system == "windows":
cmake_command += """ -G "Visual Studio 17 2022" -A x64"""
cmake_command = cmake_command.replace("\\", "/")
cmake_command += " -DCMAKE_BUILD_TYPE=RelWithDebInfo"
print(f"""call '{cmake_command}'""")
os.system(cmake_command)
except Exception:
print(f"""### Error: {traceback.format_exc()}""")
abort: str = input(f"Continue anyway? (y/n)")
if abort.lower() != "y":
sys.exit()
os.chdir(currend_working_directory)
duration: float = time() - start_time
print(f"""### Done in {timedelta(seconds=duration)} s""")
def build_wireshark():
start_time: float = time()
currend_working_directory: str = os.getcwd()
print(f"""### Build wireshark""")
try:
os.chdir(wireshark_build_directory)
if system == "windows":
msbuild_path: str = get_msbuild_path()
command: str = f"""{msbuild_path} /m /p:Configuration=RelWithDebInfo /p:Platform=x64 Wireshark.sln"""
print(f"""call '{command}'""")
os.system(command)
elif system == "linux":
command: str = f"""make -j{os.cpu_count()} CXXFLAGS='-fPIC'"""
print(f"""call '{command}'""")
os.system(command)
except Exception:
print(f"""### Error: {traceback.format_exc()}""")
abort: str = input(f"Continue anyway? (y/n)")
if abort.lower() != "y":
sys.exit()
os.chdir(currend_working_directory)
duration: float = time() - start_time
print(f"""### Done in {timedelta(seconds=duration)} s""")
def main():
if " " in os.getcwd():
raise Exception(f"Path of current working directory must not contain whitespaces. ({os.getcwd()})")
start_time: float = time()
current_working_directory: str = os.getcwd()
wireshark_tag: str = f"v{wireshark_major_version}.{wireshark_minor_version}.{wireshark_patch_version}{wireshark_suffix_version}"
clone_git(wireshark_git_url, wireshark_base_directory, wireshark_tag)
get_wireshark_dependencies()
integrate_lemonshark()
generate_wireshark()
build_wireshark()
os.chdir(current_working_directory)
duration: float = time() - start_time
print(f"""### Done in {timedelta(seconds=duration)} s""")
if __name__ == "__main__":
main()