Skip to content

Commit

Permalink
CMake: Install blosc2Config.cmake
Browse files Browse the repository at this point in the history
This adds a CMake installer to conserve targets and properties
on install, so CMake users do not need to write `Findblosc2.cmake`
files anymore.

This also helps to preserve transitive dependencies on CMake
targets, especially useful for fully static builds, e.g., for
Python wheels.
  • Loading branch information
ax3l committed Jul 25, 2023
1 parent 2714de0 commit 80cc257
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 87 deletions.
116 changes: 99 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -385,23 +385,6 @@ if(NOT DEFINED BLOSC_INSTALL)
endif()
endif()

# uninstall target
if(BLOSC_INSTALL)
include(GNUInstallDirs)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/blosc2.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/blosc2.pc"
@ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/blosc2.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" COMPONENT DEV)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
@ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()

# include directories
include_directories(include)
if(BUILD_PLUGINS)
Expand Down Expand Up @@ -441,6 +424,105 @@ if(BUILD_EXAMPLES)
endif()


# install targets
if(BLOSC_INSTALL)
include(GNUInstallDirs)

# C++ files
install(FILES ${PROJECT_SOURCE_DIR}/include/blosc2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT DEV)
install(FILES ${PROJECT_SOURCE_DIR}/include/b2nd.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT DEV)
install(FILES
${PROJECT_SOURCE_DIR}/include/blosc2/blosc2-export.h
${PROJECT_SOURCE_DIR}/include/blosc2/blosc2-common.h
${PROJECT_SOURCE_DIR}/include/blosc2/blosc2-stdio.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/blosc2 COMPONENT DEV)
if(BUILD_PLUGINS)
install(FILES
${PROJECT_SOURCE_DIR}/include/blosc2/filters-registry.h
${PROJECT_SOURCE_DIR}/include/blosc2/codecs-registry.h
${PROJECT_SOURCE_DIR}/include/blosc2/tuners-registry.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/blosc2 COMPONENT DEV)
endif()

if(BUILD_SHARED)
install(TARGETS blosc2_shared
LIBRARY COMPONENT LIB
ARCHIVE COMPONENT DEV
RUNTIME COMPONENT LIB)
endif()
if(BUILD_STATIC)
install(TARGETS blosc2_static COMPONENT DEV)
endif()

# config files
include(CMakePackageConfigHelpers)

if(NOT blosc2_INSTALL_CMAKEDIR)
if(CMAKE_INSTALL_CMAKEDIR)
set(blosc2_INSTALL_CMAKEDIR "${CMAKE_INSTALL_CMAKEDIR}/blosc2")
else()
if(WIN32)
set(blosc2_INSTALL_CMAKEDIR "cmake")
else()
set(blosc2_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/blosc2")
endif()
endif()
endif()

# CMake config file
set(blosc2_INSTALL_TARGET_NAMES)
if(BUILD_SHARED)
list(APPEND blosc2_INSTALL_TARGET_NAMES blosc2_shared)
endif()
if(BUILD_STATIC)
list(APPEND blosc2_INSTALL_TARGET_NAMES blosc2_static)
endif()
configure_file(
${PROJECT_SOURCE_DIR}/blosc2Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/blosc2Config.cmake
@ONLY
)
install(TARGETS ${blosc2_INSTALL_TARGET_NAMES}
EXPORT blosc2Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT blosc2Targets
FILE blosc2Targets.cmake
NAMESPACE blosc2::
DESTINATION ${blosc2_INSTALL_CMAKEDIR}
)
write_basic_package_version_file("blosc2ConfigVersion.cmake"
VERSION ${BLOSC2_VERSION_STRING}
COMPATIBILITY SameMajorVersion
)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/blosc2Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/blosc2ConfigVersion.cmake
DESTINATION ${blosc2_INSTALL_CMAKEDIR}
)

# pkg-config .pc file
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/blosc2.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/blosc2.pc"
@ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/blosc2.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" COMPONENT DEV)

# uninstaller
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
@ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()


# packaging
if(NOT BLOSC_IS_SUBPROJECT)
include(InstallRequiredSystemLibraries)
Expand Down
2 changes: 1 addition & 1 deletion bench/b2nd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ foreach (source ${SOURCES})
get_filename_component(target_name ${source} NAME_WE)
set(target b2nd_${target_name})
add_executable(${target} ${target_name}.c)
target_link_libraries(${target} blosc_testing ${LIBS})
target_link_libraries(${target} PUBLIC blosc_testing ${LIBS})
endforeach (source)
166 changes: 97 additions & 69 deletions blosc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@
# A simple way to detect that we are using CMAKE
add_definitions(-DUSING_CMAKE)

set(version_string ${BLOSC2_VERSION_MAJOR}.${BLOSC2_VERSION_MINOR}.${BLOSC2_VERSION_PATCH})

# targets
if(BUILD_SHARED)
add_library(blosc2_shared SHARED)
add_library(blosc2::blosc2_shared ALIAS blosc2_shared)
set_target_properties(blosc2_shared PROPERTIES OUTPUT_NAME blosc2)
if(MSVC OR MINGW)
set_target_properties(blosc2_shared PROPERTIES PREFIX lib)
endif()
set_target_properties(blosc2_shared PROPERTIES
VERSION ${version_string}
SOVERSION 2 # Change this when an ABI change happens
)
set_property(
TARGET blosc2_shared
APPEND PROPERTY COMPILE_DEFINITIONS BLOSC_SHARED_LIBRARY)
target_include_directories(blosc2_shared PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
endif()
if(BUILD_STATIC)
add_library(blosc2_static STATIC)
add_library(blosc2::blosc2_static ALIAS blosc2_static)
set_target_properties(blosc2_static PROPERTIES OUTPUT_NAME blosc2)
set_target_properties(blosc2_static PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(MSVC OR MINGW)
set_target_properties(blosc2_static PROPERTIES PREFIX lib)
endif()
target_include_directories(blosc2_static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
endif()

set(INTERNAL_LIBS ${PROJECT_SOURCE_DIR}/internal-complibs)

# Hide symbols by default unless they're specifically exported.
Expand All @@ -17,36 +51,81 @@ set(INTERNAL_LIBS ${PROJECT_SOURCE_DIR}/internal-complibs)
set(CMAKE_C_VISIBILITY_PRESET hidden)

# includes
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR})
if(LZ4_FOUND)
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${LZ4_INCLUDE_DIR})
if(BUILD_SHARED)
target_include_directories(blosc2_shared PUBLIC ${LZ4_INCLUDE_DIR})
endif()
if(BUILD_STATIC)
target_include_directories(blosc2_static PUBLIC ${LZ4_INCLUDE_DIR})
endif()
else()
set(LZ4_LOCAL_DIR ${INTERNAL_LIBS}/lz4-1.9.4)
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${LZ4_LOCAL_DIR})
#set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${LZ4_LOCAL_DIR})
if(BUILD_SHARED)
target_include_directories(blosc2_shared PRIVATE ${LZ4_LOCAL_DIR})
endif()
if(BUILD_STATIC)
target_include_directories(blosc2_static PRIVATE ${LZ4_LOCAL_DIR})
endif()
endif()

if(NOT DEACTIVATE_ZLIB)
if(ZLIB_NG_FOUND)
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${ZLIB_NG_INCLUDE_DIR})
if(BUILD_SHARED)
target_link_libraries(blosc2_shared PUBLIC ZLIB_NG::ZLIB_NG)
endif()
if(BUILD_STATIC)
target_link_libraries(blosc2_static PUBLIC ZLIB_NG::ZLIB_NG)
endif()
elseif(ZLIB_FOUND)
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS})
if(BUILD_SHARED)
target_link_libraries(blosc2_shared PUBLIC ZLIB::ZLIB)
endif()
if(BUILD_STATIC)
target_link_libraries(blosc2_static PUBLIC ZLIB::ZLIB)
endif()
else()
set(ZLIB_LOCAL_DIR ${INTERNAL_LIBS}/${ZLIB_NG_DIR})
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${ZLIB_LOCAL_DIR})
#set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${ZLIB_LOCAL_DIR})
if(BUILD_SHARED)
target_include_directories(blosc2_shared PRIVATE ${ZLIB_LOCAL_DIR})
endif()
if(BUILD_STATIC)
target_include_directories(blosc2_static PRIVATE ${ZLIB_LOCAL_DIR})
endif()
endif()
endif()

if(NOT DEACTIVATE_ZSTD)
if(ZSTD_FOUND)
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${ZSTD_INCLUDE_DIR})
if(BUILD_SHARED)
target_include_directories(blosc2_shared PUBLIC ${ZSTD_INCLUDE_DIR})
target_link_libraries(blosc2_shared PUBLIC ${ZSTD_LIBRARY})
endif()
if(BUILD_STATIC)
target_include_directories(blosc2_static PUBLIC ${ZSTD_INCLUDE_DIR})
target_link_libraries(blosc2_static PUBLIC ${ZSTD_LIBRARY})
endif()
else()
set(ZSTD_LOCAL_DIR ${INTERNAL_LIBS}/zstd-1.5.5)
set(BLOSC_INCLUDE_DIRS ${BLOSC_INCLUDE_DIRS} ${ZSTD_LOCAL_DIR}
${ZSTD_LOCAL_DIR}/common)
if(BUILD_SHARED)
target_include_directories(blosc2_shared PRIVATE ${ZSTD_LOCAL_DIR} ${ZSTD_LOCAL_DIR}/common)
endif()
if(BUILD_STATIC)
target_include_directories(blosc2_static PRIVATE ${ZSTD_LOCAL_DIR} ${ZSTD_LOCAL_DIR}/common)
endif()
endif()
endif()

include_directories(${BLOSC_INCLUDE_DIRS})
# targets
if(BUILD_SHARED)
target_link_libraries(blosc2_shared PUBLIC ${LIBS})
endif()
if(BUILD_STATIC)
target_link_libraries(blosc2_static PUBLIC ${LIBS})
endif()


# library sources
set(SOURCES ${SOURCES} blosc2.c blosclz.c fastcopy.c fastcopy.h schunk.c frame.c stune.c stune.h
Expand All @@ -73,8 +152,6 @@ if(COMPILER_SUPPORT_ALTIVEC)
endif()
set(SOURCES ${SOURCES} shuffle.c)

set(version_string ${BLOSC2_VERSION_MAJOR}.${BLOSC2_VERSION_MINOR}.${BLOSC2_VERSION_PATCH})

set(CMAKE_THREAD_PREFER_PTHREAD TRUE) # pre 3.1
set(THREADS_PREFER_PTHREAD_FLAG TRUE) # CMake 3.1+
if(WIN32)
Expand Down Expand Up @@ -155,23 +232,6 @@ if(UNIX AND NOT APPLE)
# set(LIBS ${LIBS} "profiler")
endif()


# targets
if(BUILD_SHARED)
add_library(blosc2_shared SHARED ${SOURCES})
set_target_properties(blosc2_shared PROPERTIES OUTPUT_NAME blosc2)
if(MSVC OR MINGW)
set_target_properties(blosc2_shared PROPERTIES PREFIX lib)
endif()
set_target_properties(blosc2_shared PROPERTIES
VERSION ${version_string}
SOVERSION 2 # Change this when an ABI change happens
)
set_property(
TARGET blosc2_shared
APPEND PROPERTY COMPILE_DEFINITIONS BLOSC_SHARED_LIBRARY)
endif()

# Based on the target architecture and hardware features supported
# by the C compiler, set hardware architecture optimization flags
# for specific shuffle implementations.
Expand Down Expand Up @@ -270,6 +330,13 @@ if(COMPILER_SUPPORT_ALTIVEC)
APPEND PROPERTY COMPILE_DEFINITIONS SHUFFLE_ALTIVEC_ENABLED)
endif()

if(BUILD_SHARED)
target_sources(blosc2_shared PRIVATE ${SOURCES})
endif()
if(BUILD_STATIC)
target_sources(blosc2_static PRIVATE ${SOURCES})
endif()

# When the option has been selected to compile the test suite,
# compile an additional version of blosc2_static which exports
# some normally-hidden symbols (to facilitate unit testing).
Expand All @@ -288,50 +355,11 @@ if(BUILD_TESTS)
endif()

if(BUILD_SHARED)
target_link_libraries(blosc2_shared ${LIBS})
target_link_libraries(blosc2_shared PUBLIC ${LIBS})
target_include_directories(blosc2_shared PUBLIC ${BLOSC_INCLUDE_DIRS})
endif()

if(BUILD_TESTS)
target_link_libraries(blosc_testing ${LIBS})
target_link_libraries(blosc_testing PUBLIC ${LIBS})
target_include_directories(blosc_testing PUBLIC ${BLOSC_INCLUDE_DIRS})
endif()

if(BUILD_STATIC)
add_library(blosc2_static STATIC ${SOURCES})
set_target_properties(blosc2_static PROPERTIES OUTPUT_NAME blosc2)
set_target_properties(blosc2_static PROPERTIES POSITION_INDEPENDENT_CODE ON)
if(MSVC OR MINGW)
set_target_properties(blosc2_static PROPERTIES PREFIX lib)
endif()
target_link_libraries(blosc2_static PUBLIC ${LIBS})
target_include_directories(blosc2_static PUBLIC ${BLOSC_INCLUDE_DIRS})
endif()

# install
if(BLOSC_INSTALL)
install(FILES ${PROJECT_SOURCE_DIR}/include/blosc2.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT DEV)
install(FILES ${PROJECT_SOURCE_DIR}/include/b2nd.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} COMPONENT DEV)
install(FILES
${PROJECT_SOURCE_DIR}/include/blosc2/blosc2-export.h
${PROJECT_SOURCE_DIR}/include/blosc2/blosc2-common.h
${PROJECT_SOURCE_DIR}/include/blosc2/blosc2-stdio.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/blosc2 COMPONENT DEV)
if(BUILD_PLUGINS)
install(FILES
${PROJECT_SOURCE_DIR}/include/blosc2/filters-registry.h
${PROJECT_SOURCE_DIR}/include/blosc2/codecs-registry.h
${PROJECT_SOURCE_DIR}/include/blosc2/tuners-registry.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/blosc2 COMPONENT DEV)
endif()

if(BUILD_SHARED)
install(TARGETS blosc2_shared
LIBRARY COMPONENT LIB
ARCHIVE COMPONENT DEV
RUNTIME COMPONENT LIB)
endif()
if(BUILD_STATIC)
install(TARGETS blosc2_static COMPONENT DEV)
endif()
endif()
Loading

0 comments on commit 80cc257

Please sign in to comment.