From 9ea4edcc77c7502fa29b0e414dfd19a6d9932871 Mon Sep 17 00:00:00 2001 From: Kyler Date: Fri, 19 Dec 2025 14:07:45 -0700 Subject: [PATCH] Implemented platform.py --- SLS_C/build_system/platform.py | 161 +++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) diff --git a/SLS_C/build_system/platform.py b/SLS_C/build_system/platform.py index e69de29..a08d8a0 100644 --- a/SLS_C/build_system/platform.py +++ b/SLS_C/build_system/platform.py @@ -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