You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2021/12/21 01:33:46 UTC

[pulsar] 10/18: [c++] Define and expose PULSAR_VERSION macro (#12769)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch branch-2.9
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit a5c34667e1eb293a928efded16d7de73c0add6ea
Author: Masahiro Sakamoto <ma...@yahoo-corp.jp>
AuthorDate: Fri Nov 26 11:43:13 2021 +0900

    [c++] Define and expose PULSAR_VERSION macro (#12769)
    
    * Define and expose PULSAR_VERSION macro
    
    * Change default value of _PULSAR_VERSION_INTERNAL_ to unknown
    
    (cherry picked from commit 2dcf7e924e3d393a23fca3522f539e95c5ae4d62)
---
 pulsar-client-cpp/.gitignore                       |  3 ++
 pulsar-client-cpp/CMakeLists.txt                   |  4 +++
 .../{lib/Version.h => include/pulsar/c/version.h}  |  8 ++---
 pulsar-client-cpp/lib/CMakeLists.txt               |  2 +-
 pulsar-client-cpp/lib/Commands.cc                  |  6 ++--
 pulsar-client-cpp/lib/HTTPLookupService.cc         |  2 +-
 pulsar-client-cpp/lib/HTTPLookupService.h          |  2 +-
 .../lib/{Version.h => VersionInternal.h}           | 10 +++----
 .../{lib/Version.h => templates/Version.h.in}      | 14 +++++----
 .../{lib/Version.h => tests/VersionTest.cc}        | 15 ++++++----
 src/gen-pulsar-version-macro.py                    | 35 ++++++++++++++++++++++
 11 files changed, 72 insertions(+), 29 deletions(-)

diff --git a/pulsar-client-cpp/.gitignore b/pulsar-client-cpp/.gitignore
index 0f3d36d..f2a623d 100644
--- a/pulsar-client-cpp/.gitignore
+++ b/pulsar-client-cpp/.gitignore
@@ -47,6 +47,9 @@ lib*.so*
 /perf/perfConsumer
 /system-test/SystemTest
 
+# Files generated from templates by CMAKE
+include/pulsar/Version.h
+
 # IDE generated files
 .csettings
 .cproject
diff --git a/pulsar-client-cpp/CMakeLists.txt b/pulsar-client-cpp/CMakeLists.txt
index 7c95791..8521dbf 100644
--- a/pulsar-client-cpp/CMakeLists.txt
+++ b/pulsar-client-cpp/CMakeLists.txt
@@ -21,6 +21,10 @@ cmake_minimum_required(VERSION 3.4)
 project (pulsar-cpp)
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
 
+execute_process(COMMAND python ${CMAKE_SOURCE_DIR}/../src/gen-pulsar-version-macro.py OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE PVM)
+set(PVM_COMMENT "This is generated from Version.h.in by CMAKE. DO NOT EDIT DIRECTLY")
+configure_file(templates/Version.h.in include/pulsar/Version.h @ONLY)
+
 if (VCPKG_TRIPLET)
     message(STATUS "Use vcpkg, triplet is ${VCPKG_TRIPLET}")
     set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}")
diff --git a/pulsar-client-cpp/lib/Version.h b/pulsar-client-cpp/include/pulsar/c/version.h
similarity index 85%
copy from pulsar-client-cpp/lib/Version.h
copy to pulsar-client-cpp/include/pulsar/c/version.h
index a274f47..ab63c8a 100644
--- a/pulsar-client-cpp/lib/Version.h
+++ b/pulsar-client-cpp/include/pulsar/c/version.h
@@ -16,11 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef LIB_VERSION_H_
-#define LIB_VERSION_H_
 
-#ifndef _PULSAR_VERSION_
-#define _PULSAR_VERSION_ "1.17"
-#endif
+#pragma once
 
-#endif /* LIB_VERSION_H_ */
+#include <pulsar/Version.h>
diff --git a/pulsar-client-cpp/lib/CMakeLists.txt b/pulsar-client-cpp/lib/CMakeLists.txt
index 6e970fa..ee9214e 100644
--- a/pulsar-client-cpp/lib/CMakeLists.txt
+++ b/pulsar-client-cpp/lib/CMakeLists.txt
@@ -20,7 +20,7 @@
 file(GLOB PULSAR_SOURCES *.cc *.h lz4/*.cc lz4/*.h checksum/*.cc checksum/*.h stats/*.cc stats/*.h c/*.cc c/*.h auth/*.cc auth/*.h auth/athenz/*.cc auth/athenz/*.h)
 
 execute_process(COMMAND python ${CMAKE_SOURCE_DIR}/../src/get-project-version.py OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE PV)
-set (CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -D_PULSAR_VERSION_=\\\"${PV}\\\"")
+set (CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -D_PULSAR_VERSION_INTERNAL_=\\\"${PV}\\\"")
 
 if (NOT PROTOC_PATH)
     set(PROTOC_PATH protoc)
diff --git a/pulsar-client-cpp/lib/Commands.cc b/pulsar-client-cpp/lib/Commands.cc
index 54c8c65..1094efb 100644
--- a/pulsar-client-cpp/lib/Commands.cc
+++ b/pulsar-client-cpp/lib/Commands.cc
@@ -18,7 +18,7 @@
  */
 #include "Commands.h"
 #include "MessageImpl.h"
-#include "Version.h"
+#include "VersionInternal.h"
 #include "pulsar/MessageBuilder.h"
 #include "LogUtils.h"
 #include "PulsarApi.pb.h"
@@ -215,7 +215,7 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const
     BaseCommand cmd;
     cmd.set_type(BaseCommand::CONNECT);
     CommandConnect* connect = cmd.mutable_connect();
-    connect->set_client_version(_PULSAR_VERSION_);
+    connect->set_client_version(_PULSAR_VERSION_INTERNAL_);
     connect->set_auth_method_name(authentication->getAuthMethodName());
     connect->set_protocol_version(ProtocolVersion_MAX);
 
@@ -243,7 +243,7 @@ SharedBuffer Commands::newAuthResponse(const AuthenticationPtr& authentication,
     BaseCommand cmd;
     cmd.set_type(BaseCommand::AUTH_RESPONSE);
     CommandAuthResponse* authResponse = cmd.mutable_authresponse();
-    authResponse->set_client_version(_PULSAR_VERSION_);
+    authResponse->set_client_version(_PULSAR_VERSION_INTERNAL_);
 
     AuthData* authData = authResponse->mutable_response();
     authData->set_auth_method_name(authentication->getAuthMethodName());
diff --git a/pulsar-client-cpp/lib/HTTPLookupService.cc b/pulsar-client-cpp/lib/HTTPLookupService.cc
index a54a4c1..d377171 100644
--- a/pulsar-client-cpp/lib/HTTPLookupService.cc
+++ b/pulsar-client-cpp/lib/HTTPLookupService.cc
@@ -151,7 +151,7 @@ void HTTPLookupService::handleNamespaceTopicsHTTPRequest(NamespaceTopicsPromise
 Result HTTPLookupService::sendHTTPRequest(const std::string completeUrl, std::string &responseData) {
     CURL *handle;
     CURLcode res;
-    std::string version = std::string("Pulsar-CPP-v") + _PULSAR_VERSION_;
+    std::string version = std::string("Pulsar-CPP-v") + _PULSAR_VERSION_INTERNAL_;
     handle = curl_easy_init();
 
     if (!handle) {
diff --git a/pulsar-client-cpp/lib/HTTPLookupService.h b/pulsar-client-cpp/lib/HTTPLookupService.h
index 166a14a..eb73654 100644
--- a/pulsar-client-cpp/lib/HTTPLookupService.h
+++ b/pulsar-client-cpp/lib/HTTPLookupService.h
@@ -22,7 +22,7 @@
 #include <lib/LookupService.h>
 #include <lib/ClientImpl.h>
 #include <lib/Url.h>
-#include <lib/Version.h>
+#include <lib/VersionInternal.h>
 
 namespace pulsar {
 class HTTPLookupService : public LookupService, public std::enable_shared_from_this<HTTPLookupService> {
diff --git a/pulsar-client-cpp/lib/Version.h b/pulsar-client-cpp/lib/VersionInternal.h
similarity index 82%
copy from pulsar-client-cpp/lib/Version.h
copy to pulsar-client-cpp/lib/VersionInternal.h
index a274f47..c256035 100644
--- a/pulsar-client-cpp/lib/Version.h
+++ b/pulsar-client-cpp/lib/VersionInternal.h
@@ -16,11 +16,11 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef LIB_VERSION_H_
-#define LIB_VERSION_H_
+#ifndef LIB_VERSION_INTERNAL_H_
+#define LIB_VERSION_INTERNAL_H_
 
-#ifndef _PULSAR_VERSION_
-#define _PULSAR_VERSION_ "1.17"
+#ifndef _PULSAR_VERSION_INTERNAL_
+#define _PULSAR_VERSION_INTERNAL_ "unknown"
 #endif
 
-#endif /* LIB_VERSION_H_ */
+#endif /* LIB_VERSION_INTERNAL_H_ */
diff --git a/pulsar-client-cpp/lib/Version.h b/pulsar-client-cpp/templates/Version.h.in
similarity index 85%
copy from pulsar-client-cpp/lib/Version.h
copy to pulsar-client-cpp/templates/Version.h.in
index a274f47..d52121a 100644
--- a/pulsar-client-cpp/lib/Version.h
+++ b/pulsar-client-cpp/templates/Version.h.in
@@ -16,11 +16,13 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef LIB_VERSION_H_
-#define LIB_VERSION_H_
 
-#ifndef _PULSAR_VERSION_
-#define _PULSAR_VERSION_ "1.17"
-#endif
+/**
+ * @PVM_COMMENT@
+ */
+#ifndef PULSAR_VERSION_H_
+#define PULSAR_VERSION_H_
+
+#define PULSAR_VERSION @PVM@
 
-#endif /* LIB_VERSION_H_ */
+#endif /* PULSAR_VERSION_H_ */
diff --git a/pulsar-client-cpp/lib/Version.h b/pulsar-client-cpp/tests/VersionTest.cc
similarity index 79%
rename from pulsar-client-cpp/lib/Version.h
rename to pulsar-client-cpp/tests/VersionTest.cc
index a274f47..57e1e78 100644
--- a/pulsar-client-cpp/lib/Version.h
+++ b/pulsar-client-cpp/tests/VersionTest.cc
@@ -16,11 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifndef LIB_VERSION_H_
-#define LIB_VERSION_H_
+#include <pulsar/Version.h>
+#include <gtest/gtest.h>
 
-#ifndef _PULSAR_VERSION_
-#define _PULSAR_VERSION_ "1.17"
+TEST(VersionTest, testMacro) {
+#ifdef PULSAR_VERSION
+    ASSERT_GE(PULSAR_VERSION, 2000000);
+    ASSERT_LE(PULSAR_VERSION, 999999999);
+#else
+    FAIL();
 #endif
-
-#endif /* LIB_VERSION_H_ */
+}
diff --git a/src/gen-pulsar-version-macro.py b/src/gen-pulsar-version-macro.py
new file mode 100755
index 0000000..f32df91
--- /dev/null
+++ b/src/gen-pulsar-version-macro.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+import xml.etree.ElementTree as ET
+import re
+from os.path import dirname, realpath, join
+
+# Derive the POM path from the current script location
+TOP_LEVEL_PATH = dirname(dirname(realpath(__file__)))
+POM_PATH = join(TOP_LEVEL_PATH, 'pom.xml')
+
+root = ET.XML(open(POM_PATH).read())
+m = re.search(r'^(\d+)\.(\d+)\.(\d+)', root.find('{http://maven.apache.org/POM/4.0.0}version').text)
+
+version_macro = 0
+for i in range(3):
+    version_macro += int(m.group(3 - i)) * (1000 ** i)
+print(version_macro)