From 44520f6bd09826e74a05fac6742a7b88384d1c20 Mon Sep 17 00:00:00 2001 From: Muhammad Usama Date: Fri, 26 May 2023 18:49:30 +0000 Subject: [PATCH] Allow creation of shared library for libTink This commit enable to cross compile the libTink by creating a shared library. Signed-off-by: Muhammad Usama --- CMakeLists.txt | 31 ++++++++++++++++++++++++++- cmake/TinkWorkspace.cmake | 28 ++++++++++++++---------- tink/CMakeLists.txt | 45 ++++++++++++++++++++++++++++++++++----- 3 files changed, 87 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a90d24bf..73a9d5b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.13) -project(Tink VERSION 2.1.1 LANGUAGES CXX) +project(tink VERSION 2.1.1 LANGUAGES CXX) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") @@ -7,7 +7,14 @@ option(TINK_BUILD_TESTS "Build Tink tests" OFF) option(TINK_USE_SYSTEM_OPENSSL "Build Tink linking to OpenSSL installed in the system" OFF) option(TINK_USE_INSTALLED_ABSEIL "Build Tink linking to Abseil installed in the system" OFF) option(TINK_USE_INSTALLED_GOOGLETEST "Build Tink linking to GTest installed in the system" OFF) +option(TINK_USE_INSTALLED_PROTOBUF "Build Tink linking to Protobuf installed in the system" OFF) +option(TINK_USE_INSTALLED_RAPIDJSON "Build Tink linking to Rapidjson installed in the system" OFF) option(USE_ONLY_FIPS "Enables the FIPS only mode in Tink" OFF) +option(TINK_BUILD_SHARED_LIB "Build libtink bundle it with the headers" OFF) + +if (TINK_BUILD_SHARED_LIB) + set(CMAKE_POSITION_INDEPENDENT_CODE ON CACHE BOOL "libtink override" FORCE) +endif() set(CPACK_GENERATOR TGZ) set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) @@ -34,3 +41,25 @@ list(APPEND TINK_INCLUDE_DIRS "${TINK_INCLUDE_ALIAS_DIR}") add_subdirectory(tink) add_subdirectory(proto) + +if (TINK_BUILD_SHARED_LIB) + install( + DIRECTORY + "${CMAKE_CURRENT_SOURCE_DIR}/tink/" + "${TINK_GENFILE_DIR}/tink/" + DESTINATION "include/tink" + FILES_MATCHING PATTERN "*.h" + ) + + install( + DIRECTORY + "${TINK_GENFILE_DIR}/proto" + DESTINATION "include" + FILES_MATCHING PATTERN "*.h" + ) + + export(EXPORT Tink FILE tinkConfig.cmake) + install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" + ) +endif() diff --git a/cmake/TinkWorkspace.cmake b/cmake/TinkWorkspace.cmake index 46eef918..ef26ac93 100644 --- a/cmake/TinkWorkspace.cmake +++ b/cmake/TinkWorkspace.cmake @@ -110,7 +110,7 @@ if (NOT TARGET crypto) "$") else() # Support for ED25519 was added from 1.1.1. - find_package(OpenSSL 1.1.1 REQUIRED) + find_package(OpenSSL REQUIRED) _create_interface_target(crypto OpenSSL::Crypto) endif() else() @@ -123,11 +123,13 @@ set(RAPIDJSON_BUILD_DOC OFF CACHE BOOL "Tink dependency override" FORCE) set(RAPIDJSON_BUILD_EXAMPLES OFF CACHE BOOL "Tink dependency override" FORCE) set(RAPIDJSON_BUILD_TESTS OFF CACHE BOOL "Tink dependency override" FORCE) -http_archive( - NAME rapidjson - URL https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz - SHA256 bf7ced29704a1e696fbccf2a2b4ea068e7774fa37f6d7dd4039d0787f8bed98e -) +if(NOT TINK_USE_INSTALLED_RAPIDJSON) + http_archive( + NAME rapidjson + URL https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz + SHA256 bf7ced29704a1e696fbccf2a2b4ea068e7774fa37f6d7dd4039d0787f8bed98e + ) +endif() # Rapidjson is a header-only library with no explicit target. Here we create one. add_library(rapidjson INTERFACE) target_include_directories(rapidjson INTERFACE "${rapidjson_SOURCE_DIR}") @@ -136,8 +138,12 @@ set(protobuf_BUILD_TESTS OFF CACHE BOOL "Tink dependency override" FORCE) set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "Tink dependency override" FORCE) set(protobuf_INSTALL OFF CACHE BOOL "Tink dependency override" FORCE) -http_archive( - NAME com_google_protobuf - URL https://github.com/protocolbuffers/protobuf/releases/download/v25.1/protobuf-25.1.zip - SHA256 5c86c077b0794c3e9bb30cac872cf883043febfb0f992137f0a8b1c3d534617c -) +if(NOT TINK_USE_INSTALLED_PROTOBUF) + http_archive( + NAME com_google_protobuf + URL https://github.com/protocolbuffers/protobuf/releases/download/v25.1/protobuf-25.1.zip + SHA256 5c86c077b0794c3e9bb30cac872cf883043febfb0f992137f0a8b1c3d534617c + ) +else() + find_package(Protobuf REQUIRED) +endif() diff --git a/tink/CMakeLists.txt b/tink/CMakeLists.txt index 5814a97e..f7fece0e 100644 --- a/tink/CMakeLists.txt +++ b/tink/CMakeLists.txt @@ -25,9 +25,7 @@ endif() set(TINK_VERSION_H "${TINK_GENFILE_DIR}/tink/version.h") -tink_cc_library( - NAME cc - SRCS +set(TINK_PUBLIC_APIS aead.h aead_config.h aead_factory.h @@ -74,7 +72,9 @@ tink_cc_library( streaming_mac.h tink_config.h "${TINK_VERSION_H}" - DEPS +) + +set(TINK_PUBLIC_API_DEPS tink::core::aead tink::core::binary_keyset_reader tink::core::binary_keyset_writer @@ -139,7 +139,15 @@ tink_cc_library( tink::util::validation tink::proto::config_cc_proto tink::proto::tink_cc_proto - PUBLIC +) + +tink_cc_library( + NAME cc + SRCS + ${TINK_PUBLIC_APIS} + DEPS + ${TINK_PUBLIC_API_DEPS} + PUBLIC ) add_library(tink::static ALIAS tink_core_cc) @@ -1123,6 +1131,33 @@ tink_cc_test( tink::util::test_matchers ) + +if (TINK_BUILD_SHARED_LIB) + add_library(tink SHARED + ${TINK_PUBLIC_APIS} + version_script.lds + exported_symbols.lds + ) + target_link_libraries(tink + PRIVATE + -fuse-ld=gold # GNU ld does not support ICF. + -Wl,--version-script="${CMAKE_CURRENT_SOURCE_DIR}/version_script.lds" + -Wl,--gc-sections + -Wl,--icf=all + -Wl,--strip-all + ) + target_include_directories(tink PUBLIC ${TINK_INCLUDE_DIRS}) + target_link_libraries(tink + PRIVATE + -Wl,--whole-archive + ${TINK_PUBLIC_API_DEPS} + -Wl,--no-whole-archive + ) + set_target_properties(tink PROPERTIES SOVERSION ${TINK_CC_VERSION_LABEL}) + + install(TARGETS tink EXPORT Tink LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() + tink_cc_test( NAME big_integer_test SRCS