diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml index 997ee9b87..882effc5d 100644 --- a/.github/workflows/compile.yml +++ b/.github/workflows/compile.yml @@ -16,6 +16,9 @@ jobs: - name: Check out repository code uses: actions/checkout@v4 + with: + # Needed for `git describe` in the build to work. + fetch-depth: 0 - name: Ensure pre-commit checks pass run: python3 -m pip install pre-commit && pre-commit run --all-files --show-diff-on-failure --color=always diff --git a/CMakeLists.txt b/CMakeLists.txt index 58fe2324f..7c5620311 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,8 @@ cmake_minimum_required(VERSION 3.20) cmake_policy(VERSION 3.20...3.31) -project(sail_riscv) +include(cmake/project_version.cmake) +project(sail_riscv VERSION ${sail_riscv_cmake_version}) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) diff --git a/c_emulator/CMakeLists.txt b/c_emulator/CMakeLists.txt index 87447cfc1..347588e8b 100644 --- a/c_emulator/CMakeLists.txt +++ b/c_emulator/CMakeLists.txt @@ -1,3 +1,12 @@ +# make version available to emulator +configure_file(version.h.in version.h @ONLY) +add_library(version + INTERFACE "version.h" +) +target_include_directories(version + INTERFACE "${CMAKE_CURRENT_BINARY_DIR}" +) + set(EMULATOR_COMMON_SRCS riscv_config.h riscv_platform.cpp @@ -50,7 +59,7 @@ foreach (xlen IN ITEMS 32 64) add_dependencies(riscv_sim_${arch} generated_model_${arch}) target_link_libraries(riscv_sim_${arch} - PRIVATE softfloat sail_runtime default_config GMP::GMP + PRIVATE softfloat sail_runtime default_config version GMP::GMP ) target_include_directories(riscv_sim_${arch} diff --git a/c_emulator/riscv_sim.cpp b/c_emulator/riscv_sim.cpp index 03113c924..770a0471d 100644 --- a/c_emulator/riscv_sim.cpp +++ b/c_emulator/riscv_sim.cpp @@ -28,12 +28,14 @@ #include "riscv_sail.h" #include "rvfi_dii.h" #include "default_config.h" +#include "version.h" enum { OPT_TRACE_OUTPUT = 1000, OPT_PRINT_CONFIG, OPT_SAILCOV, OPT_ENABLE_EXPERIMENTAL_EXTENSIONS, + OPT_VERSION, }; static bool do_show_times = false; @@ -93,6 +95,7 @@ char *sailcov_file = NULL; #endif static struct option options[] = { + {"version", no_argument, 0, OPT_VERSION }, {"device-tree-blob", required_argument, 0, 'b' }, {"terminal-log", required_argument, 0, 't' }, {"show-times", required_argument, 0, 'p' }, @@ -243,6 +246,9 @@ static int process_args(int argc, char **argv) } break; } + case OPT_VERSION: + printf("%s\n", SAIL_RISCV_VERSION); + exit(0); case OPT_PRINT_CONFIG: printf("%s", DEFAULT_JSON); exit(0); diff --git a/c_emulator/version.h.in b/c_emulator/version.h.in new file mode 100644 index 000000000..f88677dab --- /dev/null +++ b/c_emulator/version.h.in @@ -0,0 +1,3 @@ +#pragma once + +#define SAIL_RISCV_VERSION "@sail_riscv_git_version@" diff --git a/cmake/project_version.cmake b/cmake/project_version.cmake new file mode 100644 index 000000000..003f23313 --- /dev/null +++ b/cmake/project_version.cmake @@ -0,0 +1,45 @@ +# Set a default version for archive builds that do not have +# git history. Increment this appropriately at tag-and-release time. +set(archive_build_version "0.7.0") + +# Sets GIT_EXECUTABLE +find_package(Git REQUIRED) + +# We could remove --tags if we started annotating the release tags. +execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags HEAD + RESULT_VARIABLE git_error + OUTPUT_VARIABLE git_describe + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +if (git_error) + message(STATUS "Failed to run git describe: ${git_error}") + + set(sail_riscv_git_version ${archive_build_version}) + set(sail_riscv_cmake_version ${archive_build_version}) +else() + # `git describe` cannot directly be used as a CMake VERSION argument + # for project() due the trailing commit hash failing CMake's semver + # format check. + # + # We could directly set PROJECT_VERSION to ${git_describe}, but that + # makes it incompatible with CPack in case we decide to use it. + # https://gitlab.kitware.com/cmake/cmake/-/issues/16716#note_956803 + # + # Instead, parse the version components for CMake and CPack, and use + # git_describe for the simulator --version. + + if (git_describe MATCHES "^([0-9]+)\\.([0-9]+)(-.*)?$") + set(v_major ${CMAKE_MATCH_1}) + set(v_minor ${CMAKE_MATCH_2}) + else() + message(FATAL_ERROR "Cannot parse git describe output: ${git_describe}") + endif() + + set(sail_riscv_git_version ${git_describe}) + set(sail_riscv_cmake_version "${v_major}.${v_minor}") +endif() + +# Log versions in the build log. +message(STATUS "Building versions: ${sail_riscv_git_version} (git), ${sail_riscv_cmake_version} (cmake).")