diff options
author | Brad King <brad.king@kitware.com> | 2024-05-16 13:16:48 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2024-05-16 15:46:58 (GMT) |
commit | ae1d5ef47fd4175a31f8bbfc8957cd6cb26d5e75 (patch) | |
tree | c8e006a8eb90ad4473b5239f764889573eb53964 /Modules/CMakeCommonLanguageInclude.cmake | |
parent | 3f28a819ce00dfd2b26f863ce7e761a68b95726a (diff) | |
download | CMake-ae1d5ef47fd4175a31f8bbfc8957cd6cb26d5e75.zip CMake-ae1d5ef47fd4175a31f8bbfc8957cd6cb26d5e75.tar.gz CMake-ae1d5ef47fd4175a31f8bbfc8957cd6cb26d5e75.tar.bz2 |
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
Diffstat (limited to 'Modules/CMakeCommonLanguageInclude.cmake')
-rw-r--r-- | Modules/CMakeCommonLanguageInclude.cmake | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/Modules/CMakeCommonLanguageInclude.cmake b/Modules/CMakeCommonLanguageInclude.cmake index b043e18..0cd144e 100644 --- a/Modules/CMakeCommonLanguageInclude.cmake +++ b/Modules/CMakeCommonLanguageInclude.cmake @@ -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() |