feat: embed version string and git commit hash (#1008)

This commit is contained in:
Wagner Bruna 2025-12-09 11:38:54 -03:00 committed by GitHub
parent a908436729
commit e72aea796e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 0 deletions

View File

@ -87,6 +87,38 @@ file(GLOB SD_LIB_SOURCES
"*.hpp" "*.hpp"
) )
find_program(GIT_EXE NAMES git git.exe NO_CMAKE_FIND_ROOT_PATH)
if(GIT_EXE)
execute_process(COMMAND ${GIT_EXE} describe --tags --abbrev=7 --dirty=+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE SDCPP_BUILD_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(COMMAND ${GIT_EXE} rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE SDCPP_BUILD_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()
if(NOT SDCPP_BUILD_VERSION)
set(SDCPP_BUILD_VERSION unknown)
endif()
message(STATUS "stable-diffusion.cpp version ${SDCPP_BUILD_VERSION}")
if(NOT SDCPP_BUILD_COMMIT)
set(SDCPP_BUILD_COMMIT unknown)
endif()
message(STATUS "stable-diffusion.cpp commit ${SDCPP_BUILD_COMMIT}")
set_property(
SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp
APPEND PROPERTY COMPILE_DEFINITIONS
SDCPP_BUILD_COMMIT=${SDCPP_BUILD_COMMIT} SDCPP_BUILD_VERSION=${SDCPP_BUILD_VERSION}
)
if(SD_BUILD_SHARED_LIBS) if(SD_BUILD_SHARED_LIBS)
message("-- Build shared library") message("-- Build shared library")
message(${SD_LIB_SOURCES}) message(${SD_LIB_SOURCES})

View File

@ -324,6 +324,7 @@ struct SDCliParams {
std::string output_path = "output.png"; std::string output_path = "output.png";
bool verbose = false; bool verbose = false;
bool version = false;
bool canny_preprocess = false; bool canny_preprocess = false;
preview_t preview_method = PREVIEW_NONE; preview_t preview_method = PREVIEW_NONE;
@ -366,6 +367,10 @@ struct SDCliParams {
"--verbose", "--verbose",
"print extra info", "print extra info",
true, &verbose}, true, &verbose},
{"",
"--version",
"print stable-diffusion.cpp version",
true, &version},
{"", {"",
"--color", "--color",
"colors the logging tags according to level", "colors the logging tags according to level",
@ -1598,7 +1603,12 @@ struct SDGenerationParams {
} }
}; };
static std::string version_string() {
return std::string("stable-diffusion.cpp version ") + sd_version() + ", commit " + sd_commit();
}
void print_usage(int argc, const char* argv[], const std::vector<ArgOptions>& options_list) { void print_usage(int argc, const char* argv[], const std::vector<ArgOptions>& options_list) {
std::cout << version_string() << "\n";
std::cout << "Usage: " << argv[0] << " [options]\n\n"; std::cout << "Usage: " << argv[0] << " [options]\n\n";
std::cout << "CLI Options:\n"; std::cout << "CLI Options:\n";
options_list[0].print(); options_list[0].print();
@ -1881,11 +1891,19 @@ void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy,
} }
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
if (argc > 1 && std::string(argv[1]) == "--version") {
std::cout << version_string() << "\n";
return EXIT_SUCCESS;
}
SDCliParams cli_params; SDCliParams cli_params;
SDContextParams ctx_params; SDContextParams ctx_params;
SDGenerationParams gen_params; SDGenerationParams gen_params;
parse_args(argc, argv, cli_params, ctx_params, gen_params); parse_args(argc, argv, cli_params, ctx_params, gen_params);
if (cli_params.verbose || cli_params.version) {
std::cout << version_string() << "\n";
}
if (gen_params.video_frames > 4) { if (gen_params.video_frames > 4) {
size_t last_dot_pos = cli_params.preview_path.find_last_of("."); size_t last_dot_pos = cli_params.preview_path.find_last_of(".");
std::string base_path = cli_params.preview_path; std::string base_path = cli_params.preview_path;

View File

@ -359,6 +359,9 @@ SD_API bool preprocess_canny(sd_image_t image,
float strong, float strong,
bool inverse); bool inverse);
SD_API const char* sd_commit(void);
SD_API const char* sd_version(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

20
version.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "stable-diffusion.h"
#ifndef SDCPP_BUILD_COMMIT
#define SDCPP_BUILD_COMMIT unknown
#endif
#ifndef SDCPP_BUILD_VERSION
#define SDCPP_BUILD_VERSION unknown
#endif
#define STRINGIZE2(x) #x
#define STRINGIZE(x) STRINGIZE2(x)
const char* sd_commit(void) {
return STRINGIZE(SDCPP_BUILD_COMMIT);
}
const char* sd_version(void) {
return STRINGIZE(SDCPP_BUILD_VERSION);
}