From c6f1fd25f02f3d5c33fdc1b3039b40d22d1f0e2a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 7 Mar 2023 10:00:22 +0800 Subject: [PATCH] build: git describe tag as version info --- .gitignore | 4 ++- CMakeLists.txt | 43 +++++++++++++++++++++++++-- include/{constant.h => constant.h.in} | 4 +-- src/CMakeLists.txt | 5 ++++ src/cleardns.c | 1 - src/common/sundry.c | 2 +- 6 files changed, 51 insertions(+), 8 deletions(-) rename include/{constant.h => constant.h.in} (95%) diff --git a/.gitignore b/.gitignore index d081224..88a9696 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ /bin/ /.idea/ -/assets/*.txt /src/target/ /cmake-build/ /cmake-build-debug/ /cmake-build-release/ + +/assets/*.txt +/include/constant.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e793597..0699993 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,45 @@ cmake_minimum_required(VERSION 2.8.12) -project(cleardns) -set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin) +project(cleardns LANGUAGES C) + +set(CMAKE_C_STANDARD 99) + +set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror") + +############################################################### + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + +if(NOT CMAKE_EXE_LINKER_FLAGS) + set(CMAKE_EXE_LINKER_FLAGS -static) +endif() + +############################################################### + +macro(git_tag _tag) + find_package(Git QUIET) + if (GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags + OUTPUT_VARIABLE ${_tag} + OUTPUT_STRIP_TRAILING_WHITESPACE + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) + endif() +endmacro() + +set(VERSION "") +git_tag(VERSION) +if(VERSION STREQUAL "") # without git tag + message(FATAL_ERROR "Git command not found") +endif() + +############################################################### add_subdirectory(src) + +############################################################### diff --git a/include/constant.h b/include/constant.h.in similarity index 95% rename from include/constant.h rename to include/constant.h.in index c25c4f7..768409c 100644 --- a/include/constant.h +++ b/include/constant.h.in @@ -13,9 +13,7 @@ #define RESTART_DELAY 1 #define DIVERTER_TIMEOUT 6 -// TODO: using CMAKE template - -#define VERSION "1.3.3" +#define VERSION "@VERSION@" #define CONFIG_FILE "cleardns.yml" #define DNSPROXY_BIN "dnsproxy" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 44b62f9..51cb6ac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,11 @@ include_directories(${PROJECT_SOURCE_DIR}/include/loader) link_directories(${PROJECT_SOURCE_DIR}/src/target/release) +configure_file( + ${PROJECT_SOURCE_DIR}/include/constant.h.in + ${PROJECT_SOURCE_DIR}/include/constant.h +) + add_subdirectory(utils) add_subdirectory(applet) add_subdirectory(bcrypt) diff --git a/src/cleardns.c b/src/cleardns.c index fdfda2a..492c8fc 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -5,7 +5,6 @@ #include #include "loader.h" #include "logger.h" -#include "sundry.h" #include "system.h" #include "assets.h" #include "adguard.h" diff --git a/src/common/sundry.c b/src/common/sundry.c index e425b64..b09a213 100644 --- a/src/common/sundry.c +++ b/src/common/sundry.c @@ -49,7 +49,7 @@ void uint32_list_debug(char *describe, uint32_t **uint32_list) { // show uint32 } uint8_t check_port(uint16_t port) { // whether port is valid - if (port > 0 && port <= 65535) { // 1 ~ 65535 + if (port > 0) { // 1 ~ 65535 (uint16_t <= 65535) return TRUE; } return FALSE;