| # Copyright 2023 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # If set to true, build for IREE using clang. If false, build for gcc. |
| set(BUILD_WITH_CLANG ON CACHE BOOL "Build the library with clang.") |
| |
| set(CMAKE_SYSTEM_NAME Generic) |
| set(CMAKE_SYSTEM_ABI ilp32) |
| set(CMAKE_EXECUTABLE_SUFFIX ".elf") |
| |
| set(VEC_DEFAULT_COPTS |
| "-Wall" |
| "-O0" |
| "-g3" |
| "-ggdb" |
| "-Werror" |
| "-ffreestanding" |
| "-ffunction-sections" |
| "-fstack-usage" |
| ) |
| |
| set(VEC_DEFAULT_LINKOPTS |
| -Wl,--gc-sections |
| -Wl,--print-memory-usage |
| -Wl,-Map=${PROJECT_NAME}.map |
| ) |
| |
| if(${BUILD_WITH_CLANG}) |
| if(${BUILD_SIMPLIFIED_CORE}) |
| set(CMAKE_SYSTEM_PROCESSOR rv32im) |
| else() |
| set(CMAKE_SYSTEM_PROCESSOR rv32imfv1p0) |
| endif() |
| set(RISCV_TOOLCHAIN_ROOT "$ENV{CACHE}/toolchain_iree_rv32imf/bin/" CACHE PATH "RISC-V toolchain root path") |
| message (STATUS "RISC-V toolchain path is ${RISCV_TOOLCHAIN_ROOT}") |
| find_file(RISCV_COMPILER "clang" HINTS ${RISCV_TOOLCHAIN_ROOT} PATHS ENV INCLUDE) |
| |
| set(CMAKE_FIND_ROOT_PATH ${RISCV_TOOLCHAIN_ROOT}) |
| list(APPEND CMAKE_PREFIX_PATH "${RISCV_TOOLCHAIN_ROOT}") |
| |
| set(CMAKE_C_COMPILER "${RISCV_TOOLCHAIN_ROOT}/clang") |
| set(CMAKE_CXX_COMPILER "${RISCV_TOOLCHAIN_ROOT}/clang++") |
| set(CMAKE_AR "${RISCV_TOOLCHAIN_ROOT}/llvm-ar") |
| set(CMAKE_RANLIB "${RISCV_TOOLCHAIN_ROOT}/llvm-ranlib") |
| set(CMAKE_STRIP "${RISCV_TOOLCHAIN_ROOT}/llvm-strip") |
| |
| set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -menable-experimental-extensions -march=${CMAKE_SYSTEM_PROCESSOR} -mabi=${CMAKE_SYSTEM_ABI}") |
| # Use Nano header and libraries. |
| include_directories(BEFORE SYSTEM "${RISCV_TOOLCHAIN_ROOT}/../riscv32-unknown-elf/include/newlib-nano/") |
| link_directories(BEFORE "${RISCV_TOOLCHAIN_ROOT}/../riscv32-unknown-elf/lib/newlib-nano") |
| else() |
| set(CMAKE_SYSTEM_PROCESSOR rv32imv) |
| |
| set(RISCV_TOOLCHAIN_ROOT "$ENV{CACHE}/toolchain_vp/bin" CACHE STRING "RISC-V toolchain path") |
| message (STATUS "RISC-V toolchain path is ${RISCV_TOOLCHAIN_ROOT}") |
| find_file( RISCV_COMPILER "riscv32-unknown-elf-gcc" HINTS ${RISCV_TOOLCHAIN_ROOT} PATHS ENV INCLUDE) |
| |
| get_filename_component(RISCV_TOOLCHAIN_BIN_PATH ${RISCV_COMPILER} DIRECTORY) |
| get_filename_component(RISCV_TOOLCHAIN_BIN_GCC ${RISCV_COMPILER} NAME_WE) |
| get_filename_component(RISCV_TOOLCHAIN_BIN_EXT ${RISCV_COMPILER} EXT) |
| |
| message( "RISC-V GCC Path: ${RISCV_TOOLCHAIN_BIN_PATH}" ) |
| |
| STRING(REGEX REPLACE "\-gcc" "-" CROSS_COMPILE ${RISCV_COMPILER}) |
| message( "RISC-V Cross Compile Prefix: ${CROSS_COMPILE}" ) |
| |
| set(CMAKE_AR ${CROSS_COMPILE}ar) |
| set(CMAKE_ASM_COMPILER ${RISCV_COMPILER}) |
| set(CMAKE_C_COMPILER ${RISCV_COMPILER}) |
| set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++) |
| |
| set( CMAKE_OBJCOPY ${RISCV_TOOLCHAIN_BIN_PATH}/${CROSS_COMPILE}objcopy |
| CACHE FILEPATH "The toolchain objcopy command " FORCE ) |
| |
| set( CMAKE_OBJDUMP ${RISCV_TOOLCHAIN_BIN_PATH}/${CROSS_COMPILE}objdump |
| CACHE FILEPATH "The toolchain objdump command " FORCE ) |
| |
| set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR} -mabi=${CMAKE_SYSTEM_ABI}") |
| |
| set(VEC_DEFAULT_COPTS "${VEC_DEFAULT_COPTS}" |
| "-Wno-format" |
| ) |
| set(VEC_DEFAULT_LINKOPTS "${VEC_DEFAULT_LINKOPTS}" |
| "-specs=nano.specs" |
| ) |
| endif(${BUILD_WITH_CLANG}) |
| |
| # Check compiler found |
| if (EXISTS ${RISCV_COMPILER}) |
| message(STATUS "RISC-V compiler found. Using ${RISCV_COMPILER}") |
| else() |
| message(FATAL_ERROR "RISC-V compiler not found. ${RISCV_OMPILER}") |
| endif() |
| |
| set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" ) |
| set( CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" ) |
| set( CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" ) |
| set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nostartfiles " ) |