Skip to content
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ endif()
#
# It appears that on macOS, all code is position independent and it'll work regardless.
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
if(BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
if(NOT BUILD_SHARED_LIBS AND NOT CMAKE_CROSSCOMPILING)
message(WARNING "It is probably best to build a static library as-if cross compiling (e.g., use -DCMAKE_CROSSCOMPILING=1 -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} -DCMAKE_PREFIX_PATH=<path to shared-library build>)")
endif()
Expand All @@ -57,7 +60,8 @@ endif()
# small mistake would run into errors. CI builds can be configured differently.
option(WERROR "Treat compiler warnings as errors" OFF)

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")
# Update CMake module path variable with our own for this project
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")

option(BUILD_IDLC "Build IDL preprocessor" ${not_crosscompiling})
option(BUILD_DDSPERF "Build ddsperf tool" ${not_crosscompiling})
Expand Down
42 changes: 0 additions & 42 deletions cmake/Modules/FindOpenSSL.cmake

This file was deleted.

6 changes: 3 additions & 3 deletions cmake/Modules/Generate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function(IDLC_GENERATE)
message(STATUS "Building internal IDLC backend")
set(_idlc_generate_skipreport 1 CACHE INTERNAL "")
endif()
set(_idlc_depends CycloneDDS::libidlc)
# set(_idlc_depends CycloneDDS::libidlc)
else()
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" AND NOT ".so" IN_LIST CMAKE_FIND_LIBRARY_SUFFIXES)
# When cross-compiling, find_library looks for libraries using naming conventions of the target,
Expand Down Expand Up @@ -84,10 +84,10 @@ function(IDLC_GENERATE_GENERIC)
# By using the internal target when building CycloneDDS itself, prevent using an external idlc
# This prevents a problem when an installed cyclone is on your prefix path when building cyclone again
set(_idlc_executable idlc)
set(_idlc_depends idlc)
# set(_idlc_depends idlc)
else()
set(_idlc_executable CycloneDDS::idlc)
set(_idlc_depends CycloneDDS::idlc)
# set(_idlc_depends CycloneDDS::idlc)
endif()
else()
find_program(_idlc_executable "idlc" NO_CMAKE_FIND_ROOT_PATH REQUIRED)
Expand Down
7 changes: 4 additions & 3 deletions compat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ include(CheckIncludeFile)

check_include_file(getopt.h HAVE_GETOPT_H)
if(WIN32 OR NOT HAVE_GETOPT_H)
add_library(compat OBJECT)
target_include_directories(compat INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
target_sources(compat PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/getopt.c")
add_library(compat OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/getopt.c" "${CMAKE_CURRENT_SOURCE_DIR}/getopt.h")
target_include_directories(compat PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
else()
add_library(compat INTERFACE)
endif()

add_library(${PROJECT_NAME}::compat ALIAS compat)
42 changes: 30 additions & 12 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,46 @@ endif()
# currently relying on it.
set(ENABLE_SSL "AUTO" CACHE STRING "Enable OpenSSL support")
set_property(CACHE ENABLE_SSL PROPERTY STRINGS ON OFF AUTO)
if(ENABLE_SSL)
if(NOT ENABLE_SSL STREQUAL "AUTO")
find_package(OpenSSL REQUIRED)
if(ENABLE_SSL AND NOT TARGET OpenSSL::SSL) # Search for OpenSSL only if is not already a target

if(TARGET CONAN_PKG::openssl)
add_library(OpenSSL::SSL INTERFACE IMPORTED)
target_link_libraries(OpenSSL::SSL INTERFACE CONAN_PKG::openssl)
set(OPENSSL_FOUND ON) # Manually set it
else()
find_package(OpenSSL QUIET)
if(OPENSSL_FOUND)
message(STATUS "Building with OpenSSL support")
set(ENABLE_SSL "ON")
if(NOT BUILD_SHARED_LIBS)
set(OPENSSL_USE_STATIC_LIBS ON) # Require OpenSSL static if is installed on the system
endif()

include(FindOpenSSL) # Use CMake official module
if(NOT ENABLE_SSL STREQUAL "AUTO")
find_package(OpenSSL REQUIRED)
else()
message(STATUS "Building without OpenSSL support")
set(ENABLE_SSL "OFF")
find_package(OpenSSL QUIET)
endif()
endif()

if(OPENSSL_FOUND)
# OpenSSL DLL on Windows: use of BIO_s_fd and BIO_s_file (directly or indirectly) requires
# the executable to incorporate OpenSSL applink.c. CMake 18 adds support for handling
# this as part of the OpenSSL package, but we can't require such a new CMake version.
if(EXISTS "${OPENSSL_INCLUDE_DIR}/openssl/applink.c")
set(CYCLONEDDS_OPENSSL_APPLINK "${OPENSSL_INCLUDE_DIR}/openssl/applink.c")
endif()
message(STATUS "Building with OpenSSL support")
else()
message(STATUS "Building without OpenSSL support")
endif()
set(ENABLE_SSL "${OPENSSL_FOUND}")
endif()

set(ENABLE_TCP_TLS "AUTO" CACHE STRING "Enable TCP+TLS support (depends on ENABLE_SSL)")
set_property(CACHE ENABLE_TCP_TLS PROPERTY STRINGS ON OFF AUTO)
if(ENABLE_TCP_TLS)
if(ENABLE_TCP_TLS) # ENABLE_TCP_TLS = AUTO | ON
if(ENABLE_TCP_TLS STREQUAL "AUTO")
set(ENABLE_TCP_TLS "${ENABLE_SSL}")
elseif(ENABLE_TCP_TLS AND NOT ENABLE_SSL)
message(FATAL "ENABLE_TCP_TLS requires ENABLE_SSL")
elseif(NOT ENABLE_SSL) # ENABLE_TCP_TLS=ON and ENABLE_SSL=OFF
message(FATAL_ERROR "ENABLE_TCP_TLS requires ENABLE_SSL")
endif()
endif()

Expand Down
28 changes: 19 additions & 9 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
target_link_libraries(ddsc PRIVATE dbghelp)
endif()

if(ENABLE_TCP_TLS AND OPENSSL_FOUND)
if(ENABLE_TCP_TLS AND ENABLE_SSL)
target_link_libraries(ddsc PRIVATE OpenSSL::SSL)
if(CMAKE_GENERATOR MATCHES "Visual Studio")
set_target_properties(ddsc PROPERTIES LINK_FLAGS "/ignore:4099")
Expand Down Expand Up @@ -58,13 +58,16 @@ if(ENABLE_SECURITY)
$<BUILD_INTERFACE:$<TARGET_PROPERTY:security_api,INTERFACE_INCLUDE_DIRECTORIES>>)
endif()

include(cdr/CMakeLists.txt)
include(ddsi/CMakeLists.txt)
include(ddsc/CMakeLists.txt)
add_subdirectory(cdr)
add_subdirectory(ddsi)
add_subdirectory(ddsc)

# The not-so-elegant inclusion of all configured plug-ins for static builds. At least it
# keeps most of the dirty things in one place.
if(NOT BUILD_SHARED_LIBS)
# Full list should be: dds_security_access_control_wrapper; dds_security_authentication_wrapper;
# dds_security_cryptography_wrapper; dds_security_ac; dds_security_auth;
# dds_security_crypto; psmx_cdds; psmx_dummy; psmx_dummy_v0
get_property(plugin_list GLOBAL PROPERTY cdds_plugin_list)
if(plugin_list)
list(JOIN plugin_list "," plugin_commasep)
Expand All @@ -77,13 +80,16 @@ if(NOT BUILD_SHARED_LIBS)
target_compile_options(ddsc PRIVATE "-DPLUGIN_SYMBOLS_${plugin}=${plugin_symbols_commasep}")
endforeach()
endif()
if(ENABLE_SSL)
target_link_libraries(ddsc PUBLIC security_openssl)
endif()
if(ENABLE_SSL)
target_link_libraries(ddsc PUBLIC ${PROJECT_NAME}::security_openssl)
endif()
endif()

add_coverage(ddsc)
target_link_libraries(ddsc PRIVATE "$<BUILD_INTERFACE:ddsrt>")
target_link_libraries(ddsc
PUBLIC $<$<NOT:$<STREQUAL:"${CMAKE_PROJECT_NAME}","CycloneDDS">>:${PROJECT_NAME}::cdr>
PRIVATE $<BUILD_INTERFACE:${PROJECT_NAME}::ddsrt>
)
target_compile_definitions(
ddsc PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:ddsrt,INTERFACE_COMPILE_DEFINITIONS>>)
Expand All @@ -92,7 +98,11 @@ target_include_directories(
$<BUILD_INTERFACE:$<TARGET_PROPERTY:ddsrt,INTERFACE_INCLUDE_DIRECTORIES>>)

# SOVERSION should increase on incompatible ABI change
set_target_properties(ddsc PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
set_target_properties(ddsc PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
POSITION_INDEPENDENT_CODE ON
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this force position independent code always? I remember trying to set it to ON and running into a problem. I think it was the one mentioned here:

# By default we do shared libraries (we really prefer shared libraries because of the

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property will set position indipendent code only for the DDSC target (and, in a future commit, also for CDR). I had to enable it due to relocation issues happening when building CycloneDDS as a static library on linux.
Specifically the errors were:

  • relocation R_X86_64_TPOFF32 for symbol tsd_thread_state from ddsi_thread.c. Fixed by adding POSITION_INDEPENDENT_CODE property on DDSC target
  • relocation R_X86_64_PC32 for symbol ddsrt_hh_empty from dds_cdrstream.c. Fixed by adding POSITION_INDEPENDENT_CODE property on CDR target

Seeing the shared nature of the project and the many relocation issues, I suggest to directly set CMAKE_POSITION_INDEPENDENT_CODE=ON whenever the library is built with BUILD_SHARED_LIBS=ON.
Further tests with this setting on Windows Environment are required though.

)

# define target property to indicate if Cyclone DDS is compiled with type library support
define_property(TARGET
Expand Down
22 changes: 16 additions & 6 deletions src/core/cdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@

if(NOT ${CMAKE_PROJECT_NAME} STREQUAL "CycloneDDS")
cmake_minimum_required(VERSION 3.16)
project(cdr VERSION 0.11.0 LANGUAGES C)
include(CheckLanguage)

# Check if the Compiler in use has C support, otherwise throw error
check_language(C)
if(CMAKE_C_COMPILER)
enable_language(C) # Enables C support
else()
message(FATAL_ERROR "Current compiler does not support C language")
endif()
endif()

set(srcs_cdr
Expand All @@ -39,11 +47,13 @@ else()
add_library(cdr)
add_library(${PROJECT_NAME}::cdr ALIAS cdr)

set_target_properties(cdr PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_sources(cdr PRIVATE ${srcs_cdr} ${hdrs_private_cdr} ${CMAKE_CURRENT_LIST_DIR}/../../ddsrt/src/bswap.c)
target_include_directories(cdr PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include"
"${PROJECT_BINARY_DIR}/include"
"${CMAKE_CURRENT_LIST_DIR}/../../ddsrt/include"
"${CMAKE_CURRENT_LIST_DIR}/../ddsc/include")
target_include_directories(cdr PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../../ddsrt/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../ddsc/include>")

configure_file(${CMAKE_CURRENT_LIST_DIR}/../../ddsrt/include/dds/config.h.in include/dds/config.h)
configure_file(${CMAKE_CURRENT_LIST_DIR}/../../ddsrt/include/dds/features.h.in include/dds/features.h)
Expand All @@ -62,7 +72,7 @@ else()

install(
DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/include/"
"${PROJECT_BINARY_DIR}/include/"
"${CMAKE_CURRENT_BINARY_DIR}/include/"
"${CMAKE_CURRENT_LIST_DIR}/../../ddsrt/include/"
"${CMAKE_CURRENT_LIST_DIR}/../ddsc/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Expand Down
22 changes: 12 additions & 10 deletions src/core/ddsc/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ target_link_libraries(cunit_ddsc PRIVATE
CdrStreamKeyExt
CdrStreamKeyFlags
SerdataData
ddsc
${PROJECT_NAME}::ddsc
)

if(ENABLE_TYPELIB)
Expand Down Expand Up @@ -267,7 +267,7 @@ target_include_directories(
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../ddsi/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../ddsi/src>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../cdr/include>")
target_link_libraries(oneliner PRIVATE RoundTrip Space ddsc)
target_link_libraries(oneliner PRIVATE RoundTrip Space ${PROJECT_NAME}::ddsc)

##############################################################
# PSMX implementation with Cyclone as transport, for testing #
Expand Down Expand Up @@ -297,17 +297,19 @@ if(BUILD_SHARED_LIBS)
add_library(psmx_dummy_v0 SHARED ${psmx_dummy_v0_sources})
else()
list(APPEND psmx_includes
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/src/ddsrt/include>"
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/src/core/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src/ddsrt/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src/core/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../../ddsc/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../../../ddsrt/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../ddsc/include>"
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../../ddsrt/include>")
add_library(psmx_cdds OBJECT ${psmx_cdds_sources} psmx_cdds_data.c)
add_library(psmx_cdds ${psmx_cdds_sources} psmx_cdds_data.c) # psmx_cdds_data.c causes the cyclic dependency
set_property(GLOBAL APPEND PROPERTY cdds_plugin_list psmx_cdds)
set_property(GLOBAL PROPERTY psmx_cdds_symbols cdds_create_psmx)
add_library(psmx_dummy OBJECT ${psmx_dummy_sources})
add_library(psmx_dummy ${psmx_dummy_sources})
set_property(GLOBAL APPEND PROPERTY cdds_plugin_list psmx_dummy)
set_property(GLOBAL PROPERTY psmx_dummy_symbols dummy_create_psmx)
add_library(psmx_dummy_v0 OBJECT ${psmx_dummy_v0_sources})
add_library(psmx_dummy_v0 ${psmx_dummy_v0_sources})
set_property(GLOBAL APPEND PROPERTY cdds_plugin_list psmx_dummy_v0)
set_property(GLOBAL PROPERTY psmx_dummy_v0_symbols dummy_v0_create_psmx)
endif()
Expand All @@ -318,9 +320,9 @@ foreach(plugin psmx_cdds psmx_dummy psmx_dummy_v0)
target_include_directories(${plugin} PRIVATE ${psmx_includes})
endforeach()
if(BUILD_SHARED_LIBS)
target_link_libraries(psmx_cdds PRIVATE ddsc psmx_cdds_data)
target_link_libraries(psmx_dummy PRIVATE ddsc)
target_link_libraries(psmx_dummy_v0 PRIVATE ddsc)
target_link_libraries(psmx_cdds PRIVATE ${PROJECT_NAME}::ddsc psmx_cdds_data)
target_link_libraries(psmx_dummy PRIVATE ${PROJECT_NAME}::ddsc)
target_link_libraries(psmx_dummy_v0 PRIVATE ${PROJECT_NAME}::ddsc)
else()
install(
TARGETS psmx_cdds psmx_dummy psmx_dummy_v0
Expand Down
4 changes: 4 additions & 0 deletions src/core/ddsi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ target_sources(ddsc PRIVATE ${srcs_ddsi} ${generated_defconfig_src} ${hdrs_priva
add_dependencies(ddsc _confgen)
target_include_directories(ddsc PRIVATE "${CMAKE_CURRENT_LIST_DIR}/include")

if(BUILD_TESTING AND DDS_ALLOW_NESTED_DOMAIN)
target_compile_definitions(ddsc PUBLIC DDS_ALLOW_NESTED_DOMAIN)
endif()

install(
DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
Expand Down
6 changes: 4 additions & 2 deletions src/core/xtests/symbol_export/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

add_executable(symbol_export_test symbol_export.c)

target_link_libraries(symbol_export_test PRIVATE ddsc)
target_link_libraries(symbol_export_test PRIVATE
${PROJECT_NAME}::ddsc
)
target_include_directories(symbol_export_test PRIVATE
"$<BUILD_INTERFACE:$<TARGET_PROPERTY:ddsrt,INTERFACE_INCLUDE_DIRECTORIES>>"
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/src/include/>"
Expand All @@ -22,7 +24,7 @@ target_include_directories(symbol_export_test PRIVATE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../cdr/include>")

if (ENABLE_SECURITY)
target_link_libraries(symbol_export_test PRIVATE security_api)
target_link_libraries(symbol_export_test PRIVATE ${PROJECT_NAME}::security_api)
target_include_directories(symbol_export_test PRIVATE
"$<BUILD_INTERFACE:$<TARGET_PROPERTY:security_api,INTERFACE_INCLUDE_DIRECTORIES>>"
"$<BUILD_INTERFACE:$<TARGET_PROPERTY:security_core,INTERFACE_INCLUDE_DIRECTORIES>>")
Expand Down
Loading
Loading