Files
dusk/cmake/modules/envtoh.cmake
Dominic Masters 7b9f8b190e
Some checks failed
Build Dusk / build-linux (push) Failing after 52s
Build Dusk / build-psp (push) Failing after 55s
Fix?
2025-11-15 22:29:07 -06:00

69 lines
2.1 KiB
CMake

if(NOT DEFINED ENV_FILE)
message(FATAL_ERROR "ENV_FILE is not set")
endif()
if(NOT DEFINED OUT_HEADER)
message(FATAL_ERROR "OUT_HEADER is not set")
endif()
if(NOT EXISTS "${ENV_FILE}")
message(FATAL_ERROR ".env file not found: ${ENV_FILE}")
endif()
file(STRINGS "${ENV_FILE}" ENV_LINES)
set(HEADER_CONTENT "#pragma once\n#include \"dusk.h\"\n\n")
foreach(line IN LISTS ENV_LINES)
# Skip comments and empty lines (allow whitespace before # or ;)
if(line STREQUAL "" OR line MATCHES "^[ \t]*[#;]")
continue()
endif()
# Trim whitespace only for non-empty, non-comment lines
string(REGEX REPLACE "^[ \t]*" "" line "${line}")
string(REGEX REPLACE "[ \t]*$" "" line "${line}")
# Expect KEY=VALUE (allow spaces around '=')
if(NOT line MATCHES "^[A-Za-z_][A-Za-z0-9_]*[ \t]*=[ \t]*")
message(WARNING "Skipping invalid line in ${ENV_FILE}: '${line}'")
continue()
endif()
# Extract key
string(REGEX MATCH "^[A-Za-z_][A-Za-z0-9_]*" KEY "${line}")
string(LENGTH "${KEY}" key_len)
# Extract value (allow spaces around '=')
string(REGEX REPLACE "^[A-Za-z_][A-Za-z0-9_]*[ \t]*=[ \t]*" "" RAW_VALUE "${line}")
# Lowercase copy for boolean detection
string(TOLOWER "${RAW_VALUE}" VALUE_LC)
set(VALUE "${RAW_VALUE}")
# Determine type and format accordingly
if(VALUE_LC STREQUAL "true")
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${KEY} true\n")
elseif(VALUE_LC STREQUAL "false")
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${KEY} false\n")
elseif(VALUE MATCHES "^[+-]?[0-9]+$")
# Integer
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${KEY} ${VALUE}\n")
elseif(VALUE MATCHES "^[+-]?[0-9]*\\.[0-9]+$")
# Float → append "f"
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${KEY} ${VALUE}f\n")
else()
# String → escape for C literal
string(REPLACE "\\" "\\\\" VALUE_ESC "${VALUE}")
string(REPLACE "\"" "\\\"" VALUE_ESC "${VALUE_ESC}")
set(HEADER_CONTENT "${HEADER_CONTENT}#define ${KEY} \"${VALUE_ESC}\"\n")
endif()
endforeach()
file(WRITE "${OUT_HEADER}" "${HEADER_CONTENT}")
message(STATUS "Generated header: ${OUT_HEADER}")