OpenDDS Post Release 3.31.0
[OpenDDS.git] / CMakeLists.txt
blobb8df092369ace4166f006737a3fe3a814a3e8a3f
1 cmake_minimum_required(VERSION 3.23...3.27)
2 include("${CMAKE_CURRENT_LIST_DIR}/cmake/opendds_version.cmake")
3 project(opendds
4   DESCRIPTION
5     "OpenDDS is an open-source C++ framework for exchanging data in distributed systems."
6   VERSION "${OPENDDS_VERSION}"
7   HOMEPAGE_URL "https://opendds.org/"
8   LANGUAGES CXX
10 set(CPACK_PACKAGE_CONTACT info@opendds.org)
12 include(CMakePackageConfigHelpers)
13 include(GNUInstallDirs)
14 include(FetchContent)
15 include(CPack)
17 set(OPENDDS_IS_BEING_BUILT TRUE CACHE INTERNAL "")
18 set(OPENDDS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "")
19 if(EXISTS "${OPENDDS_SOURCE_DIR}/dds/DdsDcpsC.h")
20   message(FATAL_ERROR "It looks like OpenDDS was already built here using MPC. This could cause "
21     "issues with a CMake build. Please clean this source tree or use a separate one.")
22 endif()
23 set(OPENDDS_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
24 get_filename_component(_OPENDDS_SOURCE_DIR_REAL "${OPENDDS_SOURCE_DIR}" REALPATH)
25 get_filename_component(_OPENDDS_BUILD_DIR_REAL "${OPENDDS_BUILD_DIR}" REALPATH)
26 if("${_OPENDDS_SOURCE_DIR_REAL}" STREQUAL "${_OPENDDS_BUILD_DIR_REAL}")
27   message(FATAL_ERROR "Please build OpenDDS in a different directory from the source tree "
28     "directory. Use something like \"cmake -S . -B build\".")
29 endif()
30 set(OPENDDS_BIN_DIR "${OPENDDS_BUILD_DIR}/bin" CACHE INTERNAL "")
31 set(OPENDDS_LIB_DIR "${OPENDDS_BUILD_DIR}/lib" CACHE INTERNAL "")
32 list(APPEND CMAKE_PREFIX_PATH "${OPENDDS_SOURCE_DIR}")
33 list(APPEND CMAKE_FIND_ROOT_PATH "${OPENDDS_SOURCE_DIR}")
34 set(_OPENDDS_CMAKE_DIR "${OPENDDS_SOURCE_DIR}/cmake" CACHE INTERNAL "")
36 # Get and build ACE/TAO if OPENDDS_ACE wasn't defined and initialize the core
37 # of the CMake support in init.cmake.
38 if(NOT DEFINED OPENDDS_ACE OR DEFINED ACE_IS_BEING_BUILT)
39   # This is before init.cmake so init.cmake can get the features from ACE if
40   # ACE was already built.
41   include("${_OPENDDS_CMAKE_DIR}/get_ace_tao.cmake")
42 endif()
43 include("${_OPENDDS_CMAKE_DIR}/init.cmake")
44 if(DEFINED ACE_IS_BEING_BUILT)
45   # This is after init.cmake so ACE/TAO can be configured using the features
46   # from init.cmake.
47   include("${_OPENDDS_CMAKE_DIR}/build_ace_tao.cmake")
48 endif()
50 if(NOT DEFINED OPENDDS_RAPIDJSON)
51   set(OPENDDS_RAPIDJSON "${OPENDDS_SOURCE_DIR}/tools/rapidjson")
52   if(NOT EXISTS "${OPENDDS_RAPIDJSON}/include")
53     FetchContent_Declare(rapidjson
54       GIT_REPOSITORY https://github.com/Tencent/rapidjson.git
55       GIT_TAG fd3dc29a5c2852df569e1ea81dbde2c412ac5051
56     )
57     FetchContent_Populate(rapidjson)
58     set(OPENDDS_RAPIDJSON "${rapidjson_SOURCE_DIR}")
59   endif()
60 endif()
62 # Generate OpenDDSConfig.h, make it available for all targets (see
63 # opendds_build_helpers.cmake), and install it.
64 set(OPENDDS_CONFIG_AUTO_STATIC_INCLUDES 1) # Always true for CMake builds
65 set(config_file "dds/OpenDDSConfig.h")
66 set(config_out_dir "${CMAKE_CURRENT_BINARY_DIR}/opendds_config_include")
67 set(OPENDDS_CONFIG_INCLUDE_DIR "${config_out_dir}" CACHE INTERNAL "")
68 set(config_out_file "${config_out_dir}/${config_file}")
69 configure_file("${config_file}.in" "${config_out_file}")
70 add_library(OpenDDS_Config INTERFACE)
71 target_include_directories(OpenDDS_Config
72   INTERFACE
73     "$<BUILD_INTERFACE:${config_out_dir}>"
74     "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
76 target_sources(OpenDDS_Config
77   INTERFACE FILE_SET HEADERS BASE_DIRS "${config_out_dir}" FILES "${config_out_file}")
78 install(TARGETS OpenDDS_Config
79   EXPORT opendds_targets
80   FILE_SET HEADERS
81     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
84 if(NOT CMAKE_CROSSCOMPILING)
85   add_subdirectory(dds/DCPS) # OpenDDS_Util
86   add_subdirectory(dds/idl) # opendds_idl
87   if(OPENDDS_JUST_BUILD_HOST_TOOLS)
88     return()
89   endif()
90 endif()
92 add_subdirectory(dds) # OpenDDS_Dcps
93 add_subdirectory(dds/DCPS/transport/multicast)
94 add_subdirectory(dds/DCPS/RTPS)
95 add_subdirectory(dds/DCPS/transport/rtps_udp)
96 add_subdirectory(dds/DCPS/transport/shmem)
97 add_subdirectory(dds/DCPS/transport/tcp)
98 add_subdirectory(dds/DCPS/InfoRepoDiscovery)
99 add_subdirectory(dds/DCPS/transport/udp)
100 add_subdirectory(dds/InfoRepo)
101 if(OPENDDS_XERCES3)
102   add_subdirectory(dds/DCPS/QOS_XML_Handler)
103 endif()
104 if(OPENDDS_SECURITY)
105   add_subdirectory(dds/DCPS/security)
106   if(OPENDDS_BUILT_IN_TOPICS AND NOT OPENDDS_CXX_STD_YEAR LESS 2011)
107     add_subdirectory(tools/dds/rtpsrelaylib)
108     add_subdirectory(tools/rtpsrelay)
109   endif()
110 endif()
111 add_subdirectory(tools/inspect)
113 # Installation
114 set(cmake_dest "${CMAKE_INSTALL_DATAROOTDIR}/cmake/OpenDDS")
115 install(EXPORT opendds_targets
116   FILE "opendds_targets.cmake"
117   DESTINATION ${cmake_dest}
119 set(config_cmake "${CMAKE_CURRENT_BINARY_DIR}/config.cmake")
120 set(opendds_feature_vars)
121 foreach(var ${_OPENDDS_FEATURE_VARS})
122   set(opendds_feature_vars "${opendds_feature_vars}set(${var} ${${var}})\n")
123 endforeach()
124 configure_file("${_OPENDDS_CMAKE_DIR}/config.cmake.in" "${config_cmake}")
125 install(
126   FILES
127     cmake/OpenDDSConfig.cmake
128     cmake/OpenDDSConfigVersion.cmake
129     cmake/ace_group.cmake
130     cmake/dds_idl_sources.cmake
131     cmake/export.h.in
132     cmake/import_common.cmake
133     cmake/init.cmake
134     cmake/opendds_group.cmake
135     cmake/opendds_target_sources.cmake
136     cmake/opendds_testing.cmake
137     cmake/opendds_version.cmake
138     cmake/tao_group.cmake
139     cmake/tao_idl_sources.cmake
140     cmake/test_cxx_std.cpp
141     "${config_cmake}"
142   DESTINATION "${cmake_dest}"
144 install(FILES VERSION.txt DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/dds/")
145 if(OPENDDS_RAPIDJSON)
146   install(DIRECTORY "${OPENDDS_RAPIDJSON}/include/rapidjson"
147     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
148 endif()
150 # Examples and Tests
151 if(BUILD_TESTING)
152   set(_OPENDDS_BUILD_TESTS_DEFAULT TRUE)
153 else()
154   set(_OPENDDS_BUILD_TESTS_DEFAULT FALSE)
155 endif()
156 set(OPENDDS_BUILD_TESTS ${_OPENDDS_BUILD_TESTS_DEFAULT} CACHE BOOL "Build OpenDDS Tests")
157 set(OPENDDS_BUILD_EXAMPLES TRUE CACHE BOOL "Build OpenDDS Examples")
158 if(OPENDDS_BUILD_EXAMPLES OR OPENDDS_BUILD_TESTS)
159   enable_testing()
160   add_subdirectory(DevGuideExamples/DCPS/Messenger)
161 endif()
162 if(OPENDDS_BUILD_TESTS)
163   add_subdirectory(tests)
164 endif()