Add CMake support

The library can now be installed using CMake v3.0+.

Below is an example configuration.

1. Generate configuation

cmake -H. -Bbuild
    -GNinja
    -DCMAKE_BUILD_TYPE=Release // The default profile.
    -DCMAKE_INSTALL_PREFIX=/usr/local/
    -DBUILD_SHARED_LIBS=ON
    -DOLM_TESTS=1
    -DOLM_FUZZERS=1

2. Build & install the targets

cmake --build build --config Release --target install

3. Run the tests

cd build/test && ctest .

The library can also be used as a dependency with CMake using

find_package(Olm::Olm REQUIRED)
target_link_libraries(my_exe Olm::Olm)

Signed-off-by: Konstantinos Sideris <sideris.konstantin@gmail.com>
This commit is contained in:
Konstantinos Sideris 2018-06-11 21:02:27 +03:00 committed by Hubert Chathi
parent af86a9a8b8
commit 4e94dfc7e0
4 changed files with 214 additions and 0 deletions

118
CMakeLists.txt Normal file
View file

@ -0,0 +1,118 @@
cmake_minimum_required(VERSION 3.1)
project(olm VERSION 2.2.2 LANGUAGES CXX C)
option(OLM_TESTS "Build tests" ON)
option(OLM_FUZZERS "Build fuzzers" ON)
add_definitions(-DOLMLIB_VERSION_MAJOR=${PROJECT_VERSION_MAJOR})
add_definitions(-DOLMLIB_VERSION_MINOR=${PROJECT_VERSION_MINOR})
add_definitions(-DOLMLIB_VERSION_PATCH=${PROJECT_VERSION_PATCH})
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
add_library(olm
src/account.cpp
src/base64.cpp
src/cipher.cpp
src/crypto.cpp
src/memory.cpp
src/message.cpp
src/pickle.cpp
src/ratchet.cpp
src/session.cpp
src/utility.cpp
src/ed25519.c
src/error.c
src/inbound_group_session.c
src/megolm.c
src/olm.cpp
src/outbound_group_session.c
src/pickle_encoding.c
lib/crypto-algorithms/aes.c
lib/crypto-algorithms/sha256.c
lib/curve25519-donna/curve25519-donna.c)
add_library(Olm::Olm ALIAS olm)
target_include_directories(olm
PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/lib)
set_target_properties(olm PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${PROJECT_VERSION})
set_target_properties(olm PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
#
# Installation
#
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/Olm)
install(TARGETS olm
EXPORT olm-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
# The exported target will be named Olm.
set_target_properties(olm PROPERTIES EXPORT_NAME Olm)
install(FILES
${CMAKE_SOURCE_DIR}/include/olm/olm.h
${CMAKE_SOURCE_DIR}/include/olm/outbound_group_session.h
${CMAKE_SOURCE_DIR}/include/olm/inbound_group_session.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/olm)
# Export the targets to a script.
install(EXPORT olm-targets
FILE OlmTargets.cmake
NAMESPACE Olm::
DESTINATION ${INSTALL_CONFIGDIR})
# Create a ConfigVersion.cmake file.
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/OlmConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/OlmConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/OlmConfig.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR})
#Install the config & configversion.
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/OlmConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/OlmConfigVersion.cmake
DESTINATION ${INSTALL_CONFIGDIR})
# Register package in user's package registry
export(EXPORT olm-targets
FILE ${CMAKE_CURRENT_BINARY_DIR}/OlmTargets.cmake
NAMESPACE Olm::)
export(PACKAGE Olm)
if (OLM_TESTS)
add_subdirectory(tests)
endif()
if (OLM_FUZZERS)
add_subdirectory(fuzzers)
endif()

11
cmake/OlmConfig.cmake.in Normal file
View file

@ -0,0 +1,11 @@
get_filename_component(Olm_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(CMakeFindDependencyMacro)
list(APPEND CMAKE_MODULE_PATH ${Olm_CMAKE_DIR})
list(REMOVE_AT CMAKE_MODULE_PATH -1)
if(NOT TARGET Olm::olm)
include("${Olm_CMAKE_DIR}/OlmTargets.cmake")
endif()
set(Olm_LIBRARIES Olm::olm)

19
fuzzers/CMakeLists.txt Normal file
View file

@ -0,0 +1,19 @@
add_executable(fuzz_decode_message fuzz_decode_message.cpp)
target_include_directories(fuzz_decode_message PRIVATE include)
target_link_libraries(fuzz_decode_message Olm::Olm)
add_executable(fuzz_decrypt fuzz_decrypt.cpp)
target_include_directories(fuzz_decrypt PRIVATE include)
target_link_libraries(fuzz_decrypt Olm::Olm)
add_executable(fuzz_group_decrypt fuzz_group_decrypt.cpp)
target_include_directories(fuzz_group_decrypt PRIVATE include)
target_link_libraries(fuzz_group_decrypt Olm::Olm)
add_executable(fuzz_unpickle_account fuzz_unpickle_account.cpp)
target_link_libraries(fuzz_unpickle_account Olm::Olm)
target_include_directories(fuzz_unpickle_account PRIVATE include)
add_executable(fuzz_unpickle_session fuzz_unpickle_session.cpp)
target_link_libraries(fuzz_unpickle_session Olm::Olm)
target_include_directories(fuzz_unpickle_session PRIVATE include)

66
tests/CMakeLists.txt Normal file
View file

@ -0,0 +1,66 @@
enable_testing()
add_executable(test_base64 test_base64.cpp)
target_include_directories(test_base64 PRIVATE include)
target_link_libraries(test_base64 Olm::Olm)
add_test(Base64 test_base64)
add_executable(test_crypto test_crypto.cpp)
target_include_directories(test_crypto PRIVATE include)
target_link_libraries(test_crypto Olm::Olm)
add_test(Crypto test_crypto)
add_executable(test_group_session test_group_session.cpp)
target_include_directories(test_group_session PRIVATE include)
target_link_libraries(test_group_session Olm::Olm)
add_test(GroupSession test_group_session)
add_executable(test_list test_list.cpp)
target_include_directories(test_list PRIVATE include)
target_link_libraries(test_list Olm::Olm)
add_test(List test_list)
add_executable(test_megolm test_megolm.cpp)
target_include_directories(test_megolm PRIVATE include)
target_link_libraries(test_megolm Olm::Olm)
add_test(Megolm test_megolm)
add_executable(test_message test_message.cpp)
target_include_directories(test_message PRIVATE include)
target_link_libraries(test_message Olm::Olm)
add_test(Message test_message)
add_executable(test_olm test_olm.cpp)
target_include_directories(test_olm PRIVATE include)
target_link_libraries(test_olm Olm::Olm)
add_test(Olm test_olm)
add_executable(test_olm_decrypt test_olm_decrypt.cpp)
target_include_directories(test_olm_decrypt PRIVATE include)
target_link_libraries(test_olm_decrypt Olm::Olm)
add_test(OlmDecrypt test_olm_decrypt)
add_executable(test_olm_sha256 test_olm_sha256.cpp)
target_include_directories(test_olm_sha256 PRIVATE include)
target_link_libraries(test_olm_sha256 Olm::Olm)
add_test(OlmSha256 test_olm_sha256)
add_executable(test_olm_signature test_olm_signature.cpp)
target_include_directories(test_olm_signature PRIVATE include)
target_link_libraries(test_olm_signature Olm::Olm)
add_test(OlmSignature test_olm_signature)
add_executable(test_olm_using_malloc test_olm_using_malloc.cpp)
target_include_directories(test_olm_using_malloc PRIVATE include)
target_link_libraries(test_olm_using_malloc Olm::Olm)
add_test(OlmUsingMalloc test_olm_using_malloc)
add_executable(test_ratchet test_ratchet.cpp)
target_include_directories(test_ratchet PRIVATE include)
target_link_libraries(test_ratchet Olm::Olm)
add_test(Ratchet test_ratchet)
add_executable(test_session test_session.cpp)
target_include_directories(test_session PRIVATE include)
target_link_libraries(test_session Olm::Olm)
add_test(Session test_session)