Skip to content

PIP-209: Compile Python client wrapper #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.


BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 110
SortIncludes: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterEnum: true
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ MANIFEST
build
dist
*.egg-info
.idea
CMakeCache.txt
CMakeFiles
Makefile
_pulsar.so
cmake_install.cmake
__pycache__
108 changes: 102 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,81 @@
# under the License.
#

INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "${PYTHON_INCLUDE_DIRS}")
project (pulsar-client-python)
cmake_minimum_required(VERSION 3.12)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")

MESSAGE(STATUS "CMAKE_BUILD_TYPE: " ${CMAKE_BUILD_TYPE})
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
MESSAGE(STATUS "Threads library: " ${CMAKE_THREAD_LIBS_INIT})


find_library(PULSAR_LIBRARY NAMES libpulsar.a)
message(STATUS "PULSAR_LIBRARY: ${PULSAR_LIBRARY}")

find_path(PULSAR_INCLUDE pulsar/Client.h)
message(STATUS "PULSAR_INCLUDE: ${PULSAR_INCLUDE}")

SET(Boost_NO_BOOST_CMAKE ON)
SET(Boost_USE_STATIC_LIBS ON)

SET(CMAKE_CXX_STANDARD 11)

find_package(Boost)

find_package (Python3 COMPONENTS Development)
MESSAGE(STATUS "PYTHON: " ${Python3_VERSION} " - " ${Python3_INCLUDE_DIRS})

string(REPLACE "." ";" PYTHONLIBS_VERSION_NO_LIST ${Python3_VERSION})

set(BOOST_PYTHON_NAME_POSTFIX ${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR})
# For python3 the lib name is boost_python3
set(BOOST_PYTHON_NAME_LIST python${BOOST_PYTHON_NAME_POSTFIX};python37;python38;python39;python310;python3;python3-mt;python-py${BOOST_PYTHON_NAME_POSTFIX};python${BOOST_PYTHON_NAME_POSTFIX}-mt)

foreach (BOOST_PYTHON_NAME IN LISTS BOOST_PYTHON_NAME_LIST)
find_package(Boost QUIET COMPONENTS ${BOOST_PYTHON_NAME})
if (${Boost_FOUND})
set(BOOST_PYTHON_NAME_FOUND ${BOOST_PYTHON_NAME})
break()
endif()
endforeach()

if (NOT ${Boost_FOUND})
MESSAGE(FATAL_ERROR "Could not find Boost Python library")
endif ()

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

set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/lib64/)

### This part is to find and keep SSL dynamic libs in RECORD_OPENSSL_SSL_LIBRARY and RECORD_OPENSSL_CRYPTO_LIBRARY
### After find the libs, will unset related cache, and will not affect another same call to find_package.
if (APPLE)
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/include/ /opt/homebrew/opt/openssl/include)
set(OPENSSL_ROOT_DIR ${OPENSSL_ROOT_DIR} /usr/local/opt/openssl/ /opt/homebrew/opt/openssl)
endif ()

set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)

find_library(ZLIB_LIBRARIES REQUIRED NAMES libz.a z zlib)
message(STATUS "ZLIB_LIBRARIES: ${ZLIB_LIBRARIES}")

find_library(CURL_LIBRARIES NAMES libcurl.a curl curl_a libcurl_a)
message(STATUS "CURL_LIBRARIES: ${CURL_LIBRARIES}")
find_library(Protobuf_LIBRARIES NAMES libprotobuf.a libprotobuf)
message(STATUS "Protobuf: ${Protobuf_LIBRARIES}")
find_library(CURL_LIBRARIES NAMES libcurl.a curl curl_a libcurl_a)
message(STATUS "CURL_LIBRARIES: ${CURL_LIBRARIES}")
find_library(LIB_ZSTD NAMES libzstd.a)
message(STATUS "ZStd: ${LIB_ZSTD}")
find_library(LIB_SNAPPY NAMES libsnappy.a)
message(STATUS "LIB_SNAPPY: ${LIB_SNAPPY}")

########################################################################################################################

INCLUDE_DIRECTORIES(${PULSAR_INCLUDE} "${Boost_INCLUDE_DIRS}" "${Python3_INCLUDE_DIRS}")

ADD_LIBRARY(_pulsar SHARED src/pulsar.cc
src/producer.cc
Expand Down Expand Up @@ -63,7 +137,14 @@ if (NOT DEFINED ${Boost_PYTHON310-MT_LIBRARY})
endif()

# Try all possible boost-python variable namings
set(PYTHON_WRAPPER_LIBS ${Boost_PYTHON_LIBRARY}
set(PYTHON_WRAPPER_LIBS ${PULSAR_LIBRARY}
${OPENSSL_LIBRARIES}
${ZLIB_LIBRARIES}
${CURL_LIBRARIES}
${Protobuf_LIBRARIES}
${LIB_ZSTD}
${LIB_SNAPPY}
${Boost_PYTHON_LIBRARY}
${Boost_PYTHON3_LIBRARY}
${Boost_PYTHON37-MT_LIBRARY}
${Boost_PYTHON38_LIBRARY}
Expand All @@ -86,18 +167,33 @@ if (APPLE)
endif ()
endif()

message(STATUS "Using Boost Python libs: ${PYTHON_WRAPPER_LIBS}")

if (NOT PYTHON_WRAPPER_LIBS)
MESSAGE(FATAL_ERROR "Could not find Boost Python library")
endif ()

message(STATUS "All libraries: ${PYTHON_WRAPPER_LIBS}")

if (APPLE)
set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup")
target_link_libraries(_pulsar -Wl,-all_load pulsarStatic ${PYTHON_WRAPPER_LIBS} ${COMMON_LIBS} ${ICU_LIBS})
target_link_libraries(_pulsar -Wl,-all_load ${PYTHON_WRAPPER_LIBS})
else ()
if (NOT MSVC)
set (CMAKE_SHARED_LINKER_FLAGS " -static-libgcc -static-libstdc++")
endif()
target_link_libraries(_pulsar pulsarStatic ${PYTHON_WRAPPER_LIBS} ${COMMON_LIBS})
target_link_libraries(_pulsar ${PYTHON_WRAPPER_LIBS})
endif ()

find_package(ClangTools)
set(BUILD_SUPPORT_DIR "${CMAKE_SOURCE_DIR}/build-support")
add_custom_target(format ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
0
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
${CMAKE_SOURCE_DIR}/src)

# `make check-format` option (for CI test)
add_custom_target(check-format ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
1
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
${CMAKE_SOURCE_DIR}/src)
54 changes: 44 additions & 10 deletions build-mac-wheels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ SNAPPY_VERSION=1.1.3
CURL_VERSION=7.61.0

ROOT_DIR=$(git rev-parse --show-toplevel)
cd "${ROOT_DIR}/pulsar-client-cpp"
cd "${ROOT_DIR}"

PULSAR_VERSION=$(cat version.txt | grep pulsar-client-cpp | awk '{print $2}')

# Compile and cache dependencies
CACHE_DIR=~/.pulsar-mac-wheels-cache
Expand Down Expand Up @@ -246,6 +247,43 @@ else
echo "Using cached LibCurl"
fi

###############################################################################
if [ ! -f apache-pulsar-${PULSAR_VERSION}-src/.done ]; then
echo "Building Pulsar C++ client - ${PULSAR_VERSION}"
curl -O -L https://archive.apache.org/dist/pulsar/pulsar-${PULSAR_VERSION}/apache-pulsar-${PULSAR_VERSION}-src.tar.gz

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably add some validation of the integrity of the archive

and also provide a way to pass a custom .tar.gz, in order to ease development/testing with a different version
maybe making the full URL configurable will help

we can do these improvements as a follow up patch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could be checking the expected hash of the archive

rm -rf apache-pulsar-${PULSAR_VERSION}-src/pulsar-client-cpp
tar xfz apache-pulsar-${PULSAR_VERSION}-src.tar.gz
pushd apache-pulsar-${PULSAR_VERSION}-src
pushd pulsar-client-cpp
ARCHS='arm64;x86_64'

chmod +x build-support/merge_archives.sh
set -x
cmake . \
-DCMAKE_OSX_ARCHITECTURES=${ARCHS} \
-DCMAKE_OSX_DEPLOYMENT_TARGET=${MACOSX_DEPLOYMENT_TARGET} \
-DCMAKE_INSTALL_PREFIX=$PREFIX \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_PREFIX_PATH=$PREFIX \
-DCMAKE_CXX_FLAGS=-I$PREFIX/include \
-DBoost_INCLUDE_DIR=$CACHE_DIR/boost-py-$PYTHON_VERSION/include \
-DBoost_LIBRARY_DIR=$CACHE_DIR/boost-py-$PYTHON_VERSION/lib \
-DLINK_STATIC=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PYTHON_WRAPPER=OFF \
-DBUILD_WIRESHARK=OFF \
-DBUILD_DYNAMIC_LIB=OFF \
-DBUILD_STATIC_LIB=ON \
-DPROTOC_PATH=$PREFIX/bin/protoc

make -j16 install
popd
touch .done
popd
else
echo "Using cached Pulsar C++ client"
fi

###############################################################################
###############################################################################
###############################################################################
Expand All @@ -260,7 +298,7 @@ for line in "${PYTHON_VERSIONS[@]}"; do
echo '----------------------------------------------------------------------------'
echo "Build wheel for Python $PYTHON_VERSION"

cd "${ROOT_DIR}/pulsar-client-cpp"
cd "${ROOT_DIR}"

find . -name CMakeCache.txt | xargs -r rm
find . -name CMakeFiles | xargs -r rm -rf
Expand All @@ -285,16 +323,12 @@ for line in "${PYTHON_VERSIONS[@]}"; do
-DCMAKE_CXX_FLAGS=-I$PREFIX/include \
-DBoost_INCLUDE_DIR=$CACHE_DIR/boost-py-$PYTHON_VERSION/include \
-DBoost_LIBRARY_DIR=$CACHE_DIR/boost-py-$PYTHON_VERSION/lib \
-DPYTHON_INCLUDE_DIR=$PY_INCLUDE_DIR \
-DPYTHON_LIBRARY=$PY_PREFIX/lib/libpython${PYTHON_VERSION}.dylib \
-DLINK_STATIC=ON \
-DBUILD_TESTS=OFF \
-DBUILD_WIRESHARK=OFF \
-DPROTOC_PATH=$PREFIX/bin/protoc
-DPython3_INCLUDE_DIR=$PY_INCLUDE_DIR \
-DPython3_LIBRARY=$PY_PREFIX/lib/libpython${PYTHON_VERSION}.dylib \
-DPULSAR_INCLUDE=${PREFIX}/include

make clean
make _pulsar -j16
make -j16

cd python
$PY_EXE setup.py bdist_wheel
done
18 changes: 18 additions & 0 deletions build-support/clang_format_exclusions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#
80 changes: 80 additions & 0 deletions build-support/run_clang_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
#
# 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.
#

# Original: https://github.com/apache/arrow/blob/4dbce607d50031a405af39d36e08cd03c5ffc764/cpp/build-support/run_clang_format.py
# ChangeLog:
# 2018-01-08: Accept multiple source directories (@Licht-T)

import fnmatch
import os
import subprocess
import sys

if len(sys.argv) < 5:
sys.stderr.write("Usage: %s $CLANG_FORMAT $CHECK_FORMAT exclude_globs.txt "
"$source_dir1 $source_dir2\n" %
sys.argv[0])
sys.exit(1)

CLANG_FORMAT = sys.argv[1]
CHECK_FORMAT = int(sys.argv[2]) == 1
EXCLUDE_GLOBS_FILENAME = sys.argv[3]
SOURCE_DIRS = sys.argv[4:]

exclude_globs = [line.strip() for line in open(EXCLUDE_GLOBS_FILENAME, "r")]

files_to_format = []
matches = []
for source_dir in SOURCE_DIRS:
for directory, subdirs, files in os.walk(source_dir):
for name in files:
name = os.path.join(directory, name)
if not (name.endswith('.h') or name.endswith('.cc')):
continue

excluded = False
for g in exclude_globs:
if fnmatch.fnmatch(name, g):
excluded = True
break
if not excluded:
files_to_format.append(name)

if CHECK_FORMAT:
output = subprocess.check_output([CLANG_FORMAT, '-output-replacements-xml']
+ files_to_format,
stderr=subprocess.STDOUT).decode('utf8')

to_fix = []
for line in output.split('\n'):
if 'offset' in line:
to_fix.append(line)

if len(to_fix) > 0:
print("clang-format checks failed, run 'make format' to fix")
sys.exit(-1)
else:
try:
cmd = [CLANG_FORMAT, '-i'] + files_to_format
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except Exception as e:
print(e)
print(' '.join(cmd))
raise
Loading