# ================================================================================================
# FastLED Example Build Flags Configuration
# ================================================================================================
# 
# This file centralizes ALL compilation flags used by example compilation to ensure
# they remain synchronized and compatible across the entire build system.
#
# Examples use the STUB platform for Arduino-compatible functionality without hardware
# dependencies, but do NOT need unit test specific flags like FASTLED_FORCE_NAMESPACE.

[tools]
# Build tool configuration - Full compiler command with arguments
# CRITICAL: Must use 'uv run python' for subprocess execution (subprocess doesn't inherit uv environment)
cpp_compiler = ["uv", "run", "python", "-m", "ziglang", "c++"]
linker = ["uv", "run", "python", "-m", "ziglang", "c++"]  # Separate linker config for consistency
archiver = ["uv", "run", "python", "-m", "ziglang", "ar"]  # Ziglang archiver for unified toolchain
c_compiler = ["uv", "run", "python", "-m", "ziglang", "cc"]
objcopy = ["uv", "run", "python", "-m", "ziglang", "objcopy"]
nm = ["uv", "run", "python", "-m", "ziglang", "nm"]
strip = ["uv", "run", "python", "-m", "ziglang", "strip"]
ranlib = ["uv", "run", "python", "-m", "ziglang", "ranlib"]

[all]
# Universal compilation flags (compiler command handled separately)
compiler_flags = [
    "-std=gnu++11",
    "-fpermissive",
    "-Wall",
    "-Wextra",
    "-Werror=c++14-extensions",  # Error on C++14 features when using C++11
    "-Werror=c++17-extensions",  # Error on C++17 features when using C++11
    "-Wno-deprecated-register",  # Suppress 'register' keyword warnings for C++17 compatibility
    "-Wno-backslash-newline-escape",  # Suppress backslash newline escape warnings
    "-fno-exceptions",
    "-fno-rtti",
    "-Werror=unused-result",  # Treat ignored FL_NODISCARD return values as errors
]

defines = [
    "STUB_PLATFORM",
    # NOTE: Examples do NOT get FASTLED_FORCE_NAMESPACE=1 - they use global namespace
]

include_flags = [
    "-I.",
    "-Isrc",
]

[windows]
# ================================================================================================
# WINDOWS-SPECIFIC BUILD FLAGS
# ================================================================================================
# Comprehensive Windows build configuration using clang-tool-chain with GNU target

# C compiler flags for Windows
cc_flags = [
    "--target=x86_64-windows-gnu",     # Explicit target for MSYS2/MinGW compatibility
    "-fuse-ld=lld",                       # Use LLD linker
    "-std=c17",                           # C17 standard
    "-Wall",
    "-Wextra",
]

# C++ compiler flags for Windows  
cpp_flags = [
    "--target=x86_64-windows-gnu",     # Explicit target for MSYS2/MinGW compatibility
    "-fuse-ld=lld-link",                  # Use lld-link (Windows MSVC-compatible linker)
    "-std=c++11",                         # C++11 standard
    "-Wall", 
    "-Wextra",
    "-fno-exceptions",
    "-fno-rtti",
    
    # Fix Arduino INPUT macro conflict with Windows headers
    "-DNOMINMAX",                         # Prevent Windows min/max macros
    "-DWIN32_LEAN_AND_MEAN",              # Reduce Windows header conflicts
]

# Linker flags for Windows
link_flags = [
    "-mconsole",                          # Console application
    
    # Prevent automatic library inclusion to avoid CRT collisions
    "-nodefaultlibs",                     # Don't automatically include default libraries
    
    # Fix CRT collision between Clang and MSYS2/MinGW runtimes
    # This prevents "atexit was replaced" errors from conflicting CRT implementations
    # "-rtlib=compiler-rt",               # Removed - was causing conflicts
    "-unwindlib=libunwind",               # Use Clang's libunwind (avoid MinGW unwind)
    "-nostdlib++",                        # Don't auto-link standard C++ library
    "-lc++",                              # Manually link libc++ (Clang's C++ standard library)
    
    "-lkernel32",                         # Windows kernel functions
    "-luser32",                           # Windows user interface
    "-lgdi32",                            # Graphics device interface
    "-ladvapi32",                         # Advanced Windows API
]

[linking.base]
# Base linking flags (Unix-like systems)
flags = [
    "-pthread",    # POSIX threads (not used on Windows)
]

[build_modes.quick]
flags = [
    "-O1",
    "-g0",
    "-fno-inline-functions",
    "-fno-vectorize",
    "-fno-unroll-loops",
    "-fno-strict-aliasing",
]

link_flags = []

[strict_mode]
flags = [
    "-Werror",
    "-Wextra",
    "-Wconversion",
    "-Wsign-conversion",
    "-Werror=unused-variable",          # Force error on unused variables
    "-Werror=unused-parameter",         # Force error on unused parameters
    "-Werror=unused-function",          # Force error on unused functions
    "-Werror=unused-but-set-variable",  # Force error on variables that are set but never used
    "-Wuninitialized",
    "-Werror=maybe-uninitialized",      # Force error on possibly uninitialized variables (GCC only)
    "-Wdouble-promotion",
    "-Wformat=2",
    "-Wcast-align",
    "-Wcast-qual",
    "-Werror=return-type",
    "-Werror=switch-enum",              # Force error on incomplete switch over enum
]

[stub_platform]
# ================================================================================================
# STUB PLATFORM DEFINES FOR EXAMPLE COMPILATION
# ================================================================================================
# These defines are used for compiling examples on the STUB platform
# which provides Arduino-compatible functionality without hardware dependencies.

defines = [
    "STUB_PLATFORM",                        # Enable STUB platform
    "ARDUINO=10808",                         # Arduino version compatibility
    "FASTLED_USE_STUB_ARDUINO",             # Use stub Arduino implementation
    "FASTLED_STUB_IMPL",                    # Enable STUB platform Arduino function implementations
    "SKETCH_HAS_LARGE_MEMORY=1",          # Enable memory-intensive features for STUB platform
    "SKETCH_HAS_HUGE_MEMORY=1",       # Enable very large memory features for STUB platform
    "FASTLED_HAS_ENGINE_EVENTS=1",          # Enable EngineEvents for UI components
]

# Include paths required for stub platform compilation
include_paths = [
    "src/platforms/stub",                   # Arduino.h stub for examples
]

# Additional compiler arguments for stub platform
compiler_args = [
    # No additional args currently needed - all flags come from [all] and [build_modes.quick] sections
]

[archive]
# ================================================================================================
# ARCHIVE CREATION FLAGS
# ================================================================================================
# Flags for creating static libraries (.a files) with deterministic builds.
# Using llvm-ar from clang-tool-chain which supports deterministic builds on all platforms.
#
# Thin archives are ENABLED BY DEFAULT for deterministic builds.
# Thin archives store file references (~100 bytes) instead of copying object files (can be MBs).
# This reduces disk usage and speeds up archive creation, but requires object files to remain in place.
#
# To disable thin archives if needed, set: FASTLED_DISABLE_THIN_ARCHIVES=1

flags = "rcsD"  # r=insert, c=create if needed, s=write symbol table, D=deterministic (zero timestamps)

[linking.unix]
# ================================================================================================
# UNIX-SPECIFIC LINKING FLAGS
# ================================================================================================
# Linking flags specific to Unix-like systems (Linux, macOS).

flags = [
    # Core Libraries
    "-pthread",                                 # POSIX threads support
    "-lm",                                     # Math library
    "-ldl",                                    # Dynamic loading library
    
    # Platform-specific libraries (added as needed)
    # Note: Additional platform-specific flags can be added here
]
