diff --git a/LICENSE.txt b/LICENSE.txt index a078f5e6b80..0eb703f1030 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1986,3 +1986,31 @@ SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +-------------------------------------------------------------------------------- + +The file cpp/cmake_modules/BuildUtils.cmake contains code from + +https://gist.github.com/cristianadam/ef920342939a89fae3e8a85ca9459b49 + +which is made available under the MIT license + +Copyright (c) 2019 Cristian Adam + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index b9aedaf18ca..da22f2aa66d 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -626,10 +626,6 @@ endif() # # TODO: Also rework how these libs work -set(ARROW_LINK_LIBS) -set(ARROW_SHARED_INSTALL_INTERFACE_LIBS) -set(ARROW_STATIC_INSTALL_INTERFACE_LIBS) - # Libraries to link statically with libarrow.so set(ARROW_LINK_LIBS) set(ARROW_STATIC_LINK_LIBS) diff --git a/cpp/cmake_modules/BuildUtils.cmake b/cpp/cmake_modules/BuildUtils.cmake index e4e13cb7061..2a953fb0cb6 100644 --- a/cpp/cmake_modules/BuildUtils.cmake +++ b/cpp/cmake_modules/BuildUtils.cmake @@ -128,6 +128,85 @@ function(REUSE_PRECOMPILED_HEADER_LIB TARGET_NAME LIB_NAME) endif() endfunction() +# Based on MIT-licensed +# https://gist.github.com/cristianadam/ef920342939a89fae3e8a85ca9459b49 +function(create_merged_static_lib output_target) + set(options) + set(one_value_args NAME ROOT) + set(multi_value_args TO_MERGE) + cmake_parse_arguments(ARG + "${options}" + "${one_value_args}" + "${multi_value_args}" + ${ARGN}) + if(ARG_UNPARSED_ARGUMENTS) + message(SEND_ERROR "Error: unrecognized arguments: ${ARG_UNPARSED_ARGUMENTS}") + endif() + + set( + output_lib_path + ${BUILD_OUTPUT_ROOT_DIRECTORY}${CMAKE_STATIC_LIBRARY_PREFIX}${ARG_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX} + ) + + set(all_library_paths $) + foreach(lib ${ARG_TO_MERGE}) + list(APPEND all_library_paths $) + endforeach() + + if(APPLE) + set(BUNDLE_COMMAND + "libtool" + "-no_warning_for_no_symbols" + "-static" + "-o" + ${output_lib_path} + ${all_library_paths}) + elseif(CMAKE_CXX_COMPILER_ID MATCHES "^(Clang|GNU)$") + set(ar_script_path ${CMAKE_BINARY_DIR}/${ARG_NAME}.ar) + + file(WRITE ${ar_script_path}.in "CREATE ${output_lib_path}\n") + file(APPEND ${ar_script_path}.in "ADDLIB $\n") + + foreach(lib ${ARG_TO_MERGE}) + file(APPEND ${ar_script_path}.in "ADDLIB $\n") + endforeach() + + file(APPEND ${ar_script_path}.in "SAVE\nEND\n") + file(GENERATE OUTPUT ${ar_script_path} INPUT ${ar_script_path}.in) + set(ar_tool ${CMAKE_AR}) + + if(CMAKE_INTERPROCEDURAL_OPTIMIZATION) + set(ar_tool ${CMAKE_CXX_COMPILER_AR}) + endif() + + set(BUNDLE_COMMAND ${ar_tool} -M < ${ar_script_path}) + + elseif(MSVC) + if(NOT CMAKE_LIBTOOL) + find_program(lib_tool lib HINTS "${CMAKE_CXX_COMPILER}/..") + if("${lib_tool}" STREQUAL "lib_tool-NOTFOUND") + message(FATAL_ERROR "Cannot locate libtool to bundle libraries") + endif() + else() + set(${lib_tool} ${CMAKE_LIBTOOL}) + endif() + set(BUNDLE_TOOL ${lib_tool}) + set(BUNDLE_COMMAND ${BUNDLE_TOOL} /NOLOGO /OUT:${output_lib_path} + ${all_library_paths}) + else() + message(FATAL_ERROR "Unknown bundle scenario!") + endif() + + add_custom_command(COMMAND ${BUNDLE_COMMAND} + OUTPUT ${output_lib_path} + COMMENT "Bundling ${output_lib_path}" + VERBATIM) + + add_custom_target(${output_target} ALL DEPENDS ${output_lib_path}) + add_dependencies(${output_target} ${ARG_ROOT} ${ARG_TO_MERGE}) + install(FILES ${output_lib_path} DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endfunction() + # \arg OUTPUTS list to append built targets to function(ADD_ARROW_LIB LIB_NAME) set(options BUILD_SHARED BUILD_STATIC) @@ -140,6 +219,7 @@ function(ADD_ARROW_LIB LIB_NAME) STATIC_LINK_LIBS SHARED_LINK_LIBS SHARED_PRIVATE_LINK_LIBS + BUNDLE_STATIC_LIBS EXTRA_INCLUDES PRIVATE_INCLUDES DEPENDENCIES @@ -341,6 +421,11 @@ function(ADD_ARROW_LIB LIB_NAME) set(LIB_NAME_STATIC ${LIB_NAME}) endif() + if(ARG_BUNDLE_STATIC_LIBS) + set(LIB_NAME_BUNDLED ${LIB_NAME_STATIC}) + set(LIB_NAME_STATIC ${LIB_NAME_STATIC}_unbundled) + endif() + if(ARROW_BUILD_STATIC AND WIN32) target_compile_definitions(${LIB_NAME}_static PUBLIC ARROW_STATIC) endif() @@ -359,6 +444,16 @@ function(ADD_ARROW_LIB LIB_NAME) "$" "$") + if(ARG_BUNDLE_STATIC_LIBS) + create_merged_static_lib(${LIB_NAME}_static_bundled + NAME + ${LIB_NAME_BUNDLED} + ROOT + ${LIB_NAME}_static + TO_MERGE + ${ARG_BUNDLE_STATIC_LIBS}) + endif() + install(TARGETS ${LIB_NAME}_static ${INSTALL_IS_OPTIONAL} EXPORT ${LIB_NAME}_targets RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} diff --git a/cpp/src/arrow/CMakeLists.txt b/cpp/src/arrow/CMakeLists.txt index a9fcbbc596b..52ad223e5eb 100644 --- a/cpp/src/arrow/CMakeLists.txt +++ b/cpp/src/arrow/CMakeLists.txt @@ -416,6 +416,18 @@ endif() set(ARROW_ALL_SRCS ${ARROW_SRCS} ${ARROW_C_SRCS}) +# We bundle some private static dependencies into the Arrow static library, +# for convenience. +set(ARROW_BUNDLE_STATIC_LIBS) + +if(ARROW_JEMALLOC) + list(APPEND ARROW_BUNDLE_STATIC_LIBS jemalloc::jemalloc) +endif() + +if(ARROW_MIMALLOC) + list(APPEND ARROW_BUNDLE_STATIC_LIBS mimalloc::mimalloc) +endif() + add_arrow_lib(arrow CMAKE_PACKAGE_NAME Arrow @@ -437,6 +449,8 @@ add_arrow_lib(arrow ${ARROW_SHARED_PRIVATE_LINK_LIBS} STATIC_LINK_LIBS ${ARROW_STATIC_LINK_LIBS} + BUNDLE_STATIC_LIBS + ${ARROW_BUNDLE_STATIC_LIBS} SHARED_INSTALL_INTERFACE_LIBS ${ARROW_SHARED_INSTALL_INTERFACE_LIBS} STATIC_INSTALL_INTERFACE_LIBS