From e928ce51bd8c26742abfd537b054483a18a9689c Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 30 Jun 2023 02:37:27 +0200 Subject: show git hash with --version Also, use cmake's configure_file to build string constants in. --- src/CMakeLists.txt | 30 ++++++++++++++++++++++-------- src/client_main.c | 2 +- src/cmd_line.c | 3 ++- src/const.c.template | 13 +++++++++++++ src/const.h | 11 ++++++----- src/server_main.c | 2 +- src/worker.c | 6 +++--- src/worker.h | 4 ++-- src/worker_main.c | 2 +- 9 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 src/const.c.template (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a6af563..6006b7d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,17 +5,21 @@ add_compile_options(-std=c17 -Wpedantic -Wall -Wextra $<$>:- # any features macros defined at all, and prepare to be amazed! add_compile_definitions(_GNU_SOURCE) -add_compile_definitions(VERSION="${PROJECT_VERSION}") +execute_process( + COMMAND git rev-parse --verify HEAD + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + OUTPUT_VARIABLE PROJECT_REV + OUTPUT_STRIP_TRAILING_WHITESPACE + COMMAND_ERROR_IS_FATAL ANY) set(DEFAULT_HOST "127.0.0.1" CACHE STRING "Set default --host value") -add_compile_definitions(DEFAULT_HOST="${DEFAULT_HOST}") +set(DEFAULT_PORT "5556" CACHE STRING "Set default --port value") +set(DEFAULT_SQLITE_PATH "/var/lib/cimple/cimple.sqlite" CACHE STRING "Set default SQLite database path") -function(add_my_executable name) - list(POP_FRONT ARGV) - add_executable("${name}" ${ARGV}) - set_target_properties("${name}" PROPERTIES OUTPUT_NAME "${PROJECT_NAME}-${name}") - install(TARGETS "${name}" RUNTIME DESTINATION bin) -endfunction() +# The input file has a funny extension, because if I use .c.in, CMake for no +# reason thinks that it should use that to build the binaries and slaps me with +# a CMP0115 policy warning. +configure_file(const.c.template const.c ESCAPE_QUOTES @ONLY) find_package(Python3 REQUIRED COMPONENTS Interpreter) @@ -32,9 +36,17 @@ endfunction() generate_sql_header(sqlite) +function(add_my_executable name) + list(POP_FRONT ARGV) + add_executable("${name}" ${ARGV}) + set_target_properties("${name}" PROPERTIES OUTPUT_NAME "${PROJECT_NAME}-${name}") + install(TARGETS "${name}" RUNTIME DESTINATION bin) +endfunction() + add_my_executable(server server_main.c server.c cmd_line.c command.c + const.c event_loop.c file.c log.c @@ -54,6 +66,7 @@ target_include_directories(server PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") add_my_executable(client client_main.c client.c cmd_line.c + const.c file.c log.c msg.c @@ -63,6 +76,7 @@ add_my_executable(worker worker_main.c worker.c ci.c cmd_line.c command.c + const.c event_loop.c file.c git.c diff --git a/src/client_main.c b/src/client_main.c index 0a417d9..ca79800 100644 --- a/src/client_main.c +++ b/src/client_main.c @@ -15,7 +15,7 @@ static struct settings default_settings(void) { - struct settings settings = {DEFAULT_HOST, DEFAULT_PORT}; + struct settings settings = {default_host, default_port}; return settings; } diff --git a/src/cmd_line.c b/src/cmd_line.c index 7f437d1..cd1a50a 100644 --- a/src/cmd_line.c +++ b/src/cmd_line.c @@ -6,6 +6,7 @@ */ #include "cmd_line.h" +#include "const.h" #include "file.h" #include "log.h" @@ -62,7 +63,7 @@ void exit_with_version(void) { char *binary = get_current_binary_name(); - printf("%s %s\n", binary ? binary : "prog", VERSION); + printf("%s v%s (%s)\n", binary ? binary : "prog", project_version, project_rev); free(binary); exit(0); } diff --git a/src/const.c.template b/src/const.c.template new file mode 100644 index 0000000..5afd8ad --- /dev/null +++ b/src/const.c.template @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2023 Egor Tensin + * This file is part of the "cimple" project. + * For details, see https://github.com/egor-tensin/cimple. + * Distributed under the MIT License. + */ + +const char *project_version = "@PROJECT_VERSION@"; +const char *project_rev = "@PROJECT_REV@"; + +const char *default_host = "@DEFAULT_HOST@"; +const char *default_port = "@DEFAULT_PORT@"; +const char *default_sqlite_path = "@DEFAULT_SQLITE_PATH@"; diff --git a/src/const.h b/src/const.h index 2e7054b..94723c6 100644 --- a/src/const.h +++ b/src/const.h @@ -8,11 +8,12 @@ #ifndef __CONST_H__ #define __CONST_H__ -#ifndef DEFAULT_HOST -#define DEFAULT_HOST "127.0.0.1" -#endif -#define DEFAULT_PORT "5556" -#define DEFAULT_SQLITE_PATH "/var/lib/cimple/cimple.sqlite" +extern const char *project_version; +extern const char *project_rev; + +extern const char *default_host; +extern const char *default_port; +extern const char *default_sqlite_path; #define CMD_RUN "run" #define CMD_NEW_WORKER "new-worker" diff --git a/src/server_main.c b/src/server_main.c index ac43bc5..2ebabba 100644 --- a/src/server_main.c +++ b/src/server_main.c @@ -15,7 +15,7 @@ static struct settings default_settings(void) { - struct settings settings = {DEFAULT_PORT, DEFAULT_SQLITE_PATH}; + struct settings settings = {default_port, default_sqlite_path}; return settings; } diff --git a/src/worker.c b/src/worker.c index 8536013..4b4413d 100644 --- a/src/worker.c +++ b/src/worker.c @@ -57,7 +57,7 @@ static struct settings *worker_settings_copy(const struct settings *src) return result; free_host: - free(result->host); + free((void *)result->host); free_result: free(result); @@ -67,8 +67,8 @@ free_result: static void worker_settings_destroy(struct settings *settings) { - free(settings->port); - free(settings->host); + free((void *)settings->port); + free((void *)settings->host); free(settings); } diff --git a/src/worker.h b/src/worker.h index 8eb325c..67402a9 100644 --- a/src/worker.h +++ b/src/worker.h @@ -9,8 +9,8 @@ #define __WORKER_H__ struct settings { - char *host; - char *port; + const char *host; + const char *port; }; struct worker; diff --git a/src/worker_main.c b/src/worker_main.c index 227f9a9..18e656a 100644 --- a/src/worker_main.c +++ b/src/worker_main.c @@ -15,7 +15,7 @@ static struct settings default_settings(void) { - struct settings settings = {DEFAULT_HOST, DEFAULT_PORT}; + struct settings settings = {default_host, default_port}; return settings; } -- cgit v1.2.3