From 9d86137203a560dfd9244eb9699eec0285d52c56 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 17 Apr 2026 12:59:31 +0200 Subject: [PATCH] arreglos en make i cmake per estandaritzar amb la resta de projectes Co-Authored-By: Claude Opus 4.7 (1M context) --- .gitignore | 2 +- CMakeLists.txt | 67 +++++++++-- Makefile | 150 +++++++++++++++++++++--- source/main.cpp | 4 +- tools/pack_resources/pack_resources.cpp | 6 +- 5 files changed, 199 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index f0411ee..1a585d7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ aee.exe *.app # --- Generated assets --- -resource.pack +resources.pack data.jrf # --- Runtime / debug junk --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 3abb366..ba37a11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -255,10 +255,10 @@ if(NOT EMSCRIPTEN) endif() # --- EINA STANDALONE: pack_resources --- -# Executable auxiliar que empaqueta `data/` a `resource.pack` (format AEE1). +# Executable auxiliar que empaqueta `data/` a `resources.pack` (format AEE1). # No es compila per defecte (EXCLUDE_FROM_ALL). Build explícit: # cmake --build build --target pack_resources -# Després executar: ./build/pack_resources data resource.pack +# Després executar: ./build/pack_resources data resources.pack if(NOT EMSCRIPTEN) add_executable(pack_resources EXCLUDE_FROM_ALL tools/pack_resources/pack_resources.cpp @@ -267,12 +267,12 @@ if(NOT EMSCRIPTEN) target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source") target_compile_options(pack_resources PRIVATE -Wall) - # --- Regeneració automàtica de resource.pack --- + # --- Regeneració automàtica de resources.pack --- # Cada `cmake --build build` torna a empaquetar `data/` si algun fitxer ha # canviat. Evita debugar amb un pack obsolet. CONFIGURE_DEPENDS força CMake # a re-globbar a la pròxima invocació (recull fitxers nous afegits a data/). file(GLOB_RECURSE DATA_FILES CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/data/*") - set(RESOURCE_PACK "${CMAKE_SOURCE_DIR}/resource.pack") + set(RESOURCE_PACK "${CMAKE_SOURCE_DIR}/resources.pack") add_custom_command( OUTPUT ${RESOURCE_PACK} @@ -281,7 +281,7 @@ if(NOT EMSCRIPTEN) "${RESOURCE_PACK}" DEPENDS pack_resources ${DATA_FILES} WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - COMMENT "Empaquetant data/ → resource.pack" + COMMENT "Empaquetant data/ → resources.pack" VERBATIM ) @@ -289,10 +289,12 @@ if(NOT EMSCRIPTEN) add_dependencies(${PROJECT_NAME} resource_pack) endif() -# --- CLANG-FORMAT TARGETS --- +# --- STATIC ANALYSIS TARGETS --- +find_program(CLANG_TIDY_EXE NAMES clang-tidy) find_program(CLANG_FORMAT_EXE NAMES clang-format) +find_program(CPPCHECK_EXE NAMES cppcheck) -# Recopilar todos los archivos fuente para formateo (excluir external/) +# Recopilar todos los archivos fuente (excluir external/) file(GLOB_RECURSE ALL_SOURCE_FILES "${CMAKE_SOURCE_DIR}/source/*.cpp" "${CMAKE_SOURCE_DIR}/source/*.hpp" @@ -300,6 +302,37 @@ file(GLOB_RECURSE ALL_SOURCE_FILES ) list(FILTER ALL_SOURCE_FILES EXCLUDE REGEX ".*/external/.*") +# Para clang-tidy, excluir headers SPIR-V generados +set(CLANG_TIDY_SOURCES ${ALL_SOURCE_FILES}) +list(FILTER CLANG_TIDY_SOURCES EXCLUDE REGEX ".*_spv\\.h$") + +# Para cppcheck, pasar solo .cpp (los headers se procesan transitivamente). +set(CPPCHECK_SOURCES ${ALL_SOURCE_FILES}) +list(FILTER CPPCHECK_SOURCES INCLUDE REGEX ".*\\.cpp$") + +# Targets de clang-tidy +if(CLANG_TIDY_EXE) + add_custom_target(tidy + COMMAND ${CLANG_TIDY_EXE} + -p ${CMAKE_BINARY_DIR} + ${CLANG_TIDY_SOURCES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Running clang-tidy..." + ) + + add_custom_target(tidy-fix + COMMAND ${CLANG_TIDY_EXE} + -p ${CMAKE_BINARY_DIR} + --fix + ${CLANG_TIDY_SOURCES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Running clang-tidy with fixes..." + ) +else() + message(STATUS "clang-tidy no encontrado - targets 'tidy' y 'tidy-fix' no disponibles") +endif() + +# Targets de clang-format if(CLANG_FORMAT_EXE) add_custom_target(format COMMAND ${CLANG_FORMAT_EXE} @@ -320,3 +353,23 @@ if(CLANG_FORMAT_EXE) else() message(STATUS "clang-format no encontrado - targets 'format' y 'format-check' no disponibles") endif() + +# Target de cppcheck +if(CPPCHECK_EXE) + add_custom_target(cppcheck + COMMAND ${CPPCHECK_EXE} + --enable=warning,style,performance,portability + --std=c++20 + --language=c++ + --inline-suppr + --suppress=missingIncludeSystem + --suppress=toomanyconfigs + --quiet + -I ${CMAKE_SOURCE_DIR}/source + ${CPPCHECK_SOURCES} + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Running cppcheck..." + ) +else() + message(STATUS "cppcheck no encontrado - target 'cppcheck' no disponible") +endif() diff --git a/Makefile b/Makefile index ca08ac0..78b5ba2 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,18 @@ DIR_ROOT := $(dir $(abspath $(MAKEFILE_LIST))) DIR_BIN := $(addsuffix /, $(DIR_ROOT)) +# ============================================================================== +# TOOLS +# ============================================================================== +SHADER_CMAKE := $(DIR_ROOT)tools/shaders/compile_spirv.cmake +SHADERS_DIR := $(DIR_ROOT)data/shaders +HEADERS_DIR := $(DIR_ROOT)source/core/rendering/sdl3gpu +ifeq ($(OS),Windows_NT) + GLSLC := $(shell where glslc 2>NUL) +else + GLSLC := $(shell command -v glslc 2>/dev/null) +endif + # ============================================================================== # TARGET NAMES # ============================================================================== @@ -18,9 +30,9 @@ RELEASE_FILE := $(RELEASE_FOLDER)/$(TARGET_NAME) # VERSION (extracted from defines.hpp) # ============================================================================== ifeq ($(OS),Windows_NT) - VERSION := v$(shell powershell -Command "(Select-String -Path 'source/game/defines.hpp' -Pattern 'constexpr const char\* VERSION = \"(.+?)\"').Matches.Groups[1].Value") + VERSION := $(shell powershell -Command "(Select-String -Path 'source/game/defines.hpp' -Pattern 'constexpr const char\* VERSION = \"(.+?)\"').Matches.Groups[1].Value") else - VERSION := v$(shell grep 'constexpr const char\* VERSION' source/game/defines.hpp | sed -E 's/.*VERSION = "([^"]+)".*/\1/') + VERSION := $(shell grep 'constexpr const char\* VERSION' source/game/defines.hpp | sed -E 's/.*VERSION = "([^"]+)".*/\1/') endif # ============================================================================== @@ -51,9 +63,13 @@ endif ifeq ($(OS),Windows_NT) WIN_TARGET_FILE := $(DIR_BIN)$(APP_NAME) WIN_RELEASE_FILE := $(RELEASE_FOLDER)/$(APP_NAME) + # Escapa apòstrofs per a PowerShell (duplica ' → ''). Sense això, APP_NAMEs + # com "JailDoctor's Dilemma" trencarien el parsing de -Destination '...'. + WIN_RELEASE_FILE_PS := $(subst ','',$(WIN_RELEASE_FILE)) else WIN_TARGET_FILE := $(TARGET_FILE) WIN_RELEASE_FILE := $(RELEASE_FILE) + WIN_RELEASE_FILE_PS := $(WIN_RELEASE_FILE) endif # ============================================================================== @@ -79,22 +95,41 @@ else UNAME_S := $(shell uname -s) endif +# ============================================================================== +# CMAKE GENERATOR (Windows needs explicit MinGW Makefiles generator) +# ============================================================================== +ifeq ($(OS),Windows_NT) + CMAKE_GEN := -G "MinGW Makefiles" +else + CMAKE_GEN := +endif + # ============================================================================== # COMPILACIÓN CON CMAKE # ============================================================================== all: - @cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) @cmake --build build debug: - @cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DGIT_HASH=$(GIT_HASH) + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Debug -DGIT_HASH=$(GIT_HASH) @cmake --build build -# Empaqueta data/ a resource.pack (format AEE1). Build previ de l'eina + execució. +# ============================================================================== +# REGLAS PARA COMPILACIÓN DE SHADERS (multiplataforma via cmake) +# ============================================================================== +compile_shaders: +ifdef GLSLC + @cmake -D GLSLC=$(GLSLC) -D SHADERS_DIR=$(SHADERS_DIR) -D HEADERS_DIR=$(HEADERS_DIR) -P $(SHADER_CMAKE) +else + @echo "glslc no encontrado - asegurate de que los headers SPIR-V precompilados existen" +endif + +# Empaqueta data/ a resources.pack (format AEE1). Build previ de l'eina + execució. pack: - @cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) @cmake --build build --target pack_resources - @./build/pack_resources data resource.pack + @./build/pack_resources data resources.pack # ============================================================================== # RELEASE AUTOMÁTICO (detecta SO) @@ -118,7 +153,7 @@ _windows_release: pack @echo Creando release para Windows - Version: $(VERSION) # Compila con cmake - @cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) @cmake --build build # Crea carpeta de distribución y carpeta temporal 'RELEASE_FOLDER' @@ -126,13 +161,13 @@ _windows_release: pack @powershell -Command "if (Test-Path '$(RELEASE_FOLDER)') {Remove-Item '$(RELEASE_FOLDER)' -Recurse -Force}" @powershell -Command "if (-not (Test-Path '$(RELEASE_FOLDER)')) {New-Item '$(RELEASE_FOLDER)' -ItemType Directory}" -# Copia ficheros (resource.pack substitueix la carpeta data/) - @powershell -Command "Copy-Item 'resource.pack' -Destination '$(RELEASE_FOLDER)'" +# Copia ficheros (resources.pack substitueix la carpeta data/) + @powershell -Command "Copy-Item 'resources.pack' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'LICENSE' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'README.md' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'gamecontrollerdb.txt' -Destination '$(RELEASE_FOLDER)'" @powershell -Command "Copy-Item 'release\windows\dll\*.dll' -Destination '$(RELEASE_FOLDER)'" - @powershell -Command "Copy-Item -Path '$(TARGET_FILE).exe' -Destination '$(WIN_RELEASE_FILE).exe'" + @powershell -Command "Copy-Item -Path '$(TARGET_FILE).exe' -Destination '$(WIN_RELEASE_FILE_PS).exe'" strip -s -R .comment -R .gnu.version "$(WIN_RELEASE_FILE).exe" --strip-unneeded # Crea el fichero .zip @@ -168,8 +203,8 @@ _macos_release: pack $(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/MacOS" $(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" -# Copia carpetas y ficheros (resource.pack substitueix la carpeta data/) - cp resource.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" +# Copia carpetas y ficheros (resources.pack substitueix la carpeta data/) + cp resources.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" cp gamecontrollerdb.txt "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" cp -R release/macos/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks" cp release/icons/*.icns "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources" @@ -263,6 +298,22 @@ wasm: ssh maverick 'cd /home/sergio/gitea/web_jailgames && ./deploy.sh' @echo "Deployed to maverick" +# Versió Debug del build wasm: build local sense deploy. Sortida a dist/wasm_debug/. +wasm_debug: + @echo "Compilando WebAssembly Debug - Version: $(VERSION)" + docker run --rm \ + --user $(shell id -u):$(shell id -g) \ + -v $(DIR_ROOT):/src \ + -w /src \ + emscripten/emsdk:latest \ + bash -c "emcmake cmake -S . -B build/wasm_debug -DCMAKE_BUILD_TYPE=Debug -DGIT_HASH=$(GIT_HASH) && cmake --build build/wasm_debug" + @$(MKDIR) "$(DIST_DIR)/wasm_debug" + @cp build/wasm_debug/$(TARGET_NAME).html $(DIST_DIR)/wasm_debug/ + @cp build/wasm_debug/$(TARGET_NAME).js $(DIST_DIR)/wasm_debug/ + @cp build/wasm_debug/$(TARGET_NAME).wasm $(DIST_DIR)/wasm_debug/ + @cp build/wasm_debug/$(TARGET_NAME).data $(DIST_DIR)/wasm_debug/ + @echo "Output: $(DIST_DIR)/wasm_debug/$(TARGET_NAME).html" + # ============================================================================== # COMPILACIÓN PARA LINUX (RELEASE) # ============================================================================== @@ -270,15 +321,15 @@ _linux_release: pack @echo "Creando release para Linux - Version: $(VERSION)" # Compila con cmake - @cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) @cmake --build build # Elimina carpeta temporal previa y la recrea (crea dist/ si no existe) $(RMDIR) "$(RELEASE_FOLDER)" $(MKDIR) "$(RELEASE_FOLDER)" -# Copia ficheros (resource.pack substitueix la carpeta data/) - cp resource.pack "$(RELEASE_FOLDER)" +# Copia ficheros (resources.pack substitueix la carpeta data/) + cp resources.pack "$(RELEASE_FOLDER)" cp LICENSE "$(RELEASE_FOLDER)" cp README.md "$(RELEASE_FOLDER)" cp gamecontrollerdb.txt "$(RELEASE_FOLDER)" @@ -293,4 +344,69 @@ _linux_release: pack # Elimina la carpeta temporal $(RMDIR) "$(RELEASE_FOLDER)" -.PHONY: all debug pack release wasm _windows_release _linux_release _macos_release +# ============================================================================== +# ============================================================================== +# CODE QUALITY (delegados a cmake) +# ============================================================================== +format: + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake --build build --target format + +format-check: + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake --build build --target format-check + +tidy: + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake --build build --target tidy + +tidy-fix: + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake --build build --target tidy-fix + +cppcheck: + @cmake $(CMAKE_GEN) -S . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_HASH=$(GIT_HASH) + @cmake --build build --target cppcheck + +# DESCÀRREGA DE GAMECONTROLLERDB +# ============================================================================== +controllerdb: + @echo "Descarregant gamecontrollerdb.txt..." + curl -fsSL https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/master/gamecontrollerdb.txt \ + -o gamecontrollerdb.txt + @echo "gamecontrollerdb.txt actualitzat" + +# ============================================================================== +# AJUDA +# ============================================================================== +help: + @echo "Makefile per a Aventures en Egipte" + @echo "Comandes disponibles:" + @echo "" + @echo " Compilacio:" + @echo " make - Compilar amb cmake (Release)" + @echo " make debug - Compilar amb cmake (Debug)" + @echo "" + @echo " Release:" + @echo " make release - Crear release (detecta SO automaticament)" + @echo " make wasm - Build WebAssembly (requereix Docker) + deploy a maverick" + @echo " make wasm_debug - Build WebAssembly Debug local (sense deploy)" + @echo "" + @echo " Eines:" + @echo " make compile_shaders - Compilar shaders SPIR-V" + @echo " make pack - Empaquetar data/ a resources.pack (format AEE1)" + @echo " make controllerdb - Actualitzar gamecontrollerdb.txt des de SDL_GameControllerDB" + @echo "" + @echo " Qualitat de codi:" + @echo " make format - Formatar codi amb clang-format" + @echo " make format-check - Verificar format sense modificar" + @echo " make tidy - Anàlisi estàtic amb clang-tidy" + @echo " make tidy-fix - Anàlisi estàtic amb auto-fix" + @echo " make cppcheck - Anàlisi estàtic amb cppcheck" + @echo "" + @echo " Altres:" + @echo " make help - Mostrar esta ajuda" + @echo "" + @echo " Versio actual: $(VERSION) ($(GIT_HASH))" + +.PHONY: all debug pack release wasm wasm_debug _windows_release _linux_release _macos_release compile_shaders controllerdb format format-check tidy tidy-fix cppcheck help diff --git a/source/main.cpp b/source/main.cpp index 7b6b997..53115cb 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -38,9 +38,9 @@ SDL_AppResult SDL_AppInit(void** /*appstate*/, int /*argc*/, char* /*argv*/[]) { if (base_path) { const std::string data_path = std::string(base_path) + "data/"; file_setresourcefolder(data_path.c_str()); - resource_pack_path = std::string(base_path) + "resource.pack"; + resource_pack_path = std::string(base_path) + "resources.pack"; } else { - resource_pack_path = "resource.pack"; + resource_pack_path = "resources.pack"; } // Sistema de recursos: prova el pack i cau a fitxers solts dins data/. diff --git a/tools/pack_resources/pack_resources.cpp b/tools/pack_resources/pack_resources.cpp index 7ebe63a..f5011b8 100644 --- a/tools/pack_resources/pack_resources.cpp +++ b/tools/pack_resources/pack_resources.cpp @@ -15,9 +15,9 @@ void showHelp() { std::cout << " --list List contents of an existing pack file\n\n"; std::cout << "Arguments:\n"; std::cout << " input_dir Directory to pack (default: data)\n"; - std::cout << " output_file Pack file name (default: resource.pack)\n\n"; + std::cout << " output_file Pack file name (default: resources.pack)\n\n"; std::cout << "Examples:\n"; - std::cout << " pack_resources # Pack 'data' to 'resource.pack'\n"; + std::cout << " pack_resources # Pack 'data' to 'resources.pack'\n"; std::cout << " pack_resources mydata mypack.pack # Pack 'mydata' to 'mypack.pack'\n"; std::cout << " pack_resources --list my.pack # List contents of 'my.pack'\n"; } @@ -38,7 +38,7 @@ void listPackContents(const std::string& pack_file) { int main(int argc, char* argv[]) { std::string data_dir = "data"; - std::string output_file = "resource.pack"; + std::string output_file = "resources.pack"; bool list_mode = false; bool data_dir_set = false;