Skip to content

Commit

Permalink
Use our own CMakeLists.txt to compile pulsar-client-cpp:
Browse files Browse the repository at this point in the history
1. Only support Linux platform;
2. Only build libpulsar.a;
3. Reduce a lot of compile time.
  • Loading branch information
BewareMyPower committed Aug 27, 2020
1 parent 1bc9167 commit 8cb5b10
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
_builds/
bin/
lib/
./cxx/lib/
*.log
*.o
tags
Expand Down
27 changes: 27 additions & 0 deletions build_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,34 @@ build_protobuf_2_6() {
popd
}

build_pulsar() {
PROTOC_PATH="$INSTALL_DIR/bin/protoc"
PULSAR_CPP_DIR="$SOURCE_DIR/pulsar/pulsar-client-cpp"

# Use our own CMakeLists.txt to compile libpulsar.a only
cp ./pulsar-client-cpp/CMakeLists.txt "$PULSAR_CPP_DIR"
cp ./pulsar-client-cpp/lib/CMakeLists.txt "$PULSAR_CPP_DIR/lib"

pushd $PULSAR_CPP_DIR
mkdir -p _builds
pushd _builds
CXX=g++ cmake .. \
-DPROTOC_PATH="$PROTOC_PATH" \
-DCMAKE_CXX_FLAGS="-D_GLIBCXX_USE_CXX11_ABI=0" \
-DCMAKE_PREFIX_PATH="$INSTALL_DIR" \
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR"
# Here we use multiple threads to compile because it take long to compile with a single thread
make -j4
make install
pushd $PULSAR_CPP_DIR
git checkout -- .
popd
popd
popd
}

build_rdkafka
build_log4cplus
build_boost_1_70
build_protobuf_2_6
build_pulsar
147 changes: 147 additions & 0 deletions pulsar-client-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#
# 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.
#

cmake_minimum_required(VERSION 3.4)
project (pulsar-cpp)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "ccache")
MESSAGE(STATUS "Using CCache")
endif(CCACHE_PROGRAM)

MESSAGE(STATUS "ARCHITECTURE: ${CMAKE_SYSTEM_PROCESSOR}")

IF (CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE RelWithDebInfo)
ENDIF ()

MESSAGE(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE})

set(Boost_NO_BOOST_CMAKE ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 11)

add_compile_options(-Werror=switch -Wno-deprecated-declarations)
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
add_compile_options(-msse4.2 -mpclmul)
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(Protobuf_LITE_LIBRARIES $ENV{PROTOBUF_LIBRARIES})
set(LOG_CATEGORY_NAME $ENV{LOG_CATEGORY_NAME})

if (NOT LOG_CATEGORY_NAME)
set(LOG_CATEGORY_NAME "\"pulsar.\"")
endif(NOT LOG_CATEGORY_NAME)

add_definitions(-DLOG_CATEGORY_NAME=${LOG_CATEGORY_NAME} -DBUILDING_PULSAR -DBOOST_ALL_NO_LIB -DBOOST_ALLOW_DEPRECATED_HEADERS)

set(OPENSSL_ROOT_DIR /usr/lib64/)
set(OPENSSL_USE_STATIC_LIBS FALSE)
find_package(OpenSSL REQUIRED)

# Link to shared libraries
find_package(ZLIB REQUIRED)
set(ZLIB_LIBRARIES ${ZLIB_LIBRARIES})
if (NOT PROTOBUF_LIBRARIES)
find_package(ProtoBuf QUIET)
if (NOT Protobuf_FOUND OR NOT Protobuf_LITE_LIBRARIES)
find_library(Protobuf_LITE_LIBRARIES protobuf-lite libprotobuf-lite)
find_path(Protobuf_INCLUDE_DIRS google/protobuf/stubs/common.h)
endif()
endif (NOT PROTOBUF_LIBRARIES)

find_library(LIB_ZSTD zstd)
find_library(LIB_SNAPPY NAMES snappy libsnappy)
find_library(CURL_LIBRARIES NAMES curl libcurl)

find_package(Boost REQUIRED COMPONENTS regex system)

# Hide all non-exported symbols to avoid conflicts
add_compile_options(-fvisibility=hidden -Wl,--exclude-libs,ALL)

if (LIB_ZSTD)
set(HAS_ZSTD 1)
else ()
set(HAS_ZSTD 0)
endif ()
MESSAGE(STATUS "HAS_ZSTD: ${HAS_ZSTD}")

if (LIB_SNAPPY)
set(HAS_SNAPPY 1)
else ()
set(HAS_SNAPPY 0)
endif ()
MESSAGE(STATUS "HAS_SNAPPY: ${HAS_SNAPPY}")

set(ADDITIONAL_LIBRARIES $ENV{PULSAR_ADDITIONAL_LIBRARIES})
link_directories( $ENV{PULSAR_ADDITIONAL_LIBRARY_PATH} )

set(AUTOGEN_DIR ${CMAKE_BINARY_DIR}/generated)
file(MAKE_DIRECTORY ${AUTOGEN_DIR})

include_directories(
${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/include
${AUTOGEN_DIR}
${Boost_INCLUDE_DIR}
${OPENSSL_INCLUDE_DIR}
${ZLIB_INCLUDE_DIRS}
${CURL_INCLUDE_DIRS}
${Protobuf_INCLUDE_DIRS}
${LOG4CXX_INCLUDE_PATH}
${GTEST_INCLUDE_PATH}
${GMOCK_INCLUDE_PATH}
)

set(COMMON_LIBS
${COMMON_LIBS}
${Boost_REGEX_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${CURL_LIBRARIES}
${OPENSSL_LIBRARIES}
${ZLIB_LIBRARIES}
${Protobuf_LITE_LIBRARIES}
${ADDITIONAL_LIBRARIES}
)

set(COMMON_LIBS
${COMMON_LIBS}
dl
m
pthread
rt
)

if (HAS_ZSTD)
set(COMMON_LIBS ${COMMON_LIBS} ${LIB_ZSTD} )
endif ()

add_definitions(-DHAS_ZSTD=${HAS_ZSTD})

if (HAS_SNAPPY)
set(COMMON_LIBS ${COMMON_LIBS} ${LIB_SNAPPY} )
endif ()

add_definitions(-DHAS_SNAPPY=${HAS_SNAPPY})

add_subdirectory(lib)
60 changes: 60 additions & 0 deletions pulsar-client-cpp/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# 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.
#

file(GLOB PULSAR_SOURCES *.cc *.h lz4/*.c 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}\\\"")

if (NOT PROTOC_PATH)
set(PROTOC_PATH protoc)
endif()

set(LIB_AUTOGEN_DIR ${AUTOGEN_DIR}/lib)
file(MAKE_DIRECTORY ${LIB_AUTOGEN_DIR})
include_directories(${LIB_AUTOGEN_DIR})

# Protobuf generation is only supported natively starting from CMake 3.8
# Using custom command for now
set(PROTO_SOURCES ${LIB_AUTOGEN_DIR}/PulsarApi.pb.cc ${LIB_AUTOGEN_DIR}/PulsarApi.pb.h)
set(PULSAR_SOURCES ${PULSAR_SOURCES} ${PROTO_SOURCES})
ADD_CUSTOM_COMMAND(
OUTPUT ${PROTO_SOURCES}
COMMAND ${PROTOC_PATH} -I ../../pulsar-common/src/main/proto ../../pulsar-common/src/main/proto/PulsarApi.proto --cpp_out=${LIB_AUTOGEN_DIR}
DEPENDS
../../pulsar-common/src/main/proto/PulsarApi.proto
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

set(LIBRARY_VERSION $ENV{PULSAR_LIBRARY_VERSION})
if (NOT LIBRARY_VERSION)
set(LIBRARY_VERSION ${PV})
endif(NOT LIBRARY_VERSION)

add_library(pulsarStatic STATIC ${PULSAR_SOURCES})
set_property(TARGET pulsarStatic PROPERTY OUTPUT_NAME pulsar)
set_property(TARGET pulsarStatic PROPERTY VERSION ${LIBRARY_VERSION})
target_compile_definitions(pulsarStatic PRIVATE PULSAR_STATIC)
MESSAGE(STATUS "VERSION: ${LIBRARY_VERSION}")

# Install regular libpulsar.a
target_link_libraries(pulsarStatic ${COMMON_LIBS})
install(TARGETS pulsarStatic DESTINATION lib)

install(TARGETS pulsarStatic DESTINATION lib)
install(DIRECTORY "../include/pulsar" DESTINATION include)

0 comments on commit 8cb5b10

Please sign in to comment.