Skip to content

Commit

Permalink
Allow creation of shared library for libTink
Browse files Browse the repository at this point in the history
This commit enable to cross compile the libTink
by creating a shared library.

Signed-off-by: Muhammad Usama <[email protected]>
  • Loading branch information
chaudhryusama committed May 26, 2023
1 parent 005f36b commit 709d5f8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 19 deletions.
31 changes: 30 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
cmake_minimum_required(VERSION 3.13)
project(Tink VERSION 2.0.0 LANGUAGES CXX)
project(tink VERSION 2.0.0 LANGUAGES CXX)

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

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})
Expand All @@ -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()
33 changes: 20 additions & 13 deletions cmake/TinkWorkspace.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ if (NOT TARGET crypto)
"$<BUILD_INTERFACE:${boringssl_SOURCE_DIR}/src/include>")
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()
Expand All @@ -123,21 +123,28 @@ 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}")

set(protobuf_BUILD_TESTS OFF CACHE BOOL "Tink dependency override" FORCE)
set(protobuf_BUILD_EXAMPLES OFF CACHE BOOL "Tink dependency override" FORCE)
## Use protobuf X.21.9.
http_archive(
NAME com_google_protobuf
URL https://github.com/protocolbuffers/protobuf/archive/v21.9.zip
SHA256 5babb8571f1cceafe0c18e13ddb3be556e87e12ceea3463d6b0d0064e6cc1ac3
CMAKE_SUBDIR cmake
)

if(NOT TINK_USE_INSTALLED_PROTOBUF)
## Use protobuf X.21.9.
http_archive(
NAME com_google_protobuf
URL https://github.com/protocolbuffers/protobuf/archive/v21.9.zip
SHA256 5babb8571f1cceafe0c18e13ddb3be556e87e12ceea3463d6b0d0064e6cc1ac3
CMAKE_SUBDIR cmake
)
else()
find_package(Protobuf REQUIRED)
endif()
45 changes: 40 additions & 5 deletions tink/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,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
Expand Down Expand Up @@ -73,7 +71,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
Expand Down Expand Up @@ -138,7 +138,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)
Expand Down Expand Up @@ -1051,6 +1059,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_library(
NAME configuration
SRCS
Expand Down

0 comments on commit 709d5f8

Please sign in to comment.