You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
6.1 KiB
221 lines
6.1 KiB
#!/usr/bin/env python3 |
|
""" |
|
Build script to compile StormLib and prepare it for packaging |
|
""" |
|
|
|
import os |
|
import sys |
|
import shutil |
|
import subprocess |
|
import platform |
|
from pathlib import Path |
|
|
|
|
|
def get_platform_info(): |
|
"""Get platform-specific information""" |
|
system = platform.system() |
|
machine = platform.machine() |
|
|
|
if system == 'Linux': |
|
lib_name = 'libstorm.so' |
|
lib_pattern = '*.so*' |
|
elif system == 'Darwin': |
|
lib_name = 'libstorm.dylib' |
|
lib_pattern = '*.dylib' |
|
elif system == 'Windows': |
|
lib_name = 'StormLib.dll' |
|
lib_pattern = '*.dll' |
|
else: |
|
raise RuntimeError(f"Unsupported platform: {system}") |
|
|
|
return { |
|
'system': system, |
|
'machine': machine, |
|
'lib_name': lib_name, |
|
'lib_pattern': lib_pattern |
|
} |
|
|
|
|
|
def check_command(cmd): |
|
"""Check if a command is available""" |
|
try: |
|
subprocess.run([cmd, '--version'], |
|
stdout=subprocess.DEVNULL, |
|
stderr=subprocess.DEVNULL, |
|
check=False) |
|
return True |
|
except FileNotFoundError: |
|
return False |
|
|
|
|
|
def clone_stormlib(build_dir): |
|
"""Clone StormLib repository""" |
|
stormlib_dir = build_dir / 'StormLib' |
|
|
|
if stormlib_dir.exists(): |
|
print("StormLib directory already exists, using existing clone") |
|
return stormlib_dir |
|
|
|
print("Cloning StormLib repository...") |
|
result = subprocess.run( |
|
['git', 'clone', 'https://github.com/ladislav-zezula/StormLib.git', str(stormlib_dir)], |
|
cwd=build_dir |
|
) |
|
|
|
if result.returncode != 0: |
|
raise RuntimeError("Failed to clone StormLib") |
|
|
|
return stormlib_dir |
|
|
|
|
|
def build_stormlib(stormlib_dir, platform_info): |
|
"""Build StormLib using CMake""" |
|
build_subdir = stormlib_dir / 'build' |
|
build_subdir.mkdir(exist_ok=True) |
|
|
|
print(f"Building StormLib for {platform_info['system']}...") |
|
|
|
# Configure with CMake |
|
cmake_args = [ |
|
'cmake', |
|
'..', |
|
'-DCMAKE_BUILD_TYPE=Release', |
|
'-DBUILD_SHARED_LIBS=ON', |
|
] |
|
|
|
# Add platform-specific flags |
|
if platform_info['system'] == 'Darwin': |
|
# macOS specific flags |
|
cmake_args.extend([ |
|
'-DCMAKE_OSX_DEPLOYMENT_TARGET=10.13', |
|
]) |
|
|
|
print("Configuring with CMake...") |
|
result = subprocess.run(cmake_args, cwd=build_subdir) |
|
if result.returncode != 0: |
|
raise RuntimeError("CMake configuration failed") |
|
|
|
# Build |
|
print("Compiling...") |
|
result = subprocess.run(['cmake', '--build', '.', '--config', 'Release'], cwd=build_subdir) |
|
if result.returncode != 0: |
|
raise RuntimeError("Compilation failed") |
|
|
|
return build_subdir |
|
|
|
|
|
def find_library(build_dir, platform_info): |
|
"""Find the compiled library file""" |
|
# Search in common locations |
|
search_paths = [ |
|
build_dir, |
|
build_dir / 'Release', |
|
build_dir / 'Debug', |
|
] |
|
|
|
for search_path in search_paths: |
|
if not search_path.exists(): |
|
continue |
|
|
|
# Look for library files |
|
for lib_file in search_path.glob(platform_info['lib_pattern']): |
|
if platform_info['lib_name'] in lib_file.name: |
|
return lib_file |
|
|
|
raise RuntimeError(f"Could not find compiled library: {platform_info['lib_name']}") |
|
|
|
|
|
def copy_library_to_package(lib_file, package_dir): |
|
"""Copy the compiled library to the package directory""" |
|
dest = package_dir / lib_file.name |
|
|
|
print(f"Copying {lib_file.name} to package directory...") |
|
shutil.copy2(lib_file, dest) |
|
|
|
# On Linux, also handle symlinks |
|
if platform.system() == 'Linux': |
|
# Create a simple versioned name link |
|
base_name = 'libstorm.so' |
|
if lib_file.name != base_name: |
|
symlink_dest = package_dir / base_name |
|
if symlink_dest.exists() or symlink_dest.is_symlink(): |
|
symlink_dest.unlink() |
|
os.symlink(lib_file.name, symlink_dest) |
|
print(f"Created symlink: {base_name} -> {lib_file.name}") |
|
|
|
return dest |
|
|
|
|
|
def main(): |
|
"""Main build function""" |
|
print("="*70) |
|
print("Building StormLib for PyStorm") |
|
print("="*70) |
|
print() |
|
|
|
# Get project root directory |
|
project_root = Path(__file__).parent.absolute() |
|
build_dir = project_root / 'build' |
|
package_dir = project_root / 'pystorm' |
|
|
|
# Check prerequisites |
|
print("Checking prerequisites...") |
|
if not check_command('git'): |
|
print("ERROR: git is not installed") |
|
return 1 |
|
|
|
if not check_command('cmake'): |
|
print("ERROR: cmake is not installed") |
|
print("Please install cmake:") |
|
print(" - Linux: sudo apt-get install cmake") |
|
print(" - macOS: brew install cmake") |
|
print(" - Windows: Download from https://cmake.org/") |
|
return 1 |
|
|
|
# Get platform info |
|
platform_info = get_platform_info() |
|
print(f"Platform: {platform_info['system']} ({platform_info['machine']})") |
|
print(f"Target library: {platform_info['lib_name']}") |
|
print() |
|
|
|
# Create build directory |
|
build_dir.mkdir(exist_ok=True) |
|
|
|
try: |
|
# Clone StormLib |
|
stormlib_dir = clone_stormlib(build_dir) |
|
|
|
# Build StormLib |
|
build_subdir = build_stormlib(stormlib_dir, platform_info) |
|
|
|
# Find the compiled library |
|
lib_file = find_library(build_subdir, platform_info) |
|
print(f"Found compiled library: {lib_file}") |
|
|
|
# Copy to package directory |
|
dest_file = copy_library_to_package(lib_file, package_dir) |
|
|
|
print() |
|
print("="*70) |
|
print("Build completed successfully!") |
|
print("="*70) |
|
print(f"Library installed to: {dest_file}") |
|
print() |
|
print("You can now install the package:") |
|
print(" pip install -e .") |
|
print() |
|
|
|
return 0 |
|
|
|
except Exception as e: |
|
print() |
|
print("="*70) |
|
print("Build failed!") |
|
print("="*70) |
|
print(f"Error: {e}") |
|
print() |
|
return 1 |
|
|
|
|
|
if __name__ == '__main__': |
|
sys.exit(main())
|
|
|