Код: Выделить всё
####################
# Embed build time #
####################
find_package(Python3 REQUIRED COMPONENTS Interpreter)
set(TIMESTAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated_timestamp)
set(TIMESTAMP_H ${TIMESTAMP_DIR}/timestamp.h)
set(TIMESTAMP_CPP ${TIMESTAMP_DIR}/timestamp.cpp)
set(TIMESTAMP_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/generate_timestamp.py)
file(MAKE_DIRECTORY ${TIMESTAMP_DIR})
add_custom_target(generate_timestamp_files
COMMAND ${Python3_EXECUTABLE} ${TIMESTAMP_SCRIPT} ${TIMESTAMP_H} ${TIMESTAMP_CPP}
DEPENDS ${TIMESTAMP_SCRIPT}
COMMENT "Generating timestamp source and header"
)
set_source_files_properties(
${TIMESTAMP_CPP}
${TIMESTAMP_H}
PROPERTIES
GENERATED TRUE
)
add_library(timestamp_lib
${TIMESTAMP_CPP}
)
add_dependencies(timestamp_lib generate_timestamp_files)
target_include_directories(timestamp_lib
INTERFACE
${TIMESTAMP_DIR}
)
target_link_libraries(
timestamp_lib
)
Код: Выделить всё
# generate_timestamp.py
import datetime
import sys
header_file = sys.argv[1]
source_file = sys.argv[2]
now = str(datetime.datetime.now())
h_content = f"""
#pragma once
// File generato automaticamente, non modificare!
extern const char* BUILD_TIMESTAMP;
"""
cpp_content = f"""
#include "timestamp.h"
const char* BUILD_TIMESTAMP = "{now}";
"""
try:
with open(header_file, "w") as f:
f.write(h_content)
with open(source_file, "w") as f:
f.write(cpp_content)
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
Подробнее здесь: https://stackoverflow.com/questions/798 ... very-build
Мобильная версия