Skip to content

Commit

Permalink
Fix CMake iShapes and Comply with CMP0165
Browse files Browse the repository at this point in the history
Fixes OpenDDS#4849

Changes from OpenDDS#4487 require C++
language to be enabled in CMake, which it basically is all the time
through `project(... LANGUAGES CXX)` expect in ishapes. There
`find_project(OpenDDS)` was used to get the OpenDDS version before
`project` enabled C++ which results in `try_compile` failing fatally.
These changes both make sure C and C++ are enabled early in CMake
initialization and switch the `project` and `find_package` to
comply with
https://cmake.org/cmake/help/latest/policy/CMP0165.html#policy:CMP0165

Also changed the C++ standard checks to include the min C++ standard
(currently C++98/03), so it would never mistake a broken compiler setup
with a compiler that only supports the min standard.
  • Loading branch information
iguessthislldo committed Feb 7, 2025
1 parent 3d6572d commit 2618c28
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
1 change: 0 additions & 1 deletion cmake/ace_group.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ if(MSVC AND OPENDDS_STATIC)
_opendds_vs_force_static()
endif()

enable_language(C)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

Expand Down
28 changes: 21 additions & 7 deletions cmake/init.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ include(CMakeParseArguments)

include("${CMAKE_CURRENT_LIST_DIR}/opendds_version.cmake")

enable_language(C CXX)

function(_opendds_detect_ace)
if(OPENDDS_CMAKE_VERBOSE)
set(path "${OPENDDS_ACE}/bin/MakeProjectCreator/config/default.features")
Expand Down Expand Up @@ -169,20 +171,33 @@ function(_opendds_cxx_std_from_year out_var year)
set(${out_var} ${std} PARENT_SCOPE)
endfunction()

function(_opendds_try_compile_cplusplus compiled_var temp_dir test_cxx_std cplusplus)
try_compile(compiled
"${temp_dir}/cplusplus_${cplusplus}"
SOURCES "${test_cxx_std}"
COMPILE_DEFINITIONS "-DOPENDDS_TEST_CPLUSPLUS=${cplusplus}L"
)
set(${compiled_var} "${compiled}" PARENT_SCOPE)
endfunction()

function(_opendds_set_cxx_std)
set(default_cxx_std_year 1998)
set(cplusplus_values 201103 201402 201703 202002 202302)
set(compiler_info "compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")

set(test_cxx_std "${CMAKE_CURRENT_LIST_DIR}/test_cxx_std.cpp")
set(temp_dir "${CMAKE_CURRENT_BINARY_DIR}/opendds_test_cxx_std")
file(MAKE_DIRECTORY "${temp_dir}")

# Make sure try_compile works at all
_opendds_try_compile_cplusplus(compiled "${temp_dir}" "${test_cxx_std}" ${default_cxx_std_year})
if(NOT compiled)
message(FATAL_ERROR "try_compile using ${compiler_info} failed for ${default_cxx_std_year}")
endif()

# Get the latest known default compiler C++ standard
set(default_cxx_std_year 1998)
foreach(cplusplus IN LISTS cplusplus_values)
try_compile(compiled
"${temp_dir}/cplusplus_${cplusplus}"
SOURCES "${test_cxx_std}"
COMPILE_DEFINITIONS "-DOPENDDS_TEST_CPLUSPLUS=${cplusplus}L"
)
_opendds_try_compile_cplusplus(compiled "${temp_dir}" "${test_cxx_std}" ${cplusplus})
if(compiled)
_opendds_cplusplus_to_year(default_cxx_std_year ${cplusplus})
else()
Expand Down Expand Up @@ -218,7 +233,6 @@ function(_opendds_set_cxx_std)

# Get the C++ standard ACE requires
set(ace_info "ACE ${OPENDDS_ACE_VERSION}")
set(compiler_info "compiler ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
set(existing_cxx11 FALSE)
if(OPENDDS_CXX11 OR (DEFINED _opendds_default_features_no_cxx11 AND
_opendds_default_features_no_cxx11 STREQUAL "0"))
Expand Down
1 change: 1 addition & 0 deletions cmake/opendds_version.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ if(NOT DEFINED OPENDDS_VERSION)
"${_OPENDDS_CMAKE_DIR}/../../dds"
"${_OPENDDS_CMAKE_DIR}/.."
)
set(OpenDDS_VERSION "${OPENDDS_VERSION}")
endif()
3 changes: 2 additions & 1 deletion examples/DCPS/ishapes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.3...3.27)
find_package(OpenDDS REQUIRED)
include("${CMAKE_CURRENT_LIST_DIR}/../../../cmake/opendds_version.cmake")
project(ishapes VERSION ${OpenDDS_VERSION} LANGUAGES CXX)
find_package(OpenDDS REQUIRED)

# Make sure the MPC-generated headers are gone so the CMake build will use the
# right ones. This is not needed in a real project.
Expand Down

0 comments on commit 2618c28

Please sign in to comment.