Commit 8b4873a1 authored by Brad King's avatar Brad King

Disable shared library support when compiler links statically

When a user or a compiler wrapper adds '-static' to the compiler flags
then it will always link static binaries.  Detect this from the compiler
id binary and disable TARGET_SUPPORTS_SHARED_LIBS.  This will prevent
projects from accidentally adding shared libraries when the toolchain
does not support them.  It also helps CMake avoid linking with flags
that require shared libraries to be supported.
parent e90f463a
...@@ -2,6 +2,7 @@ set(CMAKE_C_COMPILER "@CMAKE_C_COMPILER@") ...@@ -2,6 +2,7 @@ set(CMAKE_C_COMPILER "@CMAKE_C_COMPILER@")
set(CMAKE_C_COMPILER_ARG1 "@CMAKE_C_COMPILER_ARG1@") set(CMAKE_C_COMPILER_ARG1 "@CMAKE_C_COMPILER_ARG1@")
set(CMAKE_C_COMPILER_ID "@CMAKE_C_COMPILER_ID@") set(CMAKE_C_COMPILER_ID "@CMAKE_C_COMPILER_ID@")
set(CMAKE_C_COMPILER_VERSION "@CMAKE_C_COMPILER_VERSION@") set(CMAKE_C_COMPILER_VERSION "@CMAKE_C_COMPILER_VERSION@")
set(CMAKE_C_COMPILER_LINKS_STATICALLY "@CMAKE_C_COMPILER_LINKS_STATICALLY@")
set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "@CMAKE_C_STANDARD_COMPUTED_DEFAULT@") set(CMAKE_C_STANDARD_COMPUTED_DEFAULT "@CMAKE_C_STANDARD_COMPUTED_DEFAULT@")
set(CMAKE_C_COMPILE_FEATURES "@CMAKE_C_COMPILE_FEATURES@") set(CMAKE_C_COMPILE_FEATURES "@CMAKE_C_COMPILE_FEATURES@")
set(CMAKE_C90_COMPILE_FEATURES "@CMAKE_C90_COMPILE_FEATURES@") set(CMAKE_C90_COMPILE_FEATURES "@CMAKE_C90_COMPILE_FEATURES@")
......
...@@ -75,6 +75,10 @@ if(CMAKE_C_SIZEOF_DATA_PTR) ...@@ -75,6 +75,10 @@ if(CMAKE_C_SIZEOF_DATA_PTR)
unset(CMAKE_C_ABI_FILES) unset(CMAKE_C_ABI_FILES)
endif() endif()
if(CMAKE_C_COMPILER_LINKS_STATICALLY)
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
endif()
# This should be included before the _INIT variables are # This should be included before the _INIT variables are
# used to initialize the cache. Since the rule variables # used to initialize the cache. Since the rule variables
# have if blocks on them, users can still define them here. # have if blocks on them, users can still define them here.
......
...@@ -2,6 +2,7 @@ set(CMAKE_CXX_COMPILER "@CMAKE_CXX_COMPILER@") ...@@ -2,6 +2,7 @@ set(CMAKE_CXX_COMPILER "@CMAKE_CXX_COMPILER@")
set(CMAKE_CXX_COMPILER_ARG1 "@CMAKE_CXX_COMPILER_ARG1@") set(CMAKE_CXX_COMPILER_ARG1 "@CMAKE_CXX_COMPILER_ARG1@")
set(CMAKE_CXX_COMPILER_ID "@CMAKE_CXX_COMPILER_ID@") set(CMAKE_CXX_COMPILER_ID "@CMAKE_CXX_COMPILER_ID@")
set(CMAKE_CXX_COMPILER_VERSION "@CMAKE_CXX_COMPILER_VERSION@") set(CMAKE_CXX_COMPILER_VERSION "@CMAKE_CXX_COMPILER_VERSION@")
set(CMAKE_CXX_COMPILER_LINKS_STATICALLY "@CMAKE_CXX_COMPILER_LINKS_STATICALLY@")
set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "@CMAKE_CXX_STANDARD_COMPUTED_DEFAULT@") set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "@CMAKE_CXX_STANDARD_COMPUTED_DEFAULT@")
set(CMAKE_CXX_COMPILE_FEATURES "@CMAKE_CXX_COMPILE_FEATURES@") set(CMAKE_CXX_COMPILE_FEATURES "@CMAKE_CXX_COMPILE_FEATURES@")
set(CMAKE_CXX98_COMPILE_FEATURES "@CMAKE_CXX98_COMPILE_FEATURES@") set(CMAKE_CXX98_COMPILE_FEATURES "@CMAKE_CXX98_COMPILE_FEATURES@")
......
...@@ -74,6 +74,10 @@ if(CMAKE_CXX_SIZEOF_DATA_PTR) ...@@ -74,6 +74,10 @@ if(CMAKE_CXX_SIZEOF_DATA_PTR)
unset(CMAKE_CXX_ABI_FILES) unset(CMAKE_CXX_ABI_FILES)
endif() endif()
if(CMAKE_CXX_COMPILER_LINKS_STATICALLY)
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
endif()
# This should be included before the _INIT variables are # This should be included before the _INIT variables are
# used to initialize the cache. Since the rule variables # used to initialize the cache. Since the rule variables
# have if blocks on them, users can still define them here. # have if blocks on them, users can still define them here.
......
...@@ -106,6 +106,7 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) ...@@ -106,6 +106,7 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src)
set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}" set(MSVC_${lang}_ARCHITECTURE_ID "${MSVC_${lang}_ARCHITECTURE_ID}"
PARENT_SCOPE) PARENT_SCOPE)
set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX}" PARENT_SCOPE) set(CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX "${CMAKE_${lang}_CL_SHOWINCLUDES_PREFIX}" PARENT_SCOPE)
set(CMAKE_${lang}_COMPILER_LINKS_STATICALLY "${CMAKE_${lang}_COMPILER_LINKS_STATICALLY}" PARENT_SCOPE)
set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE) set(CMAKE_${lang}_COMPILER_VERSION "${CMAKE_${lang}_COMPILER_VERSION}" PARENT_SCOPE)
set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE) set(CMAKE_${lang}_SIMULATE_ID "${CMAKE_${lang}_SIMULATE_ID}" PARENT_SCOPE)
set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE) set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_${lang}_SIMULATE_VERSION}" PARENT_SCOPE)
...@@ -529,6 +530,13 @@ function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file) ...@@ -529,6 +530,13 @@ function(CMAKE_DETERMINE_COMPILER_ID_CHECK lang file)
endif() endif()
endif() endif()
if(UNIX)
execute_process(COMMAND file "${file}" OUTPUT_VARIABLE out ERROR_VARIABLE out)
if(out MATCHES "statically linked")
set(CMAKE_${lang}_COMPILER_LINKS_STATICALLY 1 PARENT_SCOPE)
endif()
endif()
# Check if a valid compiler and platform were found. # Check if a valid compiler and platform were found.
if(COMPILER_ID AND NOT COMPILER_ID_TWICE) if(COMPILER_ID AND NOT COMPILER_ID_TWICE)
set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}") set(CMAKE_${lang}_COMPILER_ID "${COMPILER_ID}")
......
...@@ -2,6 +2,7 @@ set(CMAKE_Fortran_COMPILER "@CMAKE_Fortran_COMPILER@") ...@@ -2,6 +2,7 @@ set(CMAKE_Fortran_COMPILER "@CMAKE_Fortran_COMPILER@")
set(CMAKE_Fortran_COMPILER_ARG1 "@CMAKE_Fortran_COMPILER_ARG1@") set(CMAKE_Fortran_COMPILER_ARG1 "@CMAKE_Fortran_COMPILER_ARG1@")
set(CMAKE_Fortran_COMPILER_ID "@CMAKE_Fortran_COMPILER_ID@") set(CMAKE_Fortran_COMPILER_ID "@CMAKE_Fortran_COMPILER_ID@")
set(CMAKE_Fortran_COMPILER_VERSION "@CMAKE_Fortran_COMPILER_VERSION@") set(CMAKE_Fortran_COMPILER_VERSION "@CMAKE_Fortran_COMPILER_VERSION@")
set(CMAKE_Fortran_COMPILER_LINKS_STATICALLY "@CMAKE_Fortran_COMPILER_LINKS_STATICALLY@")
set(CMAKE_Fortran_PLATFORM_ID "@CMAKE_Fortran_PLATFORM_ID@") set(CMAKE_Fortran_PLATFORM_ID "@CMAKE_Fortran_PLATFORM_ID@")
set(CMAKE_Fortran_SIMULATE_ID "@CMAKE_Fortran_SIMULATE_ID@") set(CMAKE_Fortran_SIMULATE_ID "@CMAKE_Fortran_SIMULATE_ID@")
set(CMAKE_Fortran_SIMULATE_VERSION "@CMAKE_Fortran_SIMULATE_VERSION@") set(CMAKE_Fortran_SIMULATE_VERSION "@CMAKE_Fortran_SIMULATE_VERSION@")
......
...@@ -51,6 +51,10 @@ if(CMAKE_Fortran_SIZEOF_DATA_PTR) ...@@ -51,6 +51,10 @@ if(CMAKE_Fortran_SIZEOF_DATA_PTR)
unset(CMAKE_Fortran_ABI_FILES) unset(CMAKE_Fortran_ABI_FILES)
endif() endif()
if(CMAKE_Fortran_COMPILER_LINKS_STATICALLY)
set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE)
endif()
# This should be included before the _INIT variables are # This should be included before the _INIT variables are
# used to initialize the cache. Since the rule variables # used to initialize the cache. Since the rule variables
# have if blocks on them, users can still define them here. # have if blocks on them, users can still define them here.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment