Skip to content

Add a project version to the build. #887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
11 changes: 10 additions & 1 deletion c_emulator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}
Expand Down
6 changes: 6 additions & 0 deletions c_emulator/riscv_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions c_emulator/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#define SAIL_RISCV_VERSION "@sail_riscv_git_version@"
45 changes: 45 additions & 0 deletions cmake/project_version.cmake
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have to do this anyway is there much benefit to using git describe?

I wonder if it's simpler just to update it manually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think git describe would be useful for users who don't wait for released versions, e.g. the ACT folks (see #850). I think they also originated the discussion in #109.

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).")
Loading