Skip to content

Commit

Permalink
Adjust CMake files to used by vcpkg (#7334)
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire authored Feb 19, 2024
1 parent d79b283 commit 334d534
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 59 deletions.
94 changes: 42 additions & 52 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO /OPT:NOREF /OPT:NOICF")
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /INCREMENTAL:NO /OPT:NOREF /OPT:NOICF")
else()
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
if(NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()

Expand Down Expand Up @@ -277,19 +279,7 @@ if(CMAKE_USE_PTHREADS_INIT)
endif()

find_package(Backtrace)
if(Backtrace_FOUND)
add_library(Backtrace::Backtrace INTERFACE IMPORTED)
if(Backtrace_LIBRARIES AND NOT CMAKE_GENERATOR STREQUAL Xcode)
# Apple platforms always have backtrace. We disregard the `Backtrace_*` variables
# because their paths are hardcoded to one SDK within Xcode (e.g. macosx),
# whereas we build for several different SDKs and thus we can't use the include path from one in the other.
# Otherwise if CMake found that the backtrace facility is provided by an external library and not built-in
# we need to configure the interface target with the library include and link path.
target_include_directories(Backtrace::Backtrace INTERFACE ${Backtrace_INCLUDE_DIRS})
target_link_libraries(Backtrace::Backtrace INTERFACE ${Backtrace_LIBRARIES})
endif()
set(REALM_HAVE_BACKTRACE ON)
endif()
set(REALM_HAVE_BACKTRACE ${Backtrace_FOUND})

if(REALM_ENABLE_SYNC)
option(REALM_FORCE_OPENSSL "Always use OpenSSL for SSL needs, regardless of target platform." OFF)
Expand All @@ -314,9 +304,6 @@ if(REALM_NEEDS_OPENSSL OR REALM_FORCE_OPENSSL)
set(_REALM_USE_OPENSSL_DEFAULT_VERIFY_PATHS OFF)
endif()

if(NOT DEFINED OPENSSL_USE_STATIC_LIBS)
set(OPENSSL_USE_STATIC_LIBS ON)
endif()
find_package(OpenSSL REQUIRED)
set(REALM_HAVE_OPENSSL ON)
option(REALM_USE_SYSTEM_OPENSSL_PATHS "Use the system OpenSSL certificate store (specified by the OPENSSLDIR environment variable) at runtime for TLS handshake." ${_REALM_USE_OPENSSL_DEFAULT_VERIFY_PATHS})
Expand All @@ -332,8 +319,11 @@ endif()
# Emscripten does provide Zlib, but it doesn't work with find_package and is handled specially
if(NOT APPLE AND NOT EMSCRIPTEN AND NOT TARGET ZLIB::ZLIB)
if(WIN32 OR (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND REALM_LINUX_TOOLCHAIN))
realm_acquire_dependency(zlib ${DEP_ZLIB_VERSION} ZLIB_CMAKE_INCLUDE_FILE)
include(${ZLIB_CMAKE_INCLUDE_FILE})
find_package(ZLIB)
if (NOT ZLIB_FOUND)
realm_acquire_dependency(zlib ${DEP_ZLIB_VERSION} ZLIB_CMAKE_INCLUDE_FILE)
include(${ZLIB_CMAKE_INCLUDE_FILE})
endif()
elseif(ANDROID)
# On Android FindZLIB chooses the static libz over the dynamic one, but this leads to issues
# (see https://github.com/android/ndk/issues/1179)
Expand Down Expand Up @@ -371,6 +361,39 @@ add_subdirectory(bindgen)
# Install the licence and changelog files
install(FILES LICENSE CHANGELOG.md DESTINATION "doc/realm" COMPONENT devel)

# Make the project importable from the build directory
set(REALM_EXPORTED_TARGETS
Storage
QueryParser
ObjectStore
RealmFFI
RealmFFIStatic
)
if(REALM_ENABLE_SYNC)
list(APPEND REALM_EXPORTED_TARGETS Sync)
endif()
export(TARGETS ${REALM_EXPORTED_TARGETS} NAMESPACE Realm:: FILE RealmTargets.cmake)
configure_file(tools/cmake/RealmConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/RealmConfig.cmake @ONLY)
configure_file(tools/cmake/AcquireRealmDependency.cmake ${CMAKE_CURRENT_BINARY_DIR}/AcquireRealmDependency.cmake @ONLY)

# Make the project importable from the install directory
install(EXPORT realm
NAMESPACE Realm::
FILE RealmTargets.cmake
DESTINATION share/cmake/Realm
COMPONENT devel
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/RealmConfig.cmake
DESTINATION share/cmake/Realm
COMPONENT devel
)

install(FILES tools/cmake/AcquireRealmDependency.cmake
DESTINATION share/cmake/Realm
COMPONENT devel
)

# Only prepare test/install/package targets if we're not a submodule
if(REALM_CORE_SUBMODULE_BUILD)
return()
Expand Down Expand Up @@ -402,39 +425,6 @@ if (REALM_BUILD_TEST_CLIENT)
add_subdirectory(test/client)
endif()

# Make the project importable from the build directory
set(REALM_EXPORTED_TARGETS
Storage
QueryParser
ObjectStore
RealmFFI
RealmFFIStatic
)
if(REALM_ENABLE_SYNC)
list(APPEND REALM_EXPORTED_TARGETS Sync)
endif()
export(TARGETS ${REALM_EXPORTED_TARGETS} NAMESPACE Realm:: FILE RealmTargets.cmake)
configure_file(tools/cmake/RealmConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/RealmConfig.cmake @ONLY)
configure_file(tools/cmake/AcquireRealmDependency.cmake ${CMAKE_CURRENT_BINARY_DIR}/AcquireRealmDependency.cmake @ONLY)

# Make the project importable from the install directory
install(EXPORT realm
NAMESPACE Realm::
FILE RealmTargets.cmake
DESTINATION lib/cmake/Realm
COMPONENT devel
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/RealmConfig.cmake
DESTINATION lib/cmake/Realm
COMPONENT devel
)

install(FILES tools/cmake/AcquireRealmDependency.cmake
DESTINATION lib/cmake/Realm
COMPONENT devel
)

# CPack
set(CPACK_GENERATOR "TGZ")
set(CPACK_PACKAGE_VERSION ${REALM_VERSION})
Expand Down
10 changes: 8 additions & 2 deletions src/realm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,14 @@ endif()

target_link_libraries(Storage INTERFACE Threads::Threads)

if(TARGET Backtrace::Backtrace)
target_link_libraries(Storage PUBLIC Backtrace::Backtrace)
if(REALM_HAVE_BACKTRACE AND NOT CMAKE_GENERATOR STREQUAL Xcode)
# Apple platforms always have backtrace. We disregard the `Backtrace_*` variables
# because their paths are hardcoded to one SDK within Xcode (e.g. macosx),
# whereas we build for several different SDKs and thus we can't use the include path from one in the other.
# Otherwise if CMake found that the backtrace facility is provided by an external library and not built-in
# we need to configure the interface target with the library include and link path.
target_include_directories(Storage PRIVATE ${Backtrace_INCLUDE_DIRS})
target_link_libraries(Storage PUBLIC ${Backtrace_LIBRARIES})
endif()

if(REALM_ENABLE_ENCRYPTION AND UNIX AND NOT APPLE AND REALM_HAVE_OPENSSL)
Expand Down
11 changes: 6 additions & 5 deletions tools/cmake/RealmConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ include("${CMAKE_CURRENT_LIST_DIR}/AcquireRealmDependency.cmake")
include(CMakeFindDependencyMacro)

if(@REALM_HAVE_OPENSSL@)
if(NOT REALM_USE_SYSTEM_OPENSSL AND (ANDROID OR WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux"))
if(NOT @REALM_USE_SYSTEM_OPENSSL@ AND (ANDROID OR WIN32 OR CMAKE_SYSTEM_NAME STREQUAL "Linux"))
# Use our own prebuilt OpenSSL
realm_acquire_dependency(openssl @OPENSSL_VERSION@ OPENSSL_CMAKE_INCLUDE_FILE)

include(${OPENSSL_CMAKE_INCLUDE_FILE})
endif()

set(OPENSSL_USE_STATIC_LIBS ON)
find_dependency(OpenSSL @OPENSSL_VERSION@)
endif()

Expand All @@ -25,8 +23,11 @@ find_dependency(Threads)
# Just use -lz and let Xcode figure it out
if(TARGET Realm::Sync AND NOT APPLE AND NOT TARGET ZLIB::ZLIB)
if(WIN32 OR (CMAKE_SYSTEM_NAME STREQUAL "Linux" AND REALM_LINUX_TOOLCHAIN))
realm_acquire_dependency(zlib @DEP_ZLIB_VERSION@ ZLIB_CMAKE_INCLUDE_FILE)
include(${ZLIB_CMAKE_INCLUDE_FILE})
find_package(ZLIB)
if (NOT ZLIB_FOUND)
realm_acquire_dependency(zlib @DEP_ZLIB_VERSION@ ZLIB_CMAKE_INCLUDE_FILE)
include(${ZLIB_CMAKE_INCLUDE_FILE})
endif()
elseif(ANDROID)
# On Android FindZLIB chooses the static libz over the dynamic one, but this leads to issues
# (see https://github.com/android/ndk/issues/1179)
Expand Down

0 comments on commit 334d534

Please sign in to comment.