Modules: Initialize per-language platform-specific flag variables consistently

The `Platform/<OS>.cmake` modules record platform-specific flag
variables for the C toolchain.  The `CMake<LANG>Information` modules
initialize the per-language platform-specific flag variables to defaults
copied from C.  However, they have diverged over time, and not all
platform-specific flag variables are initialized correctly for all
languages.  Factor out the CXX language's instance of this logic into a
helper macro, and reuse it for all languages for which we previously
had the platform-specific flag variables at least partially filled out.

Previously the Fortran language's copy of this logic used
`if(NOT DEFINED <var>)` instead of just `if(NOT <var>)` to allow
`Platform/<OS>-<COMPILER_ID>-<LANG>` modules to specify empty values for
some platform-specific flag variables without being overridden.
Use this approach in the helper macro so it applies to all languages.

Fixes: #25990
This commit is contained in:
Brad King
2024-05-16 09:16:48 -04:00
parent 3f28a819ce
commit ae1d5ef47f
8 changed files with 116 additions and 475 deletions
+109
View File
@@ -21,3 +21,112 @@ set(CMAKE_BUILD_TOOL ${CMAKE_MAKE_PROGRAM})
mark_as_advanced(
CMAKE_VERBOSE_MAKEFILE
)
# The Platform/* modules set a bunch of platform-specific flags expressed
# for the C toolchain. Other languages call this to copy them as defaults.
macro(_cmake_common_language_platform_flags lang)
if(NOT DEFINED CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS)
set(CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS})
endif()
if(NOT DEFINED CMAKE_${lang}_COMPILE_OPTIONS_PIC)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIC ${CMAKE_C_COMPILE_OPTIONS_PIC})
endif()
if(NOT DEFINED CMAKE_${lang}_COMPILE_OPTIONS_PIE)
set(CMAKE_${lang}_COMPILE_OPTIONS_PIE ${CMAKE_C_COMPILE_OPTIONS_PIE})
endif()
if(NOT DEFINED CMAKE_${lang}_LINK_OPTIONS_PIE)
set(CMAKE_${lang}_LINK_OPTIONS_PIE ${CMAKE_C_LINK_OPTIONS_PIE})
endif()
if(NOT DEFINED CMAKE_${lang}_LINK_OPTIONS_NO_PIE)
set(CMAKE_${lang}_LINK_OPTIONS_NO_PIE ${CMAKE_C_LINK_OPTIONS_NO_PIE})
endif()
if(NOT DEFINED CMAKE_${lang}_COMPILE_OPTIONS_DLL)
set(CMAKE_${lang}_COMPILE_OPTIONS_DLL ${CMAKE_C_COMPILE_OPTIONS_DLL})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_${lang}_FLAGS)
set(CMAKE_SHARED_LIBRARY_${lang}_FLAGS ${CMAKE_SHARED_LIBRARY_C_FLAGS})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_${lang}_FLAGS)
set(CMAKE_SHARED_LIBRARY_LINK_${lang}_FLAGS ${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG)
set(CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG_SEP)
set(CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_RPATH_LINK_${lang}_FLAG)
set(CMAKE_SHARED_LIBRARY_RPATH_LINK_${lang}_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG})
endif()
if(NOT DEFINED CMAKE_EXE_EXPORTS_${lang}_FLAG)
set(CMAKE_EXE_EXPORTS_${lang}_FLAG ${CMAKE_EXE_EXPORTS_C_FLAG})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_SONAME_${lang}_FLAG)
set(CMAKE_SHARED_LIBRARY_SONAME_${lang}_FLAG ${CMAKE_SHARED_LIBRARY_SONAME_C_FLAG})
endif()
if(NOT DEFINED CMAKE_EXECUTABLE_RUNTIME_${lang}_FLAG)
set(CMAKE_EXECUTABLE_RUNTIME_${lang}_FLAG ${CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG})
endif()
if(NOT DEFINED CMAKE_EXECUTABLE_RUNTIME_${lang}_FLAG_SEP)
set(CMAKE_EXECUTABLE_RUNTIME_${lang}_FLAG_SEP ${CMAKE_SHARED_LIBRARY_RUNTIME_${lang}_FLAG_SEP})
endif()
if(NOT DEFINED CMAKE_EXECUTABLE_RPATH_LINK_${lang}_FLAG)
set(CMAKE_EXECUTABLE_RPATH_LINK_${lang}_FLAG ${CMAKE_SHARED_LIBRARY_RPATH_LINK_${lang}_FLAG})
endif()
if(NOT DEFINED CMAKE_SHARED_LIBRARY_LINK_${lang}_WITH_RUNTIME_PATH)
set(CMAKE_SHARED_LIBRARY_LINK_${lang}_WITH_RUNTIME_PATH ${CMAKE_SHARED_LIBRARY_LINK_C_WITH_RUNTIME_PATH})
endif()
if(NOT DEFINED CMAKE_INCLUDE_FLAG_${lang})
set(CMAKE_INCLUDE_FLAG_${lang} ${CMAKE_INCLUDE_FLAG_C})
endif()
# for most systems a module is the same as a shared library
# so unless the variable CMAKE_MODULE_EXISTS is set just
# copy the values from the LIBRARY variables
if(NOT CMAKE_MODULE_EXISTS)
set(CMAKE_SHARED_MODULE_${lang}_FLAGS ${CMAKE_SHARED_LIBRARY_${lang}_FLAGS})
set(CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS})
endif()
if(NOT DEFINED CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS)
set(CMAKE_SHARED_MODULE_CREATE_${lang}_FLAGS ${CMAKE_SHARED_MODULE_CREATE_C_FLAGS})
endif()
if(NOT DEFINED CMAKE_SHARED_MODULE_${lang}_FLAGS)
set(CMAKE_SHARED_MODULE_${lang}_FLAGS ${CMAKE_SHARED_MODULE_C_FLAGS})
endif()
foreach(type IN ITEMS SHARED_LIBRARY SHARED_MODULE EXE)
if(NOT DEFINED CMAKE_${type}_LINK_STATIC_${lang}_FLAGS)
set(CMAKE_${type}_LINK_STATIC_${lang}_FLAGS
${CMAKE_${type}_LINK_STATIC_C_FLAGS})
endif()
if(NOT DEFINED CMAKE_${type}_LINK_DYNAMIC_${lang}_FLAGS)
set(CMAKE_${type}_LINK_DYNAMIC_${lang}_FLAGS
${CMAKE_${type}_LINK_DYNAMIC_C_FLAGS})
endif()
endforeach()
if(CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF")
if(NOT DEFINED CMAKE_${lang}_LINK_WHAT_YOU_USE_FLAG)
set(CMAKE_${lang}_LINK_WHAT_YOU_USE_FLAG "LINKER:--no-as-needed")
endif()
if(NOT DEFINED CMAKE_LINK_WHAT_YOU_USE_CHECK)
set(CMAKE_LINK_WHAT_YOU_USE_CHECK ldd -u -r)
endif()
endif()
endmacro()