CMake: reorganize code segment into separate library

Required for linker script.

Refactored some asset targets to make this possible.
This commit is contained in:
Matt Penny 2024-11-02 21:03:39 -04:00
parent 6b4222e743
commit bbc5a686a6
7 changed files with 113 additions and 85 deletions

View File

@ -4,11 +4,6 @@ project(portal64 C ASM)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(AddExternalProjectExecutable)
# TODO: remove when makefile is no longer in use
add_compile_definitions(CMAKE)
set(USING_LIBULTRA TRUE)
# Programs used in build
find_package(Blender 3.6 EXACT REQUIRED)
find_program(FFmpeg_EXECUTABLE ffmpeg REQUIRED)
@ -24,16 +19,35 @@ find_program(VTF2PNG_EXECUTABLE vtf2png REQUIRED)
# Add skeletool as external project to isolate from toolchain
add_external_project_executable(skeletool64)
# Directories
# Game file directories
set(ASM_DIR "${PROJECT_SOURCE_DIR}/asm")
set(ASSETS_DIR "${PROJECT_SOURCE_DIR}/assets")
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")
set(VPK_DIR "${PROJECT_SOURCE_DIR}/vpk")
# Asset extraction/conversion output directories
set(PAK_DIR "${PROJECT_SOURCE_DIR}/portal_pak_dir")
set(PAK_MODIFIED_DIR "${PROJECT_SOURCE_DIR}/portal_pak_modified")
add_library(platform INTERFACE)
find_package(Libultra REQUIRED)
target_link_libraries(platform INTERFACE libultra::libultra)
target_compile_definitions(platform INTERFACE F3DEX_GBI_2)
add_subdirectory(${ASM_DIR})
add_subdirectory(${ASSETS_DIR})
add_subdirectory(${SRC_DIR})
add_subdirectory(${VPK_DIR})
add_library(code_segment OBJECT)
target_link_libraries(code_segment
platform
engine
level_list
materials_core
model_lists
models_static
sound_lookup_tables
string_lookup_tables
)

View File

@ -386,13 +386,16 @@ set_target_properties(textures PROPERTIES
## Material generation ##
#########################
set(MATERIALS
set(CORE_MATERIALS
hud.skm.yaml
images.skm.yaml
static.skm.yaml
ui.skm.yaml
)
set(IMAGE_MATERIALS
images.skm.yaml
)
function(_add_material_generate_command INPUT_FILE OUTPUT_LIST)
cmake_path(
GET INPUT_FILE STEM
@ -464,18 +467,31 @@ set_property(SOURCE ui.skm.yaml
${PROJECT_SOURCE_DIR}/assets/images/rounded_corners.png
)
set(GENERATED_MATERIALS "")
set(GENERATED_CORE_MATERIALS "")
set(GENERATED_IMAGE_MATERIALS "")
foreach(MATERIAL ${MATERIALS})
_add_material_generate_command(${MATERIAL} GENERATED_MATERIALS)
foreach(MATERIAL ${CORE_MATERIALS})
_add_material_generate_command(${MATERIAL} GENERATED_CORE_MATERIALS)
endforeach()
add_custom_target(generated_materials
DEPENDS ${GENERATED_MATERIALS}
foreach(MATERIAL ${IMAGE_MATERIALS})
_add_material_generate_command(${MATERIAL} GENERATED_IMAGE_MATERIALS)
endforeach()
add_custom_target(materials
DEPENDS
${GENERATED_CORE_MATERIALS}
${GENERATED_IMAGE_MATERIALS}
)
add_library(materials INTERFACE)
add_dependencies(materials generated_materials)
target_sources(materials INTERFACE
${GENERATED_MATERIALS}
add_library(materials_core INTERFACE)
add_dependencies(materials_core materials)
target_sources(materials_core INTERFACE
${GENERATED_CORE_MATERIALS}
)
add_library(materials_images INTERFACE)
add_dependencies(materials_images materials)
target_sources(materials_images INTERFACE
${GENERATED_IMAGE_MATERIALS}
)

View File

@ -213,13 +213,25 @@ set_property(SOURCE props/round_elevator_interior.flags
)
set(MODEL_GENERATED_FILES "")
set(STATIC_MODEL_GENERATED_FILES "")
set(DYNAMIC_MODEL_GENERATED_FILES "")
foreach(MODEL ${MODELS})
set(GENERATED_FILES "")
_add_model_export_command(${MODEL} MODEL_FBX)
_add_model_generate_command(${MODEL} ${MODEL_FBX} MODEL_GENERATED_FILES)
_add_model_generate_command(${MODEL} ${MODEL_FBX} GENERATED_FILES)
if(MODEL IN_LIST DYNAMIC_MODELS)
list(APPEND DYNAMIC_MODEL_GENERATED_FILES ${GENERATED_FILES})
else()
list(APPEND STATIC_MODEL_GENERATED_FILES ${GENERATED_FILES})
endif()
list(APPEND MODEL_GENERATED_FILES ${GENERATED_FILES})
endforeach()
add_custom_target(generated_models
add_custom_target(models
DEPENDS ${MODEL_GENERATED_FILES}
)
@ -257,7 +269,7 @@ set(DYNAMIC_MODEL_LIST_H "${DYNAMIC_MODEL_LIST_OUT_DIR}/dynamic_model_list.h")
set(DYNAMIC_MODEL_LIST_C "${DYNAMIC_MODEL_LIST_OUT_DIR}/dynamic_model_list.c")
add_custom_command(
DEPENDS
generated_models ${DYNAMIC_MODEL_HEADERS}
models ${DYNAMIC_MODEL_HEADERS}
${GEN_DYNAMIC_MODEL_LIST} ${MODEL_LIST_UTILS}
OUTPUT
${DYNAMIC_MODEL_LIST_H} ${DYNAMIC_MODEL_LIST_C}
@ -274,7 +286,7 @@ set(DYNAMIC_ANIMATED_MODEL_LIST_H "${DYNAMIC_MODEL_LIST_OUT_DIR}/dynamic_animate
set(DYNAMIC_ANIMATED_MODEL_LIST_C "${DYNAMIC_MODEL_LIST_OUT_DIR}/dynamic_animated_model_list.c")
add_custom_command(
DEPENDS
generated_models ${DYNAMIC_ANIMATED_MODEL_HEADERS}
models ${DYNAMIC_ANIMATED_MODEL_HEADERS}
${GEN_DYNAMIC_ANIM_MODEL_LIST} ${MODEL_LIST_UTILS}
OUTPUT
${DYNAMIC_ANIMATED_MODEL_LIST_H} ${DYNAMIC_ANIMATED_MODEL_LIST_C}
@ -294,17 +306,18 @@ set(MODEL_LIST_FILES
${DYNAMIC_ANIMATED_MODEL_LIST_C}
)
add_custom_target(model_lists
DEPENDS
add_custom_target(generated_model_lists
DEPENDS ${MODEL_LIST_FILES}
)
add_library(model_lists INTERFACE)
add_dependencies(model_lists generated_model_lists)
target_sources(model_lists INTERFACE
${MODEL_LIST_FILES}
)
add_library(models INTERFACE)
add_dependencies(models
generated_models
model_lists
)
target_sources(models INTERFACE
${MODEL_GENERATED_FILES}
${MODEL_LIST_FILES}
add_library(models_static INTERFACE)
add_dependencies(models_static models)
target_sources(models_static INTERFACE
${STATIC_MODEL_GENERATED_FILES}
)

View File

@ -178,16 +178,12 @@ add_custom_command(
VERBATIM
)
add_custom_target(level_list
add_custom_target(generated_level_list
DEPENDS ${LEVEL_LIST}
)
add_library(levels INTERFACE)
add_dependencies(levels
generated_levels
level_list
)
target_sources(levels INTERFACE
${LEVEL_GENERATED_FILES}
add_library(level_list INTERFACE)
add_dependencies(level_list generated_level_list)
target_sources(level_list INTERFACE
${LEVEL_LIST}
)

View File

@ -23,10 +23,11 @@ set(STRING_DEPENDENCIES
set(BUILD_AUDIO_DIR "${PROJECT_BINARY_DIR}/src/audio")
set(SUBTITLES_H "${BUILD_AUDIO_DIR}/subtitles.h")
set(SUBTITLES_C "${BUILD_AUDIO_DIR}/subtitles.c")
set(STRING_GENERATED_FILES
set(STRING_LOOKUP_TABLE_FILES
${SUBTITLES_H}
${SUBTITLES_C}
)
set(STRING_DATA_TABLE_FILES
# TODO: make this dynamic
${BUILD_AUDIO_DIR}/subtitles_brazilian.c
${BUILD_AUDIO_DIR}/subtitles_bulgarian.c
@ -55,7 +56,8 @@ add_custom_command(
DEPENDS
${STRING_DEPENDENCIES} ${SUBTITLE_GENERATE}
OUTPUT
${STRING_GENERATED_FILES}
${STRING_LOOKUP_TABLE_FILES}
${STRING_DATA_TABLE_FILES}
COMMAND
# TODO: update script to have configurable output location
${Python3_EXECUTABLE} ${SUBTITLE_GENERATE}
@ -67,12 +69,20 @@ add_custom_command(
VERBATIM
)
add_custom_target(generated_strings
DEPENDS ${STRING_GENERATED_FILES}
add_custom_target(strings
DEPENDS
${STRING_LOOKUP_TABLE_FILES}
${STRING_DATA_TABLE_FILES}
)
add_library(strings INTERFACE)
add_dependencies(strings generated_strings)
target_sources(strings INTERFACE
${STRING_GENERATED_FILES}
add_library(string_data_tables INTERFACE)
add_dependencies(string_data_tables strings)
target_sources(string_data_tables INTERFACE
${STRING_DATA_TABLE_FILES}
)
add_library(string_lookup_tables INTERFACE)
add_dependencies(string_lookup_tables strings)
target_sources(string_lookup_tables INTERFACE
${STRING_LOOKUP_TABLE_FILES}
)

View File

@ -4,6 +4,7 @@
set(CMAKE_SYSTEM_NAME Generic)
set(N64 TRUE)
set(N64_TOOLCHAIN_ROOT "" CACHE PATH "Root directory of N64 toolchain")
set(N64_TOOLCHAIN_PREFIX "mips64-elf-" CACHE STRING "File name prefix for toolchain programs")

View File

@ -1,29 +1,15 @@
# TODO: generated code
# TODO: linking
add_executable(portal)
add_library(engine INTERFACE)
set_target_properties(portal PROPERTIES COMPILE_WARNING_AS_ERROR ON)
target_include_directories(portal PRIVATE
target_include_directories(engine INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}"
)
# Generated code
target_link_libraries(portal
asm_sources
levels
materials
models
sound_lookup_tables
strings
)
###############
## Game code ##
###############
# TODO: file sets, headers
target_sources(portal PRIVATE
target_sources(engine INTERFACE
audio/audio.c
audio/audiomgr.c
audio/clips.c
@ -165,6 +151,7 @@ target_sources(portal PRIVATE
util/memory.c
util/profile.c
util/rom.c
util/string.c
)
#########################
@ -174,7 +161,7 @@ target_sources(portal PRIVATE
set(GEN_VERSION_HEADER "${PROJECT_SOURCE_DIR}/cmake/GenVersionHeader.cmake")
set(VERSION_H "${PROJECT_BINARY_DIR}/version.h")
add_custom_target(gen_version_header
add_custom_target(version_header
DEPENDS
${GEN_VERSION_HEADER}
COMMAND
@ -188,26 +175,17 @@ add_custom_target(gen_version_header
)
# Make sure version header is always up to date
add_dependencies(portal gen_version_header)
add_dependencies(engine version_header)
# TODO: remove when makefile is no longer in use
target_compile_definitions(engine INTERFACE CMAKE)
###########################
## Library-specific code ##
###########################
# TODO: move some of this into toolchain
if (USING_LIBULTRA)
target_sources(portal PRIVATE
# TODO: make this conditional
target_sources(engine INTERFACE
system/libultra/controller_libultra.c
system/libultra/time_libultra.c
)
target_compile_definitions(portal PRIVATE F3DEX_GBI_2)
find_package(Libultra REQUIRED)
target_link_libraries(portal libultra::libultra)
# Need explicit call for proper ordering
target_include_directories(portal BEFORE
PRIVATE $<TARGET_PROPERTY:libultra::libultra,INTERFACE_INCLUDE_DIRECTORIES>
)
endif()
)