Skip to content

Commit

Permalink
Ask the compiler what libcxx should be (conan-io#118)
Browse files Browse the repository at this point in the history
* Ask the compiler what libcxx should be

* fix typo

* merge libc++ and libstdc++ detection functions
  • Loading branch information
ohanar authored and memsharded committed Jan 30, 2019
1 parent 919c30d commit e948313
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions conan.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function(conan_cmake_settings result)
set(_CONAN_SETTING_COMPILER gcc)
set(_CONAN_SETTING_COMPILER_VERSION ${COMPILER_VERSION})
if (USING_CXX)
conan_cmake_detect_gnu_libcxx(_LIBCXX)
conan_cmake_detect_unix_libcxx(_LIBCXX)
set(_CONAN_SETTING_COMPILER_LIBCXX ${_LIBCXX})
endif ()
elseif (${CMAKE_${LANGUAGE}_COMPILER_ID} STREQUAL AppleClang)
Expand All @@ -133,37 +133,29 @@ function(conan_cmake_settings result)
set(_CONAN_SETTING_COMPILER apple-clang)
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR}.${MINOR})
if (USING_CXX)
set(_CONAN_SETTING_COMPILER_LIBCXX libc++)
conan_cmake_detect_unix_libcxx(_LIBCXX)
set(_CONAN_SETTING_COMPILER_LIBCXX ${_LIBCXX})
endif ()
elseif (${CMAKE_${LANGUAGE}_COMPILER_ID} STREQUAL Clang)
string(REPLACE "." ";" VERSION_LIST ${CMAKE_${LANGUAGE}_COMPILER_VERSION})
list(GET VERSION_LIST 0 MAJOR)
list(GET VERSION_LIST 1 MINOR)
set(_CONAN_SETTING_COMPILER clang)
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR}.${MINOR})
if(APPLE)
cmake_policy(GET CMP0025 APPLE_CLANG_POLICY_ENABLED)
if(NOT APPLE_CLANG_POLICY_ENABLED)
message(STATUS "Conan: APPLE and Clang detected. Assuming apple-clang compiler. Set CMP0025 to avoid it")
set(_CONAN_SETTING_COMPILER apple-clang)
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR}.${MINOR})
else()
set(_CONAN_SETTING_COMPILER clang)
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR}.${MINOR})
endif()
if (USING_CXX)
set(_CONAN_SETTING_COMPILER_LIBCXX libc++)
endif ()
else()
set(_CONAN_SETTING_COMPILER clang)
if(${MAJOR} GREATER 7)
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR})
else()
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR}.${MINOR})
endif()
if (USING_CXX)
conan_cmake_detect_gnu_libcxx(_LIBCXX)
set(_CONAN_SETTING_COMPILER_LIBCXX ${_LIBCXX})
endif ()
endif()
if(${_CONAN_SETTING_COMPILER} STREQUAL clang AND ${MAJOR} GREATER 7)
set(_CONAN_SETTING_COMPILER_VERSION ${MAJOR})
endif()
if (USING_CXX)
conan_cmake_detect_unix_libcxx(_LIBCXX)
set(_CONAN_SETTING_COMPILER_LIBCXX ${_LIBCXX})
endif ()
elseif(${CMAKE_${LANGUAGE}_COMPILER_ID} STREQUAL MSVC)
set(_VISUAL "Visual Studio")
_get_msvc_ide_version(_VISUAL_VERSION)
Expand Down Expand Up @@ -237,36 +229,53 @@ function(conan_cmake_settings result)
endfunction()


function(conan_cmake_detect_gnu_libcxx result)
# Allow -D_GLIBCXX_USE_CXX11_ABI=ON/OFF as argument to cmake
if(DEFINED _GLIBCXX_USE_CXX11_ABI)
if(_GLIBCXX_USE_CXX11_ABI)
set(${result} libstdc++11 PARENT_SCOPE)
return()
else()
set(${result} libstdc++ PARENT_SCOPE)
return()
endif()
endif()
function(conan_cmake_detect_unix_libcxx result)
# Take into account any -stdlib in compile options
get_directory_property(compile_options DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMPILE_OPTIONS)

# Check if there's any add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
# Take into account any _GLIBCXX_USE_CXX11_ABI in compile definitions
get_directory_property(defines DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} COMPILE_DEFINITIONS)
foreach(define ${defines})
if(define STREQUAL "_GLIBCXX_USE_CXX11_ABI=0")
set(${result} libstdc++ PARENT_SCOPE)
return()
if(define MATCHES "_GLIBCXX_USE_CXX11_ABI")
if(define MATCHES "^-D")
set(compile_options ${compile_options} "${define}")
else()
set(compile_options ${compile_options} "-D${define}")
endif()
endif()
endforeach()

# Use C++11 stdlib as default if gcc is 5.1+
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.1")
set(${result} libstdc++ PARENT_SCOPE)
execute_process(
COMMAND ${CMAKE_COMMAND} -E echo "#include <string>"
COMMAND ${CMAKE_CXX_COMPILER} -x c++ ${compile_options} -E -dM -
OUTPUT_VARIABLE string_defines
)

if(string_defines MATCHES "#define __GLIBCXX__")
# Allow -D_GLIBCXX_USE_CXX11_ABI=ON/OFF as argument to cmake
if(DEFINED _GLIBCXX_USE_CXX11_ABI)
if(_GLIBCXX_USE_CXX11_ABI)
set(${result} libstdc++11 PARENT_SCOPE)
return()
else()
set(${result} libstdc++ PARENT_SCOPE)
return()
endif()
endif()

if(string_defines MATCHES "#define _GLIBCXX_USE_CXX11_ABI 1\n")
set(${result} libstdc++11 PARENT_SCOPE)
else()
# Either the compiler is missing the define because it is old, and so
# it can't use the new abi, or the compiler was configured to use the
# old abi by the user or distro (e.g. devtoolset on RHEL/CentOS)
set(${result} libstdc++ PARENT_SCOPE)
endif()
else()
set(${result} libstdc++11 PARENT_SCOPE)
set(${result} libc++ PARENT_SCOPE)
endif()
endfunction()


function(conan_cmake_detect_vs_runtime result)
string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
set(variables CMAKE_CXX_FLAGS_${build_type} CMAKE_C_FLAGS_${build_type} CMAKE_CXX_FLAGS CMAKE_C_FLAGS)
Expand Down Expand Up @@ -516,4 +525,3 @@ macro(conan_add_remote)
execute_process(COMMAND ${CONAN_CMD} remote add ${CONAN_NAME} ${CONAN_URL}
${CONAN_INDEX_ARG} -f)
endmacro()

0 comments on commit e948313

Please sign in to comment.