Skip to content

Commit

Permalink
Implement CMake build system
Browse files Browse the repository at this point in the history
This is a draft of a CMake build system. It doesn't yet include RVFI, JSON docs, formal stuff, etc.
  • Loading branch information
Timmmm committed Dec 18, 2024
1 parent 5f4a8e6 commit 6d0c915
Show file tree
Hide file tree
Showing 514 changed files with 48,328 additions and 47,434 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ _build/
_sbuild/
*.o
*.a
/z3_problems
z3_problems

# Typical CMake build directory.
/build
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
exclude: '^(prover_snapshots)|(generated_definitions)|(c_emulator/SoftFloat-3e)'
exclude: '^(prover_snapshots)|(generated_definitions)|(dependencies)'
minimum_pre_commit_version: 2.10.0
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
76 changes: 76 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.27)

project(sail_riscv)

# Enable CTest
enable_testing()

# We technically require C++20 since the C generated by Sail - which we compile
# as C++ - uses designated initialisers, a C++20 feature. However in practice
# much older compilers support this feature so everything does work with C++17,
# but you get lots of warnings on compilers that support C++20 if you use
# this feature without -std=c++20.
if (cxx_std_20 IN_LIST CMAKE_CXX_COMPILE_FEATURES)
set(CMAKE_CXX_STANDARD 20)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

# Export compile_commands.json for IDE support.
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Always use Position Independent Code. By default it is only used for
# shared libraries (which require it), but you also need it for static
# libraries if you link them into shared libraries.
# Generally it just simplifies everything for a negligable performance cost.
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)

# Don't allow undefined symbols in shared libraries. This is generally a pain.
if (UNIX)
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined")
endif()

# Optional faster binary. Increases compilation time a lot though due to LTO.
option(EXTRA_FAST "Enable aggressive optimisation flags (-march=native, -flto, etc)" FALSE)

if (EXTRA_FAST)
include("cmake/extra_fast.cmake")
endif()

# Extra CMake files.
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")

# These are the main requirements.
# Don't use `REQUIRED` so that we can print custom help messages.
find_package(ZLIB)
if (NOT ZLIB_FOUND)
message(FATAL_ERROR "Zlib not found. Try 'sudo apt install zlib1g-dev' or 'sudo dnf install zlib-devel'.")
endif()

find_package(GMP)
if (NOT GMP_FOUND)
message(FATAL_ERROR "GMP not found. Try 'sudo apt install libgmp3-dev' or 'sudo dnf install gmp-devel'.")
endif()

find_program(sail "sail")
if (NOT sail)
message(FATAL_ERROR "Sail not found. See README.md for installation instructions.")
endif()

set(ENABLED_ARCHITECTURES "rv32;rv64" CACHE STRING "Enabled architectures to build (rv32, rv64, rv32d, rv64f)" )

# Softfloat support.
add_subdirectory("dependencies/softfloat")

# Sail C runtime.
add_subdirectory("sail_runtime")

# Sail model generated C code.
add_subdirectory("model")

# Emulator binary.
add_subdirectory("emulator")

# Old pre-compiled riscv-tests.
add_subdirectory("test/riscv-tests")
3 changes: 0 additions & 3 deletions c_emulator/SoftFloat-3e/.clang-format

This file was deleted.

54 changes: 0 additions & 54 deletions c_emulator/SoftFloat-3e/build/Linux-x86_64-GCC/platform.h

This file was deleted.

53 changes: 0 additions & 53 deletions c_emulator/SoftFloat-3e/build/Win32-SSE2-MinGW/platform.h

This file was deleted.

54 changes: 0 additions & 54 deletions c_emulator/SoftFloat-3e/build/Win64-MinGW-w64/platform.h

This file was deleted.

105 changes: 0 additions & 105 deletions c_emulator/SoftFloat-3e/source/8086/s_propagateNaNF128UI.c

This file was deleted.

28 changes: 28 additions & 0 deletions cmake/extra_fast.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
include(CheckCXXCompilerFlag)

# Enable agressive optimisation flags.

# Try to use -march=x86-64-v3, but fall back to -march=native if we are
# using an old compiler that doesn't support that flag.
check_cxx_compiler_flag("-march=x86-64-v3" COMPILER_SUPPORTS_MARCH_X86_V3)
if (COMPILER_SUPPORTS_MARCH_X86_V3)
message(STATUS "Enabling -march=x86-64-v3")
add_compile_options("-march=x86-64-v3")
else()
# Must be quite old so try -march=native.
check_cxx_compiler_flag("-march=native" COMPILER_SUPPORTS_MARCH_NATIVE)
if (COMPILER_SUPPORTS_MARCH_NATIVE)
message(STATUS "Enabling -march=native")
add_compile_options("-march=native")
endif()
endif()

# This makes a measurable difference.
check_cxx_compiler_flag("-fomit-frame-pointer" COMPILER_SUPPORTS_FOMIT_FRAME_POINTER)
if (COMPILER_SUPPORTS_FOMIT_FRAME_POINTER)
message(STATUS "Enabling -fomit-frame-pointer")
add_compile_options("-fomit-frame-pointer")
endif()

# LTO
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
Loading

0 comments on commit 6d0c915

Please sign in to comment.