ENH: Implicit link info for C, CXX, and Fortran

This teaches CMake to detect implicit link information for C, C++, and
Fortran compilers.  We detect the implicit linker search directories and
implicit linker options for UNIX-like environments using verbose output
from compiler front-ends.  We store results in new variables called

  CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES
  CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES

The implicit libraries can contain linker flags as well as library
names.
This commit is contained in:
Brad King 2009-07-23 10:07:25 -04:00
parent 797e49a1cc
commit 07ea19ad1f
22 changed files with 137 additions and 1 deletions

View File

@ -34,3 +34,6 @@ ENDIF(CMAKE_C_SIZEOF_DATA_PTR)
IF(CMAKE_C_COMPILER_ABI)
SET(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_C_COMPILER_ABI}")
ENDIF(CMAKE_C_COMPILER_ABI)
SET(CMAKE_C_IMPLICIT_LINK_LIBRARIES "@CMAKE_C_IMPLICIT_LINK_LIBRARIES@")
SET(CMAKE_C_IMPLICIT_LINK_DIRECTORIES "@CMAKE_C_IMPLICIT_LINK_DIRECTORIES@")

View File

@ -34,3 +34,6 @@ ENDIF(CMAKE_CXX_SIZEOF_DATA_PTR)
IF(CMAKE_CXX_COMPILER_ABI)
SET(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}")
ENDIF(CMAKE_CXX_COMPILER_ABI)
SET(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "@CMAKE_CXX_IMPLICIT_LINK_LIBRARIES@")
SET(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "@CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES@")

View File

@ -3,6 +3,8 @@
# This is used internally by CMake and should not be included by user
# code.
INCLUDE(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
FUNCTION(CMAKE_DETERMINE_COMPILER_ABI lang src)
IF(NOT DEFINED CMAKE_DETERMINE_${lang}_ABI_COMPILED)
MESSAGE(STATUS "Detecting ${lang} compiler ABI info")
@ -11,6 +13,8 @@ FUNCTION(CMAKE_DETERMINE_COMPILER_ABI lang src)
SET(BIN "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeDetermineCompilerABI_${lang}.bin")
TRY_COMPILE(CMAKE_DETERMINE_${lang}_ABI_COMPILED
${CMAKE_BINARY_DIR} ${src}
CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_${lang}_VERBOSE_FLAG}"
"-DCMAKE_${lang}_STANDARD_LIBRARIES="
OUTPUT_VARIABLE OUTPUT
COPY_FILE "${BIN}"
)
@ -40,6 +44,15 @@ FUNCTION(CMAKE_DETERMINE_COMPILER_ABI lang src)
SET(CMAKE_INTERNAL_PLATFORM_ABI "${ABI_NAME}" PARENT_SCOPE)
ENDIF(ABI_NAME)
# Parse implicit linker information for this language, if available.
SET(implicit_dirs "")
SET(implicit_libs "")
IF(CMAKE_${lang}_VERBOSE_FLAG)
CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs)
ENDIF()
SET(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
SET(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
ELSE(CMAKE_DETERMINE_${lang}_ABI_COMPILED)
MESSAGE(STATUS "Detecting ${lang} compiler ABI info - failed")
FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log

View File

@ -27,3 +27,6 @@ IF(UNIX)
ELSE(UNIX)
SET(CMAKE_Fortran_OUTPUT_EXTENSION .obj)
ENDIF(UNIX)
SET(CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES "@CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES@")
SET(CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES "@CMAKE_Fortran_IMPLICIT_LINK_DIRECTORIES@")

View File

@ -0,0 +1,71 @@
# Function parse implicit linker options.
# This is used internally by CMake and should not be included by user
# code.
function(CMAKE_PARSE_IMPLICIT_LINK_INFO text lib_var dir_var)
set(implicit_libs "")
set(implicit_dirs_tmp)
# Parse implicit linker arguments.
set(linker "CMAKE_LINKER-NOTFOUND")
if(CMAKE_LINKER)
get_filename_component(linker ${CMAKE_LINKER} NAME)
endif()
set(linker_regex "/(${linker}|ld|collect2)")
string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
foreach(line IN LISTS output_lines)
set(cmd)
if("${line}" MATCHES "${linker_regex}")
if(UNIX)
separate_arguments(args UNIX_COMMAND "${line}")
else()
separate_arguments(args WINDOWS_COMMAND "${line}")
endif()
list(GET args 0 cmd)
endif()
if("${cmd}" MATCHES "${linker_regex}")
string(REGEX REPLACE ";-([LY]);" ";-\\1" args "${args}")
foreach(arg IN LISTS args)
if("${arg}" MATCHES "^-L(.:)?[/\\]")
# Unix search path.
string(REGEX REPLACE "^-L" "" dir "${arg}")
list(APPEND implicit_dirs_tmp ${dir})
elseif("${arg}" MATCHES "^-l[^:]")
# Unix library.
string(REGEX REPLACE "^-l" "" lib "${arg}")
list(APPEND implicit_libs ${lib})
elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
# Unix library full path.
list(APPEND implicit_libs ${arg})
elseif("${arg}" MATCHES "^-Y(P,)?")
# Sun search path.
string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
string(REPLACE ":" ";" dirs "${dirs}")
list(APPEND implicit_dirs_tmp ${dirs})
elseif("${arg}" MATCHES "^-l:")
# HP named library.
list(APPEND implicit_libs ${arg})
endif()
endforeach()
break()
elseif("${line}" MATCHES "LPATH(=| is:? )")
# HP search path.
string(REGEX REPLACE ".*LPATH(=| is:? *)" "" paths "${line}")
string(REPLACE ":" ";" paths "${paths}")
list(APPEND implicit_dirs_tmp ${paths})
endif()
endforeach()
# Cleanup list of directories.
set(implicit_dirs "")
foreach(d IN LISTS implicit_dirs_tmp)
get_filename_component(dir "${d}" ABSOLUTE)
list(APPEND implicit_dirs "${dir}")
endforeach()
list(REMOVE_DUPLICATES implicit_dirs)
# Return results.
set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
endfunction()

View File

@ -0,0 +1 @@
SET(CMAKE_C_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_CXX_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_Fortran_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_C_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_CXX_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_Fortran_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_C_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_CXX_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_Fortran_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_C_VERBOSE_FLAG "-#")

View File

@ -0,0 +1 @@
SET(CMAKE_CXX_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_Fortran_VERBOSE_FLAG "-v")

View File

@ -0,0 +1 @@
SET(CMAKE_C_VERBOSE_FLAG "-V")

View File

@ -0,0 +1 @@
SET(CMAKE_CXX_VERBOSE_FLAG "-V")

View File

@ -0,0 +1 @@
SET(CMAKE_Fortran_VERBOSE_FLAG "-V")

View File

@ -1180,6 +1180,27 @@ void cmDocumentVariables::DefineVariables(cmake* cm)
"include directories on some compilers.", false,
"Variables for Languages");
cm->DefineProperty
("CMAKE_<LANG>_IMPLICIT_LINK_DIRECTORIES", cmProperty::VARIABLE,
"Implicit linker search path detected for language <LANG>.",
"Compilers typically pass directories containing language runtime "
"libraries and default library search paths when they invoke a linker. "
"These paths are implicit linker search directories for the compiler's "
"language. "
"CMake automatically detects these directories for each language and "
"reports the results in this variable.", false,
"Variables for Languages");
cm->DefineProperty
("CMAKE_<LANG>_IMPLICIT_LINK_LIBRARIES", cmProperty::VARIABLE,
"Implicit link libraries and flags detected for language <LANG>.",
"Compilers typically pass language runtime library names and "
"other flags when they invoke a linker. "
"These flags are implicit link options for the compiler's language. "
"CMake automatically detects these libraries and flags for each "
"language and reports the results in this variable.", false,
"Variables for Languages");
cm->DefineProperty
("CMAKE_<LANG>_LINKER_PREFERENCE", cmProperty::VARIABLE,
"Determine if a language should be used for linking.",

View File

@ -86,3 +86,10 @@ CMAKE_CXX_COMPILE_OBJECT == "${CMAKE_CXX_COMPILE_OBJECT}"
CMAKE_C_COMPILE_OBJECT == "${CMAKE_C_COMPILE_OBJECT}"
CMAKE_C_LINK_EXECUTABLE == "${CMAKE_C_LINK_EXECUTABLE}"
CMAKE_CXX_LINK_EXECUTABLE == "${CMAKE_CXX_LINK_EXECUTABLE}"
// implicit link info
CMAKE_C_IMPLICIT_LINK_LIBRARIES == "${CMAKE_C_IMPLICIT_LINK_LIBRARIES}"
CMAKE_C_IMPLICIT_LINK_DIRECTORIES == "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}"
CMAKE_CXX_IMPLICIT_LINK_LIBRARIES == "${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES}"
CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES == "${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES}"