From 355c4c079613d9c486d8ca6445535885774112e3 Mon Sep 17 00:00:00 2001 From: spicyjpeg Date: Fri, 12 Jan 2024 22:23:10 +0100 Subject: [PATCH] Update CMake scripts, shrink launchers to 32 KB --- .editorconfig | 13 +++++++ .gitignore | 4 ++ CMakeLists.txt | 75 ++++++++++++++++++++++++++++-------- cmake/executable.ld | 1 - cmake/setup.cmake | 58 ++-------------------------- cmake/toolchain.cmake | 47 +++++++++++----------- resources.json | 8 ++-- src/main/app/miscworkers.cpp | 12 +++--- 8 files changed, 113 insertions(+), 105 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..93c80e1 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +charset = utf-8 +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true + +[{*.yml,*.yaml,.clangd}] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore index 9f0a1d9..316edf0 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,10 @@ __pycache__/ *.pyc *.pyo +# Do not include user-specific workspace and configuration files. +*.code-workspace +CMakeUserPresets.json + # Do not include the dumps used to generate the cartdb files. #data/dumps/ #data/tests/ diff --git a/CMakeLists.txt b/CMakeLists.txt index a220faa..187535b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,6 +123,20 @@ target_compile_definitions( ## Executable launchers +# NOTE: in order to make sure -Os is passed after -Og or -O3 (see +# cmake/setup.cmake) and thus overrides it, it must be added to a separate +# target rather than directly to the executables. +add_library(launcherFlags INTERFACE) +target_compile_options(launcherFlags INTERFACE -Os) +target_compile_definitions( + launcherFlags INTERFACE + $, + #ENABLE_LOGGING=1 + , + #ENABLE_LOGGING=1 + > +) + function(addLauncher address stackTop) addExecutable( launcher${address} ${stackTop} @@ -135,26 +149,54 @@ function(addLauncher address stackTop) src/vendor/ffunicode.c src/vendor/printf.c ) - target_compile_definitions( - launcher${address} PRIVATE - $, - #ENABLE_LOGGING=1 - #ENABLE_FILE_WRITING=1 - , - #ENABLE_LOGGING=1 - #ENABLE_FILE_WRITING=1 - > - ) target_link_options(launcher${address} PRIVATE -Ttext=0x${address}) + target_link_libraries(launcher${address} PRIVATE launcherFlags) endfunction() -# Note that the launchers must be <48 KB (0xc000 bytes) in order for this to +# Note that the launchers must be <40 KB (0xa000 bytes) in order for this to # work properly. -addLauncher(801f4000 801ffff0) -addLauncher(803f4000 803ffff0) +addLauncher(801f8000 801ffff0) +addLauncher(803f8000 803ffff0) ## Default resource archive +function(addBinaryFile target name sizeName path) + set(_file "${PROJECT_BINARY_DIR}/includes/${target}_${name}.s") + cmake_path(ABSOLUTE_PATH path OUTPUT_VARIABLE _path) + + file( + CONFIGURE + OUTPUT "${_file}" + CONTENT [[ +.section .rodata.${name}, "a" +.balign 8 + +.global ${name} +.type ${name}, @object +.size ${name}, (${name}_end - ${name}) + +${name}: + .incbin "${_path}" +${name}_end: + +.section .rodata.${sizeName}, "a" +.balign 4 + +.global ${sizeName} +.type ${sizeName}, @object +.size ${sizeName}, 4 + +${sizeName}: + .int (${name}_end - ${name}) +]] + ESCAPE_QUOTES + NEWLINE_STYLE LF + ) + + target_sources(${target} PRIVATE "${_file}") + set_source_files_properties("${_file}" PROPERTIES OBJECT_DEPENDS "${_path}") +endfunction() + configure_file(resources.json resources.json ESCAPE_QUOTES) add_custom_command( COMMAND @@ -162,13 +204,12 @@ add_custom_command( "${PROJECT_SOURCE_DIR}/tools/buildResourceArchive.py" resources.json resources.zip OUTPUT resources.zip - DEPENDS resources.json launcher801f4000 launcher803f4000 + DEPENDS resources.json launcher801f8000 launcher803f8000 COMMENT "Building resource archive" VERBATIM ) -ps1_target_incbin( - main PRIVATE - .rodata _resources _resourcesSize +addBinaryFile( + main _resources _resourcesSize "${PROJECT_BINARY_DIR}/resources.zip" ) diff --git a/cmake/executable.ld b/cmake/executable.ld index 19bb263..73cabd7 100644 --- a/cmake/executable.ld +++ b/cmake/executable.ld @@ -14,7 +14,6 @@ * PERFORMANCE OF THIS SOFTWARE. */ -OUTPUT_FORMAT(elf32-littlemips) ENTRY(_start) MEMORY { diff --git a/cmake/setup.cmake b/cmake/setup.cmake index c2c44f2..74cbb48 100644 --- a/cmake/setup.cmake +++ b/cmake/setup.cmake @@ -44,9 +44,10 @@ target_compile_options( -ffunction-sections -fsigned-char -fno-strict-overflow - -msoft-float -march=r3000 -mabi=32 + -mfp32 + #-msoft-float -mno-mt -mno-llsc -mno-abicalls @@ -69,6 +70,7 @@ target_compile_options( # These options will be added if CMAKE_BUILD_TYPE is not set to Debug. #-O3 #-flto + -mno-check-zero-division > ) target_link_options( @@ -79,57 +81,3 @@ target_link_options( -G8 -T${CMAKE_CURRENT_LIST_DIR}/executable.ld ) - -# Define a new ps1_target_incbin() command to embed the contents of a binary -# file into an executable. This is accomplished by generating an assembly -# listing that uses the .incbin directive to embed the file from a template. -function(ps1_target_incbin - target type section symbolName sizeSymbolName path -) - string(MAKE_C_IDENTIFIER "${symbolName}" _symbolName) - string(MAKE_C_IDENTIFIER "${sizeSymbolName}" _sizeSymbolName) - - cmake_path(ABSOLUTE_PATH path OUTPUT_VARIABLE _path) - - # Generate a unique name for the assembly file by hashing the target name - # and symbol name, and place it in the current build directory. - string(SHA1 _hash "${target} ${_symbolName}") - set(_assemblyFile "${CMAKE_CURRENT_BINARY_DIR}/incbin_${_hash}.s") - - file( - CONFIGURE - OUTPUT "${_assemblyFile}" - CONTENT [[ -.section ${section}.${_symbolName}, "a" -.balign 8 - -.global ${_symbolName} -.local ${_symbolName}_end -.type ${_symbolName}, @object -.size ${_symbolName}, (${_symbolName}_end - ${_symbolName}) - -${_symbolName}: - .incbin "${_path}" -${_symbolName}_end: - -.section ${section}.${_sizeSymbolName}, "a" -.balign 4 - -.global ${_sizeSymbolName} -.type ${_sizeSymbolName}, @object -.size ${_sizeSymbolName}, 4 - -${_sizeSymbolName}: - .int (${_symbolName}_end - ${_symbolName}) -]] - ESCAPE_QUOTES - NEWLINE_STYLE LF - ) - - # Add the assembly file to the target and add the embedded binary file as a - # dependency to make sure it is built first if it needs to be built. - target_sources("${target}" "${type}" "${_assemblyFile}") - set_source_files_properties( - "${_assemblyFile}" PROPERTIES OBJECT_DEPENDS "${_path}" - ) -endfunction() diff --git a/cmake/toolchain.cmake b/cmake/toolchain.cmake index c9be047..1ff733c 100644 --- a/cmake/toolchain.cmake +++ b/cmake/toolchain.cmake @@ -14,16 +14,12 @@ cmake_minimum_required(VERSION 3.25) -# Create two user-editable variables to allow for a custom compiler toolchain to -# be used by passing -DTOOLCHAIN_PATH=... and -DTOOLCHAIN_TARGET=... to CMake. +# Create a user-editable variable to allow for a custom toolchain path to be +# specified by passing -DTOOLCHAIN_PATH=... to CMake. set( TOOLCHAIN_PATH "" CACHE PATH "Directory containing GCC toolchain executables (if not listed in PATH)" ) -set( - TOOLCHAIN_TARGET mipsel-none-elf CACHE STRING - "GCC toolchain target triplet" -) # Prevent CMake from using any host compiler by manually overriding the platform # and setting it to "generic" (i.e. no defaults). @@ -47,11 +43,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) ## Toolchain path setup -# Attempt to locate the GCC command (mipsel-none-elf-gcc) in the provided path -# (if any) as well as in the system's standard paths for programs such as the -# ones listed in the PATH environment variable. +# Attempt to locate the GCC command in the provided path (if any) as well as in +# the system's standard paths for programs such as the ones listed in the PATH +# environment variable. Try to use a mipsel-none-elf toolchain over a +# mipsel-linux-gnu one if available. find_program( - _gccPath ${TOOLCHAIN_TARGET}-gcc + _gccPath + mipsel-none-elf-gcc + mipsel-unknown-elf-gcc + mipsel-linux-gnu-gcc HINTS "${TOOLCHAIN_PATH}" "${TOOLCHAIN_PATH}/bin" @@ -72,19 +72,22 @@ endif() # Set the paths to all tools required by CMake. The appropriate extension for # executables (.exe on Windows, none on Unix) is extracted from the path to GCC -# using a regular expression, as CMake does not otherwise expose it. -set(_prefix "${_toolchainPath}/${TOOLCHAIN_TARGET}-") -string(REGEX MATCH ".+-gcc(.*)$" _dummy "${_gccPath}") +# using a regular expression, as CMake does not otherwise expose it when +# cross-compiling. +string(REGEX MATCH "^(.+-)gcc(.*)$" _dummy "${_gccPath}") -set(CMAKE_ASM_COMPILER "${_prefix}gcc${CMAKE_MATCH_1}") -set(CMAKE_C_COMPILER "${_prefix}gcc${CMAKE_MATCH_1}") -set(CMAKE_CXX_COMPILER "${_prefix}g++${CMAKE_MATCH_1}") -set(CMAKE_AR "${_prefix}ar${CMAKE_MATCH_1}") -set(CMAKE_LINKER "${_prefix}ld${CMAKE_MATCH_1}") -set(CMAKE_RANLIB "${_prefix}ranlib${CMAKE_MATCH_1}") -set(CMAKE_OBJCOPY "${_prefix}objcopy${CMAKE_MATCH_1}") -set(CMAKE_SIZE "${_prefix}size${CMAKE_MATCH_1}") -set(CMAKE_STRIP "${_prefix}strip${CMAKE_MATCH_1}") +set(CMAKE_ASM_COMPILER "${CMAKE_MATCH_1}gcc${CMAKE_MATCH_2}") +set(CMAKE_C_COMPILER "${CMAKE_MATCH_1}gcc${CMAKE_MATCH_2}") +set(CMAKE_CXX_COMPILER "${CMAKE_MATCH_1}g++${CMAKE_MATCH_2}") +set(CMAKE_AR "${CMAKE_MATCH_1}ar${CMAKE_MATCH_2}") +set(CMAKE_LINKER "${CMAKE_MATCH_1}ld${CMAKE_MATCH_2}") +set(CMAKE_RANLIB "${CMAKE_MATCH_1}ranlib${CMAKE_MATCH_2}") +set(CMAKE_OBJCOPY "${CMAKE_MATCH_1}objcopy${CMAKE_MATCH_2}") +set(CMAKE_OBJDUMP "${CMAKE_MATCH_1}objdump${CMAKE_MATCH_2}") +set(CMAKE_NM "${CMAKE_MATCH_1}nm${CMAKE_MATCH_2}") +set(CMAKE_SIZE "${CMAKE_MATCH_1}size${CMAKE_MATCH_2}") +set(CMAKE_STRIP "${CMAKE_MATCH_1}strip${CMAKE_MATCH_2}") +set(CMAKE_READELF "${CMAKE_MATCH_1}readelf${CMAKE_MATCH_2}") # Continue initialization by running setup.cmake after project() is invoked. set(CMAKE_PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/setup.cmake") diff --git a/resources.json b/resources.json index 6b14ea6..6f55ff0 100644 --- a/resources.json +++ b/resources.json @@ -88,12 +88,12 @@ { "type": "binary", - "name": "launchers/801f4000.psexe", - "source": "launcher801f4000.psexe" + "name": "launchers/801f8000.psexe", + "source": "launcher801f8000.psexe" }, { "type": "binary", - "name": "launchers/803f4000.psexe", - "source": "launcher803f4000.psexe" + "name": "launchers/803f8000.psexe", + "source": "launcher803f8000.psexe" } ] diff --git a/src/main/app/miscworkers.cpp b/src/main/app/miscworkers.cpp index 3c7f63a..d62c45a 100644 --- a/src/main/app/miscworkers.cpp +++ b/src/main/app/miscworkers.cpp @@ -270,13 +270,13 @@ public: static const LauncherEntry _LAUNCHERS[]{ { - .path = "launchers/801f4000.psexe", - .loadOffset = 0x801f4000, - .length = 0xc000 + .path = "launchers/801f8000.psexe", + .loadOffset = 0x801f8000, + .length = 0x8000 }, { - .path = "launchers/803f4000.psexe", - .loadOffset = 0x803f4000, - .length = 0xc000 + .path = "launchers/803f8000.psexe", + .loadOffset = 0x803f8000, + .length = 0x8000 } };