Skip to content

Commit

Permalink
Add commit number to cmake build
Browse files Browse the repository at this point in the history
  • Loading branch information
3d-gussner committed Aug 9, 2023
1 parent 56aabf3 commit a1f61af
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 23 deletions.
34 changes: 27 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ include(cmake/ReproducibleBuild.cmake)
include(cmake/ProjectVersion.cmake)
resolve_version_variables()

set(PROJECT_VERSION_SUFFIX
set(PROJECT_VERSION_HASH
"<auto>"
CACHE
STRING
"Version suffix to be appended to the final filename (<ver+PROJECT_VERSION_SUFFIX>). Overrides git hash if set."
"Version suffix to be appended to the final filename (<ver+PROJECT_VERSION_HASH>). Overrides git hash if set."
)
if(PROJECT_VERSION_SUFFIX STREQUAL "<auto>")
set(PROJECT_VERSION_SUFFIX "${FW_COMMIT_HASH}")
if(PROJECT_VERSION_HASH STREQUAL "<auto>")
set(PROJECT_VERSION_HASH "${FW_COMMIT_HASH}")
endif()

set(PROJECT_VERSION_COMMIT_NR
"<auto>"
CACHE
STRING
"Version commit number to be appended to the final filename (<ver+PROJECT_VERSION_COMMIT_NR>). Overrides git commit number if set."
)
if(PROJECT_VERSION_COMMIT_NR STREQUAL "<auto>")
set(PROJECT_VERSION_COMMIT_NR "${FW_COMMIT_NR}")
endif()

set(PROJECT_VERSION_FULL
"<auto>"
CACHE
Expand All @@ -27,12 +38,19 @@ set(PROJECT_REPOSITORY
"Unknown"
CACHE STRING "Repository string to be shown on the info screen in settings."
)
set(FN_PREFIX "FW${PROJECT_VERSION}+${PROJECT_VERSION_SUFFIX}")
if(PROJECT_VERSION_FLAVOR AND PROJECT_VERSION_FLAVERSION)
set(FN_PREFIX "FW${PROJECT_VERSION}-${PROJECT_VERSION_FLAVOR}${PROJECT_VERSION_FLAVERSION}-${PROJECT_VERSION_COMMIT_NR}+${PROJECT_VERSION_HASH}")
else()
set(FN_PREFIX "FW${PROJECT_VERSION}-${PROJECT_VERSION_COMMIT_NR}+${PROJECT_VERSION_HASH}")
endif()

# Inform user about the resolved settings
message(STATUS "Project version ...........: ${PROJECT_VERSION}")
message(STATUS "Project version suffix ....: ${PROJECT_VERSION_SUFFIX}")
message(STATUS "Project version falvor.....: ${PROJECT_VERSION_FLAVOR}${PROJECT_VERSION_FLAVERSION}")
message(STATUS "Project version build_nr...: ${PROJECT_VERSION_COMMIT_NR}")
message(STATUS "Project version hash ......: ${PROJECT_VERSION_HASH}")
message(STATUS "Project version description: ${PROJECT_VERSION_FULL}")
message(STATUS "Project file: ${FN_PREFIX}")

# Language configuration
set(MAIN_LANGUAGES
Expand Down Expand Up @@ -307,7 +325,9 @@ function(add_base_binary variant_name)
target_compile_definitions(
${variant_name}
PRIVATE CMAKE_CONTROL FW_REPOSITORY="${PROJECT_REPOSITORY}"
FW_VERSION_FULL="${PROJECT_VERSION_FULL}" FW_COMMIT_HASH=0x${FW_COMMIT_HASH}
FW_VERSION_FULL="${PROJECT_VERSION_FULL}"
FW_COMMIT_HASH=0x${FW_COMMIT_HASH}
FW_COMMIT_NR=${FW_COMMIT_NR}
)
endfunction()

Expand Down
17 changes: 15 additions & 2 deletions Firmware/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern const char _sPrinterMmuName[] PROGMEM;
#define FW_MINOR 13
#define FW_REVISION 0
#define FW_FLAVOR RC //uncomment if DEBUG, DEVEL, ALPHA, BETA or RC
#define FW_FLAVERSION 1 //uncomment if FW_FLAVOR is defined and versioning is needed. Limited to max 8.
#define FW_FLAVERSION 1 //uncomment if FW_FLAVOR is defined and versioning is needed. Limited to max 8.
#ifndef FW_FLAVOR
#define FW_VERSION STR(FW_MAJOR) "." STR(FW_MINOR) "." STR(FW_REVISION)
#else
Expand All @@ -35,7 +35,6 @@ extern const char _sPrinterMmuName[] PROGMEM;
#define FW_COMMIT_NR 6853
#define FW_COMMIT_HASH 0
#define FW_REPOSITORY "Unknown"
#define FW_DEV_VERSION FW_VERSION_UNKNOWN
#define FW_VERSION_FULL FW_VERSION "-unknown"
#endif

Expand All @@ -61,6 +60,20 @@ extern const char _sPrinterMmuName[] PROGMEM;
// the build server shall never produce builds with this build type.
#define FW_VERSION_UNKNOWN 0

#ifdef FW_FLAVOR
#if FW_FLAVOR == RC
#define FW_DEV_VERSION FW_VERSION_UNKNOWN
#elif FW_FLAVOR == BETA
#define FW_DEV_VERSION FW_VERSION_UNKNOWN
#elif FW_FLAVOR == ALPHA
#define FW_DEV_VERSION FW_VERSION_UNKNOWN
#elif FW_FLAVOR == DEV
#define FW_DEV_VERSION FW_VERSION_UNKNOWN
#else
#define FW_DEV_VERSION FW_VERSION_UNKNOWN
#endif
#endif

#if FW_DEV_VERSION == FW_VERSION_DEBUG
#define DEBUG_BUILD
#else
Expand Down
2 changes: 2 additions & 0 deletions Firmware/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6421,6 +6421,8 @@ SERIAL_PROTOCOLPGM("\n\n");
} else {
SERIAL_ECHOPGM("FIRMWARE_NAME:Prusa-Firmware ");
SERIAL_ECHORPGM(FW_VERSION_STR_P());
SERIAL_ECHOPGM("-");
SERIAL_ECHOPGM(STR(FW_COMMIT_HASH));
SERIAL_ECHOPGM(" based on Marlin FIRMWARE_URL:https://github.com/prusa3d/Prusa-Firmware PROTOCOL_VERSION:");
SERIAL_ECHOPGM(PROTOCOL_VERSION);
SERIAL_ECHOPGM(" MACHINE_TYPE:");
Expand Down
11 changes: 6 additions & 5 deletions Firmware/ultralcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,17 +1605,18 @@ static void lcd_support_menu()
MENU_ITEM_BACK_P(_T(MSG_MAIN));

MENU_ITEM_BACK_P(PSTR("Firmware:"));
MENU_ITEM_BACK_P(PSTR(" " FW_VERSION_FULL));
MENU_ITEM_BACK_P(PSTR(" repo " FW_REPOSITORY));
MENU_ITEM_BACK_P(PSTR(" date " SOURCE_DATE_EPOCH));
MENU_ITEM_BACK_P(_n("prusa3d.com"));////MSG_PRUSA3D c=18
MENU_ITEM_BACK_P(PSTR(FW_VERSION_FULL));
MENU_ITEM_BACK_P(PSTR("Build:" STR(FW_COMMIT_NR)));
MENU_ITEM_BACK_P(PSTR("Hash :" STR(FW_COMMIT_HASH)));
MENU_ITEM_BACK_P(PSTR("Repo :" FW_REPOSITORY));
MENU_ITEM_BACK_P(PSTR("Date :" SOURCE_DATE_EPOCH));
MENU_ITEM_BACK_P(STR_SEPARATOR); MENU_ITEM_BACK_P(_n("prusa3d.com"));////MSG_PRUSA3D c=18
MENU_ITEM_BACK_P(_n("forum.prusa3d.com"));////MSG_PRUSA3D_FORUM c=18
MENU_ITEM_BACK_P(_n("help.prusa3d.com"));////MSG_PRUSA3D_HELP c=18
MENU_ITEM_BACK_P(STR_SEPARATOR);
MENU_ITEM_BACK_P(PSTR(FILAMENT_SIZE));
MENU_ITEM_BACK_P(PSTR(ELECTRONICS));
MENU_ITEM_BACK_P(PSTR(NOZZLE_TYPE));
MENU_ITEM_BACK_P(STR_SEPARATOR);

#if defined(FILAMENT_SENSOR) && (FILAMENT_SENSOR_TYPE == FSENSOR_IR_ANALOG)
MENU_ITEM_BACK_P(STR_SEPARATOR);
Expand Down
28 changes: 27 additions & 1 deletion cmake/GetGitRevisionDescription.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ function(git_describe_working_tree _var)
endif()

execute_process(
COMMAND "${GIT_EXECUTABLE}" describe --dirty ${ARGN}
COMMAND "${GIT_EXECUTABLE}" describe --abbrev=0 --dirty --broken ${ARGN}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE out
Expand Down Expand Up @@ -306,3 +306,29 @@ function(git_head_commit_data _var _format)
${out}
PARENT_SCOPE)
endfunction()

function(git_head_commit_number _var)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
if(NOT GIT_FOUND)
set(${_var}
"GIT-NOTFOUND"
PARENT_SCOPE)
return()
endif()

execute_process(
COMMAND "${GIT_EXECUTABLE}" rev-list --count HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE res
OUTPUT_VARIABLE out
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND")
endif()

set(${_var}
"${out}"
PARENT_SCOPE)
endfunction()
32 changes: 25 additions & 7 deletions cmake/ProjectVersion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# This file is responsible for setting the following variables:
#
# ~~~
# PROJECT_VERSION_MAJOR (4)
# PROJECT_VERSION_MINOR (0)
# PROJECT_VERSION_REV (3)
# PROJECT_VERSION (4.0.3)
# FW_COMMIT_DSC ("v4.0.3-deadbeef")
# PROJECT_VERSION_MAJOR (3)
# PROJECT_VERSION_MINOR (13)
# PROJECT_VERSION_REV (1)
# PROJECT_VERSION (3.13.1)
# FW_COMMIT_DSC ("v3.13.1-deadbeef")
# FW_COMMIT_HASH (deadbeef)
# FW_COMMIT_DATE (1665051856)
#
Expand All @@ -16,11 +16,14 @@
#]]

file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/Firmware/Configuration.h CFG_VER_DATA
REGEX "#define FW_[A-Z]+ ([0-9]+)"
REGEX "^#define FW_[A-Z_]+ ([A-Z0-9]+)"
)
list(GET CFG_VER_DATA 0 PROJECT_VERSION_MAJOR)
list(GET CFG_VER_DATA 1 PROJECT_VERSION_MINOR)
list(GET CFG_VER_DATA 2 PROJECT_VERSION_REV)
list(GET CFG_VER_DATA 3 PROJECT_VERSION_FLAVOR)
list(GET CFG_VER_DATA 4 PROJECT_VERSION_FLAVERSION)
#list(GET CFG_VER_DATA 6 PROJECT_VERSION_COMMIT_NR)

string(REGEX MATCH "FW_MAJOR ([0-9]+)" PROJECT_VERSION_MAJOR "${PROJECT_VERSION_MAJOR}")
set(PROJECT_VERSION_MAJOR "${CMAKE_MATCH_1}")
Expand All @@ -31,7 +34,17 @@ set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_1})
string(REGEX MATCH "FW_REVISION +([0-9]+)" PROJECT_VERSION_REV "${PROJECT_VERSION_REV}")
set(PROJECT_VERSION_REV ${CMAKE_MATCH_1})

string(REGEX MATCH "^#define FW_FLAVOR +([A-Z]+)" PROJECT_VERSION_FLAVOR "${PROJECT_VERSION_FLAVOR}")
set(PROJECT_VERSION_FLAVOR ${CMAKE_MATCH_1})

string(REGEX MATCH "^#define FW_FLAVERSION +([0-9]+)" PROJECT_VERSION_FLAVERSION "${PROJECT_VERSION_FLAVERSION}")
set(PROJECT_VERSION_FLAVERSION ${CMAKE_MATCH_1})

#string(REGEX MATCH "^#define FW_COMMIT_NR +([0-9]+)" PROJECT_VERSION_COMMIT_NR "${PROJECT_VERSION_COMMIT_NR}")
#set(PROJECT_VERSION_COMMIT_NR ${CMAKE_MATCH_1})

set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_REV}")
set(PROJECT_VERSION_COMMIT_NR ${FW_COMMIT_NR})

function(resolve_version_variables)
if(FW_COMMIT_DSC)
Expand All @@ -45,11 +58,12 @@ function(resolve_version_variables)
if(FW_COMMIT_HASH IN_LIST ERRORS)
# git not available, set fallback values
set(FW_COMMIT_HASH "UNKNOWN")
set(FW_COMMIT_DSC "v${PROJECT_VERSION}-${FW_COMMIT_HASH}")
set(FW_COMMIT_DSC "v${PROJECT_VERSION}")
string(TIMESTAMP FW_COMMIT_DATE "%s")
else()
git_describe_working_tree(FW_COMMIT_DSC)
git_head_commit_data(FW_COMMIT_DATE "%ct")
git_head_commit_number(FW_COMMIT_NR)
endif()
set(FW_COMMIT_DSC
"${FW_COMMIT_DSC}"
Expand All @@ -59,6 +73,10 @@ function(resolve_version_variables)
"${FW_COMMIT_HASH}"
PARENT_SCOPE
)
set(FW_COMMIT_NR
"${FW_COMMIT_NR}"
PARENT_SCOPE
)
set(FW_COMMIT_DATE
"${FW_COMMIT_DATE}"
PARENT_SCOPE
Expand Down
2 changes: 1 addition & 1 deletion utils/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_cmake_cache_entries(self):
('CMAKE_TOOLCHAIN_FILE', 'FILEPATH', str(self.toolchain)),
('AVR_TOOLCHAIN_DIR', 'DIRPATH', str(get_dependency('avr-gcc'))),
('CMAKE_BUILD_TYPE', 'STRING', self.build_type.value.title()),
('PROJECT_VERSION_SUFFIX', 'STRING', self.version_suffix or ''),
('PROJECT_VERSION_HASH', 'STRING', self.version_suffix or ''),
('PROJECT_VERSION_SUFFIX_SHORT', 'STRING',
self.version_suffix_short or ''),
])
Expand Down

0 comments on commit a1f61af

Please sign in to comment.