Implemented platform.py
This commit is contained in:
parent
1a3bfad739
commit
9ea4edcc77
|
|
@ -0,0 +1,161 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Platform detection and management for the build system.
|
||||||
|
Provides utilities to detect the current operating system and
|
||||||
|
map target names to platform identifiers.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import platform
|
||||||
|
import os
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
class Platform(Enum):
|
||||||
|
"""Supported platforms."""
|
||||||
|
LINUX = "linux"
|
||||||
|
WINDOWS = "windows"
|
||||||
|
MACOS = "macos"
|
||||||
|
UNKNOWN = "unknown"
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
|
def detect_platform() -> Platform:
|
||||||
|
"""
|
||||||
|
Detect the current operating system.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Platform enum representing the current OS
|
||||||
|
"""
|
||||||
|
system = platform.system()
|
||||||
|
|
||||||
|
if system == "Linux":
|
||||||
|
return Platform.LINUX
|
||||||
|
elif system == "Windows":
|
||||||
|
return Platform.WINDOWS
|
||||||
|
elif system == "Darwin":
|
||||||
|
return Platform.MACOS
|
||||||
|
else:
|
||||||
|
return Platform.UNKNOWN
|
||||||
|
|
||||||
|
|
||||||
|
def get_platform_name() -> str:
|
||||||
|
"""
|
||||||
|
Get the current platform name as a string.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Platform name (e.g., "linux", "windows", "macos")
|
||||||
|
"""
|
||||||
|
return detect_platform().value
|
||||||
|
|
||||||
|
|
||||||
|
def is_windows() -> bool:
|
||||||
|
"""Check if running on Windows."""
|
||||||
|
return detect_platform() == Platform.WINDOWS or os.name == "nt"
|
||||||
|
|
||||||
|
|
||||||
|
def is_linux() -> bool:
|
||||||
|
"""Check if running on Linux."""
|
||||||
|
return detect_platform() == Platform.LINUX
|
||||||
|
|
||||||
|
|
||||||
|
def is_macos() -> bool:
|
||||||
|
"""Check if running on macOS."""
|
||||||
|
return detect_platform() == Platform.MACOS
|
||||||
|
|
||||||
|
|
||||||
|
def is_unix() -> bool:
|
||||||
|
"""Check if running on a Unix-like system (Linux or macOS)."""
|
||||||
|
return is_linux() or is_macos()
|
||||||
|
|
||||||
|
|
||||||
|
def resolve_target(target: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
Resolve a target string to a platform name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target: Target name (e.g., "self", "linux", "windows", "rp2040")
|
||||||
|
If None or "self", returns the current platform
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Resolved target name as a string
|
||||||
|
"""
|
||||||
|
if target is None or target == "self":
|
||||||
|
return get_platform_name()
|
||||||
|
|
||||||
|
return target.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def get_executable_extension(target: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
Get the executable file extension for a target platform.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target: Target platform name (defaults to current platform)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Executable extension (e.g., ".exe" for Windows, "" for Unix)
|
||||||
|
"""
|
||||||
|
resolved = resolve_target(target)
|
||||||
|
|
||||||
|
if resolved == "windows":
|
||||||
|
return ".exe"
|
||||||
|
else:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def get_shared_library_extension(target: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
Get the shared library extension for a target platform.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target: Target platform name (defaults to current platform)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Shared library extension (e.g., ".so", ".dll", ".dylib")
|
||||||
|
"""
|
||||||
|
resolved = resolve_target(target)
|
||||||
|
|
||||||
|
if resolved == "windows":
|
||||||
|
return ".dll"
|
||||||
|
elif resolved == "macos":
|
||||||
|
return ".dylib"
|
||||||
|
else:
|
||||||
|
return ".so"
|
||||||
|
|
||||||
|
|
||||||
|
def get_static_library_extension(target: Optional[str] = None) -> str:
|
||||||
|
"""
|
||||||
|
Get the static library extension for a target platform.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
target: Target platform name (defaults to current platform)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Static library extension (e.g., ".a" or ".lib")
|
||||||
|
"""
|
||||||
|
resolved = resolve_target(target)
|
||||||
|
|
||||||
|
if resolved == "windows":
|
||||||
|
return ".lib"
|
||||||
|
else:
|
||||||
|
return ".a"
|
||||||
|
|
||||||
|
|
||||||
|
# Global cached platform
|
||||||
|
_current_platform: Optional[Platform] = None
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_platform() -> Platform:
|
||||||
|
"""
|
||||||
|
Get the current platform (cached).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Platform enum for the current OS
|
||||||
|
"""
|
||||||
|
global _current_platform
|
||||||
|
if _current_platform is None:
|
||||||
|
_current_platform = detect_platform()
|
||||||
|
return _current_platform
|
||||||
Loading…
Reference in New Issue