cmake/Modules/CMakeDetermineCompilerABI.cmake

202 lines
8.3 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2009-10-04 10:30:41 +03:00
# Function to compile a source file to identify the compiler ABI.
# This is used internally by CMake and should not be included by user
# code.
2019-11-11 23:01:05 +01:00
include(${CMAKE_ROOT}/Modules/CMakeParseImplicitIncludeInfo.cmake)
2013-03-16 19:13:01 +02:00
include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
2021-09-14 00:13:48 +02:00
include(${CMAKE_ROOT}/Modules/CMakeParseLibraryArchitecture.cmake)
2019-11-11 23:01:05 +01:00
include(CMakeTestCompilerCommon)
2009-10-04 10:30:41 +03:00
2013-03-16 19:13:01 +02:00
function(CMAKE_DETERMINE_COMPILER_ABI lang src)
if(NOT DEFINED CMAKE_${lang}_ABI_COMPILED)
2020-08-30 11:54:41 +02:00
message(CHECK_START "Detecting ${lang} compiler ABI info")
# Compile the ABI identification source.
2013-03-16 19:13:01 +02:00
set(BIN "${CMAKE_PLATFORM_INFO_DIR}/CMakeDetermineCompilerABI_${lang}.bin")
set(CMAKE_FLAGS )
2019-11-11 23:01:05 +01:00
set(COMPILE_DEFINITIONS )
2013-03-16 19:13:01 +02:00
if(DEFINED CMAKE_${lang}_VERBOSE_FLAG)
2015-04-27 22:25:09 +02:00
set(CMAKE_FLAGS "-DEXE_LINKER_FLAGS=${CMAKE_${lang}_VERBOSE_FLAG}")
2019-11-11 23:01:05 +01:00
set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_FLAG}")
endif()
if(DEFINED CMAKE_${lang}_VERBOSE_COMPILE_FLAG)
set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_COMPILE_FLAG}")
2015-04-27 22:25:09 +02:00
endif()
2022-08-04 22:12:04 +02:00
if(lang STREQUAL "CUDA")
if(CMAKE_CUDA_ARCHITECTURES STREQUAL "native")
# We are about to detect the native architectures, so we do
# not yet know them. Use all architectures during detection.
set(CMAKE_CUDA_ARCHITECTURES "all")
endif()
set(CMAKE_CUDA_RUNTIME_LIBRARY "Static")
endif()
2015-04-27 22:25:09 +02:00
if(NOT "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xMSVC")
# Avoid adding our own platform standard libraries for compilers
# from which we might detect implicit link libraries.
list(APPEND CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD_LIBRARIES=")
2013-03-16 19:13:01 +02:00
endif()
2019-11-11 23:01:05 +01:00
__TestCompiler_setTryCompileTargetType()
2020-08-30 11:54:41 +02:00
# Avoid failing ABI detection on warnings.
2023-07-02 19:51:09 +02:00
string(REGEX REPLACE "(^| )-Werror([= ][^-][^ ]*)?( |$)" " " CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS}")
2020-08-30 11:54:41 +02:00
2019-11-11 23:01:05 +01:00
# Save the current LC_ALL, LC_MESSAGES, and LANG environment variables
# and set them to "C" that way GCC's "search starts here" text is in
# English and we can grok it.
set(_orig_lc_all $ENV{LC_ALL})
set(_orig_lc_messages $ENV{LC_MESSAGES})
set(_orig_lang $ENV{LANG})
set(ENV{LC_ALL} C)
set(ENV{LC_MESSAGES} C)
set(ENV{LANG} C)
2013-03-16 19:13:01 +02:00
try_compile(CMAKE_${lang}_ABI_COMPILED
2022-11-16 20:14:03 +01:00
SOURCES ${src}
2015-04-27 22:25:09 +02:00
CMAKE_FLAGS ${CMAKE_FLAGS}
# Ignore unused flags when we are just determining the ABI.
2011-02-07 16:37:25 +01:00
"--no-warn-unused-cli"
2019-11-11 23:01:05 +01:00
COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS}
OUTPUT_VARIABLE OUTPUT
COPY_FILE "${BIN}"
2013-11-03 12:27:13 +02:00
COPY_FILE_ERROR _copy_error
2019-11-11 23:01:05 +01:00
__CMAKE_INTERNAL ABI
)
2019-11-11 23:01:05 +01:00
# Restore original LC_ALL, LC_MESSAGES, and LANG
set(ENV{LC_ALL} ${_orig_lc_all})
set(ENV{LC_MESSAGES} ${_orig_lc_messages})
set(ENV{LANG} ${_orig_lang})
2013-03-16 19:13:01 +02:00
# Move result from cache to normal variable.
set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED})
unset(CMAKE_${lang}_ABI_COMPILED CACHE)
2020-08-30 11:54:41 +02:00
if(CMAKE_${lang}_ABI_COMPILED AND _copy_error)
set(CMAKE_${lang}_ABI_COMPILED 0)
endif()
2013-03-16 19:13:01 +02:00
set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED} PARENT_SCOPE)
# Load the resulting information strings.
2020-08-30 11:54:41 +02:00
if(CMAKE_${lang}_ABI_COMPILED)
message(CHECK_PASS "done")
2021-09-14 00:13:48 +02:00
file(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 32 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
set(ABI_SIZEOF_DPTR "NOTFOUND")
set(ABI_BYTE_ORDER "NOTFOUND")
set(ABI_NAME "NOTFOUND")
2013-03-16 19:13:01 +02:00
foreach(info ${ABI_STRINGS})
2021-09-14 00:13:48 +02:00
if("${info}" MATCHES "INFO:sizeof_dptr\\[0*([^]]*)\\]" AND NOT ABI_SIZEOF_DPTR)
2015-04-27 22:25:09 +02:00
set(ABI_SIZEOF_DPTR "${CMAKE_MATCH_1}")
2013-03-16 19:13:01 +02:00
endif()
2021-09-14 00:13:48 +02:00
if("${info}" MATCHES "INFO:byte_order\\[(BIG_ENDIAN|LITTLE_ENDIAN)\\]")
set(byte_order "${CMAKE_MATCH_1}")
if(ABI_BYTE_ORDER STREQUAL "NOTFOUND")
# Tentatively use the value because this is the first occurrence.
set(ABI_BYTE_ORDER "${byte_order}")
elseif(NOT ABI_BYTE_ORDER STREQUAL "${byte_order}")
# Drop value because multiple occurrences do not match.
set(ABI_BYTE_ORDER "")
endif()
endif()
if("${info}" MATCHES "INFO:abi\\[([^]]*)\\]" AND NOT ABI_NAME)
2015-04-27 22:25:09 +02:00
set(ABI_NAME "${CMAKE_MATCH_1}")
2013-03-16 19:13:01 +02:00
endif()
endforeach()
2013-03-16 19:13:01 +02:00
if(ABI_SIZEOF_DPTR)
set(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
elseif(CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT)
set(CMAKE_${lang}_SIZEOF_DATA_PTR "${CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT}" PARENT_SCOPE)
endif()
2021-09-14 00:13:48 +02:00
if(ABI_BYTE_ORDER)
set(CMAKE_${lang}_BYTE_ORDER "${ABI_BYTE_ORDER}" PARENT_SCOPE)
endif()
2013-03-16 19:13:01 +02:00
if(ABI_NAME)
set(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
endif()
2019-11-11 23:01:05 +01:00
# Parse implicit include directory for this language, if available.
if(CMAKE_${lang}_VERBOSE_FLAG)
set (implicit_incdirs "")
cmake_parse_implicit_include_info("${OUTPUT}" "${lang}"
implicit_incdirs log rv)
2023-05-23 16:38:00 +02:00
message(CONFIGURE_LOG
"Parsed ${lang} implicit include dir info: rv=${rv}\n${log}\n\n")
2019-11-11 23:01:05 +01:00
if("${rv}" STREQUAL "done")
# Entries that we have been told to explicitly pass as standard include
# directories will not be implicitly added by the compiler.
if(CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES)
list(REMOVE_ITEM implicit_incdirs ${CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES})
endif()
# We parsed implicit include directories, so override the default initializer.
set(_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT "${implicit_incdirs}")
endif()
endif()
set(CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES "${_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT}" PARENT_SCOPE)
2009-10-04 10:30:41 +03:00
# Parse implicit linker information for this language, if available.
2013-03-16 19:13:01 +02:00
set(implicit_dirs "")
2021-09-14 00:13:48 +02:00
set(implicit_objs "")
2013-03-16 19:13:01 +02:00
set(implicit_libs "")
set(implicit_fwks "")
if(CMAKE_${lang}_VERBOSE_FLAG)
CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs implicit_fwks log
2021-09-14 00:13:48 +02:00
"${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}"
2022-11-16 20:14:03 +01:00
COMPUTE_IMPLICIT_OBJECTS implicit_objs
LANGUAGE ${lang})
2023-05-23 16:38:00 +02:00
message(CONFIGURE_LOG
"Parsed ${lang} implicit link information:\n${log}\n\n")
2013-03-16 19:13:01 +02:00
endif()
2012-02-18 12:40:36 +02:00
# for VS IDE Intel Fortran we have to figure out the
# implicit link path for the fortran run time using
# a try-compile
2013-03-16 19:13:01 +02:00
if("${lang}" MATCHES "Fortran"
2012-02-18 12:40:36 +02:00
AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
2020-08-30 11:54:41 +02:00
message(CHECK_START "Determine Intel Fortran Compiler Implicit Link Path")
2012-02-18 12:40:36 +02:00
# Build a sample project which reports symbols.
2013-03-16 19:13:01 +02:00
try_compile(IFORT_LIB_PATH_COMPILED
2022-11-16 20:14:03 +01:00
PROJECT IntelFortranImplicit
SOURCE_DIR ${CMAKE_ROOT}/Modules/IntelVSImplicitPath
BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath
2012-02-18 12:40:36 +02:00
CMAKE_FLAGS
"-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
OUTPUT_VARIABLE _output)
2013-03-16 19:13:01 +02:00
file(WRITE
2012-02-18 12:40:36 +02:00
"${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.txt"
"${_output}")
2013-03-16 19:13:01 +02:00
include(${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.cmake OPTIONAL)
2020-08-30 11:54:41 +02:00
message(CHECK_PASS "done")
2013-03-16 19:13:01 +02:00
endif()
2012-02-18 12:40:36 +02:00
2013-03-16 19:13:01 +02:00
# Implicit link libraries cannot be used explicitly for multiple
# OS X architectures, so we skip it.
if(DEFINED CMAKE_OSX_ARCHITECTURES)
if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
set(implicit_libs "")
endif()
endif()
2023-07-02 19:51:09 +02:00
if(DEFINED ENV{CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
list(REMOVE_ITEM implicit_dirs $ENV{CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
endif()
2013-03-16 19:13:01 +02:00
set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
2009-10-04 10:30:41 +03:00
2021-09-14 00:13:48 +02:00
cmake_parse_library_architecture(${lang} "${implicit_dirs}" "${implicit_objs}" architecture_flag)
if(architecture_flag)
set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${architecture_flag}" PARENT_SCOPE)
2013-03-16 19:13:01 +02:00
endif()
2011-06-19 15:41:06 +03:00
2013-03-16 19:13:01 +02:00
else()
2020-08-30 11:54:41 +02:00
message(CHECK_FAIL "failed")
2013-03-16 19:13:01 +02:00
endif()
endif()
endfunction()