Skip to content

Commit

Permalink
ARROW-6312: [C++] Add support for "pkg-config --static arrow"
Browse files Browse the repository at this point in the history
Closes #10626 from kou/cpp-pc-libs-private

Authored-by: Sutou Kouhei <kou@clear-code.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
kou committed Jul 5, 2021
1 parent d7a8b46 commit 74af39d
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 25 deletions.
10 changes: 8 additions & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,11 @@ endif()
include(BuildUtils)
enable_testing()

# For arrow.pc. Requires.private and Libs.private are used when
# "pkg-config --libs --static arrow" is used.
set(ARROW_PC_REQUIRES_PRIVATE)
set(ARROW_PC_LIBS_PRIVATE)

include(ThirdpartyToolchain)

# Add common flags
Expand Down Expand Up @@ -855,8 +860,9 @@ endif()

set(ARROW_SYSTEM_LINK_LIBS)

if(THREADS_FOUND)
list(APPEND ARROW_SYSTEM_LINK_LIBS Threads::Threads)
list(APPEND ARROW_SYSTEM_LINK_LIBS Threads::Threads)
if(CMAKE_THREAD_LIBS_INIT)
string(APPEND ARROW_PC_LIBS_PRIVATE " ${CMAKE_THREAD_LIBS_INIT}")
endif()

if(WIN32)
Expand Down
74 changes: 59 additions & 15 deletions cpp/cmake_modules/ThirdpartyToolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ endmacro()
macro(resolve_dependency DEPENDENCY_NAME)
set(options)
set(one_value_args HAVE_ALT IS_RUNTIME_DEPENDENCY REQUIRED_VERSION USE_CONFIG)
set(multi_value_args PC_PACKAGE_NAMES)
cmake_parse_arguments(ARG
"${options}"
"${one_value_args}"
Expand Down Expand Up @@ -236,6 +237,13 @@ macro(resolve_dependency DEPENDENCY_NAME)
if(${DEPENDENCY_NAME}_SOURCE STREQUAL "SYSTEM" AND ARG_IS_RUNTIME_DEPENDENCY)
provide_find_module(${PACKAGE_NAME})
list(APPEND ARROW_SYSTEM_DEPENDENCIES ${PACKAGE_NAME})
find_package(PkgConfig QUIET)
foreach(ARG_PC_PACKAGE_NAME ${ARG_PC_PACKAGE_NAMES})
pkg_check_modules(${ARG_PC_PACKAGE_NAME}_PC ${ARG_PC_PACKAGE_NAME} QUIET)
if(${${ARG_PC_PACKAGE_NAME}_PC_FOUND})
string(APPEND ARROW_PC_REQUIRES_PRIVATE " ${ARG_PC_PACKAGE_NAME}")
endif()
endforeach()
endif()
endmacro()

Expand Down Expand Up @@ -933,7 +941,11 @@ macro(build_snappy)
endmacro()

if(ARROW_WITH_SNAPPY)
resolve_dependency(Snappy)
resolve_dependency(Snappy PC_PACKAGE_NAMES snappy)
if(${Snappy_SOURCE} STREQUAL "SYSTEM" AND NOT snappy_PC_FOUND)
get_target_property(SNAPPY_LIB Snappy::snappy IMPORTED_LOCATION)
string(APPEND ARROW_PC_LIBS_PRIVATE " ${SNAPPY_LIB}")
endif()
# TODO: Don't use global includes but rather target_include_directories
get_target_property(SNAPPY_INCLUDE_DIRS Snappy::snappy INTERFACE_INCLUDE_DIRECTORIES)
include_directories(SYSTEM ${SNAPPY_INCLUDE_DIRS})
Expand Down Expand Up @@ -998,7 +1010,7 @@ macro(build_brotli)
endmacro()

if(ARROW_WITH_BROTLI)
resolve_dependency(Brotli)
resolve_dependency(Brotli PC_PACKAGE_NAMES libbrotlidec libbrotlienc)
# TODO: Don't use global includes but rather target_include_directories
get_target_property(BROTLI_INCLUDE_DIR Brotli::brotlicommon
INTERFACE_INCLUDE_DIRECTORIES)
Expand Down Expand Up @@ -1079,9 +1091,9 @@ macro(build_glog)
)
set(GLOG_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(GLOG_CMAKE_C_FLAGS "${EP_C_FLAGS} -fPIC")
if(Threads::Threads)
set(GLOG_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -pthread")
set(GLOG_CMAKE_C_FLAGS "${EP_C_FLAGS} -fPIC -pthread")
if(CMAKE_THREAD_LIBS_INIT)
set(GLOG_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_THREAD_LIBS_INIT}")
set(GLOG_CMAKE_C_FLAGS "${EP_C_FLAGS} ${CMAKE_THREAD_LIBS_INIT}")
endif()

if(APPLE)
Expand Down Expand Up @@ -1117,7 +1129,7 @@ macro(build_glog)
endmacro()

if(ARROW_USE_GLOG)
resolve_dependency(GLOG)
resolve_dependency(GLOG PC_PACKAGE_NAMES libglog)
# TODO: Don't use global includes but rather target_include_directories
get_target_property(GLOG_INCLUDE_DIR glog::glog INTERFACE_INCLUDE_DIRECTORIES)
include_directories(SYSTEM ${GLOG_INCLUDE_DIR})
Expand Down Expand Up @@ -1292,7 +1304,11 @@ if(ARROW_WITH_THRIFT)
# to build Boost, so don't look again if already found.
if(NOT Thrift_FOUND AND NOT THRIFT_FOUND)
# Thrift c++ code generated by 0.13 requires 0.11 or greater
resolve_dependency(Thrift REQUIRED_VERSION 0.11.0)
resolve_dependency(Thrift
REQUIRED_VERSION
0.11.0
PC_PACKAGE_NAMES
thrift)
endif()
# TODO: Don't use global includes but rather target_include_directories
include_directories(SYSTEM ${THRIFT_INCLUDE_DIR})
Expand Down Expand Up @@ -1392,7 +1408,11 @@ if(ARROW_WITH_PROTOBUF)
else()
set(ARROW_PROTOBUF_REQUIRED_VERSION "2.6.1")
endif()
resolve_dependency(Protobuf REQUIRED_VERSION ${ARROW_PROTOBUF_REQUIRED_VERSION})
resolve_dependency(Protobuf
REQUIRED_VERSION
${ARROW_PROTOBUF_REQUIRED_VERSION}
PC_PACKAGE_NAMES
protobuf)

if(ARROW_PROTOBUF_USE_SHARED AND MSVC_TOOLCHAIN)
add_definitions(-DPROTOBUF_USE_DLLS)
Expand Down Expand Up @@ -1825,7 +1845,11 @@ if(ARROW_BUILD_BENCHMARKS)
# ci/conda_env_cpp.yml.
set(BENCHMARK_REQUIRED_VERSION 0.0.0)
endif()
resolve_dependency(benchmark REQUIRED_VERSION ${BENCHMARK_REQUIRED_VERSION})
resolve_dependency(benchmark
REQUIRED_VERSION
${BENCHMARK_REQUIRED_VERSION}
IS_RUNTIME_DEPENDENCY
FALSE)
# TODO: Don't use global includes but rather target_include_directories
get_target_property(BENCHMARK_INCLUDE_DIR benchmark::benchmark
INTERFACE_INCLUDE_DIRECTORIES)
Expand Down Expand Up @@ -1940,7 +1964,7 @@ macro(build_zlib)
endmacro()

if(ARROW_WITH_ZLIB)
resolve_dependency(ZLIB)
resolve_dependency(ZLIB PC_PACKAGE_NAMES zlib)

# TODO: Don't use global includes but rather target_include_directories
get_target_property(ZLIB_INCLUDE_DIR ZLIB::ZLIB INTERFACE_INCLUDE_DIRECTORIES)
Expand Down Expand Up @@ -1996,7 +2020,7 @@ macro(build_lz4)
endmacro()

if(ARROW_WITH_LZ4)
resolve_dependency(Lz4)
resolve_dependency(Lz4 PC_PACKAGE_NAMES liblz4)

# TODO: Don't use global includes but rather target_include_directories
get_target_property(LZ4_INCLUDE_DIR LZ4::lz4 INTERFACE_INCLUDE_DIRECTORIES)
Expand Down Expand Up @@ -2060,7 +2084,7 @@ macro(build_zstd)
endmacro()

if(ARROW_WITH_ZSTD)
resolve_dependency(zstd)
resolve_dependency(zstd PC_PACKAGE_NAMES libzstd)

if(TARGET zstd::libzstd)
set(ARROW_ZSTD_LIBZSTD zstd::libzstd)
Expand Down Expand Up @@ -2120,7 +2144,14 @@ macro(build_re2)
endmacro()

if(ARROW_WITH_RE2)
# Don't specify "PC_PACKAGE_NAMES re2" here because re2.pc may
# include -std=c++11. It's not compatible with C source and C++
# source not uses C++ 11.
resolve_dependency(re2 HAVE_ALT TRUE)
if(${re2_SOURCE} STREQUAL "SYSTEM")
get_target_property(RE2_LIB re2::re2 IMPORTED_LOCATION)
string(APPEND ARROW_PC_LIBS_PRIVATE " ${RE2_LIB}")
endif()
add_definitions(-DARROW_WITH_RE2)

# TODO: Don't use global includes but rather target_include_directories
Expand Down Expand Up @@ -2169,6 +2200,9 @@ endmacro()

if(ARROW_WITH_BZ2)
resolve_dependency(BZip2)
if(${BZip2_SOURCE} STREQUAL "SYSTEM")
string(APPEND ARROW_PC_LIBS_PRIVATE " ${BZIP2_LIBRARIES}")
endif()

if(NOT TARGET BZip2::BZip2)
add_library(BZip2::BZip2 UNKNOWN IMPORTED)
Expand Down Expand Up @@ -2219,7 +2253,11 @@ macro(build_utf8proc)
endmacro()

if(ARROW_WITH_UTF8PROC)
resolve_dependency(utf8proc REQUIRED_VERSION "2.2.0")
resolve_dependency(utf8proc
REQUIRED_VERSION
"2.2.0"
PC_PACKAGE_NAMES
libutf8proc)

add_definitions(-DARROW_WITH_UTF8PROC)

Expand Down Expand Up @@ -2287,7 +2325,11 @@ endmacro()
# Dependencies for Arrow Flight RPC

macro(build_grpc)
resolve_dependency(c-ares HAVE_ALT TRUE)
resolve_dependency(c-ares
HAVE_ALT
TRUE
PC_PACKAGE_NAMES
libcares)
# TODO: Don't use global includes but rather target_include_directories
get_target_property(c-ares_INCLUDE_DIR c-ares::cares INTERFACE_INCLUDE_DIRECTORIES)
include_directories(SYSTEM ${c-ares_INCLUDE_DIR})
Expand Down Expand Up @@ -2551,7 +2593,9 @@ if(ARROW_WITH_GRPC)
HAVE_ALT
TRUE
REQUIRED_VERSION
${ARROW_GRPC_REQUIRED_VERSION})
${ARROW_GRPC_REQUIRED_VERSION}
PC_PACKAGE_NAMES
grpc++)

# TODO: Don't use global includes but rather target_include_directories
get_target_property(GRPC_INCLUDE_DIR gRPC::grpc++ INTERFACE_INCLUDE_DIRECTORIES)
Expand Down
3 changes: 2 additions & 1 deletion cpp/examples/minimal_build/minimal.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y -q && \
apt-get install -y -q --no-install-recommends \
build-essential \
cmake && \
cmake \
pkg-config && \
apt-get clean && rm -rf /var/lib/apt/lists*
33 changes: 32 additions & 1 deletion cpp/examples/minimal_build/run_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ popd

echo
echo "=="
echo "== CMake:"
echo "== Building example project using Arrow C++ library"
echo "=="
echo

rm -rf $EXAMPLE_BUILD_DIR
mkdir -p $EXAMPLE_BUILD_DIR
pushd $EXAMPLE_BUILD_DIR

Expand All @@ -81,10 +83,39 @@ popd

echo
echo "=="
echo "== CMake:"
echo "== Running example project"
echo "=="
echo

pushd $EXAMPLE_DIR

${EXAMPLE_BUILD_DIR}/arrow_example
$EXAMPLE_BUILD_DIR/arrow_example

echo
echo "=="
echo "== pkg-config"
echo "== Building example project using Arrow C++ library"
echo "=="
echo

rm -rf $EXAMPLE_BUILD_DIR
mkdir -p $EXAMPLE_BUILD_DIR
${CXX:-c++} \
-o $EXAMPLE_BUILD_DIR/arrow_example \
$EXAMPLE_DIR/example.cc \
$(PKG_CONFIG_PATH=$ARROW_BUILD_DIR/lib/pkgconfig \
pkg-config --cflags --libs --static arrow)

popd

echo
echo "=="
echo "== pkg-config:"
echo "== Running example project"
echo "=="
echo

pushd $EXAMPLE_DIR

$EXAMPLE_BUILD_DIR/arrow_example
1 change: 1 addition & 0 deletions cpp/examples/minimal_build/system_dependency.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ RUN apt-get update -y -q && \
libthrift-dev \
libutf8proc-dev \
libzstd-dev \
pkg-config \
protobuf-compiler \
rapidjson-dev \
zlib1g-dev && \
Expand Down
17 changes: 16 additions & 1 deletion cpp/src/arrow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,21 @@ endif()

set(ARROW_ALL_SRCS ${ARROW_SRCS} ${ARROW_C_SRCS})

if(ARROW_BUILD_STATIC AND ARROW_BUNDLED_STATIC_LIBS)
set(ARROW_BUILD_BUNDLED_DEPENDENCIES TRUE)
else()
set(ARROW_BUILD_BUNDLED_DEPENDENCIES FALSE)
endif()

if(ARROW_BUILD_BUNDLED_DEPENDENCIES)
string(APPEND ARROW_PC_LIBS_PRIVATE " -larrow_bundled_dependencies")
endif()
# Need -latomic on Raspbian.
# See also: https://issues.apache.org/jira/browse/ARROW-12860
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND ${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7")
string(APPEND ARROW_PC_LIBS_PRIVATE " -latomic")
endif()

add_arrow_lib(arrow
CMAKE_PACKAGE_NAME
Arrow
Expand Down Expand Up @@ -536,7 +551,7 @@ if(ARROW_WITH_BACKTRACE)
endforeach()
endif()

if(ARROW_BUILD_STATIC AND ARROW_BUNDLED_STATIC_LIBS)
if(ARROW_BUILD_BUNDLED_DEPENDENCIES)
arrow_car(_FIRST_LIB ${ARROW_BUNDLED_STATIC_LIBS})
arrow_cdr(_OTHER_LIBS ${ARROW_BUNDLED_STATIC_LIBS})
create_merged_static_lib(arrow_bundled_dependencies
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/arrow.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ full_so_version=@ARROW_FULL_SO_VERSION@
Name: Apache Arrow
Description: Arrow is a set of technologies that enable big-data systems to process and move data fast.
Version: @ARROW_VERSION@
Requires.private:@ARROW_PC_REQUIRES_PRIVATE@
Libs: -L${libdir} -larrow
Libs.private:@ARROW_PC_LIBS_PRIVATE@
Cflags: -I${includedir}
Loading

0 comments on commit 74af39d

Please sign in to comment.