From 07ea19ad1f280e7a1cb07ab2b52c0081f72251dd Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Thu, 23 Jul 2009 10:07:25 -0400
Subject: 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.
---
 Modules/CMakeCCompiler.cmake.in              |  3 ++
 Modules/CMakeCXXCompiler.cmake.in            |  3 ++
 Modules/CMakeDetermineCompilerABI.cmake      | 13 +++++
 Modules/CMakeFortranCompiler.cmake.in        |  3 ++
 Modules/CMakeParseImplicitLinkInfo.cmake     | 71 ++++++++++++++++++++++++++++
 Modules/Compiler/GNU-C.cmake                 |  1 +
 Modules/Compiler/GNU-CXX.cmake               |  1 +
 Modules/Compiler/GNU-Fortran.cmake           |  1 +
 Modules/Compiler/HP-C.cmake                  |  1 +
 Modules/Compiler/HP-CXX.cmake                |  1 +
 Modules/Compiler/HP-Fortran.cmake            |  1 +
 Modules/Compiler/MIPSpro-C.cmake             |  1 +
 Modules/Compiler/MIPSpro-CXX.cmake           |  1 +
 Modules/Compiler/MIPSpro-Fortran.cmake       |  1 +
 Modules/Compiler/SunPro-C.cmake              |  1 +
 Modules/Compiler/SunPro-CXX.cmake            |  1 +
 Modules/Compiler/SunPro-Fortran.cmake        |  1 +
 Modules/Compiler/VisualAge-C.cmake           |  1 +
 Modules/Compiler/VisualAge-CXX.cmake         |  1 +
 Modules/Compiler/VisualAge-Fortran.cmake     |  1 +
 Source/cmDocumentVariables.cxx               | 23 ++++++++-
 Tests/SystemInformation/SystemInformation.in |  7 +++
 22 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 Modules/CMakeParseImplicitLinkInfo.cmake
 create mode 100644 Modules/Compiler/GNU-C.cmake
 create mode 100644 Modules/Compiler/GNU-CXX.cmake
 create mode 100644 Modules/Compiler/GNU-Fortran.cmake
 create mode 100644 Modules/Compiler/HP-C.cmake
 create mode 100644 Modules/Compiler/HP-CXX.cmake
 create mode 100644 Modules/Compiler/HP-Fortran.cmake
 create mode 100644 Modules/Compiler/MIPSpro-C.cmake
 create mode 100644 Modules/Compiler/MIPSpro-CXX.cmake
 create mode 100644 Modules/Compiler/MIPSpro-Fortran.cmake
 create mode 100644 Modules/Compiler/SunPro-C.cmake
 create mode 100644 Modules/Compiler/SunPro-CXX.cmake
 create mode 100644 Modules/Compiler/SunPro-Fortran.cmake
 create mode 100644 Modules/Compiler/VisualAge-C.cmake
 create mode 100644 Modules/Compiler/VisualAge-CXX.cmake
 create mode 100644 Modules/Compiler/VisualAge-Fortran.cmake

diff --git a/Modules/CMakeCCompiler.cmake.in b/Modules/CMakeCCompiler.cmake.in
index e334c47..209a33f 100644
--- a/Modules/CMakeCCompiler.cmake.in
+++ b/Modules/CMakeCCompiler.cmake.in
@@ -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@")
diff --git a/Modules/CMakeCXXCompiler.cmake.in b/Modules/CMakeCXXCompiler.cmake.in
index 81380c9..d87c788 100644
--- a/Modules/CMakeCXXCompiler.cmake.in
+++ b/Modules/CMakeCXXCompiler.cmake.in
@@ -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@")
diff --git a/Modules/CMakeDetermineCompilerABI.cmake b/Modules/CMakeDetermineCompilerABI.cmake
index 3fd06a9..d519237 100644
--- a/Modules/CMakeDetermineCompilerABI.cmake
+++ b/Modules/CMakeDetermineCompilerABI.cmake
@@ -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
diff --git a/Modules/CMakeFortranCompiler.cmake.in b/Modules/CMakeFortranCompiler.cmake.in
index 65cfda7..afd9ce0 100644
--- a/Modules/CMakeFortranCompiler.cmake.in
+++ b/Modules/CMakeFortranCompiler.cmake.in
@@ -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@")
diff --git a/Modules/CMakeParseImplicitLinkInfo.cmake b/Modules/CMakeParseImplicitLinkInfo.cmake
new file mode 100644
index 0000000..b56781f
--- /dev/null
+++ b/Modules/CMakeParseImplicitLinkInfo.cmake
@@ -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()
diff --git a/Modules/Compiler/GNU-C.cmake b/Modules/Compiler/GNU-C.cmake
new file mode 100644
index 0000000..abf384a
--- /dev/null
+++ b/Modules/Compiler/GNU-C.cmake
@@ -0,0 +1 @@
+SET(CMAKE_C_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/GNU-CXX.cmake b/Modules/Compiler/GNU-CXX.cmake
new file mode 100644
index 0000000..f3c6b5f
--- /dev/null
+++ b/Modules/Compiler/GNU-CXX.cmake
@@ -0,0 +1 @@
+SET(CMAKE_CXX_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/GNU-Fortran.cmake b/Modules/Compiler/GNU-Fortran.cmake
new file mode 100644
index 0000000..7f7c128
--- /dev/null
+++ b/Modules/Compiler/GNU-Fortran.cmake
@@ -0,0 +1 @@
+SET(CMAKE_Fortran_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/HP-C.cmake b/Modules/Compiler/HP-C.cmake
new file mode 100644
index 0000000..abf384a
--- /dev/null
+++ b/Modules/Compiler/HP-C.cmake
@@ -0,0 +1 @@
+SET(CMAKE_C_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/HP-CXX.cmake b/Modules/Compiler/HP-CXX.cmake
new file mode 100644
index 0000000..f3c6b5f
--- /dev/null
+++ b/Modules/Compiler/HP-CXX.cmake
@@ -0,0 +1 @@
+SET(CMAKE_CXX_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/HP-Fortran.cmake b/Modules/Compiler/HP-Fortran.cmake
new file mode 100644
index 0000000..7f7c128
--- /dev/null
+++ b/Modules/Compiler/HP-Fortran.cmake
@@ -0,0 +1 @@
+SET(CMAKE_Fortran_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/MIPSpro-C.cmake b/Modules/Compiler/MIPSpro-C.cmake
new file mode 100644
index 0000000..abf384a
--- /dev/null
+++ b/Modules/Compiler/MIPSpro-C.cmake
@@ -0,0 +1 @@
+SET(CMAKE_C_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/MIPSpro-CXX.cmake b/Modules/Compiler/MIPSpro-CXX.cmake
new file mode 100644
index 0000000..f3c6b5f
--- /dev/null
+++ b/Modules/Compiler/MIPSpro-CXX.cmake
@@ -0,0 +1 @@
+SET(CMAKE_CXX_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/MIPSpro-Fortran.cmake b/Modules/Compiler/MIPSpro-Fortran.cmake
new file mode 100644
index 0000000..7f7c128
--- /dev/null
+++ b/Modules/Compiler/MIPSpro-Fortran.cmake
@@ -0,0 +1 @@
+SET(CMAKE_Fortran_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/SunPro-C.cmake b/Modules/Compiler/SunPro-C.cmake
new file mode 100644
index 0000000..2c60b89
--- /dev/null
+++ b/Modules/Compiler/SunPro-C.cmake
@@ -0,0 +1 @@
+SET(CMAKE_C_VERBOSE_FLAG "-#")
diff --git a/Modules/Compiler/SunPro-CXX.cmake b/Modules/Compiler/SunPro-CXX.cmake
new file mode 100644
index 0000000..f3c6b5f
--- /dev/null
+++ b/Modules/Compiler/SunPro-CXX.cmake
@@ -0,0 +1 @@
+SET(CMAKE_CXX_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/SunPro-Fortran.cmake b/Modules/Compiler/SunPro-Fortran.cmake
new file mode 100644
index 0000000..7f7c128
--- /dev/null
+++ b/Modules/Compiler/SunPro-Fortran.cmake
@@ -0,0 +1 @@
+SET(CMAKE_Fortran_VERBOSE_FLAG "-v")
diff --git a/Modules/Compiler/VisualAge-C.cmake b/Modules/Compiler/VisualAge-C.cmake
new file mode 100644
index 0000000..3120478
--- /dev/null
+++ b/Modules/Compiler/VisualAge-C.cmake
@@ -0,0 +1 @@
+SET(CMAKE_C_VERBOSE_FLAG "-V")
diff --git a/Modules/Compiler/VisualAge-CXX.cmake b/Modules/Compiler/VisualAge-CXX.cmake
new file mode 100644
index 0000000..618ff5a
--- /dev/null
+++ b/Modules/Compiler/VisualAge-CXX.cmake
@@ -0,0 +1 @@
+SET(CMAKE_CXX_VERBOSE_FLAG "-V")
diff --git a/Modules/Compiler/VisualAge-Fortran.cmake b/Modules/Compiler/VisualAge-Fortran.cmake
new file mode 100644
index 0000000..5ae3895
--- /dev/null
+++ b/Modules/Compiler/VisualAge-Fortran.cmake
@@ -0,0 +1 @@
+SET(CMAKE_Fortran_VERBOSE_FLAG "-V")
diff --git a/Source/cmDocumentVariables.cxx b/Source/cmDocumentVariables.cxx
index 85d8c85..fa762f6 100644
--- a/Source/cmDocumentVariables.cxx
+++ b/Source/cmDocumentVariables.cxx
@@ -1179,7 +1179,28 @@ void cmDocumentVariables::DefineVariables(cmake* cm)
      "This prevents system include directories from being treated as user "
      "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.",
diff --git a/Tests/SystemInformation/SystemInformation.in b/Tests/SystemInformation/SystemInformation.in
index c6018a8..477dea0 100644
--- a/Tests/SystemInformation/SystemInformation.in
+++ b/Tests/SystemInformation/SystemInformation.in
@@ -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}"
+
-- 
cgit v0.12