Skip to content

Commit

Permalink
Better dependency management for Cereal (#740)
Browse files Browse the repository at this point in the history
* Better dependency management

Without this, KaMPIng does not work in conjuction with code which also
depends on cereal.

This requires us to bump to minimum required CMake version to
3.24 (shipped by all major distributions/easy to install).

This first tries to find a system installed cereal, and otherwise
fetches it via FetchContent and builds from source.

This also provides an option to completely disable serialization,
thereby getting rid of the dependency.

More dependency/CMake cleanup will be part of another PR.

* Just install cereal.

* Remove old debug output.

* Add `SYSTEM` keyword to `FetchContent_Declare` and bump CMake to 3.25.
  • Loading branch information
niklas-uhl authored Jul 15, 2024
1 parent 29cc725 commit a3c61e3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 27 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
[submodule "extern/pfr"]
path = extern/pfr
url = https://github.com/apolukhin/pfr_non_boost
[submodule "extern/cereal"]
path = extern/cereal
url = https://github.com/USCiLab/cereal
[submodule "cmake/Format.cmake"]
path = cmake/Format.cmake
url = https://github.com/TheLartians/Format.cmake
37 changes: 27 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.25)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")

Expand All @@ -7,10 +7,11 @@ project(
kamping
DESCRIPTION "An MPI wrapper which makes using MPI feel like C++"
LANGUAGES CXX
VERSION 0.1.0
VERSION 0.1.1
)
include(FetchContent)

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
if (PROJECT_IS_TOP_LEVEL)
# folder support for IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

Expand Down Expand Up @@ -56,9 +57,10 @@ option(KAMPING_WARNINGS_ARE_ERRORS OFF)
option(KAMPING_BUILD_EXAMPLES_AND_TESTS OFF)
option(KAMPING_TESTS_DISCOVER OFF)
option(KAMPING_ENABLE_ULFM "Enable User-Level Failure-Mitigation (ULFM)" OFF)
option(KAMPING_ENABLE_SERIALIZATION "Enable support for serialization (requires Cereal)" ON)

# Enable compilation with ccache. Defaults to ON if this is the main project.
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
if (PROJECT_IS_TOP_LEVEL)
option(KAMPING_USE_CCACHE "Globally enable ccache." ON)
else ()
option(KAMPING_USE_CCACHE "Globally enable ccache." OFF)
Expand Down Expand Up @@ -203,17 +205,32 @@ add_library(pfr INTERFACE)
target_include_directories(pfr SYSTEM INTERFACE extern/pfr/include)
target_link_libraries(kamping_base INTERFACE pfr)

add_library(cereal INTERFACE)
add_library(cereal::cereal ALIAS cereal)
target_include_directories(cereal SYSTEM INTERFACE extern/cereal/include)
target_compile_features(cereal INTERFACE cxx_std_11)
target_link_libraries(kamping_base INTERFACE cereal)
if (KAMPING_ENABLE_SERIALIZATION)
FetchContent_Declare(
cereal
GIT_REPOSITORY https://github.com/USCiLab/cereal
GIT_TAG v1.3.2
SYSTEM FIND_PACKAGE_ARGS NAMES cereal 1.3.2
)
set(JUST_INSTALL_CEREAL ON)
FetchContent_MakeAvailable(cereal)
if (cereal_FOUND)
message(STATUS "Found cereal: ${cereal_DIR}")
else ()
message(STATUS "Cereal: building from source.")
endif ()
target_link_libraries(kamping_base INTERFACE cereal::cereal)
message(STATUS "Serialization: enabled")
else ()
target_compile_definitions(kamping_base INTERFACE KAMPING_DISABLE_SERIALIZATION)
message(STATUS "Serialization: disabled")
endif ()

add_library(kamping::kamping ALIAS kamping)

# Testing and examples are only built if this is the main project or if KAMPING_BUILD_EXAMPLES_AND_TESTS is set (OFF by
# default)
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR KAMPING_BUILD_EXAMPLES_AND_TESTS)
if (PROJECT_IS_TOP_LEVEL OR KAMPING_BUILD_EXAMPLES_AND_TESTS)
add_subdirectory(examples)
add_subdirectory(tests)
endif ()
8 changes: 5 additions & 3 deletions examples/usage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ add_executable(example_datatype datatype_example.cpp)
target_link_libraries(example_datatype kamping)
target_compile_options(example_datatype PRIVATE ${KAMPING_WARNING_FLAGS})

add_executable(example_serialization serialization_example.cpp)
target_link_libraries(example_serialization kamping cereal::cereal)
target_compile_options(example_serialization PRIVATE ${KAMPING_WARNING_FLAGS})
if (KAMPING_ENABLE_SERIALIZATION)
add_executable(example_serialization serialization_example.cpp)
target_link_libraries(example_serialization kamping cereal::cereal)
target_compile_options(example_serialization PRIVATE ${KAMPING_WARNING_FLAGS})
endif ()
2 changes: 0 additions & 2 deletions examples/usage/allgatherv_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <kamping/serialization.hpp>
#include <mpi.h>

#include "cereal/types/string.hpp"
#include "cereal/types/unordered_map.hpp"
#include "helpers_for_examples.hpp"
#include "kamping/checking_casts.hpp"
#include "kamping/collectives/allgather.hpp"
Expand Down
1 change: 0 additions & 1 deletion extern/cereal
Submodule cereal deleted from d1fcec
12 changes: 9 additions & 3 deletions include/kamping/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

#include <type_traits>

#include "cereal/archives/binary.hpp"
#if !defined(KAMPING_DISABLE_SERIALIZATION)
#include "cereal/archives/binary.hpp"
#endif
#include "kamping/data_buffer.hpp"

namespace kamping {
namespace internal {
#if !defined(KAMPING_DISABLE_SERIALIZATION)

/// @brief Buffer holding serialized data.
///
Expand Down Expand Up @@ -92,17 +95,19 @@ class SerializationBuffer {
return _data.size();
}
};
#endif

/// @brief Tag type to identify serialization support.
struct serialization_support_tag {};

/// @brief Type trait to check if a type is a serialization buffer.
template <typename>
constexpr bool is_serialization_buffer_v_impl = false;

#if !defined(KAMPING_DISABLE_SERIALIZATION)
/// @brief Type trait to check if a type is a serialization buffer.
template <typename... Args>
constexpr bool is_serialization_buffer_v_impl<SerializationBuffer<Args...>> = true;
#endif

/// @brief Type trait to check if a type is a serialization buffer.
template <typename T>
Expand All @@ -123,7 +128,7 @@ auto deserialization_repack(BufferType buffer) {
}
}
} // namespace internal

#if !defined(KAMPING_DISABLE_SERIALIZATION)
/// @brief Serializes an object using [`cereal`](https://uscilab.github.io/cereal/).
/// @tparam Archive Type of the archive to use for serialization (see
/// https://uscilab.github.io/cereal/serialization_archives.html). Default is `cereal::BinaryOutputArchive`.
Expand Down Expand Up @@ -235,5 +240,6 @@ auto as_deserializable(T&& object) {
return internal::SerializationBuffer<void, Archive, Allocator, decltype(buffer)>(std::move(buffer));
}
}
#endif

} // namespace kamping
12 changes: 7 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,13 @@ kamping_register_mpi_test(
FILES helpers_for_testing_mpi_test.cpp
CORES 1 4
)
kamping_register_mpi_test(
test_serialization
FILES serialization_test.cpp
CORES 1 2 4
)
if (KAMPING_ENABLE_SERIALIZATION)
kamping_register_mpi_test(
test_serialization
FILES serialization_test.cpp
CORES 1 2 4
)
endif ()
kamping_register_mpi_test(
test_flatten
FILES utils/flatten_test.cpp
Expand Down

0 comments on commit a3c61e3

Please sign in to comment.