Skip to content

Build static libraries on Windows #4

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

Closed
wants to merge 10 commits into from
Closed
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
131 changes: 17 additions & 114 deletions .github/workflows/ci-pr-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,58 +29,13 @@ concurrency:

jobs:

unit-tests:
name: Run unit tests
runs-on: ubuntu-latest
timeout-minutes: 120

steps:
- name: checkout
uses: actions/checkout@v3

- name: Install deps
run: |
sudo apt-get install -y \
libcurl4-openssl-dev \
protobuf-compiler \
libprotobuf-dev \
libboost-dev \
libboost-program-options-dev \
libzstd-dev \
libsnappy-dev \
libgmock-dev \
libgtest-dev

- name: Install gtest-parallel
run: |
sudo curl -o /gtest-parallel https://raw.githubusercontent.com/google/gtest-parallel/master/gtest_parallel.py

- name: CMake
run: cmake . -DBUILD_PERF_TOOLS=ON

- name: Check formatting
run: make check-format

- name: Build
run: make -j8

- name: Start Pulsar service
run: ./pulsar-test-service-start.sh

- name: Run unit tests
run: RETRY_FAILED=3 ./run-unit-tests.sh

- name: Stop Pulsar service
run: ./pulsar-test-service-stop.sh


cpp-build-windows:
timeout-minutes: 120
name: Build CPP Client on ${{ matrix.name }}
needs: unit-tests
runs-on: ${{ matrix.os }}
env:
VCPKG_ROOT: '${{ github.workspace }}/vcpkg'
INSTALL_DIR: 'C:\pulsar-cpp'
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -132,99 +87,47 @@ jobs:
run: rm -rf "$VCPKG_INSTALLATION_ROOT"
shell: bash

- name: Install vcpkg packages
- name: Install static vcpkg packages
run: |
${{ env.VCPKG_ROOT }}\vcpkg.exe install --triplet ${{ matrix.triplet }}

- name: Configure (default)
${{ env.VCPKG_ROOT }}\vcpkg.exe install --triplet "${{ matrix.triplet }}-static"

- name: Configure (LINK_STATIC=ON)
shell: bash
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
cmake \
-B ./build-0 \
-B ./build-1 \
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
-DBUILD_TESTS=OFF \
-DVCPKG_TRIPLET=${{ matrix.triplet }} \
-DVCPKG_TRIPLET="${{ matrix.triplet }}-static" \
-DCMAKE_INSTALL_PREFIX="${{ env.INSTALL_DIR }}" \
-S .
fi

- name: Compile
- name: Install
shell: bash
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
cmake --build ./build-0 --parallel --config Release
fi

- name: Configure (dynamic library only)
shell: bash
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
cmake \
-B ./build-1 \
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
-DBUILD_TESTS=OFF \
-DVCPKG_TRIPLET=${{ matrix.triplet }} \
-DBUILD_STATIC_LIB=OFF \
-S .
cmake --build ./build-1 --parallel --config Release
cmake --install ./build-1
fi

- name: Compile
- name: Test examples
shell: bash
run: |
if [ "$RUNNER_OS" == "Windows" ]; then
cmake --build ./build-1 --parallel --config Release
cd win-examples
cmake -B build-dynamic -DLINK_STATIC=OFF -DCMAKE_PREFIX_PATH=${{ env.INSTALL_DIR }}
cmake --build build-dynamic --config Release
cmake -B build-static -DLINK_STATIC=ON -DCMAKE_PREFIX_PATH=${{ env.INSTALL_DIR }}
cmake --build build-static --config Release
fi

package:
name: Build ${{matrix.pkg.name}} ${{matrix.cpu.platform}}
runs-on: ubuntu-22.04
needs: unit-tests
timeout-minutes: 500

strategy:
fail-fast: true
matrix:
pkg:
- { name: 'RPM', type: 'rpm', path: 'pkg/rpm/RPMS' }
- { name: 'Deb', type: 'deb', path: 'pkg/deb/BUILD/DEB' }
- { name: 'Alpine', type: 'apk', path: 'pkg/apk/build' }
cpu:
- { arch: 'x86_64', platform: 'x86_64' }

steps:
- name: checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Package Pulsar source
run: build-support/generate-source-archive.sh

- uses: docker/setup-buildx-action@v2
- run: build-support/copy-deps-versionfile.sh

- name: Build dependencies Docker image
uses: docker/build-push-action@v3
with:
context: ./pkg/${{matrix.pkg.type}}
load: true
tags: build:latest
platforms: linux/${{matrix.cpu.platform}}
build-args: PLATFORM=${{matrix.cpu.arch}}
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Build packages
run: pkg/${{matrix.pkg.type}}/docker-build-${{matrix.pkg.type}}-${{matrix.cpu.platform}}.sh build:latest


# Job that will be required to complete and depends on all the other jobs
check-completion:
name: Check Completion
runs-on: ubuntu-latest
needs: [unit-tests, cpp-build-windows, package]
needs: [cpp-build-windows]

steps:
- run: true
Expand Down
69 changes: 54 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ message(STATUS "Pulsar Client version macro: ${PULSAR_CLIENT_VERSION_MACRO}")
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)

option(LINK_STATIC "Link against static libraries" OFF)
if (VCPKG_TRIPLET)
message(STATUS "Use vcpkg, triplet is ${VCPKG_TRIPLET}")
set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/vcpkg_installed/${VCPKG_TRIPLET}")
Expand All @@ -44,7 +45,13 @@ if (VCPKG_TRIPLET)
set(ZLIB_ROOT ${VCPKG_DEBUG_ROOT})
set(OPENSSL_ROOT_DIR ${VCPKG_DEBUG_ROOT})
endif ()
if (VCPKG_TRIPLET MATCHES ".*-static")
set(LINK_STATIC ON)
else ()
set(LINK_STATIC OFF)
endif ()
endif()
MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC})

find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
Expand All @@ -69,9 +76,6 @@ MESSAGE(STATUS "BUILD_WIRESHARK: " ${BUILD_WIRESHARK})
option(BUILD_PERF_TOOLS "Build Pulsar CLI perf producer/consumer" OFF)
MESSAGE(STATUS "BUILD_PERF_TOOLS: " ${BUILD_PERF_TOOLS})

option(LINK_STATIC "Link against static libraries" OFF)
MESSAGE(STATUS "LINK_STATIC: " ${LINK_STATIC})

option(USE_LOG4CXX "Build with Log4cxx support" OFF)
MESSAGE(STATUS "USE_LOG4CXX: " ${USE_LOG4CXX})

Expand All @@ -93,7 +97,7 @@ set(CMAKE_C_STANDARD 11)
# https://stackoverflow.com/questions/10046114/in-cmake-how-can-i-test-if-the-compiler-is-clang
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_definitions(-DWIN32_LEAN_AND_MEAN -DNOGDI -D_WIN32_WINNT=0x0501 -D_CRT_SECURE_NO_WARNINGS)
add_compile_options(/wd4244 /wd4267 /wd4018 /wd4715 /wd4251 /wd4275)
add_compile_options(/wd4244 /wd4267 /wd4018 /wd4715 /wd4251 /wd4275 /wd4819)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
# ?? Don't have this to test with
else() # GCC or Clang are mostly compatible:
Expand Down Expand Up @@ -168,7 +172,7 @@ if (NOT ZLIB_INCLUDE_DIRS OR NOT ZLIB_LIBRARIES)
message(FATAL_ERROR "Could not find zlib")
endif ()

if (LINK_STATIC)
if (LINK_STATIC AND NOT VCPKG_TRIPLET)
find_library(LIB_ZSTD NAMES libzstd.a)
message(STATUS "ZStd: ${LIB_ZSTD}")
find_library(LIB_SNAPPY NAMES libsnappy.a)
Expand All @@ -195,6 +199,29 @@ if (LINK_STATIC)
if (MSVC)
add_definitions(-DCURL_STATICLIB)
endif()
elseif (LINK_STATIC AND VCPKG_TRIPLET)
find_package(protobuf REQUIRED)
message(STATUS "Found protobuf static library: " ${Protobuf_LIBRARIES})
find_library(ZLIB_LIBRARIES NAMES zlib z)
if (ZLIB_LIBRARIES)
message(STATUS "Found zlib static library: " ${ZLIB_LIBRARIES})
else ()
message(FATAL_ERROR "Failed to find zlib static library")
endif ()
find_library(CURL_LIBRARIES NAMES libcurl)
if (CURL_LIBRARIES)
message(STATUS "Found libcurl: ${CURL_LIBRARIES}")
else ()
message(FATAL_ERROR "Cannot find libcurl")
endif ()
find_library(LIB_ZSTD zstd)
if (LIB_ZSTD)
message(STATUS "Found ZSTD library: ${LIB_ZSTD}")
endif ()
find_library(LIB_SNAPPY NAMES snappy)
if (LIB_SNAPPY)
message(STATUS "Found Snappy library: ${LIB_SNAPPY}")
endif ()
else()
if (MSVC AND (${CMAKE_BUILD_TYPE} STREQUAL Debug))
find_library(LIB_ZSTD zstdd HINTS "${VCPKG_DEBUG_ROOT}/lib")
Expand All @@ -211,7 +238,7 @@ else()
find_library(LOG4CXX_LIBRARY_PATH log4cxx)
find_path(LOG4CXX_INCLUDE_PATH log4cxx/logger.h)
endif (USE_LOG4CXX)
endif (LINK_STATIC)
endif ()

if (Boost_MAJOR_VERSION EQUAL 1 AND Boost_MINOR_VERSION LESS 69)
# Boost System does not require linking since 1.69
Expand All @@ -220,15 +247,15 @@ if (Boost_MAJOR_VERSION EQUAL 1 AND Boost_MINOR_VERSION LESS 69)
endif()

if (MSVC)
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} date_time)
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} date_time)
endif()

if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
# GCC 4.8.2 implementation of std::regex is buggy
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex)
set(CMAKE_CXX_FLAGS " -DPULSAR_USE_BOOST_REGEX")
MESSAGE(STATUS "Using Boost::Regex")
else()
elseif (CMAKE_COMPILER_IS_GNUCC)
MESSAGE(STATUS "Using std::regex")
# Turn on color error messages and show additional help with errors (only available in GCC v4.9+):
add_compile_options(-fdiagnostics-show-option -fdiagnostics-color)
Expand Down Expand Up @@ -295,7 +322,7 @@ include_directories(

set(COMMON_LIBS
${COMMON_LIBS}
Threads::Threads
${CMAKE_THREAD_LIBS_INIT}
${Boost_REGEX_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_DATE_TIME_LIBRARY}
Expand All @@ -307,13 +334,25 @@ set(COMMON_LIBS
${CMAKE_DL_LIBS}
)

if (NOT MSVC)
set(COMMON_LIBS ${COMMON_LIBS} m)
else()
if (MSVC)
set(COMMON_LIBS
${COMMON_LIBS}
wldap32.lib
Normaliz.lib)
${COMMON_LIBS}
${Boost_DATE_TIME_LIBRARY}
wldap32.lib
Normaliz.lib)
if (LINK_STATIC)
# add external dependencies of libcurl
set(COMMON_LIBS ${COMMON_LIBS} ws2_32.lib crypt32.lib)
# the default compile options have /MD, which cannot be used to build DLLs that link static libraries
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
message(STATUS "CMAKE_CXX_FLAGS_DEBUG: " ${CMAKE_CXX_FLAGS_DEBUG})
message(STATUS "CMAKE_CXX_FLAGS_RELEASE: " ${CMAKE_CXX_FLAGS_RELEASE})
message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: " ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
endif ()
else()
set(COMMON_LIBS ${COMMON_LIBS} m)
endif()

if (USE_LOG4CXX)
Expand Down
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ endif(NOT LIBRARY_VERSION)
if (MSVC)
find_package(dlfcn-win32 REQUIRED)
set(CMAKE_DL_LIBS dlfcn-win32::dl psapi.lib)
get_target_property(dlfcn-win32_LIBRARY dlfcn-win32::dl IMPORTED_LOCATION_RELEASE)
message(STATUS "dlfcn-win32_LIBRARY: " ${dlfcn-win32_LIBRARY})
endif(MSVC)


Expand Down Expand Up @@ -88,7 +90,7 @@ endif()
# required dependencies except ssl
if (LINK_STATIC AND BUILD_STATIC_LIB)
if (MSVC)

set(COMMON_LIBS ${COMMON_LIBS} ${dlfcn-win32_LIBRARY})
# This function is to remove either "debug" or "optimized" library names
# out of the COMMON_LIBS list and return the sanitized list of libraries
function(remove_libtype LIBLIST LIBTYPE OUTLIST)
Expand Down
49 changes: 49 additions & 0 deletions win-examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# 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-win-examples)

find_path(PULSAR_INCLUDES NAMES "pulsar/Client.h")
if (PULSAR_INCLUDES)
message(STATUS "PULSAR_INCLUDES: " ${PULSAR_INCLUDES})
else ()
message(FATAL_ERROR "Failed to find PULSAR_INCLUDES")
endif ()
option(LINK_STATIC "Link statically to pulsar" ON)
if (LINK_STATIC)
find_library(PULSAR_LIBRARIES NAMES "pulsarWithDeps")
else ()
find_library(PULSAR_LIBRARIES NAMES "pulsar")
endif ()
if (PULSAR_LIBRARIES)
message(STATUS "PULSAR_LIBRARIES: " ${PULSAR_LIBRARIES})
else ()
message(FATAL_ERROR "Failed to find PULSAR_LIBRARIES")
endif ()

if (LINK_STATIC)
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
add_definitions(-DPULSAR_STATIC)
endif ()
message(STATUS "CMAKE_CXX_FLAGS_RELEASE: " ${CMAKE_CXX_FLAGS_RELEASE})

add_executable(win-example "example.cc")
include_directories(${PULSAR_INCLUDES})
target_link_libraries(win-example PRIVATE ${PULSAR_LIBRARIES})
Loading