diff --git a/CMakeLists.txt b/CMakeLists.txt index 7dc36f0fd..8ea1c47b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,38 @@ file(GLOB SD_LIB_SOURCES "*.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) message("-- Build shared library") message(${SD_LIB_SOURCES}) diff --git a/examples/cli/main.cpp b/examples/cli/main.cpp index 427364a46..3038b7ca4 100644 --- a/examples/cli/main.cpp +++ b/examples/cli/main.cpp @@ -1646,6 +1646,11 @@ void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy) } int main(int argc, const char* argv[]) { + for (int i = 1; i < argc; i++) { + if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--verbose") == 0) { + printf("stable-diffusion.cpp version %s, commit %s\n", sd_version(), sd_commit()); + } + } SDParams params; parse_args(argc, argv, params); preview_path = params.preview_path; diff --git a/stable-diffusion.h b/stable-diffusion.h index 309da9b1a..0f261e0ef 100644 --- a/stable-diffusion.h +++ b/stable-diffusion.h @@ -353,6 +353,10 @@ SD_API bool preprocess_canny(sd_image_t image, float strong, bool inverse); +SD_API const char * sd_commit(void); +SD_API const char * sd_version(void); + + #ifdef __cplusplus } #endif diff --git a/version.cpp b/version.cpp new file mode 100644 index 000000000..8ac1d7f11 --- /dev/null +++ b/version.cpp @@ -0,0 +1,22 @@ +#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); +} + +