Skip to content

Commit da32635

Browse files
committed
Add versioning with incremental build numbers
1 parent 1595320 commit da32635

7 files changed

Lines changed: 85 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,17 @@ project(Kanso LANGUAGES C ASM)
66

77
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
88

9+
include(cmake/utils.cmake)
10+
11+
set(VERSION_HEADER_PATH "${CMAKE_BINARY_DIR}/src/Common/Version.h")
12+
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/src/Common")
13+
14+
add_custom_target(generate_version_header ALL
15+
COMMAND ${CMAKE_COMMAND} -DOUT_FILE=${VERSION_HEADER_PATH}
16+
-P "${CMAKE_SOURCE_DIR}/cmake/generate_version_header.cmake"
17+
COMMENT "Regenerating version.h from Git tag"
18+
)
19+
920
add_subdirectory(src/Common)
1021
add_subdirectory(src/Kernel)
22+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
execute_process(
2+
COMMAND git describe --tags --abbrev=0
3+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
4+
OUTPUT_VARIABLE GIT_TAG
5+
OUTPUT_STRIP_TRAILING_WHITESPACE
6+
ERROR_QUIET
7+
)
8+
9+
if(NOT GIT_TAG)
10+
set(GIT_TAG "0.0.0-DEV")
11+
endif()
12+
13+
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-([A-Za-z0-9]+)$" _ "${GIT_TAG}")
14+
15+
set(MAJOR ${CMAKE_MATCH_1})
16+
set(MINOR ${CMAKE_MATCH_2})
17+
math(EXPR BUILD "${CMAKE_MATCH_3} + 1")
18+
set(LABEL ${CMAKE_MATCH_4})
19+
20+
set(VERSION_FULL "${MAJOR}.${MINOR}.${BUILD}-${LABEL}")
21+
22+
file(WRITE "${OUT_FILE}" "
23+
// Auto-generated version.h from Git tag ${GIT_TAG}
24+
25+
#pragma once
26+
27+
#define KANSO_VERSION_MAJOR ${MAJOR}
28+
#define KANSO_VERSION_MINOR ${MINOR}
29+
#define KANSO_VERSION_BUILD ${BUILD}
30+
#define KANSO_VERSION_LABEL \"${LABEL}\"
31+
#define KANSO_VERSION_FULL \"${VERSION_FULL}\"
32+
")
33+

cmake/utils.cmake

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function(get_git_version OUT_PREFIX)
2+
execute_process(
3+
COMMAND git describe --tags --abbrev=0
4+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
5+
OUTPUT_VARIABLE _GIT_TAG
6+
OUTPUT_STRIP_TRAILING_WHITESPACE
7+
ERROR_QUIET
8+
)
9+
10+
if(NOT _GIT_TAG)
11+
set(_GIT_TAG "0.0.0-UNKNOWN")
12+
endif()
13+
14+
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-([A-Za-z0-9]+)$" _ "${_GIT_TAG}")
15+
16+
set(${OUT_PREFIX}_MAJOR "${CMAKE_MATCH_1}" PARENT_SCOPE)
17+
set(${OUT_PREFIX}_MINOR "${CMAKE_MATCH_2}" PARENT_SCOPE)
18+
set(${OUT_PREFIX}_BUILD "${CMAKE_MATCH_3}" PARENT_SCOPE)
19+
set(${OUT_PREFIX}_LABEL "${CMAKE_MATCH_4}" PARENT_SCOPE)
20+
21+
set(${OUT_PREFIX}_FULL
22+
"${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}-${CMAKE_MATCH_4}"
23+
PARENT_SCOPE
24+
)
25+
endfunction()
26+

src/Common/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
add_library(Common INTERFACE)
2+
add_dependencies(Common generate_version_header)
3+
4+
target_include_directories(Common INTERFACE .)
5+
target_include_directories(Common INTERFACE ${CMAKE_BINARY_DIR}/src/Common)
26

3-
target_include_directories(Common INTERFACE .)

src/Kernel/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# TODO: Add the correct platform
22
add_subdirectory(Platforms/RiscV)
3+
add_dependencies(Kernel generate_version_header)
4+
add_dependencies(KernelTest generate_version_header)

src/Kernel/KernelMain.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Types.h"
22
#include "String.h"
33
#include "Memory.h"
4+
#include "Version.h"
45
#include "Platform.h"
56
#include "KernelConsole.h"
67
#include "Kernel.h"
@@ -30,7 +31,7 @@ void KernelMain()
3031
auto platformInformation = PlatformGetInformation();
3132

3233
KernelConsolePrint(String("\n\n\x1b[36m%s\x1b[0m\n"), KernelLogo);
33-
KernelConsolePrint(String("Kanso OS 1.0-DEV1 - GitHubActions "));
34+
KernelConsolePrint(String("Kanso OS %s "), KANSO_VERSION_FULL);
3435
KernelConsolePrint(String("(%s %d-bit)\n\n"), platformInformation.Name.Pointer, platformInformation.ArchitectureBits);
3536

3637
//CpuSetSupervisorTrapHandler(&KernelSupervisorTrapHandler);

src/Kernel/KernelTest.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "String.h"
44
#include "KernelConsole.h"
55
#include "Platform.h"
6+
#include "Version.h"
67

78
const char* TEST_CONSOLE_RESET = "\x1b[0m";
89
const char* TEST_CONSOLE_GREEN = "\x1b[32m";
@@ -44,6 +45,11 @@ void KernelTestHandler(TestRunState state, ReadOnlySpanChar message, ...)
4445

4546
void KernelMain()
4647
{
48+
auto platformInformation = PlatformGetInformation();
49+
50+
KernelConsolePrint(String("\n\nKanso OS Kernel Tests %s "), KANSO_VERSION_FULL);
51+
KernelConsolePrint(String("(%s %d-bit)\n\n"), platformInformation.Name.Pointer, platformInformation.ArchitectureBits);
52+
4753
TestRun(KernelTestHandler);
4854
BiosReset(BiosResetType_Shutdown, BiosResetReason_None);
4955
}

0 commit comments

Comments
 (0)