diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-30 02:37:27 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-06-30 03:32:14 +0200 |
commit | e928ce51bd8c26742abfd537b054483a18a9689c (patch) | |
tree | ad3bfcf7b509dbde4681c011237c8bd23ee8583f | |
parent | fix function names in error messages (diff) | |
download | cimple-e928ce51bd8c26742abfd537b054483a18a9689c.tar.gz cimple-e928ce51bd8c26742abfd537b054483a18a9689c.zip |
show git hash with --version
Also, use cmake's configure_file to build string constants in.
Diffstat (limited to '')
-rw-r--r-- | .dockerignore | 1 | ||||
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | Dockerfile | 2 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | src/CMakeLists.txt | 30 | ||||
-rw-r--r-- | src/client_main.c | 2 | ||||
-rw-r--r-- | src/cmd_line.c | 3 | ||||
-rw-r--r-- | src/const.c.template | 13 | ||||
-rw-r--r-- | src/const.h | 11 | ||||
-rw-r--r-- | src/server_main.c | 2 | ||||
-rw-r--r-- | src/worker.c | 6 | ||||
-rw-r--r-- | src/worker.h | 4 | ||||
-rw-r--r-- | src/worker_main.c | 2 | ||||
-rw-r--r-- | test/py/test_basic.py | 8 |
14 files changed, 64 insertions, 25 deletions
diff --git a/.dockerignore b/.dockerignore index d6441d7..3c0c9fc 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,6 @@ * +!/.git/** !/CMakeLists.txt !/LICENSE.txt !/Makefile diff --git a/CMakeLists.txt b/CMakeLists.txt index 848890d..95f423e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.3) # for include-what-you-use +cmake_minimum_required(VERSION 3.19) # for COMMAND_ERROR_IS_FATAL project(cimple VERSION 0.0.1 LANGUAGES C) @@ -10,6 +10,7 @@ RUN build_deps='bash bsd-compat-headers build-base clang cmake coreutils git lib ARG COMPILER=clang ARG CONFIGURATION=Release ARG DEFAULT_HOST=127.0.0.1 +ARG DEFAULT_PORT=5556 ARG src_dir="/app/src" ARG install_dir @@ -21,6 +22,7 @@ RUN cd -- "$src_dir" && \ "COMPILER=$COMPILER" \ "CONFIGURATION=$CONFIGURATION" \ "DEFAULT_HOST=$DEFAULT_HOST" \ + "DEFAULT_PORT=$DEFAULT_PORT" \ "INSTALL_PREFIX=$install_dir" && \ ulimit -n 1024 && \ make test/all CONFIGURATION="$CONFIGURATION" @@ -28,11 +28,13 @@ install_dir := $(build_dir)/install COMPILER ?= clang CONFIGURATION ?= Debug DEFAULT_HOST ?= 127.0.0.1 +DEFAULT_PORT ?= 5556 INSTALL_PREFIX ?= $(install_dir) $(eval $(call noexpand,COMPILER)) $(eval $(call noexpand,CONFIGURATION)) $(eval $(call noexpand,DEFAULT_HOST)) +$(eval $(call noexpand,DEFAULT_PORT)) $(eval $(call noexpand,INSTALL_PREFIX)) .PHONY: all @@ -51,6 +53,7 @@ build: -D 'CMAKE_BUILD_TYPE=$(call escape,$(CONFIGURATION))' \ -D 'CMAKE_INSTALL_PREFIX=$(call escape,$(INSTALL_PREFIX))' \ -D 'DEFAULT_HOST=$(call escape,$(DEFAULT_HOST))' \ + -D 'DEFAULT_PORT=$(call escape,$(DEFAULT_PORT))' \ -S '$(call escape,$(src_dir))' \ -B '$(call escape,$(cmake_dir))' cmake --build '$(call escape,$(cmake_dir))' -- -j 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 $<$<NOT:$<CONFIG:Debug>>:- # 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 <Egor.Tensin@gmail.com> + * 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; } diff --git a/test/py/test_basic.py b/test/py/test_basic.py index c34f9ef..ab062e5 100644 --- a/test/py/test_basic.py +++ b/test/py/test_basic.py @@ -3,11 +3,15 @@ # For details, see https://github.com/egor-tensin/cimple. # Distributed under the MIT License. +import re + def test_server_and_workers_run(server_and_workers): pass def test_client_version(client, version): - output = client.run('--version') - assert output.endswith(version + '\n') + output = client.run('--version').removesuffix('\n') + match = re.match(r'^cimple-client v(\d+\.\d+\.\d+) \([0-9a-f]{40,}\)$', output) + assert match, f'Invalid --version output: {output}' + assert match.group(1) == version |