From 386f52a85f10d30a26755181d7341d5911e13616 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Tue, 19 Nov 2024 19:57:43 -0500 Subject: [PATCH] CMake Refactor --- cmake/util/embed-resource.cmake | 3 -- cmake/util/util.cmake | 54 ++++++++++++++++++++++++-- dependencies/LIEF/CMakeLists.txt | 5 +-- dependencies/glfw/CMakeLists.txt | 2 +- dependencies/stb_image/CMakeLists.txt | 16 +------- libreborn/CMakeLists.txt | 28 ++++--------- media-layer/CMakeLists.txt | 18 +-------- media-layer/core/CMakeLists.txt | 10 ++--- media-layer/core/src/window/events.cpp | 7 +++- media-layer/trampoline/CMakeLists.txt | 7 +--- mods/CMakeLists.txt | 33 ++++++++-------- symbols/CMakeLists.txt | 21 ++-------- 12 files changed, 97 insertions(+), 107 deletions(-) diff --git a/cmake/util/embed-resource.cmake b/cmake/util/embed-resource.cmake index 8f864f7ea2..a020c08d20 100644 --- a/cmake/util/embed-resource.cmake +++ b/cmake/util/embed-resource.cmake @@ -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 \n" diff --git a/cmake/util/util.cmake b/cmake/util/util.cmake index 75c7d8b720..e614693d9b 100644 --- a/cmake/util/util.cmake +++ b/cmake/util/util.cmake @@ -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}" "$") + # 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}" "$") + 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() \ No newline at end of file diff --git a/dependencies/LIEF/CMakeLists.txt b/dependencies/LIEF/CMakeLists.txt index fc00c8f604..0f2b4088d8 100644 --- a/dependencies/LIEF/CMakeLists.txt +++ b/dependencies/LIEF/CMakeLists.txt @@ -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") diff --git a/dependencies/glfw/CMakeLists.txt b/dependencies/glfw/CMakeLists.txt index 21b4b5bd73..74941f5f62 100644 --- a/dependencies/glfw/CMakeLists.txt +++ b/dependencies/glfw/CMakeLists.txt @@ -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") diff --git a/dependencies/stb_image/CMakeLists.txt b/dependencies/stb_image/CMakeLists.txt index e4961b8fee..3b47ecde03 100644 --- a/dependencies/stb_image/CMakeLists.txt +++ b/dependencies/stb_image/CMakeLists.txt @@ -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 - "$" - "$" -) 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") diff --git a/libreborn/CMakeLists.txt b/libreborn/CMakeLists.txt index 0b54d7740c..c079a6c164 100644 --- a/libreborn/CMakeLists.txt +++ b/libreborn/CMakeLists.txt @@ -13,22 +13,12 @@ add_library(reborn-util SHARED src/util/cp437.cpp src/util/env.cpp ) -target_include_directories( - reborn-util - PUBLIC - "$" - "$" - "$" -) 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() diff --git a/media-layer/CMakeLists.txt b/media-layer/CMakeLists.txt index 55e4e7711f..f23e9cb7a3 100644 --- a/media-layer/CMakeLists.txt +++ b/media-layer/CMakeLists.txt @@ -2,22 +2,8 @@ project(media-layer) # Add Headers add_library(media-layer-headers INTERFACE) -target_include_directories( - media-layer-headers - INTERFACE - "$" - "$" -) -# 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) diff --git a/media-layer/core/CMakeLists.txt b/media-layer/core/CMakeLists.txt index 58b934ce65..ef2af87911 100644 --- a/media-layer/core/CMakeLists.txt +++ b/media-layer/core/CMakeLists.txt @@ -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) diff --git a/media-layer/core/src/window/events.cpp b/media-layer/core/src/window/events.cpp index 01b04ff03f..f53dff2a48 100644 --- a/media-layer/core/src/window/events.cpp +++ b/media-layer/core/src/window/events.cpp @@ -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; diff --git a/media-layer/trampoline/CMakeLists.txt b/media-layer/trampoline/CMakeLists.txt index 9543b59491..8214b643f3 100644 --- a/media-layer/trampoline/CMakeLists.txt +++ b/media-layer/trampoline/CMakeLists.txt @@ -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() diff --git a/mods/CMakeLists.txt b/mods/CMakeLists.txt index 93ef2bfd46..ffd99c00e1 100644 --- a/mods/CMakeLists.txt +++ b/mods/CMakeLists.txt @@ -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 - "$" - "$" + symbols + reborn-patch + media-layer-core + PRIVATE + stb_image + dl + pthread ) -# SDK -install(DIRECTORY "include/" DESTINATION "${MCPI_SDK_INCLUDE_DIR}/mods") diff --git a/symbols/CMakeLists.txt b/symbols/CMakeLists.txt index f596abbb0d..5f2ee4087a 100644 --- a/symbols/CMakeLists.txt +++ b/symbols/CMakeLists.txt @@ -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 - "$" - "$" -) +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) \ No newline at end of file