Skip to content

Commit ff8abbf

Browse files
committed
feat: include a version string from git at build time
1 parent 2034588 commit ff8abbf

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,11 @@ void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy)
16461646
}
16471647

16481648
int main(int argc, const char* argv[]) {
1649+
for (int i = 1; i < argc; i++) {
1650+
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--verbose") == 0) {
1651+
printf("stable-diffusion.cpp version %s, commit %s\n", sd_version(), sd_commit());
1652+
}
1653+
}
16491654
SDParams params;
16501655
parse_args(argc, argv, params);
16511656
preview_path = params.preview_path;

stable-diffusion.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ SD_API bool preprocess_canny(sd_image_t image,
353353
float strong,
354354
bool inverse);
355355

356+
SD_API const char * sd_commit(void);
357+
SD_API const char * sd_version(void);
358+
359+
356360
#ifdef __cplusplus
357361
}
358362
#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)