-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild_native.py
More file actions
389 lines (308 loc) · 13.1 KB
/
build_native.py
File metadata and controls
389 lines (308 loc) · 13.1 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
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
#!/usr/bin/env python3
"""
Build Script for fastCrypter Native Libraries
This script automatically compiles C/C++ native libraries for the fastCrypter package
to provide maximum performance for cryptographic operations.
"""
import os
import sys
import subprocess
import platform
import shutil
from pathlib import Path
class NativeLibraryBuilder:
"""Builder for native C/C++ libraries."""
def __init__(self):
"""Initialize the builder."""
self.project_root = Path(__file__).parent
self.native_dir = self.project_root / 'fastCrypter' / 'native'
self.libs_dir = self.native_dir / 'libs'
self.platform = self._detect_platform()
self.platform_dir = self.libs_dir / self.platform
# Compiler settings
self.compilers = self._detect_compilers()
self.lib_extension = self._get_lib_extension()
def _detect_platform(self) -> str:
"""Detect current platform."""
system = platform.system()
if system == 'Windows':
return 'windows'
elif system == 'Darwin':
return 'macos'
else:
return 'linux'
def _detect_compilers(self) -> dict:
"""Detect available compilers."""
compilers = {}
# C compiler
for cc in ['gcc', 'clang', 'cc']:
if shutil.which(cc):
compilers['c'] = cc
break
# C++ compiler
for cxx in ['g++', 'clang++', 'c++']:
if shutil.which(cxx):
compilers['cxx'] = cxx
break
# Windows-specific
if self.platform == 'windows':
if shutil.which('x86_64-w64-mingw32-gcc'):
compilers['c'] = 'x86_64-w64-mingw32-gcc'
if shutil.which('x86_64-w64-mingw32-g++'):
compilers['cxx'] = 'x86_64-w64-mingw32-g++'
return compilers
def _get_lib_extension(self) -> str:
"""Get library extension for current platform."""
extensions = {
'windows': '.dll',
'linux': '.so',
'macos': '.dylib'
}
return extensions.get(self.platform, '.so')
def _get_compile_flags(self, language: str) -> list:
"""Get compilation flags for the language."""
base_flags = [
'-O3', # Maximum optimization
'-fPIC', # Position independent code
'-Wall', # All warnings
'-Wextra', # Extra warnings
'-march=native', # Optimize for current CPU
'-ffast-math', # Fast math operations
'-DNDEBUG', # Release mode
]
if language == 'cxx':
base_flags.append('-std=c++17')
# Platform-specific flags
if self.platform == 'linux':
base_flags.extend(['-DLINUX', '-flto', '-funroll-loops'])
elif self.platform == 'macos':
base_flags.extend(['-DMACOS', '-flto', '-funroll-loops'])
elif self.platform == 'windows':
base_flags.extend(['-DWINDOWS'])
return base_flags
def _get_link_flags(self) -> list:
"""Get linking flags."""
flags = ['-shared']
if self.platform in ['linux', 'macos']:
flags.extend(['-lm', '-flto'])
elif self.platform == 'windows':
flags.append('-Wl,--out-implib,lib$@.a')
return flags
def check_dependencies(self) -> bool:
"""Check if all required dependencies are available."""
print("Checking build dependencies...")
# Check compilers
if 'c' not in self.compilers:
print("C compiler not found. Please install gcc, clang, or equivalent.")
return False
if 'cxx' not in self.compilers:
print("C++ compiler not found. Please install g++, clang++, or equivalent.")
return False
print(f"C compiler: {self.compilers['c']}")
print(f"C++ compiler: {self.compilers['cxx']}")
print(f"Platform: {self.platform}")
print(f"Library extension: {self.lib_extension}")
return True
def create_directories(self):
"""Create necessary directories."""
print("Creating directories...")
self.platform_dir.mkdir(parents=True, exist_ok=True)
print(f"Created: {self.platform_dir}")
def compile_crypto_core(self) -> bool:
"""Compile crypto_core library."""
print("Compiling crypto_core library...")
source_file = self.native_dir / 'crypto_core.c'
output_file = self.platform_dir / f'libcrypto_core{self.lib_extension}'
if not source_file.exists():
print(f"Source file not found: {source_file}")
return False
# Build command
cmd = [
self.compilers['c'],
*self._get_compile_flags('c'),
*self._get_link_flags(),
'-o', str(output_file),
str(source_file)
]
try:
print(f" Command: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True, cwd=self.native_dir)
if result.returncode == 0:
print(f"crypto_core library built: {output_file}")
return True
else:
print(f"Compilation failed:")
print(f" stdout: {result.stdout}")
print(f" stderr: {result.stderr}")
return False
except Exception as e:
print(f"Compilation error: {e}")
return False
def compile_hash_algorithms(self) -> bool:
"""Compile hash_algorithms library."""
print("Compiling hash_algorithms library...")
source_file = self.native_dir / 'hash_algorithms.cpp'
output_file = self.platform_dir / f'libhash_algorithms{self.lib_extension}'
if not source_file.exists():
print(f"Source file not found: {source_file}")
return False
# Build command
cmd = [
self.compilers['cxx'],
*self._get_compile_flags('cxx'),
*self._get_link_flags(),
'-o', str(output_file),
str(source_file)
]
try:
print(f" Command: {' '.join(cmd)}")
result = subprocess.run(cmd, capture_output=True, text=True, cwd=self.native_dir)
if result.returncode == 0:
print(f"hash_algorithms library built: {output_file}")
return True
else:
print(f"Compilation failed:")
print(f" stdout: {result.stdout}")
print(f" stderr: {result.stderr}")
return False
except Exception as e:
print(f"Compilation error: {e}")
return False
def test_libraries(self) -> bool:
"""Test compiled libraries."""
print("Testing compiled libraries...")
try:
# Try to load the libraries using Python
sys.path.insert(0, str(self.project_root))
from fastCrypterer.native.native_loader import get_native_manager
manager = get_native_manager()
results = manager.load_all()
success = True
for lib_name, loaded in results.items():
if loaded:
print(f"{lib_name}: loaded successfully")
else:
print(f"{lib_name}: failed to load")
success = False
if success:
print("All libraries loaded successfully!")
# Run basic functionality tests
if manager.crypto_core:
try:
# Test XOR operation
test_data = b"Hello, World!"
test_key = b"key123"
result = manager.crypto_core.fast_xor(test_data, test_key)
print("XOR operation test passed")
# Test entropy calculation
entropy = manager.crypto_core.calculate_entropy(test_data)
print(f"Entropy calculation test passed (entropy: {entropy:.2f})")
except Exception as e:
print(f"Functionality test failed: {e}")
success = False
if manager.hash_algorithms:
try:
# Test SHA-256
test_data = b"Hello, World!"
hash_result = manager.hash_algorithms.fast_sha256(test_data)
print(f"hash_algorithms: SHA-256 test passed (hash: {hash_result[:8].hex()}...)")
# Test key generation
private_key, public_key = manager.hash_algorithms.generate_keypair()
print(f"hash_algorithms: Key generation test passed")
except Exception as e:
print(f"hash_algorithms: Functionality test failed: {e}")
success = False
return success
except Exception as e:
print(f"Library testing failed: {e}")
return False
def build_all(self) -> bool:
"""Build all native libraries."""
print("Building fastCrypter Native Libraries")
print("=" * 50)
# Check dependencies
if not self.check_dependencies():
return False
# Create directories
self.create_directories()
# Compile libraries
success = True
if not self.compile_crypto_core():
success = False
if not self.compile_hash_algorithms():
success = False
if success:
print("\nAll libraries compiled successfully!")
# Test libraries
if self.test_libraries():
print("\nBuild completed successfully!")
print(f"Libraries available in: {self.platform_dir}")
return True
else:
print("\nLibrary testing failed!")
return False
else:
print("\nBuild failed!")
return False
def clean(self):
"""Clean build artifacts."""
print("Cleaning build artifacts...")
if self.libs_dir.exists():
shutil.rmtree(self.libs_dir)
print(f"Removed: {self.libs_dir}")
else:
print("No build artifacts to clean")
def install_system_wide(self) -> bool:
"""Install libraries system-wide (optional)."""
print("Installing libraries system-wide...")
if self.platform == 'linux':
try:
for lib_file in self.platform_dir.glob('*.so'):
dest = Path('/usr/local/lib') / lib_file.name
subprocess.run(['sudo', 'cp', str(lib_file), str(dest)], check=True)
print(f"Installed: {dest}")
subprocess.run(['sudo', 'ldconfig'], check=True)
print("Updated library cache")
return True
except subprocess.CalledProcessError as e:
print(f"Installation failed: {e}")
return False
elif self.platform == 'macos':
try:
for lib_file in self.platform_dir.glob('*.dylib'):
dest = Path('/usr/local/lib') / lib_file.name
subprocess.run(['sudo', 'cp', str(lib_file), str(dest)], check=True)
print(f"Installed: {dest}")
return True
except subprocess.CalledProcessError as e:
print(f"Installation failed: {e}")
return False
else:
print("System-wide installation not supported on Windows")
return True
def main():
"""Main function."""
import argparse
parser = argparse.ArgumentParser(description='Build fastCrypter native libraries')
parser.add_argument('--clean', action='store_true', help='Clean build artifacts')
parser.add_argument('--install', action='store_true', help='Install libraries system-wide')
parser.add_argument('--test-only', action='store_true', help='Only test existing libraries')
args = parser.parse_args()
builder = NativeLibraryBuilder()
if args.clean:
builder.clean()
return 0
if args.test_only:
if builder.test_libraries():
return 0
else:
return 1
# Build libraries
if builder.build_all():
if args.install:
builder.install_system_wide()
return 0
else:
return 1
if __name__ == '__main__':
sys.exit(main())