Meta: Extract platform detection utilities to their own file

This will be needed by other scripts.

To do so, this patch gives up on the importlib method of importing
packages. I tried extracting this helper to e.g. __init__.py, but the
python runtime was unable to find the imported symbols.
This commit is contained in:
Timothy Flynn 2025-05-27 16:14:30 -04:00 committed by Tim Flynn
commit a76e880dfe
Notes: github-actions[bot] 2025-05-29 23:35:33 +00:00
2 changed files with 49 additions and 61 deletions

42
Meta/host_platform.py Normal file
View file

@ -0,0 +1,42 @@
# Copyright (c) 2025, ayeteadoe <ayeteadoe@gmail.com>
# Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
#
# SPDX-License-Identifier: BSD-2-Clause
import enum
import platform
import sys
class HostArchitecture(enum.IntEnum):
x86_64 = enum.auto()
AArch64 = enum.auto()
class HostSystem(enum.IntEnum):
Linux = enum.auto()
macOS = enum.auto()
Windows = enum.auto()
class Platform:
def __init__(self):
self.system = platform.system()
if self.system == "Windows":
self.host_system = HostSystem.Windows
elif self.system == "Darwin":
self.host_system = HostSystem.macOS
elif self.system == "Linux":
self.host_system = HostSystem.Linux
else:
print(f"Unsupported host system {self.system}", file=sys.stderr)
sys.exit(1)
self.architecture = platform.machine().lower()
if self.architecture in ("x86_64", "amd64"):
self.host_architecture = HostArchitecture.x86_64
elif self.architecture in ("aarch64", "arm64"):
self.host_architecture = HostArchitecture.AArch64
else:
print(f"Unsupported host architecture {self.architecture}", file=sys.stderr)
sys.exit(1)

View file

@ -6,7 +6,6 @@
# SPDX-License-Identifier: BSD-2-Clause # SPDX-License-Identifier: BSD-2-Clause
import argparse import argparse
import importlib.util
import multiprocessing import multiprocessing
import os import os
import platform import platform
@ -14,73 +13,20 @@ import re
import shutil import shutil
import subprocess import subprocess
import sys import sys
import types
from enum import IntEnum
from pathlib import Path from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent))
def import_module(module_path: Path) -> types.ModuleType: from Meta.host_platform import HostArchitecture
spec = importlib.util.spec_from_file_location(module_path.stem, module_path) from Meta.host_platform import HostSystem
if not spec or not spec.loader: from Meta.host_platform import Platform
raise ModuleNotFoundError(f"Could not find module {module_path}") from Toolchain.BuildVcpkg import build_vcpkg
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
META_PATH = Path(__file__).parent
TOOLCHAIN_PATH = META_PATH.parent / "Toolchain"
BuildVcpkg = import_module(TOOLCHAIN_PATH / "BuildVcpkg.py")
class HostArchitecture(IntEnum):
Unsupported = 0
x86_64 = 1
AArch64 = 2
class HostSystem(IntEnum):
Unsupported = 0
Linux = 1
macOS = 2
Windows = 3
class Platform:
def __init__(self):
self.system = platform.system()
if self.system == "Windows":
self.host_system = HostSystem.Windows
elif self.system == "Darwin":
self.host_system = HostSystem.macOS
elif self.system == "Linux":
self.host_system = HostSystem.Linux
else:
self.host_system = HostSystem.Unsupported
self.architecture = platform.machine().lower()
if self.architecture in ("x86_64", "amd64"):
self.host_architecture = HostArchitecture.x86_64
elif self.architecture in ("aarch64", "arm64"):
self.host_architecture = HostArchitecture.AArch64
else:
self.host_architecture = HostArchitecture.Unsupported
def main(): def main():
platform = Platform() platform = Platform()
if platform.host_system == HostSystem.Unsupported:
print(f"Unsupported host system {platform.system}", file=sys.stderr)
sys.exit(1)
if platform.host_architecture == HostArchitecture.Unsupported:
print(f"Unsupported host architecture {platform.architecture}", file=sys.stderr)
sys.exit(1)
parser = argparse.ArgumentParser(description="Ladybird") parser = argparse.ArgumentParser(description="Ladybird")
subparsers = parser.add_subparsers(dest="command") subparsers = parser.add_subparsers(dest="command")
@ -225,7 +171,7 @@ def main():
build_main(build_dir, "install", args.args) build_main(build_dir, "install", args.args)
elif args.command == "vcpkg": elif args.command == "vcpkg":
configure_build_env(args.preset, args.cc, args.cxx) configure_build_env(args.preset, args.cc, args.cxx)
BuildVcpkg.build_vcpkg() build_vcpkg()
elif args.command == "clean": elif args.command == "clean":
clean_main(args.preset, args.cc, args.cxx) clean_main(args.preset, args.cc, args.cxx)
elif args.command == "rebuild": elif args.command == "rebuild":
@ -240,7 +186,7 @@ def main():
def configure_main(platform: Platform, preset: str, cc: str, cxx: str) -> Path: def configure_main(platform: Platform, preset: str, cc: str, cxx: str) -> Path:
ladybird_source_dir, build_preset_dir, build_env_cmake_args = configure_build_env(preset, cc, cxx) ladybird_source_dir, build_preset_dir, build_env_cmake_args = configure_build_env(preset, cc, cxx)
BuildVcpkg.build_vcpkg() build_vcpkg()
if build_preset_dir.joinpath("build.ninja").exists() or build_preset_dir.joinpath("ladybird.sln").exists(): if build_preset_dir.joinpath("build.ninja").exists() or build_preset_dir.joinpath("ladybird.sln").exists():
return build_preset_dir return build_preset_dir