Skip to content

Commit 0f031e4

Browse files
committed
Allow external setting of GIT_VERSION
1 parent f52a85f commit 0f031e4

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

CMakeLists.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-gnu-zero-variadic-macro-arguments -Wno-
3535
endif()
3636

3737
# VERSION
38+
# It is possible to define external default value like: cmake -DSW_VERSION=1.2-custom
3839

3940
find_package(Git)
4041
if(Git_FOUND)
@@ -43,17 +44,27 @@ if(Git_FOUND)
4344
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
4445
OUTPUT_VARIABLE GIT_VERSION
4546
OUTPUT_STRIP_TRAILING_WHITESPACE)
46-
message(STATUS "Version: ${GIT_VERSION}")
47+
48+
if(GIT_VERSION)
49+
set(SW_VERSION "${GIT_VERSION}")
50+
else()
51+
message(WARNING "Could not find out version from git command!")
52+
endif()
4753

4854
# May not update version in some cases (example: git commit --amend)
4955
set_property(GLOBAL APPEND
5056
PROPERTY CMAKE_CONFIGURE_DEPENDS
5157
"${CMAKE_SOURCE_DIR}/.git/index")
5258
else()
53-
set(GIT_VERSION "UNKNOWN")
54-
message(WARNING "Could not find git command! Version is set to: ${GIT_VERSION}")
59+
message(WARNING "Could not find git command!")
60+
endif()
61+
62+
if(NOT SW_VERSION)
63+
set(SW_VERSION "UNKNOWN")
5564
endif()
5665

66+
message(STATUS "Version is set to: ${SW_VERSION}")
67+
5768
# LIBRARY DEPENDENCIES
5869

5970
find_path(LIBCARES_INCLUDE_DIR ares.h)
@@ -100,8 +111,8 @@ set_property(TARGET ${TARGET_NAME} PROPERTY C_STANDARD 11)
100111
define_file_basename_for_sources("https_dns_proxy")
101112

102113
set_source_files_properties(
103-
src/main.c src/options.c
104-
PROPERTIES COMPILE_FLAGS "-DGIT_VERSION='\"${GIT_VERSION}\"'")
114+
src/options.c
115+
PROPERTIES COMPILE_FLAGS "-DSW_VERSION='\"${SW_VERSION}\"'")
105116

106117
if(CLANG_TIDY_EXE)
107118
set_target_properties(

src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ int main(int argc, char *argv[]) {
216216

217217
logging_init(opt.logfd, opt.loglevel);
218218

219-
ILOG("Version " GIT_VERSION);
220-
ILOG("Built "__DATE__" "__TIME__".");
219+
ILOG("Version: %s", options_sw_version());
220+
ILOG("Built: " __DATE__ " " __TIME__);
221221
ILOG("System c-ares: %s", ares_version(NULL));
222222
ILOG("System libcurl: %s", curl_version());
223223

src/options.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717

1818
#define DEFAULT_HTTP_VERSION 2
1919

20+
21+
const char * options_sw_version() {
22+
#ifdef SW_VERSION
23+
return SW_VERSION;
24+
#else
25+
return "UNKNOWN";
26+
#endif
27+
}
28+
2029
void options_init(struct Options *opt) {
2130
opt->listen_addr = "127.0.0.1";
2231
opt->listen_port = 5053;
@@ -107,7 +116,7 @@ int options_parse_args(struct Options *opt, int argc, char **argv) {
107116
case 'h':
108117
return -1;
109118
case 'V': // version
110-
printf("%s\n", GIT_VERSION);
119+
printf("%s\n", options_sw_version());
111120
exit(0);
112121
default:
113122
printf("Unknown state!");

src/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct Options options_t;
5757
#ifdef __cplusplus
5858
extern "C" {
5959
#endif
60+
const char * options_sw_version();
6061
void options_init(struct Options *opt);
6162
int options_parse_args(struct Options *opt, int argc, char **argv);
6263
void options_show_usage(int argc, char **argv);

0 commit comments

Comments
 (0)