Update CMake scripts, shrink launchers to 32 KB

This commit is contained in:
spicyjpeg 2024-01-12 22:23:10 +01:00
parent 4bd8b02cb9
commit 355c4c0796
No known key found for this signature in database
GPG Key ID: 5CC87404C01DF393
8 changed files with 113 additions and 105 deletions

13
.editorconfig Normal file
View File

@ -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

4
.gitignore vendored
View File

@ -10,6 +10,10 @@ __pycache__/
*.pyc *.pyc
*.pyo *.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. # Do not include the dumps used to generate the cartdb files.
#data/dumps/ #data/dumps/
#data/tests/ #data/tests/

View File

@ -123,6 +123,20 @@ target_compile_definitions(
## Executable launchers ## 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
$<IF:$<CONFIG:Debug>,
#ENABLE_LOGGING=1
,
#ENABLE_LOGGING=1
>
)
function(addLauncher address stackTop) function(addLauncher address stackTop)
addExecutable( addExecutable(
launcher${address} ${stackTop} launcher${address} ${stackTop}
@ -135,26 +149,54 @@ function(addLauncher address stackTop)
src/vendor/ffunicode.c src/vendor/ffunicode.c
src/vendor/printf.c src/vendor/printf.c
) )
target_compile_definitions(
launcher${address} PRIVATE
$<IF:$<CONFIG:Debug>,
#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_options(launcher${address} PRIVATE -Ttext=0x${address})
target_link_libraries(launcher${address} PRIVATE launcherFlags)
endfunction() 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. # work properly.
addLauncher(801f4000 801ffff0) addLauncher(801f8000 801ffff0)
addLauncher(803f4000 803ffff0) addLauncher(803f8000 803ffff0)
## Default resource archive ## 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) configure_file(resources.json resources.json ESCAPE_QUOTES)
add_custom_command( add_custom_command(
COMMAND COMMAND
@ -162,13 +204,12 @@ add_custom_command(
"${PROJECT_SOURCE_DIR}/tools/buildResourceArchive.py" "${PROJECT_SOURCE_DIR}/tools/buildResourceArchive.py"
resources.json resources.zip resources.json resources.zip
OUTPUT resources.zip OUTPUT resources.zip
DEPENDS resources.json launcher801f4000 launcher803f4000 DEPENDS resources.json launcher801f8000 launcher803f8000
COMMENT "Building resource archive" COMMENT "Building resource archive"
VERBATIM VERBATIM
) )
ps1_target_incbin( addBinaryFile(
main PRIVATE main _resources _resourcesSize
.rodata _resources _resourcesSize
"${PROJECT_BINARY_DIR}/resources.zip" "${PROJECT_BINARY_DIR}/resources.zip"
) )

View File

@ -14,7 +14,6 @@
* PERFORMANCE OF THIS SOFTWARE. * PERFORMANCE OF THIS SOFTWARE.
*/ */
OUTPUT_FORMAT(elf32-littlemips)
ENTRY(_start) ENTRY(_start)
MEMORY { MEMORY {

View File

@ -44,9 +44,10 @@ target_compile_options(
-ffunction-sections -ffunction-sections
-fsigned-char -fsigned-char
-fno-strict-overflow -fno-strict-overflow
-msoft-float
-march=r3000 -march=r3000
-mabi=32 -mabi=32
-mfp32
#-msoft-float
-mno-mt -mno-mt
-mno-llsc -mno-llsc
-mno-abicalls -mno-abicalls
@ -69,6 +70,7 @@ target_compile_options(
# These options will be added if CMAKE_BUILD_TYPE is not set to Debug. # These options will be added if CMAKE_BUILD_TYPE is not set to Debug.
#-O3 #-O3
#-flto #-flto
-mno-check-zero-division
> >
) )
target_link_options( target_link_options(
@ -79,57 +81,3 @@ target_link_options(
-G8 -G8
-T${CMAKE_CURRENT_LIST_DIR}/executable.ld -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()

View File

@ -14,16 +14,12 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
# Create two user-editable variables to allow for a custom compiler toolchain to # Create a user-editable variable to allow for a custom toolchain path to be
# be used by passing -DTOOLCHAIN_PATH=... and -DTOOLCHAIN_TARGET=... to CMake. # specified by passing -DTOOLCHAIN_PATH=... to CMake.
set( set(
TOOLCHAIN_PATH "" CACHE PATH TOOLCHAIN_PATH "" CACHE PATH
"Directory containing GCC toolchain executables (if not listed in 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 # Prevent CMake from using any host compiler by manually overriding the platform
# and setting it to "generic" (i.e. no defaults). # and setting it to "generic" (i.e. no defaults).
@ -47,11 +43,15 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
## Toolchain path setup ## Toolchain path setup
# Attempt to locate the GCC command (mipsel-none-elf-gcc) in the provided path # Attempt to locate the GCC command in the provided path (if any) as well as in
# (if any) as well as in the system's standard paths for programs such as the # the system's standard paths for programs such as the ones listed in the PATH
# ones listed in the PATH environment variable. # environment variable. Try to use a mipsel-none-elf toolchain over a
# mipsel-linux-gnu one if available.
find_program( find_program(
_gccPath ${TOOLCHAIN_TARGET}-gcc _gccPath
mipsel-none-elf-gcc
mipsel-unknown-elf-gcc
mipsel-linux-gnu-gcc
HINTS HINTS
"${TOOLCHAIN_PATH}" "${TOOLCHAIN_PATH}"
"${TOOLCHAIN_PATH}/bin" "${TOOLCHAIN_PATH}/bin"
@ -72,19 +72,22 @@ endif()
# Set the paths to all tools required by CMake. The appropriate extension for # 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 # 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. # using a regular expression, as CMake does not otherwise expose it when
set(_prefix "${_toolchainPath}/${TOOLCHAIN_TARGET}-") # cross-compiling.
string(REGEX MATCH ".+-gcc(.*)$" _dummy "${_gccPath}") string(REGEX MATCH "^(.+-)gcc(.*)$" _dummy "${_gccPath}")
set(CMAKE_ASM_COMPILER "${_prefix}gcc${CMAKE_MATCH_1}") set(CMAKE_ASM_COMPILER "${CMAKE_MATCH_1}gcc${CMAKE_MATCH_2}")
set(CMAKE_C_COMPILER "${_prefix}gcc${CMAKE_MATCH_1}") set(CMAKE_C_COMPILER "${CMAKE_MATCH_1}gcc${CMAKE_MATCH_2}")
set(CMAKE_CXX_COMPILER "${_prefix}g++${CMAKE_MATCH_1}") set(CMAKE_CXX_COMPILER "${CMAKE_MATCH_1}g++${CMAKE_MATCH_2}")
set(CMAKE_AR "${_prefix}ar${CMAKE_MATCH_1}") set(CMAKE_AR "${CMAKE_MATCH_1}ar${CMAKE_MATCH_2}")
set(CMAKE_LINKER "${_prefix}ld${CMAKE_MATCH_1}") set(CMAKE_LINKER "${CMAKE_MATCH_1}ld${CMAKE_MATCH_2}")
set(CMAKE_RANLIB "${_prefix}ranlib${CMAKE_MATCH_1}") set(CMAKE_RANLIB "${CMAKE_MATCH_1}ranlib${CMAKE_MATCH_2}")
set(CMAKE_OBJCOPY "${_prefix}objcopy${CMAKE_MATCH_1}") set(CMAKE_OBJCOPY "${CMAKE_MATCH_1}objcopy${CMAKE_MATCH_2}")
set(CMAKE_SIZE "${_prefix}size${CMAKE_MATCH_1}") set(CMAKE_OBJDUMP "${CMAKE_MATCH_1}objdump${CMAKE_MATCH_2}")
set(CMAKE_STRIP "${_prefix}strip${CMAKE_MATCH_1}") 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. # Continue initialization by running setup.cmake after project() is invoked.
set(CMAKE_PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/setup.cmake") set(CMAKE_PROJECT_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/setup.cmake")

View File

@ -88,12 +88,12 @@
{ {
"type": "binary", "type": "binary",
"name": "launchers/801f4000.psexe", "name": "launchers/801f8000.psexe",
"source": "launcher801f4000.psexe" "source": "launcher801f8000.psexe"
}, },
{ {
"type": "binary", "type": "binary",
"name": "launchers/803f4000.psexe", "name": "launchers/803f8000.psexe",
"source": "launcher803f4000.psexe" "source": "launcher803f8000.psexe"
} }
] ]

View File

@ -270,13 +270,13 @@ public:
static const LauncherEntry _LAUNCHERS[]{ static const LauncherEntry _LAUNCHERS[]{
{ {
.path = "launchers/801f4000.psexe", .path = "launchers/801f8000.psexe",
.loadOffset = 0x801f4000, .loadOffset = 0x801f8000,
.length = 0xc000 .length = 0x8000
}, { }, {
.path = "launchers/803f4000.psexe", .path = "launchers/803f8000.psexe",
.loadOffset = 0x803f4000, .loadOffset = 0x803f8000,
.length = 0xc000 .length = 0x8000
} }
}; };