Remake/remake/remake_config.py

117 lines
2.5 KiB
Python
Raw Normal View History

import sys
import yaml
2025-04-13 18:17:59 +00:00
from pathlib import Path
class Style:
RED = '\033[91m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
BOLD = '\033[1m'
END = '\033[0m'
def die(msg):
print(f"{Style.RED}{Style.BOLD}❌ Error:{Style.END} {msg}")
sys.exit(1)
def warn(msg):
print(f"{Style.YELLOW}⚠️ {msg}{Style.END}")
def success(msg):
print(f"{Style.GREEN}{msg}{Style.END}")
def info(msg):
print(f"{Style.BLUE}{msg}{Style.END}")
config_path = Path("remake.yaml")
default_config = """\
# 🔧 Remake Build Configuration
# Source folders to recursively find .c/.cpp files
src_dirs:
- src
- lua
# Include directories (-I)
include_dirs:
- include
- lua
- C:/msys64/mingw64/include
# Library search paths (-L)
lib_dirs:
- C:/msys64/mingw64/lib
- C:/libs
# Output paths
build_dir: build
target: build/app.exe
log_file: remake/build.log
cache_file: remake/.remake_cache.json
# C compiler and flags
cc: gcc
cflags:
- -std=c99
- -Wall
# C++ compiler and flags
cxx: g++
cxxflags:
- -std=c++20
- -Wall
# Auto-detect these libraries (e.g. glfw3 → -lglfw3)
auto_libs: []
# Auto-detect headers from these include folders
auto_includes: []
"""
# Create default if missing
if not config_path.exists():
config_path.write_text(default_config, encoding="utf-8")
success("Generated default remake.yaml ✨")
# Load YAML
try:
with open(config_path, "r", encoding="utf-8") as f:
config = yaml.safe_load(f)
except yaml.YAMLError as e:
die(f"Failed to parse remake.yaml:\n{e}")
if config is None:
die("remake.yaml is empty or invalid YAML. Please fix the file.")
# Validate fields
def get_field(name, expected_type):
try:
val = config[name]
if not isinstance(val, expected_type):
raise TypeError(f"Expected {expected_type.__name__}, got {type(val).__name__}")
return val
except KeyError:
die(f"Missing required field: {Style.BOLD}{name}{Style.END}")
except TypeError as e:
die(f"{name} -> {e}")
# Extract values safely
SRC_DIRS = get_field("src_dirs", list)
INCLUDE_DIRS = get_field("include_dirs", list)
LIB_DIRS = get_field("lib_dirs", list)
BUILD_DIR = Path(get_field("build_dir", str))
TARGET = Path(get_field("target", str))
LOG_FILE = Path(get_field("log_file", str))
CACHE_FILE = Path(get_field("cache_file", str))
CC = get_field("cc", str)
CFLAGS = get_field("cflags", list)
CXX = get_field("cxx", str)
CXXFLAGS = get_field("cxxflags", list)
AUTO_LIBS = get_field("auto_libs", list)
AUTO_INCLUDES = get_field("auto_includes", list)