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

View File

@ -16,13 +16,17 @@ function(embed_resource target file)
cmake_path(GET file FILENAME name)
string(MAKE_C_IDENTIFIER "${name}" name)
# 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}"
ARGS "-DEMBED_IN=${CMAKE_CURRENT_SOURCE_DIR}/${file}" "-DEMBED_OUT=${CMAKE_CURRENT_BINARY_DIR}/${name}.c" "-P" "${util_list_dir}/embed-resource.cmake"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${file}" "${util_list_dir}/embed-resource.cmake"
ARGS "-DEMBED_IN=${in}" "-DEMBED_OUT=${out}" "-P" "${script}"
DEPENDS "${in}" "${script}"
VERBATIM
)
# Add To Target
target_sources("${target}" PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/${name}.c")
target_sources("${target}" PRIVATE "${out}")
endfunction()
# Nicer Output
@ -31,3 +35,45 @@ function(message log_level)
_message("${log_level}" ${ARGN})
endif()
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)
# Install
install(TARGETS LIB_LIEF DESTINATION "${MCPI_LIB_DIR}")
if(BUILD_ARM_COMPONENTS)
install(TARGETS LIB_LIEF EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
endif()
setup_library(LIB_LIEF TRUE TRUE)
# License
install(FILES src/LICENSE DESTINATION "${MCPI_LEGAL_DIR}/LIEF")

View File

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

View File

@ -7,24 +7,12 @@ add_compile_options(-w)
# Build
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_compile_definitions(stb_image PUBLIC STBI_ONLY_PNG)
setup_header_dirs(stb_image "${CMAKE_CURRENT_SOURCE_DIR}/include")
# Install
install(TARGETS stb_image DESTINATION "${MCPI_LIB_DIR}")
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}")
setup_library(stb_image TRUE TRUE)
# License
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/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)
# Install
install(TARGETS reborn-util DESTINATION "${MCPI_LIB_DIR}")
# SDK
if(BUILD_ARM_COMPONENTS)
install(TARGETS reborn-util EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
install(DIRECTORY "include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/libreborn")
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/libreborn")
endif()
setup_header_dirs(reborn-util
"${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_BINARY_DIR}/include"
)
setup_library(reborn-util TRUE TRUE)
# Patch
if(BUILD_ARM_COMPONENTS)
@ -39,11 +29,9 @@ if(BUILD_ARM_COMPONENTS)
src/patch/instruction.cpp
)
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(TARGETS reborn-patch DESTINATION "${MCPI_LIB_DIR}")
# SDK
install(TARGETS reborn-patch EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
setup_library(reborn-patch TRUE TRUE)
endif()
# 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")
# Install
install(TARGETS fake-libpng DESTINATION "${MCPI_LIB_DIR}")
setup_library(fake-libpng TRUE FALSE)
endif()

View File

@ -2,22 +2,8 @@ project(media-layer)
# Add Headers
add_library(media-layer-headers INTERFACE)
target_include_directories(
media-layer-headers
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()
setup_header_dirs(media-layer-headers "${CMAKE_CURRENT_SOURCE_DIR}/include")
setup_library(media-layer-headers FALSE TRUE)
# Add Core
if(BUILD_MEDIA_LAYER_CORE)

View File

@ -1,7 +1,7 @@
project(media-layer-core)
# SDL Re-Implementation Using GLFW
set(CORE_SRC
# Build
add_library(media-layer-core-real SHARED
src/base.cpp
src/window/media.cpp
src/window/cursor.cpp
@ -14,14 +14,14 @@ set(CORE_SRC
src/audio/file.cpp
)
# Build
add_library(media-layer-core-real SHARED ${CORE_SRC}) # Dependencies Are Setup Later
# Set Name
set_target_properties(media-layer-core-real PROPERTIES OUTPUT_NAME "media-layer-core")
if(BUILD_NATIVE_COMPONENTS)
add_library(media-layer-core ALIAS media-layer-core-real)
endif()
# Install
install(TARGETS media-layer-core-real DESTINATION "${MCPI_LIB_DIR}")
setup_library(media-layer-core-real TRUE FALSE)
# Link
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_height;
glfwGetWindowSize(window, &window_width, &window_height);
if (window_width <= 0 || window_height <= 0) {
return;
}
// Get Framebuffer Size
int framebuffer_width;
int framebuffer_height;
glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
// Calculate Ratios
const double width_ratio = ((double) framebuffer_width) / ((double) window_width);
const double height_ratio = ((double) framebuffer_height) / ((double) window_height);
const double width_ratio = double(framebuffer_width) / double(window_width);
const double height_ratio = double(framebuffer_height) / double(window_height);
// Multiply
*xpos *= width_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_compile_definitions(media-layer-trampoline PRIVATE MEDIA_LAYER_TRAMPOLINE_HOST)
# Install
install(TARGETS media-layer-trampoline DESTINATION "${MCPI_LIB_DIR}")
setup_library(media-layer-trampoline TRUE TRUE)
elseif(BUILD_ARM_COMPONENTS)
# Guest Component
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)
# Install
if(MCPI_USE_MEDIA_LAYER_TRAMPOLINE)
install(TARGETS media-layer-core DESTINATION "${MCPI_LIB_DIR}")
endif()
install(TARGETS media-layer-core EXPORT sdk DESTINATION "${MCPI_SDK_LIB_DIR}")
setup_library(media-layer-core "${MCPI_USE_MEDIA_LAYER_TRAMPOLINE}" TRUE)
endif()

View File

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