Skip to content
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

Enhanced CMakeLists.txt to create a proper export #129

Merged
merged 1 commit into from
Apr 5, 2024
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
32 changes: 23 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ project(miniocpp

include(GNUInstallDirs)
include(CheckIncludeFiles)
include(CMakePackageConfigHelpers)

option(MINIO_CPP_TEST "Build tests" OFF)
option(MINIO_CPP_MAKE_DOC "Build documentation" OFF)

# TODO: Leftovers from previous CMake build script:
#set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(MINIO_CPP_CFLAGS)
set(MINIO_CPP_LIBS)
set(MINIO_CPP_STD "17")
Expand All @@ -58,26 +56,25 @@ endif()
# Dependencies
# ------------

find_package(CURL REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(unofficial-curlpp CONFIG REQUIRED)
find_package(unofficial-inih CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(pugixml CONFIG REQUIRED)
find_package(ZLIB REQUIRED)

list(APPEND MINIO_CPP_LIBS
CURL::libcurl
unofficial::curlpp::curlpp
unofficial::inih::inireader
nlohmann_json::nlohmann_json
pugixml
OpenSSL::SSL OpenSSL::Crypto
ZLIB::ZLIB
)

if (WIN32)
list(APPEND MINIO_CPP_LIBS wsock32)
list(APPEND MINIO_CPP_LIBS ws2_32)
list(APPEND MINIO_CPP_LIBS ZLIB::ZLIB)
endif()

# Minio C++ Library
Expand Down Expand Up @@ -121,9 +118,13 @@ set(MINIO_CPP_HEADERS
add_library(miniocpp STATIC ${MINIO_CPP_SOURCES} ${MINIO_CPP_HEADERS})
target_compile_options(miniocpp PRIVATE ${MINIO_CPP_CFLAGS})
target_compile_features(miniocpp PUBLIC cxx_std_${MINIO_CPP_STD})
target_include_directories(miniocpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/include)
target_include_directories(miniocpp PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_libraries(miniocpp PUBLIC ${MINIO_CPP_LIBS})
set_target_properties(miniocpp PROPERTIES VERSION "${MINIO_CPP_VERSION_STRING}")
set_target_properties(miniocpp PROPERTIES POSITION_INDEPENDENT_CODE ON)

# Add a cmake alias - this is how users should use minio-cpp in their cmake projects.
add_library(miniocpp::miniocpp ALIAS miniocpp)
Expand Down Expand Up @@ -227,15 +228,28 @@ endif()
# Installation Instructions
# -------------------------

configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/miniocpp-config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/miniocpp-config.cmake"
INSTALL_DESTINATION
"{CMAKE_INSTALL_LIBDIR}/cmake/miniocpp"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)

install(TARGETS miniocpp
EXPORT miniocpp-config
EXPORT miniocpp-targets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

install(EXPORT miniocpp-config
install(EXPORT miniocpp-targets
NAMESPACE miniocpp::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/miniocpp"
EXPORT_LINK_INTERFACE_LIBRARIES)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/miniocpp-config.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/miniocpp")

install(FILES
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ Typically `minio-cpp` will be part of dependencies specified in `vcpkg.json` fil
}
```

## Using `minio-cpp` with cmake

MinIO C++ cliend SDK can be consumed as a dependency in CMakeLists.txt, the following can be used as an example:

```cmake
cmake_minimum_required(VERSION 3.10)

project(miniocpp_example LANGUAGES C CXX)

# This will try to find miniocpp package and all its dependencies.
find_package(miniocpp REQUIRED)

# Create an executable called miniocpp-example:
add_executable(miniocpp-example example.cpp)

# Link the executable to miniocpp and all its dependencies:
target_link_libraries(miniocpp-example PRIVATE miniocpp::miniocpp)

# Make sure you are using at least C++17:
target_compile_features(miniocpp-example PUBLIC cxx_std_17)
```

Note that `miniocpp::miniocpp` is a cmake imported target, which contains all the instructions necessary to use `minio-cpp` library from your cmake projet file.

## Hacking minio-cpp

In order to run minio-cpp tests and examples, you can do the following assuming `VCPKG_ROOT` points to a valid `vcpkg` installation:
Expand Down
1 change: 0 additions & 1 deletion configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
BUILD_OPTIONS="-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"

if [ -n "$VCPKG_ROOT" ]; then
$VCPKG_ROOT/vcpkg install
BUILD_OPTIONS="${BUILD_OPTIONS} -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
fi

Expand Down
10 changes: 10 additions & 0 deletions miniocpp-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@PACKAGE_INIT@

find_package(OpenSSL REQUIRED)
find_package(unofficial-curlpp CONFIG REQUIRED)
find_package(unofficial-inih CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(pugixml CONFIG REQUIRED)
find_package(ZLIB REQUIRED)

include("${CMAKE_CURRENT_LIST_DIR}/miniocpp-targets.cmake")
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{ "name": "nlohmann-json" },
{ "name": "openssl" },
{ "name": "pugixml" },
{ "name": "zlib", "platform": "windows" },
{ "name": "vcpkg-cmake", "host": true },
{ "name": "vcpkg-cmake-config", "host": true }
]
Expand Down