Skip to content

Commit 3af2c7d

Browse files
committed
feat: embed version string and git commit hash
1 parent 96c3e64 commit 3af2c7d

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,38 @@ file(GLOB SD_LIB_SOURCES
8787
"*.hpp"
8888
)
8989

90+
find_program(GIT_EXE NAMES git git.exe NO_CMAKE_FIND_ROOT_PATH)
91+
if(GIT_EXE)
92+
execute_process(COMMAND ${GIT_EXE} describe --tags --abbrev=7 --dirty=+
93+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
94+
OUTPUT_VARIABLE SDCPP_BUILD_VERSION
95+
OUTPUT_STRIP_TRAILING_WHITESPACE
96+
ERROR_QUIET
97+
)
98+
execute_process(COMMAND ${GIT_EXE} rev-parse --short HEAD
99+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
100+
OUTPUT_VARIABLE SDCPP_BUILD_COMMIT
101+
OUTPUT_STRIP_TRAILING_WHITESPACE
102+
ERROR_QUIET
103+
)
104+
endif()
105+
106+
if(NOT SDCPP_BUILD_VERSION)
107+
set(SDCPP_BUILD_VERSION unknown)
108+
endif()
109+
message(STATUS "stable-diffusion.cpp version ${SDCPP_BUILD_VERSION}")
110+
111+
if(NOT SDCPP_BUILD_COMMIT)
112+
set(SDCPP_BUILD_COMMIT unknown)
113+
endif()
114+
message(STATUS "stable-diffusion.cpp commit ${SDCPP_BUILD_COMMIT}")
115+
116+
set_property(
117+
SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp
118+
APPEND PROPERTY COMPILE_DEFINITIONS
119+
SDCPP_BUILD_COMMIT=${SDCPP_BUILD_COMMIT} SDCPP_BUILD_VERSION=${SDCPP_BUILD_VERSION}
120+
)
121+
90122
if(SD_BUILD_SHARED_LIBS)
91123
message("-- Build shared library")
92124
message(${SD_LIB_SOURCES})

examples/cli/main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ struct SDCliParams {
324324
std::string output_path = "output.png";
325325

326326
bool verbose = false;
327+
bool version = false;
327328
bool canny_preprocess = false;
328329

329330
preview_t preview_method = PREVIEW_NONE;
@@ -366,6 +367,10 @@ struct SDCliParams {
366367
"--verbose",
367368
"print extra info",
368369
true, &verbose},
370+
{"",
371+
"--version",
372+
"print stable-diffusion.cpp version",
373+
true, &version},
369374
{"",
370375
"--color",
371376
"colors the logging tags according to level",
@@ -1598,7 +1603,12 @@ struct SDGenerationParams {
15981603
}
15991604
};
16001605

1606+
static std::string version_string() {
1607+
return std::string("stable-diffusion.cpp version ") + sd_version() + ", commit " + sd_commit();
1608+
}
1609+
16011610
void print_usage(int argc, const char* argv[], const std::vector<ArgOptions>& options_list) {
1611+
std::cout << version_string() << "\n";
16021612
std::cout << "Usage: " << argv[0] << " [options]\n\n";
16031613
std::cout << "CLI Options:\n";
16041614
options_list[0].print();
@@ -1881,11 +1891,20 @@ void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy,
18811891
}
18821892

18831893
int main(int argc, const char* argv[]) {
1894+
1895+
if (argc > 1 && std::string(argv[1]) == "--version") {
1896+
std::cout << version_string() << "\n";
1897+
return EXIT_SUCCESS;
1898+
}
1899+
18841900
SDCliParams cli_params;
18851901
SDContextParams ctx_params;
18861902
SDGenerationParams gen_params;
18871903

18881904
parse_args(argc, argv, cli_params, ctx_params, gen_params);
1905+
if (cli_params.verbose || cli_params.version) {
1906+
std::cout << version_string() << "\n";
1907+
}
18891908
if (gen_params.video_frames > 4) {
18901909
size_t last_dot_pos = cli_params.preview_path.find_last_of(".");
18911910
std::string base_path = cli_params.preview_path;

stable-diffusion.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ SD_API bool preprocess_canny(sd_image_t image,
359359
float strong,
360360
bool inverse);
361361

362+
SD_API const char * sd_commit(void);
363+
SD_API const char * sd_version(void);
364+
365+
362366
#ifdef __cplusplus
363367
}
364368
#endif

version.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "stable-diffusion.h"
2+
3+
#ifndef SDCPP_BUILD_COMMIT
4+
#define SDCPP_BUILD_COMMIT unknown
5+
#endif
6+
7+
#ifndef SDCPP_BUILD_VERSION
8+
#define SDCPP_BUILD_VERSION unknown
9+
#endif
10+
11+
#define STRINGIZE2(x) #x
12+
#define STRINGIZE(x) STRINGIZE2(x)
13+
14+
const char * sd_commit(void) {
15+
return STRINGIZE(SDCPP_BUILD_COMMIT);
16+
}
17+
18+
const char * sd_version(void) {
19+
return STRINGIZE(SDCPP_BUILD_VERSION);
20+
}
21+
22+

0 commit comments

Comments
 (0)