Skip to content

Commit

Permalink
Add RISC-V to cmake linux (scp-fs2open#6375)
Browse files Browse the repository at this point in the history
* Add support for RISC-V compile to cmake

* No need to pass -march for gcc in riscv

* -march=native is not supported in Risc-V clang

* fix comment and remove unnecessary newline

* Remove incorrect values while checking CMAKE_SYSTEM_PROCESSOR

* Support 32bit RISCV as well

I dont think this will be ever be needed, but i dont like that compilation will fail due to incorrect compiler flags if you try.
  • Loading branch information
Shivansps authored Oct 28, 2024
1 parent 80625fd commit 2eb2af6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
12 changes: 12 additions & 0 deletions cmake/globals.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ else()
set(IS_ARM64 TRUE)
endif()
endif()

set(IS_RISCV FALSE)

if (NOT "${CMAKE_GENERATOR_PLATFORM}" STREQUAL "") # needed to cover Visual Studio generator
if(CMAKE_GENERATOR_PLATFORM MATCHES "^(riscv64|RISCV64|riscv32|RISCV32)")
set(IS_RISCV TRUE)
endif()
else()
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv64|RISCV64|riscv32|RISCV32)")
set(IS_RISCV TRUE)
endif()
endif()
1 change: 1 addition & 0 deletions cmake/platformChecks.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#cmakedefine01 IS_64BIT
#cmakedefine01 IS_ARM64
#cmakedefine01 IS_RISCV

#cmakedefine01 SCP_HAVE_STRCASECMP
#cmakedefine01 SCP_HAVE_STRNCASECMP
Expand Down
14 changes: 11 additions & 3 deletions cmake/toolchain-clang.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ option(CLANG_ENABLE_ADDRESS_SANITIZER "Enable -fsanitize=address" OFF)

option(CLANG_USE_LIBCXX "Use libc++" OFF)

# These are the default values
set(C_BASE_FLAGS "-march=native -pipe")
set(CXX_BASE_FLAGS "-march=native -pipe")
# Clang does not support -march=native for RISC-V
if(IS_RISCV)
# You do not need to pass a -march, passing nothing will make clang to choose itself.
# If you want a specific set of instructions like vectors, set -march in CFLAGS and CXXFLAGS env variables
set(C_BASE_FLAGS "-pipe")
set(CXX_BASE_FLAGS "-pipe")
else()
# These are the default values
set(C_BASE_FLAGS "-march=native -pipe")
set(CXX_BASE_FLAGS "-march=native -pipe")
endif()

# For C and C++, the values can be overwritten independently
if(DEFINED ENV{CXXFLAGS})
Expand Down
16 changes: 13 additions & 3 deletions cmake/toolchain-gcc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ option(GCC_ENABLE_SANITIZE_UNDEFINED "Enable -fsanitize=undefined" OFF)
option(GCC_USE_GOLD "Use the gold linker instead of the standard linker" OFF)
option(GCC_GENERATE_GDB_INDEX "Adds linker option to generate the gdb index for debug builds" OFF)

# These are the default values
set(C_BASE_FLAGS "-march=native -pipe")
set(CXX_BASE_FLAGS "-march=native -pipe")
# GCC does not support -march=native in RISC-V
if(IS_RISCV)
# You do not need to pass a -march, passing nothing will make gcc to choose itself.
# If you want a specific set of instructions like vectors, set -march in CFLAGS and CXXFLAGS env variables
# Example for vectors: -march=rv64gcv
# https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Options.html
set(C_BASE_FLAGS "-pipe")
set(CXX_BASE_FLAGS "-pipe")
else()
# These are the default values
set(C_BASE_FLAGS "-march=native -pipe")
set(CXX_BASE_FLAGS "-march=native -pipe")
endif()

# For C and C++, the values can be overwritten independently
if(DEFINED ENV{CFLAGS})
Expand Down
8 changes: 7 additions & 1 deletion cmake/version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ ELSE()
SET(FSO_BINARY_SUFFIX "${FSO_VERSION_MAJOR}_${FSO_VERSION_MINOR}_${FSO_VERSION_BUILD}_${FSO_VERSION_REVISION_STR}")
ENDIF()

IF(IS_ARM64)
IF(IS_RISCV)
IF(IS_64BIT)
SET(FSO_BINARY_SUFFIX "${FSO_BINARY_SUFFIX}_riscv64")
ELSE()
SET(FSO_BINARY_SUFFIX "${FSO_BINARY_SUFFIX}_riscv32")
ENDIF()
ELSEIF(IS_ARM64)
SET(FSO_BINARY_SUFFIX "${FSO_BINARY_SUFFIX}_arm64")
ELSEIF(IS_64BIT)
# This is a 64-bit builds
Expand Down

0 comments on commit 2eb2af6

Please sign in to comment.