Generate game version for CMake builds and fix generated level names

With CMake, the game version is stored in a generated header file so
dependencies on it are kept track of automatically.

The game now compiles when using CMake but does not link.

Generated code dependencies are not hooked up yet and corresponding
targets need to be built manually.
This commit is contained in:
Matt Penny 2024-09-25 22:24:16 -04:00
parent 9e633b868b
commit bc2052527c
5 changed files with 81 additions and 1 deletions

View file

@ -1,11 +1,15 @@
cmake_minimum_required(VERSION 3.28) cmake_minimum_required(VERSION 3.28)
project(portal64) project(portal64)
# TODO: remove when makefile is no longer in use
add_compile_definitions(CMAKE)
# Tools # Tools
# TODO: find automatically # TODO: find automatically
# TODO: test on Windows # TODO: test on Windows
set(BLENDER_3_6 "blender") set(BLENDER_3_6 "blender")
set(FFMPEG "ffmpeg") set(FFMPEG "ffmpeg")
set(GIT "git")
set(IMAGEMAGICK_CONVERT "convert") set(IMAGEMAGICK_CONVERT "convert")
set(MPG123 "mpg123") set(MPG123 "mpg123")
set(NODEJS "node") set(NODEJS "node")

View file

@ -103,7 +103,7 @@ function(_add_level_generate_command LEVEL_NAME LEVEL_FBX OUTPUT_LIST)
--script ${EXPORT_LEVEL} --script ${EXPORT_LEVEL}
--fixed-point-scale ${SCENE_SCALE} --fixed-point-scale ${SCENE_SCALE}
--model-scale ${MODEL_SCALE} --model-scale ${MODEL_SCALE}
--name ${LEVEL_NAME} --name ${LEVEL_NAME}/${LEVEL_NAME}
${MATERIAL_ARGS} ${MATERIAL_ARGS}
--output ${OUTPUT_FILE_H} --output ${OUTPUT_FILE_H}
${LEVEL_FBX} ${LEVEL_FBX}

View file

@ -0,0 +1,43 @@
#################################
## Get current project version ##
#################################
# First try getting the version from git
execute_process(
COMMAND
${GIT} describe --tags HEAD
RESULT_VARIABLE
GIT_DESCRIBE_RC
OUTPUT_VARIABLE
GIT_DESCRIBE_OUTPUT
)
# If not in a git repo, fall back to exported version
if (NOT GIT_DESCRIBE_RC EQUAL 0)
file(READ version.txt GIT_DESCRIBE_OUTPUT)
endif()
# Extract the version
string(STRIP "${GIT_DESCRIBE_OUTPUT}" GIT_DESCRIBE_OUTPUT)
string(REPLACE "-" ";" GIT_DESCRIBE_COMPONENTS "${GIT_DESCRIBE_OUTPUT}")
list(LENGTH GIT_DESCRIBE_COMPONENTS COMPONENT_COUNT)
if (COMPONENT_COUNT GREATER_EQUAL 3)
# Not on a git tag, use commit hash (trim the "g" prefix)
list(GET GIT_DESCRIBE_COMPONENTS 2 GAME_VERSION)
string(SUBSTRING "${GAME_VERSION}" 1 -1 GAME_VERSION)
else()
# On a git tag, use it
list(GET GIT_DESCRIBE_COMPONENTS 0 GAME_VERSION)
endif()
file(CONFIGURE
OUTPUT ${OUTPUT_FILE}
CONTENT
"#ifndef __GAME_VERSION_H__
#define __GAME_VERSION_H__
#define GAME_VERSION \"${GAME_VERSION}\"
#endif"
)

View file

@ -6,6 +6,10 @@ target_include_directories(portal PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}"
) )
###############
## Game code ##
###############
# TODO: file sets, headers # TODO: file sets, headers
target_sources(portal PRIVATE target_sources(portal PRIVATE
audio/audio.c audio/audio.c
@ -154,6 +158,30 @@ target_sources(portal PRIVATE
util/rom.c util/rom.c
) )
#########################
## Version information ##
#########################
set(GEN_VERSION_HEADER "${PROJECT_SOURCE_DIR}/cmake/gen_version_header.cmake")
set(VERSION_H "${CMAKE_BINARY_DIR}/version.h")
add_custom_target(gen_version_header
DEPENDS
${GEN_VERSION_HEADER}
COMMAND
${CMAKE_COMMAND} -D GIT=${GIT} -D OUTPUT_FILE=${VERSION_H} -P ${GEN_VERSION_HEADER}
WORKING_DIRECTORY
${PROJECT_SOURCE_DIR}
VERBATIM
)
# Make sure version header is always up to date
add_dependencies(portal gen_version_header)
###########################
## Library-specific code ##
###########################
# TODO: move some of this into toolchain # TODO: move some of this into toolchain
if (LIBULTRA) if (LIBULTRA)
target_sources(portal PRIVATE target_sources(portal PRIVATE

View file

@ -11,6 +11,11 @@
#include "cheat_codes.h" #include "cheat_codes.h"
#include "./translations.h" #include "./translations.h"
// TODO: remove when makefile is no longer in use
#ifdef CMAKE
#include "../build/version.h"
#endif
#define PORTAL_LOGO_X 30 #define PORTAL_LOGO_X 30
#define PORTAL_LOGO_Y 74 #define PORTAL_LOGO_Y 74