Skip to content

Commit 89ba1b6

Browse files
committed
Add a project version to the build.
Uses `git describe` to get the version according to git, and sanitizes it for CMake project function for potential future use with CPack. Add an option to the c_emulator to report the (git) version.
1 parent 8ff2a70 commit 89ba1b6

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
cmake_minimum_required(VERSION 3.20)
22
cmake_policy(VERSION 3.20...3.31)
33

4-
project(sail_riscv)
4+
include(cmake/project_version.cmake)
5+
project(sail_riscv VERSION ${sail_riscv_cmake_version})
56

67
set(CMAKE_CXX_STANDARD 17)
78
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

c_emulator/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# make version available to emulator
2+
configure_file(version.h.in version.h @ONLY)
3+
add_library(version
4+
INTERFACE "version.h"
5+
)
6+
target_include_directories(version
7+
INTERFACE "${CMAKE_CURRENT_BINARY_DIR}"
8+
)
9+
110
set(EMULATOR_COMMON_SRCS
211
riscv_config.h
312
riscv_platform.cpp
@@ -50,7 +59,7 @@ foreach (xlen IN ITEMS 32 64)
5059
add_dependencies(riscv_sim_${arch} generated_model_${arch})
5160

5261
target_link_libraries(riscv_sim_${arch}
53-
PRIVATE softfloat sail_runtime default_config GMP::GMP
62+
PRIVATE softfloat sail_runtime default_config version GMP::GMP
5463
)
5564

5665
target_include_directories(riscv_sim_${arch}

c_emulator/riscv_sim.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
#include "riscv_sail.h"
2929
#include "rvfi_dii.h"
3030
#include "default_config.h"
31+
#include "version.h"
3132

3233
enum {
3334
OPT_TRACE_OUTPUT = 1000,
3435
OPT_PRINT_CONFIG,
3536
OPT_SAILCOV,
3637
OPT_ENABLE_EXPERIMENTAL_EXTENSIONS,
38+
OPT_VERSION,
3739
};
3840

3941
static bool do_show_times = false;
@@ -93,6 +95,7 @@ char *sailcov_file = NULL;
9395
#endif
9496

9597
static struct option options[] = {
98+
{"version", no_argument, 0, OPT_VERSION },
9699
{"device-tree-blob", required_argument, 0, 'b' },
97100
{"terminal-log", required_argument, 0, 't' },
98101
{"show-times", required_argument, 0, 'p' },
@@ -243,6 +246,9 @@ static int process_args(int argc, char **argv)
243246
}
244247
break;
245248
}
249+
case OPT_VERSION:
250+
printf("%s\n", SAIL_RISCV_VERSION);
251+
exit(0);
246252
case OPT_PRINT_CONFIG:
247253
printf("%s", DEFAULT_JSON);
248254
exit(0);

c_emulator/version.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#define SAIL_RISCV_VERSION "@sail_riscv_version@"

cmake/project_version.cmake

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Sets GIT_EXECUTABLE
2+
find_package(Git REQUIRED)
3+
4+
if (NOT ${Git_FOUND})
5+
message(FATAL_ERROR "Git not found.")
6+
endif()
7+
8+
# We could remove --tags if we started annotating the release tags.
9+
execute_process(
10+
COMMAND ${GIT_EXECUTABLE} describe --tags
11+
RESULT_VARIABLE git_error
12+
OUTPUT_VARIABLE git_describe
13+
OUTPUT_STRIP_TRAILING_WHITESPACE
14+
)
15+
16+
if (git_error)
17+
message(FATAL_ERROR "Failed to run git describe: ${git_error}")
18+
endif()
19+
20+
# `git describe` cannot directly be used as a CMake VERSION argument
21+
# for project() due the trailing commit hash failing CMake's semver
22+
# format check.
23+
#
24+
# We could directly set PROJECT_VERSION to ${git_describe}, but that
25+
# makes it incompatible with CPack in case we decide to use it.
26+
# https://gitlab.kitware.com/cmake/cmake/-/issues/16716#note_956803
27+
#
28+
# Instead, parse the version components for CMake and CPack, and use
29+
# git_describe for the simulator --version.
30+
31+
if (git_describe MATCHES "^([0-9]+)\\.([0-9]+)(-.*)?$")
32+
set(v_major ${CMAKE_MATCH_1})
33+
set(v_minor ${CMAKE_MATCH_2})
34+
else()
35+
message(FATAL_ERROR "Cannot parse git describe output: ${git_describe}")
36+
endif()
37+
38+
set(sail_riscv_version ${git_describe})
39+
set(sail_riscv_cmake_version "${v_major}.${v_minor}")

0 commit comments

Comments
 (0)