# ps1-bare-metal - Copyright (C) 2023-2026 spicyjpeg # SPDX-License-Identifier: MIT cmake_minimum_required(VERSION 3.30) # Set up a cache 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 (if not listed in PATH)" ) # Put CMake in cross-compiling mode by setting the target platform to "generic" # and preventing usage of any external libraries. set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_SYSTEM_PROCESSOR mipsel) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) # When using a custom toolchain script, CMake will test it by attempting to # compile a dummy project. Make sure TOOLCHAIN_PATH is passed through to the # dummy project and change the target type to a library to prevent the linker # from running (it would fail due to missing flags). set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES TOOLCHAIN_PATH) # Disable support for C++ modules. If not disabled CMake will also attempt to # test this feature, resulting in a failure with certain GCC versions. set(CMAKE_CXX_SCAN_FOR_MODULES OFF) # Generate compile_commands.json when building. This allows some IDEs and tools # (such as clangd) to automatically detect include paths and compiler flags. set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Search TOOLCHAIN_PATH (if specified) as well as the system's PATH for the GCC # executable. Prefer a mipsel-none-elf toolchain over a mipsel-linux-gnu one if # available. find_program( gccBinaryPath mipsel-none-elf-gcc mipsel-unknown-elf-gcc mipsel-linux-gnu-gcc HINTS "${TOOLCHAIN_PATH}" "${TOOLCHAIN_PATH}/bin" "${TOOLCHAIN_PATH}/../bin" NO_CACHE ) if("${gccBinaryPath}" STREQUAL "gccBinaryPath-NOTFOUND") message(FATAL_ERROR "Unable to find the GCC toolchain. Ensure your PATH \ environment variable includes the full path to the toolchain's /bin subfolder, \ or pass -DTOOLCHAIN_PATH=... to CMake to specify its location manually.") endif() cmake_path(GET gccBinaryPath PARENT_PATH toolchainPath) # If TOOLCHAIN_PATH was not provided but GCC was found in another location, # update it to avoid searching again the next time the project is configured. if(NOT IS_DIRECTORY "${TOOLCHAIN_PATH}") set( TOOLCHAIN_PATH "${toolchainPath}" CACHE PATH "Directory containing GCC toolchain (if not listed in PATH)" FORCE ) endif() # Set the paths to all tools. 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 when cross-compiling. string(REGEX MATCH "^(.+-)gcc(.*)$" dummy "${gccBinaryPath}") 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}gcc${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}")