CMake Refactor

This commit is contained in:
TheBrokenRail 2024-11-19 19:57:43 -05:00
parent 871288ee12
commit 386f52a85f
12 changed files with 97 additions and 107 deletions

View File

@ -1,12 +1,9 @@
# Read Hex Data # Read Hex Data
file(READ "${EMBED_IN}" data HEX) file(READ "${EMBED_IN}" data HEX)
# Convert Hex Data For C Compatibility # Convert Hex Data For C Compatibility
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," data "${data}") string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," data "${data}")
# Get C Name # Get C Name
cmake_path(GET EMBED_OUT STEM name) cmake_path(GET EMBED_OUT STEM name)
# Write Data # Write Data
file(WRITE "${EMBED_OUT}" file(WRITE "${EMBED_OUT}"
"#include <stddef.h>\n" "#include <stddef.h>\n"

View File

@ -16,13 +16,17 @@ function(embed_resource target file)
cmake_path(GET file FILENAME name) cmake_path(GET file FILENAME name)
string(MAKE_C_IDENTIFIER "${name}" name) string(MAKE_C_IDENTIFIER "${name}" name)
# Add Command # Add Command
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${name}.c" set(in "${CMAKE_CURRENT_SOURCE_DIR}/${file}")
set(out "${CMAKE_CURRENT_BINARY_DIR}/${name}.c")
set(script "${util_list_dir}/embed-resource.cmake")
add_custom_command(OUTPUT "${out}"
COMMAND "${CMAKE_COMMAND}" COMMAND "${CMAKE_COMMAND}"
ARGS "-DEMBED_IN=${CMAKE_CURRENT_SOURCE_DIR}/${file}" "-DEMBED_OUT=${CMAKE_CURRENT_BINARY_DIR}/${name}.c" "-P" "${util_list_dir}/embed-resource.cmake" ARGS "-DEMBED_IN=${in}" "-DEMBED_OUT=${out}" "-P" "${script}"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${file}" "${util_list_dir}/embed-resource.cmake" DEPENDS "${in}" "${script}"
VERBATIM
) )
# Add To Target # Add To Target
target_sources("${target}" PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${name}.c") target_sources("${target}" PRIVATE "${out}")
endfunction() endfunction()
# Nicer Output # Nicer Output
@ -31,3 +35,45 @@ function(message log_level)
_message("${log_level}" ${ARGN}) _message("${log_level}" ${ARGN})
endif() endif()
endfunction() endfunction()
# Exporting Targets And Headers
macro(_get_sdk_header_dir target)
set(sdk_dir "${MCPI_SDK_INCLUDE_DIR}/${target}")
endmacro()
function(setup_header_dirs target)
_get_sdk_header_dir("${target}")
# Get Header Type
set(header_type "PUBLIC")
get_target_property(type "${target}" TYPE)
if ("${type}" STREQUAL "INTERFACE_LIBRARY")
set(header_type "INTERFACE")
endif()
# Loop
foreach(dir IN LISTS ARGN)
# Add To Target
target_include_directories("${target}" "${header_type}" "$<BUILD_INTERFACE:${dir}>")
# Add To SDK
if(BUILD_ARM_COMPONENTS)
install(
DIRECTORY "${dir}/"
DESTINATION "${sdk_dir}"
FILES_MATCHING
PATTERN "*.h"
)
endif()
endforeach()
# Add SDK Headers To Target
if(BUILD_ARM_COMPONENTS)
target_include_directories("${target}" "${header_type}" "$<INSTALL_INTERFACE:${sdk_dir}>")
endif()
endfunction()
function(setup_library target should_install should_export)
# Install
if(should_install)
install(TARGETS "${target}" DESTINATION "${MCPI_LIB_DIR}")
endif()
# SDK
if(should_export AND BUILD_ARM_COMPONENTS)
install(TARGETS "${target}" EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
endif()
endfunction()

View File

@ -29,10 +29,7 @@ add_subdirectory(src EXCLUDE_FROM_ALL)
unset(MESSAGE_QUIET) unset(MESSAGE_QUIET)
# Install # Install
install(TARGETS LIB_LIEF DESTINATION "${MCPI_LIB_DIR}") setup_library(LIB_LIEF TRUE TRUE)
if(BUILD_ARM_COMPONENTS)
install(TARGETS LIB_LIEF EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
endif()
# License # License
install(FILES src/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/LIEF") install(FILES src/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/LIEF")

View File

@ -21,7 +21,7 @@ add_subdirectory(src EXCLUDE_FROM_ALL)
unset(MESSAGE_QUIET) unset(MESSAGE_QUIET)
# Install # Install
install(TARGETS glfw DESTINATION "${MCPI_LIB_DIR}") setup_library(glfw TRUE FALSE)
# License # License
install(FILES src/LICENSE.md DESTINATION "${MCPI_LEGAL_DIR}/glfw") install(FILES src/LICENSE.md DESTINATION "${MCPI_LEGAL_DIR}/glfw")

View File

@ -7,24 +7,12 @@ add_compile_options(-w)
# Build # Build
add_library(stb_image SHARED src/stb_image_impl.c) add_library(stb_image SHARED src/stb_image_impl.c)
target_include_directories(
stb_image
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${MCPI_SDK_INCLUDE_DIR}/stb_image>"
)
target_link_libraries(stb_image PRIVATE m) target_link_libraries(stb_image PRIVATE m)
target_compile_definitions(stb_image PUBLIC STBI_ONLY_PNG) target_compile_definitions(stb_image PUBLIC STBI_ONLY_PNG)
setup_header_dirs(stb_image "${CMAKE_CURRENT_SOURCE_DIR}/include")
# Install # Install
install(TARGETS stb_image DESTINATION "${MCPI_LIB_DIR}") setup_library(stb_image TRUE TRUE)
install(
DIRECTORY "include/"
DESTINATION "${MCPI_SDK_INCLUDE_DIR}/stb_image"
FILES_MATCHING
PATTERN "*.h"
)
install(TARGETS stb_image EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
# License # License
install(FILES include/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/stb_image") install(FILES include/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/stb_image")

View File

@ -13,22 +13,12 @@ add_library(reborn-util SHARED
src/util/cp437.cpp src/util/cp437.cpp
src/util/env.cpp src/util/env.cpp
) )
target_include_directories(
reborn-util
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
"$<INSTALL_INTERFACE:${MCPI_SDK_INCLUDE_DIR}/libreborn>"
)
target_link_libraries(reborn-util PRIVATE utf8cpp) target_link_libraries(reborn-util PRIVATE utf8cpp)
# Install setup_header_dirs(reborn-util
install(TARGETS reborn-util DESTINATION "${MCPI_LIB_DIR}") "${CMAKE_CURRENT_SOURCE_DIR}/include"
# SDK "${CMAKE_CURRENT_BINARY_DIR}/include"
if(BUILD_ARM_COMPONENTS) )
install(TARGETS reborn-util EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}") setup_library(reborn-util TRUE TRUE)
install(DIRECTORY "include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/libreborn")
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/libreborn")
endif()
# Patch # Patch
if(BUILD_ARM_COMPONENTS) if(BUILD_ARM_COMPONENTS)
@ -39,11 +29,9 @@ if(BUILD_ARM_COMPONENTS)
src/patch/instruction.cpp src/patch/instruction.cpp
) )
target_link_libraries(reborn-patch dl pthread reborn-util) target_link_libraries(reborn-patch dl pthread reborn-util)
target_compile_definitions(reborn-patch PUBLIC -DREBORN_HAS_PATCH_CODE) target_compile_definitions(reborn-patch PUBLIC REBORN_HAS_PATCH_CODE)
# Install # Install
install(TARGETS reborn-patch DESTINATION "${MCPI_LIB_DIR}") setup_library(reborn-patch TRUE TRUE)
# SDK
install(TARGETS reborn-patch EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
endif() endif()
# Fake LibPNG To Satisfy Symbol Versioning Requirement # Fake LibPNG To Satisfy Symbol Versioning Requirement
@ -55,5 +43,5 @@ if(BUILD_ARM_COMPONENTS)
) )
target_link_options(fake-libpng PRIVATE "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/fake-libpng/empty.vers") target_link_options(fake-libpng PRIVATE "LINKER:--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/fake-libpng/empty.vers")
# Install # Install
install(TARGETS fake-libpng DESTINATION "${MCPI_LIB_DIR}") setup_library(fake-libpng TRUE FALSE)
endif() endif()

View File

@ -2,22 +2,8 @@ project(media-layer)
# Add Headers # Add Headers
add_library(media-layer-headers INTERFACE) add_library(media-layer-headers INTERFACE)
target_include_directories( setup_header_dirs(media-layer-headers "${CMAKE_CURRENT_SOURCE_DIR}/include")
media-layer-headers setup_library(media-layer-headers FALSE TRUE)
INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${MCPI_SDK_INCLUDE_DIR}/media-layer>"
)
# SDK
if(BUILD_ARM_COMPONENTS)
install(TARGETS media-layer-headers EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
# Copy Headers
set(GLES_HEADERS "include/GLES")
install(
DIRECTORY "include/"
DESTINATION "${MCPI_SDK_INCLUDE_DIR}/media-layer"
)
endif()
# Add Core # Add Core
if(BUILD_MEDIA_LAYER_CORE) if(BUILD_MEDIA_LAYER_CORE)

View File

@ -1,7 +1,7 @@
project(media-layer-core) project(media-layer-core)
# SDL Re-Implementation Using GLFW # Build
set(CORE_SRC add_library(media-layer-core-real SHARED
src/base.cpp src/base.cpp
src/window/media.cpp src/window/media.cpp
src/window/cursor.cpp src/window/cursor.cpp
@ -14,14 +14,14 @@ set(CORE_SRC
src/audio/file.cpp src/audio/file.cpp
) )
# Build # Set Name
add_library(media-layer-core-real SHARED ${CORE_SRC}) # Dependencies Are Setup Later
set_target_properties(media-layer-core-real PROPERTIES OUTPUT_NAME "media-layer-core") set_target_properties(media-layer-core-real PROPERTIES OUTPUT_NAME "media-layer-core")
if(BUILD_NATIVE_COMPONENTS) if(BUILD_NATIVE_COMPONENTS)
add_library(media-layer-core ALIAS media-layer-core-real) add_library(media-layer-core ALIAS media-layer-core-real)
endif() endif()
# Install # Install
install(TARGETS media-layer-core-real DESTINATION "${MCPI_LIB_DIR}") setup_library(media-layer-core-real TRUE FALSE)
# Link # Link
find_library(OPENAL_LIBRARY NAMES openal REQUIRED) find_library(OPENAL_LIBRARY NAMES openal REQUIRED)

View File

@ -183,13 +183,16 @@ static void convert_to_pixels(GLFWwindow *window, double *xpos, double *ypos) {
int window_width; int window_width;
int window_height; int window_height;
glfwGetWindowSize(window, &window_width, &window_height); glfwGetWindowSize(window, &window_width, &window_height);
if (window_width <= 0 || window_height <= 0) {
return;
}
// Get Framebuffer Size // Get Framebuffer Size
int framebuffer_width; int framebuffer_width;
int framebuffer_height; int framebuffer_height;
glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height); glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
// Calculate Ratios // Calculate Ratios
const double width_ratio = ((double) framebuffer_width) / ((double) window_width); const double width_ratio = double(framebuffer_width) / double(window_width);
const double height_ratio = ((double) framebuffer_height) / ((double) window_height); const double height_ratio = double(framebuffer_height) / double(window_height);
// Multiply // Multiply
*xpos *= width_ratio; *xpos *= width_ratio;
*ypos *= height_ratio; *ypos *= height_ratio;

View File

@ -10,7 +10,7 @@ if(BUILD_NATIVE_COMPONENTS)
target_link_libraries(media-layer-trampoline reborn-util media-layer-core trampoline-headers) target_link_libraries(media-layer-trampoline reborn-util media-layer-core trampoline-headers)
target_compile_definitions(media-layer-trampoline PRIVATE MEDIA_LAYER_TRAMPOLINE_HOST) target_compile_definitions(media-layer-trampoline PRIVATE MEDIA_LAYER_TRAMPOLINE_HOST)
# Install # Install
install(TARGETS media-layer-trampoline DESTINATION "${MCPI_LIB_DIR}") setup_library(media-layer-trampoline TRUE TRUE)
elseif(BUILD_ARM_COMPONENTS) elseif(BUILD_ARM_COMPONENTS)
# Guest Component # Guest Component
add_library(media-layer-core SHARED src/guest/guest.cpp ${MEDIA_LAYER_TRAMPOLINE_SRC}) add_library(media-layer-core SHARED src/guest/guest.cpp ${MEDIA_LAYER_TRAMPOLINE_SRC})
@ -24,8 +24,5 @@ elseif(BUILD_ARM_COMPONENTS)
) )
target_compile_definitions(media-layer-core PRIVATE MEDIA_LAYER_TRAMPOLINE_GUEST) target_compile_definitions(media-layer-core PRIVATE MEDIA_LAYER_TRAMPOLINE_GUEST)
# Install # Install
if(MCPI_USE_MEDIA_LAYER_TRAMPOLINE) setup_library(media-layer-core "${MCPI_USE_MEDIA_LAYER_TRAMPOLINE}" TRUE)
install(TARGETS media-layer-core DESTINATION "${MCPI_LIB_DIR}")
endif()
install(TARGETS media-layer-core EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
endif() endif()

View File

@ -1,7 +1,7 @@
project(mods) project(mods)
# Common Sources # Build
set(SRC add_library(mods SHARED
# compat # compat
src/compat/compat.cpp src/compat/compat.cpp
src/compat/egl.cpp src/compat/egl.cpp
@ -109,29 +109,30 @@ set(SRC
src/shading/lighting.cpp src/shading/lighting.cpp
src/shading/normals.cpp src/shading/normals.cpp
) )
# Install Splashes # Install Splashes
install( install(
FILES "src/title-screen/splashes.txt" FILES "src/title-screen/splashes.txt"
DESTINATION "${MCPI_INSTALL_DIR}/data" DESTINATION "${MCPI_INSTALL_DIR}/data"
) )
# Build # Headers
add_library(mods SHARED ${SRC}) setup_header_dirs(mods "${CMAKE_CURRENT_SOURCE_DIR}/include")
# Install # Install
install(TARGETS mods DESTINATION "${MCPI_INSTALL_DIR}/mods") set(old_lib_dir "${MCPI_LIB_DIR}")
# SDK set(MCPI_LIB_DIR "${MCPI_INSTALL_DIR}/mods")
install(TARGETS mods EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}") setup_library(mods TRUE TRUE)
set(MCPI_LIB_DIR "${old_lib_dir}")
# Dependencies # Dependencies
target_link_libraries(mods symbols reborn-patch media-layer-core stb_image dl pthread) target_link_libraries(mods
# Headers
target_include_directories(
mods
PUBLIC PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>" symbols
"$<INSTALL_INTERFACE:${MCPI_SDK_INCLUDE_DIR}/mods>" reborn-patch
media-layer-core
PRIVATE
stb_image
dl
pthread
) )
# SDK
install(DIRECTORY "include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/mods")

View File

@ -231,7 +231,8 @@ set(OUTPUT_FILES ${SRC_OUTPUT_FILES} "${HEADER_OUTPUT_FILE}")
add_custom_command( add_custom_command(
OUTPUT ${OUTPUT_FILES} OUTPUT ${OUTPUT_FILES}
DEPENDS ${INPUT_FILES} DEPENDS ${INPUT_FILES}
COMMAND npm start --silent -- "${SRC_OUTPUT_DIR}" "${HEADER_OUTPUT_FILE}" ${INPUT_FILES} COMMAND "npm"
ARGS "start" "--silent" "--" "${SRC_OUTPUT_DIR}" "${HEADER_OUTPUT_FILE}" ${INPUT_FILES}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../dependencies/symbol-processor/src" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../dependencies/symbol-processor/src"
VERBATIM VERBATIM
) )
@ -239,22 +240,8 @@ add_custom_command(
# Build # Build
add_library(symbols SHARED ${OUTPUT_FILES}) add_library(symbols SHARED ${OUTPUT_FILES})
# Show In IDE
add_custom_target(symbols-src
DEPENDS "${SRC_OUTPUT_FILE}" "${HEADER_OUTPUT_FILE}"
SOURCES ${RESOLVED_SRC}
)
# Include Directory # Include Directory
target_include_directories( setup_header_dirs(symbols "${INCLUDE_OUTPUT_DIR}")
symbols
PUBLIC
"$<BUILD_INTERFACE:${INCLUDE_OUTPUT_DIR}>"
"$<INSTALL_INTERFACE:${MCPI_SDK_INCLUDE_DIR}/symbols>"
)
# Install # Install
install(TARGETS symbols DESTINATION "${MCPI_LIB_DIR}") setup_library(symbols TRUE TRUE)
# SDK
install(TARGETS symbols EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
install(DIRECTORY "${INCLUDE_OUTPUT_DIR}/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/symbols")