From daa957823f7da2fa2eb9a44916ea726b76e0d99e Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Sun, 20 Sep 2026 10:31:18 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=B8=BB=E7=BA=BF=E5=BE=85=E5=90=88?= =?UTF-8?q?=E3=80=91=E6=AF=8F=E6=AC=A1=E9=83=BD=E9=87=8D=E6=96=B0=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=97=B6=E9=97=B4=E6=88=B3=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=88=B0=E7=9A=84=E6=98=AF=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?=E7=9A=84=E7=BC=96=E8=AF=91=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/common/CMakeLists.txt | 16 ++++++++++++++++ library/common/cmake/gen_build_time.cmake | 16 ++++++++++++++++ library/common/common.c | 5 +++-- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 library/common/cmake/gen_build_time.cmake diff --git a/library/common/CMakeLists.txt b/library/common/CMakeLists.txt index d05f73b..969e96a 100644 --- a/library/common/CMakeLists.txt +++ b/library/common/CMakeLists.txt @@ -8,3 +8,19 @@ if(EXISTS ${COMMON_CMAKE_PATH}) else() message(FATAL_ERROR "Cannot find common build logic at ${COMMON_CMAKE_PATH}") endif() + +# 每次构建重新生成 build_time.h(custom target 永远过期), +# 保证 common.c 因头文件内容/时间变化而重新编译,Rd_GetBuildTime() 拿到最新时间。 +set(BUILD_TIME_HEADER "${CMAKE_CURRENT_BINARY_DIR}/build_time.h") + +add_custom_target(GenBuildTime + COMMAND ${CMAKE_COMMAND} + "-DOUTPUT=${BUILD_TIME_HEADER}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/cmake/gen_build_time.cmake" + BYPRODUCTS "${BUILD_TIME_HEADER}" + COMMENT "[${TARGET_NAME}] Generating build_time.h" + VERBATIM +) + +add_dependencies(${TARGET_NAME} GenBuildTime) +target_include_directories(${TARGET_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}") \ No newline at end of file diff --git a/library/common/cmake/gen_build_time.cmake b/library/common/cmake/gen_build_time.cmake new file mode 100644 index 0000000..295a883 --- /dev/null +++ b/library/common/cmake/gen_build_time.cmake @@ -0,0 +1,16 @@ +# 构建时生成 build_time.h —— 每次构建都会重新生成,内容必然变化, +# 从而保证包含它的 common.c 必定重新编译,Rd_GetBuildTime() 返回最新构建时间。 +# 用法(由 add_custom_target 以脚本模式调用): +# cmake -DOUTPUT= -P gen_build_time.cmake + +string(TIMESTAMP RD_BUILD_TIME "%Y-%m-%d %H:%M:%S") + +file(WRITE "${OUTPUT}" + "/* Auto-generated by gen_build_time.cmake. DO NOT EDIT. */\n" + "#ifndef RD_BUILD_TIME_H\n" + "#define RD_BUILD_TIME_H\n" + "#define RD_BUILD_TIME \"${RD_BUILD_TIME}\"\n" + "#endif /* RD_BUILD_TIME_H */\n" +) + +message(STATUS "[GenBuildTime] build time -> ${RD_BUILD_TIME} (${OUTPUT})") diff --git a/library/common/common.c b/library/common/common.c index eacac22..cc732e2 100644 --- a/library/common/common.c +++ b/library/common/common.c @@ -2,6 +2,7 @@ #include #include #include "common.h" +#include "build_time.h" // 构建时生成,保证每次编译拿到最新时间 #include // 必须包含此头文件以使用变参宏 static char g_sBuildTime[128] = {0}; @@ -98,9 +99,9 @@ int Rd_RegularFileExists(const char *_psPath) char *Rd_GetBuildTime(void) { #ifdef RD_HOSTNAME - snprintf(g_sBuildTime, sizeof(g_sBuildTime), "%s build [%s %s] by %s", RD_VERSION, __DATE__, __TIME__, RD_HOSTNAME); + snprintf(g_sBuildTime, sizeof(g_sBuildTime), "%s build [%s] by %s", RD_VERSION, RD_BUILD_TIME, RD_HOSTNAME); #else - snprintf(g_sBuildTime, sizeof(g_sBuildTime), "%s build [%s %s]", RD_VERSION, __DATE__, __TIME__); + snprintf(g_sBuildTime, sizeof(g_sBuildTime), "%s build [%s]", RD_VERSION, RD_BUILD_TIME); #endif return g_sBuildTime; }