Skip to content

Commit 0e63af9

Browse files
Fix the build failure with C++20 standard (apache#302)
### Motivation When building the project with the `-DCMAKE_CXX_STANDARD=20` option and GCC 11.3, it failed. There are two main reasons. One is the `ObjectPool.h`, see http://eel.is/c++draft/diff.cpp17.class#2 In short, see the code below: ```c++ template <typename T> struct A { // A<T>() {} // error: simple-template-id not allowed for constructor A() {} // OK, injected-class-name used }; ``` The other reason is deeply hidden and OS-specific. When building the target for the unit test, the `lib/` directory is added into the include directories. So for `#include "Semaphore.h"`, the `Semaphore.h` header will be looked up first in the `lib/` directory. However, C++20 introduced a `<semaphore>` header, which finds the POSIX semaphore header `semaphore.h` in the system path. For example, the include order in `ubuntu:22.04` arm64 container is: - `$PROJECT_DIR/lib/` (where `Semaphore.h` is) - ... - `/usr/lib/gcc/aarch64-linux-gnu/11/include` (where `semaphore.h` is) The C++ header is case insensitive so the `lib/Semaphore.h` will be included by the `<semaphore>` header, which is implicitly included by `<thread>`. Our own `Semaphore.h` does not have the POSIX semaphore struct definitions so the build failed. ### Modifications - Fix the semantics error in `ObjectPool.h` - Remove the `lib/` directory from the included directories of the unit test and include `lib/xxx.h` for header in `lib/` directory. - Add a workflow to verify now it can be built with C++20
1 parent e737716 commit 0e63af9

13 files changed

+45
-18
lines changed

.github/workflows/ci-pr-validation.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,35 @@ jobs:
9090
run: make check-format
9191

9292
- name: Build
93-
run: make -j8
93+
run: |
94+
# Build the libraries first to avoid possible link failures
95+
cmake --build . -j8 --target pulsarShared pulsarStatic
96+
cmake --build . -j8
9497
9598
- name: Run unit tests
9699
run: RETRY_FAILED=3 ./run-unit-tests.sh
97100

101+
cpp20-build:
102+
name: Build with the C++20 standard
103+
runs-on: ubuntu-22.04
104+
timeout-minutes: 60
105+
106+
steps:
107+
- name: checkout
108+
uses: actions/checkout@v3
109+
- name: Install deps
110+
run: |
111+
sudo apt-get update -y
112+
sudo apt-get install -y libcurl4-openssl-dev libssl-dev \
113+
protobuf-compiler libprotobuf-dev libboost-dev \
114+
libboost-dev libboost-program-options-dev \
115+
libzstd-dev libsnappy-dev libgmock-dev libgtest-dev
116+
- name: CMake
117+
run: cmake -B build -DBUILD_PERF_TOOLS=ON -DCMAKE_CXX_STANDARD=20
118+
- name: Build
119+
run: |
120+
cmake --build build -j8 --target pulsarShared pulsarStatic
121+
cmake --build build -j8
98122
99123
cpp-build-windows:
100124
timeout-minutes: 120
@@ -281,7 +305,7 @@ jobs:
281305
check-completion:
282306
name: Check Completion
283307
runs-on: ubuntu-latest
284-
needs: [wireshark-dissector-build, unit-tests, cpp-build-windows, package, cpp-build-macos]
308+
needs: [wireshark-dissector-build, unit-tests, cpp20-build, cpp-build-windows, package, cpp-build-macos]
285309

286310
steps:
287311
- run: true

lib/ObjectPool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ class ObjectPool {
223223
}
224224

225225
private:
226-
ObjectPool<Type, MaxSize>(const ObjectPool<Type, MaxSize>&);
227-
ObjectPool<Type, MaxSize>& operator=(const ObjectPool<Type, MaxSize>&);
226+
ObjectPool(const ObjectPool<Type, MaxSize>&);
227+
ObjectPool& operator=(const ObjectPool<Type, MaxSize>&);
228228
};
229229
} // namespace pulsar
230230
#endif /* LIB_OBJECTPOOL_H_ */

lib/ProducerImpl.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "OpSendMsg.h"
3737
#include "ProducerConfigurationImpl.h"
3838
#include "PulsarApi.pb.h"
39+
#include "Semaphore.h"
3940
#include "TimeUtils.h"
4041
#include "TopicName.h"
4142
#include "stats/ProducerStatsDisabled.h"

lib/ProducerImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include "PendingFailures.h"
3232
#include "PeriodicTask.h"
3333
#include "ProducerImplBase.h"
34-
#include "Semaphore.h"
3534

3635
namespace pulsar {
3736

@@ -53,6 +52,7 @@ class PulsarFriend;
5352

5453
class Producer;
5554
class MemoryLimitController;
55+
class Semaphore;
5656
class TopicName;
5757
struct OpSendMsg;
5858

tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ file(GLOB TEST_SOURCES *.cc c/*.cc)
5656

5757
add_executable(pulsar-tests ${TEST_SOURCES} ${PROTO_SOURCES})
5858

59-
target_include_directories(pulsar-tests PRIVATE ${PROJECT_SOURCE_DIR}/lib ${AUTOGEN_DIR}/lib)
59+
target_include_directories(pulsar-tests PRIVATE ${AUTOGEN_DIR}/lib)
6060

6161
target_link_libraries(pulsar-tests ${CLIENT_LIBS} pulsarStatic $<$<CONFIG:Debug>:${GMOCKD_LIBRARY_PATH}> $<$<CONFIG:Debug>:${GTESTD_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GMOCK_LIBRARY_PATH}> $<$<NOT:$<CONFIG:Debug>>:${GTEST_LIBRARY_PATH}>)
6262

tests/InterceptorsTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <utility>
2626

2727
#include "HttpHelper.h"
28-
#include "Latch.h"
28+
#include "lib/Latch.h"
2929
#include "lib/LogUtils.h"
3030

3131
DECLARE_LOG_OBJECT()

tests/KeyValueImplTest.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
#include <KeyValueImpl.h>
2019
#include <gtest/gtest.h>
2120

21+
#include "lib/KeyValueImpl.h"
22+
2223
using namespace pulsar;
2324

2425
TEST(KeyValueTest, testEncodeAndDeCode) {

tests/LookupServiceTest.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
#include <BinaryProtoLookupService.h>
20-
#include <Future.h>
21-
#include <HTTPLookupService.h>
22-
#include <Utils.h>
2319
#include <gtest/gtest.h>
2420
#include <pulsar/Authentication.h>
2521
#include <pulsar/Client.h>
@@ -31,11 +27,15 @@
3127

3228
#include "HttpHelper.h"
3329
#include "PulsarFriend.h"
30+
#include "lib/BinaryProtoLookupService.h"
3431
#include "lib/ClientConnection.h"
3532
#include "lib/ConnectionPool.h"
33+
#include "lib/Future.h"
34+
#include "lib/HTTPLookupService.h"
3635
#include "lib/LogUtils.h"
3736
#include "lib/RetryableLookupService.h"
3837
#include "lib/TimeUtils.h"
38+
#include "lib/Utils.h"
3939

4040
DECLARE_LOG_OBJECT()
4141

tests/MessageChunkingTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#include <ctime>
2424
#include <random>
2525

26-
#include "ChunkMessageIdImpl.h"
2726
#include "PulsarFriend.h"
2827
#include "WaitUtils.h"
28+
#include "lib/ChunkMessageIdImpl.h"
2929
#include "lib/LogUtils.h"
3030

3131
DECLARE_LOG_OBJECT()

tests/MessageTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include <string>
2424

25-
#include "MessageImpl.h"
25+
#include "lib/MessageImpl.h"
2626

2727
using namespace pulsar;
2828
TEST(MessageTest, testMessageContents) {

0 commit comments

Comments
 (0)