232 lines
8.9 KiB
C++
232 lines
8.9 KiB
C++
#pragma once
|
||
/**
|
||
* @file version.h
|
||
* @brief ViewDesignEngine 版本信息
|
||
*
|
||
* 提供编译时版本宏和运行时版本查询函数。
|
||
* 编译时间戳通过 CMake 的 configure_file 在构建时注入。
|
||
*
|
||
* @ingroup core
|
||
*/
|
||
|
||
#include <cstdint>
|
||
#include <string>
|
||
|
||
// 包含 CMake 生成的构建配置
|
||
#if __has_include("vde_build_config.h")
|
||
#include "vde_build_config.h"
|
||
#endif
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// 编译时版本宏
|
||
// ═══════════════════════════════════════════════════════════════
|
||
|
||
/// 主版本号(不兼容的 API 变更)
|
||
#define VDE_VERSION_MAJOR 1
|
||
|
||
/// 次版本号(向后兼容的功能新增)
|
||
#define VDE_VERSION_MINOR 0
|
||
|
||
/// 修订版本号(向后兼容的问题修复)
|
||
#define VDE_VERSION_PATCH 400
|
||
|
||
/// 版本字符串(与 CHANGELOG / CMake project VERSION 保持同步;
|
||
/// 1.0.1 之前的漂移已在本版修正)
|
||
#define VDE_VERSION_STRING "1.0.400"
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// D11: API 版本化 + 稳定性声明
|
||
// ═══════════════════════════════════════════════════════════════
|
||
|
||
/// 当前 API 兼容版本(API 版本独立于库版本,向后兼容保证的载体)
|
||
/// 规则:新增 API → minor+1;不兼容变更 → major+1 并废弃旧 API 一个周期
|
||
#define VDE_API_VERSION_MAJOR 1
|
||
#define VDE_API_VERSION_MINOR 0
|
||
|
||
/// API 版本编码(用于运行时比较)
|
||
#define VDE_API_VERSION_CODE \
|
||
VDE_VERSION_CODE(VDE_API_VERSION_MAJOR, VDE_API_VERSION_MINOR, 0)
|
||
|
||
/**
|
||
* @def VDE_DEPRECATED(msg)
|
||
* @brief 标记已废弃 API(D11 废弃策略)
|
||
*
|
||
* 在声明处使用,编译时产生 deprecated 警告。
|
||
* 用法:
|
||
* @code
|
||
* VDE_DEPRECATED("使用 make_box 替代") BrepModel old_box(...);
|
||
* @endcode
|
||
*
|
||
* 兼容 GCC 4.9+ / Clang / MSVC 2015+。
|
||
*/
|
||
#if defined(__GNUC__) || defined(__clang__)
|
||
#define VDE_DEPRECATED(msg) __attribute__((deprecated(msg)))
|
||
#elif defined(_MSC_VER)
|
||
#define VDE_DEPRECATED(msg) __declspec(deprecated(msg))
|
||
#else
|
||
#define VDE_DEPRECATED(msg)
|
||
#endif
|
||
|
||
/**
|
||
* @def VDE_API_SINCE(major, minor)
|
||
* @brief 标记 API 引入版本(D11 稳定性声明)
|
||
*
|
||
* 用法:
|
||
* @code
|
||
* VDE_API_SINCE(1, 1) [[nodiscard]] NurbsCurve trim_curve(...);
|
||
* @endcode
|
||
*
|
||
* 展开为注释标记 + 编译期断言,确保 API 版本 ≥ 声明的引入版本。
|
||
*/
|
||
#define VDE_API_SINCE(major, minor) \
|
||
static_assert(VDE_VERSION_CODE(major, minor, 0) <= VDE_API_VERSION_CODE, \
|
||
"VDE: API since-version exceeds current API version")
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// 版本号整型(用于比较) ────────────────────────────
|
||
/// 版本号编码为大整数: VDE_VERSION(major, minor, patch) = major*10000 + minor*100 + patch
|
||
#define VDE_VERSION_CODE(major, minor, patch) \
|
||
((major) * 10000 + (minor) * 100 + (patch))
|
||
|
||
/// 当前版本号整型编码(1.0.400 → 10400)。必须保持
|
||
/// `VDE_VERSION_CODE(1, 0, 140)` 字面量形式:tools/vde_release.py 按该形式
|
||
/// 校验/递增(版本三处一致性 + --check),改为推导宏会失去自动核对能力。
|
||
#define VDE_VERSION_NUM VDE_VERSION_CODE(1, 0, 400)
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// 构建信息(由 CMake configure_file 注入)
|
||
// ═══════════════════════════════════════════════════════════════
|
||
|
||
#ifndef VDE_BUILD_TYPE
|
||
/// 构建类型:Debug / Release / RelWithDebInfo / MinSizeRel
|
||
#ifdef NDEBUG
|
||
#define VDE_BUILD_TYPE "Release"
|
||
#else
|
||
#define VDE_BUILD_TYPE "Debug"
|
||
#endif
|
||
#endif
|
||
|
||
#ifndef VDE_BUILD_TIMESTAMP
|
||
/// 构建时间戳(ISO 8601 格式,如 "2026-07-27T12:00:00Z")
|
||
/// 由 CMake 的 configure_file 注入
|
||
#define VDE_BUILD_TIMESTAMP "unknown"
|
||
#endif
|
||
|
||
#ifndef VDE_COMPILER_INFO
|
||
/// 编译器信息字符串
|
||
#if defined(__clang__)
|
||
#define VDE_COMPILER_INFO "Clang " __clang_version__
|
||
#elif defined(__GNUC__) || defined(__GNUG__)
|
||
#define VDE_COMPILER_INFO "GCC " __VERSION__
|
||
#elif defined(_MSC_VER)
|
||
// _MSC_VER is an integer (e.g. 1951); stringify it explicitly
|
||
#define VDE_STR2(x) #x
|
||
#define VDE_STR(x) VDE_STR2(x)
|
||
#define VDE_COMPILER_INFO "MSVC " VDE_STR(_MSC_FULL_VER)
|
||
#else
|
||
#define VDE_COMPILER_INFO "Unknown Compiler"
|
||
#endif
|
||
#endif
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// C++ 标准信息
|
||
// ═══════════════════════════════════════════════════════════════
|
||
|
||
/// C++ 标准字符串(如 "C++20")
|
||
#if __cplusplus >= 202002L
|
||
#define VDE_CPP_STANDARD_STRING "C++20"
|
||
#elif __cplusplus >= 201703L
|
||
#define VDE_CPP_STANDARD_STRING "C++17"
|
||
#elif __cplusplus >= 201402L
|
||
#define VDE_CPP_STANDARD_STRING "C++14"
|
||
#elif __cplusplus >= 201103L
|
||
#define VDE_CPP_STANDARD_STRING "C++11"
|
||
#else
|
||
#define VDE_CPP_STANDARD_STRING "C++03"
|
||
#endif
|
||
|
||
namespace vde::core {
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// VersionInfo 结构体
|
||
// ═══════════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 版本信息结构体
|
||
*
|
||
* 包含库版本号、构建类型、时间戳和编译器信息。
|
||
*/
|
||
struct VersionInfo {
|
||
int major; ///< 主版本号
|
||
int minor; ///< 次版本号
|
||
int patch; ///< 修订版本号
|
||
std::string version_string; ///< 版本字符串 "1.0.1"
|
||
std::string build_type; ///< 构建类型 "Debug"/"Release"
|
||
std::string build_timestamp;///< 构建时间戳
|
||
std::string compiler_info; ///< 编译器信息
|
||
std::string cpp_standard; ///< C++ 标准字符串 "C++20"
|
||
|
||
/// 默认构造(使用编译时宏填充)
|
||
VersionInfo()
|
||
: major(VDE_VERSION_MAJOR)
|
||
, minor(VDE_VERSION_MINOR)
|
||
, patch(VDE_VERSION_PATCH)
|
||
, version_string(VDE_VERSION_STRING)
|
||
, build_type(VDE_BUILD_TYPE)
|
||
, build_timestamp(VDE_BUILD_TIMESTAMP)
|
||
, compiler_info(VDE_COMPILER_INFO)
|
||
, cpp_standard(VDE_CPP_STANDARD_STRING) {}
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════════════
|
||
// API 函数
|
||
// ═══════════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief 获取版本信息结构体
|
||
* @return VersionInfo 包含完整的版本和构建信息
|
||
*/
|
||
inline VersionInfo vde_version() {
|
||
return VersionInfo{};
|
||
}
|
||
|
||
/**
|
||
* @brief 获取人类可读的版本字符串
|
||
*
|
||
* 格式:"ViewDesignEngine v1.0.1 (C++20, GCC 11.4)"
|
||
*
|
||
* @return 版本信息字符串
|
||
*/
|
||
inline std::string vde_version_string() {
|
||
std::string result = "ViewDesignEngine v";
|
||
result += VDE_VERSION_STRING;
|
||
result += " (";
|
||
result += VDE_CPP_STANDARD_STRING;
|
||
result += ", ";
|
||
result += VDE_COMPILER_INFO;
|
||
result += ")";
|
||
return result;
|
||
}
|
||
|
||
/**
|
||
* @brief 比较两个版本号
|
||
*
|
||
* 返回负值表示 lhs < rhs,零表示相等,正值表示 lhs > rhs。
|
||
*
|
||
* @param lhs_major 左主版本
|
||
* @param lhs_minor 左次版本
|
||
* @param lhs_patch 左修订版本
|
||
* @param rhs_major 右主版本
|
||
* @param rhs_minor 右次版本
|
||
* @param rhs_patch 右修订版本
|
||
* @return 比较结果
|
||
*/
|
||
inline int vde_version_compare(int lhs_major, int lhs_minor, int lhs_patch,
|
||
int rhs_major, int rhs_minor, int rhs_patch) {
|
||
int lhs = VDE_VERSION_CODE(lhs_major, lhs_minor, lhs_patch);
|
||
int rhs = VDE_VERSION_CODE(rhs_major, rhs_minor, rhs_patch);
|
||
return lhs - rhs;
|
||
}
|
||
|
||
} // namespace vde::core
|