Skip to content

Instantly share code, notes, and snippets.

@709924470
Last active April 11, 2024 10:29
Show Gist options
  • Save 709924470/840e1a428c68b36d862a343c032445fa to your computer and use it in GitHub Desktop.
Save 709924470/840e1a428c68b36d862a343c032445fa to your computer and use it in GitHub Desktop.
MIPS CMake Toolchain file
# This file is derived from RISC-V CMake cross compile template.
# Please install or put MIPS-MTI-ELF(or other) toolchain in your path before using this toolchain file.
#include(CMakeForceCompiler)
# usage
# cmake -DCMAKE_TOOLCHAIN_FILE={PATH_TO_TOOLCHAINS}/mips-mti.cmake {BUILD_SOURCE}
# or
# include(${CMAKE_CURRENT_SOURCE_DIR}/{PATH_TO_TOOLCHAINS}/mips-mti.cmake)
# Look for GCC in path
# Change mips-mti-elf to mips-img-elf or nanomips-elf accordingly.
FIND_FILE( MIPS_GCC_COMPILER_EXE "mips-mti-elf-gcc.exe" PATHS ENV INCLUDE)
FIND_FILE( MIPS_GCC_COMPILER "mips-mti-elf-gcc" PATHS ENV INCLUDE)
# Select which is found
if (EXISTS ${MIPS_GCC_COMPILER})
set( MIPS_CC_COMPILER ${MIPS_GCC_COMPILER})
elseif (EXISTS ${MIPS_GCC_COMPILER_EXE})
set( MIPS_CC_COMPILER ${MIPS_GCC_COMPILER_EXE})
else()
message(FATAL_ERROR "MIPS GCC not found. Make sure following executables in your env: ${MIPS_GCC_COMPILER} ${MIPS_GCC_COMPILER_EXE}")
endif()
message( "MIPS GCC found: ${MIPS_CC_COMPILER}" )
get_filename_component(MIPS_TOOLCHAIN_BIN_PATH ${MIPS_CC_COMPILER} DIRECTORY)
get_filename_component(MIPS_TOOLCHAIN_BIN_GCC ${MIPS_CC_COMPILER} NAME_WE)
get_filename_component(MIPS_TOOLCHAIN_BIN_EXT ${MIPS_CC_COMPILER} EXT)
message( "MIPS Toolchain Path: ${MIPS_TOOLCHAIN_BIN_PATH}" )
STRING(REGEX REPLACE "\-gcc" "-" CROSS_COMPILE ${MIPS_TOOLCHAIN_BIN_GCC})
message( "MIPS Cross Compile: ${CROSS_COMPILE}" )
# The Generic system name is used for embedded targets (targets without OS) in.
# Make changes if and only if you know what you are doing.
# CMake
set( CMAKE_SYSTEM_NAME Generic )
set( CMAKE_SYSTEM_PROCESSOR mips32r2 )
set( CMAKE_EXECUTABLE_SUFFIX ".elf")
# specify the cross compiler. We force the compiler so that CMake doesn't
# attempt to build a simple test program as this will fail without us using
# the -nostartfiles option on the command line
set(CMAKE_ASM_COMPILER {CROSS_COMPILE}gcc )
set(CMAKE_AR ${CROSS_COMPILE}ar)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILE}gcc)
set(CMAKE_C_COMPILER ${CROSS_COMPILE}gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++)
# We must set the OBJCOPY setting into cache so that it's available to the
# whole project. Otherwise, this does not get set into the CACHE and therefore
# the build doesn't know what the OBJCOPY filepath is
set( CMAKE_OBJCOPY ${MIPS_TOOLCHAIN_BIN_PATH}/${CROSS_COMPILE}objcopy
CACHE FILEPATH "The toolchain objcopy command " FORCE )
set( CMAKE_OBJDUMP ${MIPS_TOOLCHAIN_BIN_PATH}/${CROSS_COMPILE}objdump
CACHE FILEPATH "The toolchain objdump command " FORCE )
# Set the common build flags
# Read the docs on MIPS compile flags: https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html
set( MIPS_FLAGS "-EL -mhard-float -mno-mips16 -mno-micromips -mmcu" )
# Set the CMAKE C flags (which should also be used by the assembler!)
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fmessage-length=0 -fno-common -G0" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR} ${MIPS_FLAGS}" )
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" )
set( CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -MMD" CACHE STRING "" )
set( CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" )
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR} -nostartfiles " )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment