1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef VERSION_H 17 #define VERSION_H 18 #include <cstdint> 19 #include <string> 20 #include "securec.h" 21 class Version { 22 private: 23 #if MAJOR_VERSION 24 static constexpr const uint32_t kMajorVersion = MAJOR_VERSION; 25 #else // MAJOR_VERSION 26 static constexpr const uint32_t kMajorVersion = 1; 27 #endif // MAJOR_VERSION 28 29 #ifdef MINOR_VERSION 30 static constexpr const uint32_t kMinorVersion = MINOR_VERSION; 31 #else 32 static constexpr const uint32_t kMinorVersion = 0; 33 #endif // MINOR_VERSION 34 35 // [a-zA-Z0-9_\-]+ 36 #ifdef RELEASE_VERSION 37 static constexpr const char *kReleaseVersion = RELEASE_VERSION; 38 #else 39 static constexpr const char *kReleaseVersion = "0"; 40 #endif // RELEASE_VERSION 41 42 // B[0-9]{3} 43 #ifdef BUILD_VERSION 44 static constexpr const uint32_t kBuildVersion = BUILD_VERSION; 45 #endif // BUILD_VERSION 46 47 #ifdef GIT_REVISION 48 static constexpr const char *kGitRevision = GIT_REVISION; 49 #endif // GIT_REVISION 50 51 #ifdef MINOR_RUNTIME_VERSION 52 static constexpr const uint32_t kMinorRuntimeVersion = MINOR_RUNTIME_VERSION; 53 #else 54 static constexpr const uint32_t kMinorRuntimeVersion = 0; 55 #endif // kMinorRuntimeVersion 56 57 public: GetVersionStr()58 static std::string GetVersionStr() 59 { 60 std::ostringstream oss; 61 oss << kMajorVersion << "." << kMinorVersion << "." << kReleaseVersion; 62 63 #ifdef BUILD_VERSION 64 constexpr int BUILD_VERSION_LEN = 5; 65 char buffer[BUILD_VERSION_LEN] = {0}; 66 int ret = sprintf_s(buffer, BUILD_VERSION_LEN, "B%03d", kBuildVersion); 67 if (ret >= 0) { 68 oss << "." << buffer; 69 } 70 #endif // BUILD_VERSION 71 72 #ifdef GIT_REVISION 73 oss << " " << kGitRevision; 74 #endif // GIT_REVISION 75 return oss.str(); 76 } 77 GetMajorVersion()78 static inline uint32_t GetMajorVersion() 79 { 80 return kMajorVersion; 81 } 82 GetMinorVersion()83 static inline uint32_t GetMinorVersion() 84 { 85 return kMinorVersion; 86 } 87 GetReleaseVersion()88 static inline const char *GetReleaseVersion() 89 { 90 return kReleaseVersion; 91 } 92 GetRuntimeVersion()93 static inline uint32_t GetRuntimeVersion() 94 { 95 return kMinorRuntimeVersion; 96 } 97 }; 98 #endif // VERSION_H 99