| cmake_minimum_required (VERSION 3.13) |
| |
| # CMake invokes the toolchain file twice during the first build, but only once |
| # during subsequent rebuilds. This was causing the various flags to be added |
| # twice on the first build, and on a rebuild ninja would see only one set of the |
| # flags and rebuild the world. |
| # https://github.com/android-ndk/ndk/issues/323 |
| if(RISCV_GCC_TOOLCHAIN_INCLUDED) |
| return() |
| endif(RISCV_GCC_TOOLCHAIN_INCLUDED) |
| set(RISCV_GCC_TOOLCHAIN_INCLUDED true) |
| |
| set(CMAKE_SYSTEM_PROCESSOR riscv32) |
| set(CMAKE_CROSSCOMPILING ON CACHE BOOL "") |
| |
| if(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux) |
| set(RISCV_HOST_TAG linux) |
| elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin) |
| set(RISCV_HOST_TAG darwin) |
| endif() |
| |
| set(RISCV_TOOLCHAIN_NAME gcc) |
| |
| set(RISCV_TOOLCHAIN_ROOT "$ENV{CACHE}/toolchain_kelvin" CACHE PATH "RISC-V compiler path") |
| set(CMAKE_FIND_ROOT_PATH ${RISCV_TOOLCHAIN_ROOT}) |
| list(APPEND CMAKE_PREFIX_PATH "${RISCV_TOOLCHAIN_ROOT}") |
| |
| set(CMAKE_C_COMPILER "${RISCV_TOOLCHAIN_ROOT}/bin/riscv32-unknown-elf-gcc") |
| set(CMAKE_CXX_COMPILER "${RISCV_TOOLCHAIN_ROOT}/bin/riscv32-unknown-elf-g++") |
| set(CMAKE_AR "${RISCV_TOOLCHAIN_ROOT}/bin/riscv32-unknown-elf-ar") |
| set(CMAKE_RANLIB "${RISCV_TOOLCHAIN_ROOT}/bin/riscv32-unknown-elf-ranlib") |
| set(CMAKE_STRIP "${RISCV_TOOLCHAIN_ROOT}/bin/riscv32-unknown-elf-strip") |
| |
| set(RISCV_COMPILER_FLAGS "" CACHE STRING "RISC-V compiler flags for C, CXX, and ASM") |
| set(RISCV_COMPILER_FLAGS_CXX) |
| set(RISCV_COMPILER_FLAGS_DEBUG) |
| set(RISCV_COMPILER_FLAGS_RELEASE) |
| set(RISCV_LINKER_FLAGS) |
| |
| set(CMAKE_SYSTEM_NAME Generic) |
| set(CMAKE_C_STANDARD 11) |
| set(CMAKE_C_EXTENSIONS OFF) # Force the usage of _ISOC11_SOURCE |
| set(CMAKE_SYSTEM_LIBRARY_PATH "${RISCV_TOOLCHAIN_ROOT}/riscv32-unknown-elf/lib") |
| set(RISCV_COMPILER_FLAGS "${RISCV_COMPILER_FLAGS} -march=rv32im -mabi=ilp32") |
| |
| add_compile_definitions(IREE_PLATFORM_GENERIC) |
| |
| # Use nano spec header and libraries. |
| include_directories(BEFORE SYSTEM |
| "${RISCV_TOOLCHAIN_ROOT}/riscv32-unknown-elf/include/newlib-nano/" |
| ) |
| set(RISCV_LINKER_FLAGS_EXE |
| "-L${RISCV_TOOLCHAIN_ROOT}/riscv32-unknown-elf/lib/newlib-nano/ -specs=nano.specs" |
| ) |
| |
| # TODO(lundong): Move no_warning_flags to IREE code base |
| set(C_NO_WARNING_FLAGS "-Wno-char-subscripts -Wno-format -Wno-incompatible-pointer-types -Wno-int-to-pointer-cast -Wstrict-aliasing=0 -Wno-pointer-to-int-cast") |
| |
| set(CMAKE_C_FLAGS "${RISCV_COMPILER_FLAGS} ${CMAKE_C_FLAGS} ${C_NO_WARNING_FLAGS}") |
| set(CMAKE_CXX_FLAGS "${RISCV_COMPILER_FLAGS} ${RISCV_COMPILER_FLAGS_CXX} ${CMAKE_CXX_FLAGS}") |
| set(CMAKE_ASM_FLAGS "${RISCV_COMPILER_FLAGS} ${CMAKE_ASM_FLAGS}") |
| set(CMAKE_C_FLAGS_DEBUG "${RISCV_COMPILER_FLAGS_DEBUG} ${CMAKE_C_FLAGS_DEBUG}") |
| set(CMAKE_CXX_FLAGS_DEBUG "${RISCV_COMPILER_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_DEBUG}") |
| set(CMAKE_ASM_FLAGS_DEBUG "${RISCV_COMPILER_FLAGS_DEBUG} ${CMAKE_ASM_FLAGS_DEBUG}") |
| set(CMAKE_C_FLAGS_RELEASE "${RISCV_COMPILER_FLAGS_RELEASE} ${CMAKE_C_FLAGS_RELEASE}") |
| set(CMAKE_CXX_FLAGS_RELEASE "${RISCV_COMPILER_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE}") |
| set(CMAKE_ASM_FLAGS_RELEASE "${RISCV_COMPILER_FLAGS_RELEASE} ${CMAKE_ASM_FLAGS_RELEASE}") |
| set(CMAKE_SHARED_LINKER_FLAGS "${RISCV_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}") |
| set(CMAKE_MODULE_LINKER_FLAGS "${RISCV_LINKER_FLAGS} ${CMAKE_MODULE_LINKER_FLAGS}") |
| set(CMAKE_EXE_LINKER_FLAGS "${RISCV_LINKER_FLAGS} ${RISCV_LINKER_FLAGS_EXE} ${CMAKE_EXE_LINKER_FLAGS}") |