2024-12-17 12:34:07 +01:00
|
|
|
cmake_minimum_required(VERSION 3.12)
|
|
|
|
|
|
|
|
# Set default RISC-V toolchain if not specified
|
|
|
|
if(NOT DEFINED RISCV_TOOLCHAIN_PATH)
|
|
|
|
set(RISCV_TOOLCHAIN_PATH "/opt/shared/cross-toolchains/gcc13/CentOS/riscv64-unknown-elf/bin" CACHE STRING "Path to RISC-V toolchain")
|
2023-08-20 16:45:54 +02:00
|
|
|
endif()
|
2024-12-17 12:34:07 +01:00
|
|
|
|
|
|
|
# Allow override of compiler executables
|
|
|
|
if(NOT DEFINED RISCV_GCC)
|
|
|
|
set(RISCV_GCC "/opt/shared/cross-toolchains/gcc13/CentOS/riscv64-unknown-elf/bin/riscv64-unknown-elf-gcc" CACHE STRING "RISC-V GCC compiler")
|
2023-08-20 16:39:20 +02:00
|
|
|
endif()
|
2024-12-17 12:34:07 +01:00
|
|
|
|
|
|
|
if(NOT DEFINED RISCV_GXX)
|
|
|
|
set(RISCV_GXX "${RISCV_TOOLCHAIN_PATH}/riscv64-unknown-elf-g++" CACHE STRING "RISC-V G++ compiler")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT DEFINED RISCV_OBJCOPY)
|
|
|
|
set(RISCV_OBJCOPY "${RISCV_TOOLCHAIN_PATH}/riscv64-unknown-elf-objcopy" CACHE STRING "RISC-V objcopy")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT DEFINED RISCV_OBJDUMP)
|
|
|
|
set(RISCV_OBJDUMP "${RISCV_TOOLCHAIN_PATH}/riscv64-unknown-elf-objdump" CACHE STRING "RISC-V objdump")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Set the compilers
|
|
|
|
set(CMAKE_C_COMPILER ${RISCV_GCC})
|
|
|
|
set(CMAKE_CXX_COMPILER ${RISCV_GXX})
|
|
|
|
|
|
|
|
project(Firmware)
|
|
|
|
|
|
|
|
# Define supported configurations
|
|
|
|
set(SUPPORTED_BOARDS iss)
|
|
|
|
set(SUPPORTED_ISAS "rv32i;rv32im;rv32imc;rv64i;rv64im;rv64imc;imc")
|
|
|
|
set(SUPPORTED_ABIS "ilp32;ilp32f;lp64;lp64f")
|
|
|
|
|
|
|
|
# Build target options
|
|
|
|
option(BUILD_HELLO_WORLD "Build hello-world example" ON)
|
|
|
|
option(BUILD_DHRYSTONE "Build dhrystone benchmark" OFF)
|
|
|
|
option(BUILD_COREMARK "Build coremark benchmark" OFF)
|
|
|
|
option(BUILD_ALL "Build all targets" OFF)
|
|
|
|
|
|
|
|
# If BUILD_ALL is ON, enable all targets
|
|
|
|
if(BUILD_ALL)
|
|
|
|
set(BUILD_HELLO_WORLD ON)
|
|
|
|
set(BUILD_DHRYSTONE ON)
|
|
|
|
set(BUILD_COREMARK ON)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Set default values and validate configurations
|
|
|
|
if(NOT DEFINED BOARD)
|
|
|
|
set(BOARD iss CACHE STRING "Target board")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT DEFINED ISA)
|
|
|
|
set(ISA imc CACHE STRING "Target ISA")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT DEFINED RISCV_ABI)
|
|
|
|
if(ISA MATCHES "^rv64")
|
|
|
|
set(RISCV_ABI "lp64" CACHE STRING "RISC-V ABI")
|
|
|
|
else()
|
|
|
|
set(RISCV_ABI "ilp32" CACHE STRING "RISC-V ABI")
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Validate configurations
|
|
|
|
if(NOT BOARD IN_LIST SUPPORTED_BOARDS)
|
|
|
|
message(FATAL_ERROR "Invalid BOARD specified. Supported boards: ${SUPPORTED_BOARDS}")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT ISA IN_LIST SUPPORTED_ISAS)
|
|
|
|
message(FATAL_ERROR "Invalid ISA specified. Supported ISAs: ${SUPPORTED_ISAS}")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(NOT RISCV_ABI IN_LIST SUPPORTED_ABIS)
|
|
|
|
message(FATAL_ERROR "Invalid ABI specified. Supported ABIs: ${SUPPORTED_ABIS}")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Set RISC-V architecture based on ISA
|
|
|
|
if(ISA MATCHES "^rv")
|
|
|
|
set(RISCV_ARCH ${ISA})
|
|
|
|
else()
|
|
|
|
# Default to rv32 for backward compatibility
|
|
|
|
set(RISCV_ARCH "rv32${ISA}")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Set BSP base directory
|
|
|
|
set(BSP_BASE "${CMAKE_CURRENT_SOURCE_DIR}/bare-metal-bsp")
|
|
|
|
|
|
|
|
# Global compile definitions
|
|
|
|
add_compile_definitions(BOARD_${BOARD})
|
|
|
|
|
|
|
|
# RISC-V specific compiler flags
|
|
|
|
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${RISCV_ARCH}_zicsr_zifencei -mabi=${RISCV_ABI}")
|
|
|
|
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=${RISCV_ARCH}_zicsr_zifencei -mabi=${RISCV_ABI}")
|
|
|
|
|
|
|
|
# Optional: Enable semihosting support
|
|
|
|
option(SEMIHOSTING "Enable semihosting support" OFF)
|
|
|
|
if(SEMIHOSTING)
|
|
|
|
add_compile_definitions(SEMIHOSTING)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
#create interface library for propagating compile options
|
|
|
|
add_library(global_compile_options INTERFACE)
|
|
|
|
|
|
|
|
# Compile options
|
|
|
|
target_compile_options(global_compile_options INTERFACE
|
|
|
|
-march=${RISCV_ARCH}_zicsr_zifencei
|
|
|
|
-mabi=${RISCV_ABI}
|
|
|
|
-mcmodel=medany
|
|
|
|
-O2
|
|
|
|
-g
|
|
|
|
# -ffunction-sections
|
|
|
|
# -fdata-sections
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
message(STATUS "Building firmware with configuration:")
|
|
|
|
message(STATUS " Board: ${BOARD}")
|
|
|
|
message(STATUS " ISA: ${ISA} (Architecture: ${RISCV_ARCH})")
|
|
|
|
message(STATUS " ABI: ${RISCV_ABI}")
|
|
|
|
message(STATUS " Semihosting: ${SEMIHOSTING}")
|
|
|
|
message(STATUS " Toolchain:")
|
|
|
|
message(STATUS " Path: ${RISCV_TOOLCHAIN_PATH}")
|
|
|
|
message(STATUS " C Compiler: ${CMAKE_C_COMPILER}")
|
|
|
|
message(STATUS " C++ Compiler: ${CMAKE_CXX_COMPILER}")
|
|
|
|
message(STATUS "Targets to build:")
|
|
|
|
message(STATUS " hello-world: ${BUILD_HELLO_WORLD}")
|
|
|
|
message(STATUS " dhrystone: ${BUILD_DHRYSTONE}")
|
|
|
|
message(STATUS " coremark: ${BUILD_COREMARK}")
|
|
|
|
|
|
|
|
|
|
|
|
add_subdirectory(bare-metal-bsp)
|
|
|
|
|
|
|
|
# Add subdirectories based on build options
|
|
|
|
if(BUILD_HELLO_WORLD)
|
|
|
|
add_subdirectory(hello-world)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
|
|
if(BUILD_DHRYSTONE)
|
|
|
|
add_subdirectory(benchmarks/dhrystone)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(BUILD_COREMARK)
|
|
|
|
add_subdirectory(benchmarks/coremark)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Create an all-inclusive target only if BUILD_ALL is ON
|
|
|
|
if(BUILD_ALL)
|
|
|
|
add_custom_target(fw-common ALL
|
|
|
|
DEPENDS
|
|
|
|
hello-world
|
|
|
|
dhrystone
|
|
|
|
coremark
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Print build instructions
|
|
|
|
message(STATUS "")
|
|
|
|
message(STATUS "Build instructions:")
|
|
|
|
message(STATUS " Build all targets: cmake -DBUILD_ALL=ON ..")
|
|
|
|
message(STATUS " Build specific target: cmake -DBUILD_HELLO_WORLD=ON -DBUILD_DHRYSTONE=OFF -DBUILD_COREMARK=OFF ..")
|
|
|
|
message(STATUS " Configure board: cmake -DBOARD=iss ..")
|
|
|
|
message(STATUS " Configure ISA: cmake -DISA=rv32imc ..")
|
|
|
|
message(STATUS " Configure ABI: cmake -DRISCV_ABI=ilp32 ..")
|
|
|
|
message(STATUS " Enable semihosting: cmake -DSEMIHOSTING=ON ..")
|
|
|
|
message(STATUS " Set toolchain path: cmake -DRISCV_TOOLCHAIN_PATH=/path/to/toolchain ..")
|
|
|
|
message(STATUS " Set specific compiler: cmake -DRISCV_GCC=/path/to/riscv-gcc -DRISCV_GXX=/path/to/riscv-g++ ..")
|