Skip to content

Commit

Permalink
Merge pull request #2694 from STEllAR-GROUP/detect_cpp_mode
Browse files Browse the repository at this point in the history
Refining C++ language mode detection for MSVC
  • Loading branch information
hkaiser committed Jun 17, 2017
2 parents 0f16de3 + f218738 commit 30b37b9
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 90 deletions.
6 changes: 2 additions & 4 deletions CMakeLists.txt
Expand Up @@ -995,10 +995,8 @@ hpx_add_target_compile_definition(BOOST_DISABLE_ASSERTS
CONFIGURATIONS Release RelWithDebInfo MinSizeRelease)

# Make sure we compile in proper C++xx mode (MSVC uses it automatically)
if(NOT MSVC)
include(HPX_DetectCppDialect)
hpx_detect_cpp_dialect()
endif()
include(HPX_DetectCppDialect)
hpx_detect_cpp_dialect()

################################################################################
# CUDA features
Expand Down
8 changes: 8 additions & 0 deletions cmake/HPX_AddConfigTest.cmake
Expand Up @@ -486,6 +486,14 @@ macro(hpx_check_for_cxx14_std_result_of_sfinae)
FILE ${ARGN})
endmacro()

###############################################################################
macro(hpx_check_for_cxx17_variable_templates)
add_hpx_config_test(HPX_WITH_CXX17_VARIABLE_TEMPLATES
SOURCE cmake/tests/cxx17_variable_templates.cpp
FILE ${ARGN}
CMAKECXXFEATURE cxx_variable_templates)
endmacro()

###############################################################################
macro(hpx_check_for_libfun_std_experimental_optional)
add_hpx_config_test(HPX_WITH_LIBFUN_EXPERIMENTAL_OPTIONAL
Expand Down
204 changes: 123 additions & 81 deletions cmake/HPX_DetectCppDialect.cmake
Expand Up @@ -5,110 +5,152 @@
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

macro(hpx_detect_cpp_dialect)
if(MSVC)
hpx_error("Do not use the macro hpx_detect_cpp_dialect() for builds based on MSVC")
endif()
macro(hpx_detect_cpp_dialect_non_msvc)

# enable enforcing a particular C++ mode
if(HPX_WITH_CXX17)
set(CXX_FLAG -std=c++17)
add_definitions(-DBOOST_NO_AUTO_PTR)
hpx_info("C++ mode enforced: C++17")
elseif(HPX_WITH_CXX1Z)
set(CXX_FLAG -std=c++1z)
add_definitions(-DBOOST_NO_AUTO_PTR)
hpx_info("C++ mode enforced: C++1z")
elseif(HPX_WITH_CXX14)
set(CXX_FLAG -std=c++14)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
add_definitions(-DBOOST_NO_CXX14_CONSTEXPR)
endif()
hpx_info("C++ mode enforced: C++14")
elseif(HPX_WITH_CXX1Y)
set(CXX_FLAG -std=c++1y)
hpx_info("C++ mode enforced: C++1y")
elseif(HPX_WITH_CXX11)
if(HPX_WITH_CUDA AND NOT HPX_WITH_CUDA_CLANG)
set(CXX_FLAG -std=c++11)
hpx_info("C++ mode enforced: C++11")
elseif(HPX_WITH_CXX0X)
set(CXX_FLAG -std=c++0x)
hpx_info("C++ mode enforced: C++0x")
hpx_info("C++ mode used: C++11")
else()
# if no C++ mode is enforced, try to detect which one to use
if(HPX_WITH_CUDA AND NOT HPX_WITH_CUDA_CLANG)
set(CXX_FLAG -std=c++11)
hpx_info("C++ mode used: C++11")
else()

# Try -std=c++17 first
if(HPX_WITH_CXX17 OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# Try -std=c++17 first
if(HPX_WITH_CXX17 OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17)))
check_cxx_compiler_flag(-std=c++17 HPX_WITH_CXX17)
endif()

if(HPX_WITH_CXX17)
set(CXX_FLAG -std=c++17)
hpx_add_config_cond_define(BOOST_NO_AUTO_PTR)
hpx_info("C++ mode used: C++17")
else()
# ... otherwise try -std=c++1z
if(HPX_WITH_CXX1Z OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17)))
check_cxx_compiler_flag(-std=c++17 HPX_WITH_CXX17)
check_cxx_compiler_flag(-std=c++1z HPX_WITH_CXX1Z)
endif()

if(HPX_WITH_CXX17)
set(CXX_FLAG -std=c++17)
add_definitions(-DBOOST_NO_AUTO_PTR)
hpx_info("C++ mode used: C++17")
if(HPX_WITH_CXX1Z)
set(CXX_FLAG -std=c++1z)
hpx_add_config_cond_define(BOOST_NO_AUTO_PTR)
hpx_info("C++ mode used: C++1z")
else()
# ... otherwise try -std=c++1z
if(HPX_WITH_CXX1Z OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# ... otherwise try -std=c++14
if(HPX_WITH_CXX14 OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17)))
check_cxx_compiler_flag(-std=c++1z HPX_WITH_CXX1Z)
check_cxx_compiler_flag(-std=c++14 HPX_WITH_CXX14)
endif()

if(HPX_WITH_CXX1Z)
set(CXX_FLAG -std=c++1z)
add_definitions(-DBOOST_NO_AUTO_PTR)
hpx_info("C++ mode used: C++1z")
if(HPX_WITH_CXX14)
set(CXX_FLAG -std=c++14)
# The Intel compiler doesn't appear to have a fully functional
# implementation of C++14 constexpr. It's fine with our C++14 constexpr
# usage in HPX but chokes on Boost.
# FIXME: This should be replaced with a version-based check in the future
# when the Intel compiler is able to build Boost with -std=c++14.
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
hpx_add_config_cond_define(BOOST_NO_CXX14_CONSTEXPR)
endif()
hpx_info("C++ mode used: C++14")
else()
# ... otherwise try -std=c++14
if(HPX_WITH_CXX14 OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# ... otherwise try -std=c++1y
if(HPX_WITH_CXX1Y OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17)))
check_cxx_compiler_flag(-std=c++14 HPX_WITH_CXX14)
check_cxx_compiler_flag(-std=c++1y HPX_WITH_CXX1Y)
endif()

if(HPX_WITH_CXX14)
set(CXX_FLAG -std=c++14)
# The Intel compiler doesn't appear to have a fully functional
# implementation of C++14 constexpr. It's fine with our C++14 constexpr
# usage in HPX but chokes on Boost.
# FIXME: This should be replaced with a version-based check in the future
# when the Intel compiler is able to build Boost with -std=c++14.
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
add_definitions(-DBOOST_NO_CXX14_CONSTEXPR)
endif()
hpx_info("C++ mode used: C++14")
if(HPX_WITH_CXX1Y)
set(CXX_FLAG -std=c++1y)
hpx_info("C++ mode used: C++1y")
else()
# ... otherwise try -std=c++1y
if(HPX_WITH_CXX1Y OR NOT (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 17)))
check_cxx_compiler_flag(-std=c++1y HPX_WITH_CXX1Y)
endif()

if(HPX_WITH_CXX1Y)
set(CXX_FLAG -std=c++1y)
hpx_info("C++ mode used: C++1y")
# ... otherwise try -std=c++11
check_cxx_compiler_flag(-std=c++11 HPX_WITH_CXX11)
if(HPX_WITH_CXX11)
set(CXX_FLAG -std=c++11)
hpx_info("C++ mode used: C++11")
else()
# ... otherwise try -std=c++11
check_cxx_compiler_flag(-std=c++11 HPX_WITH_CXX11)
if(HPX_WITH_CXX11)
set(CXX_FLAG -std=c++11)
hpx_info("C++ mode used: C++11")
else()
# ... otherwise try -std=c++0x
check_cxx_compiler_flag(-std=c++0x HPX_WITH_CXX0X)
if(HPX_WITH_CXX0X)
set(CXX_FLAG -std=c++0x)
hpx_info("C++ mode used: C++0x")
endif()
# ... otherwise try -std=c++0x
check_cxx_compiler_flag(-std=c++0x HPX_WITH_CXX0X)
if(HPX_WITH_CXX0X)
set(CXX_FLAG -std=c++0x)
hpx_info("C++ mode used: C++0x")
endif()
endif()
endif()
endif()
endif()
endif()
endif()
endmacro()

macro(hpx_detect_cpp_dialect)

if(MSVC)
set(CXX_FLAG)

# enable enforcing a particular C++ mode
if(HPX_WITH_CXX17)
set(CXX_FLAG -std:c++latest)
hpx_add_config_cond_define(_HAS_AUTO_PTR_ETC 1)
hpx_info("C++ mode enforced: C++17")
elseif(HPX_WITH_CXX1Z)
set(CXX_FLAG -std:c++latest)
hpx_add_config_cond_define(_HAS_AUTO_PTR_ETC 1)
hpx_add_config_cond_define(BOOST_NO_AUTO_PTR)
hpx_info("C++ mode enforced: C++1z")
elseif(HPX_WITH_CXX14)
set(CXX_FLAG -std:c++14)
hpx_add_config_cond_define(_HAS_AUTO_PTR_ETC 1)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
hpx_add_config_cond_define(BOOST_NO_CXX14_CONSTEXPR)
endif()
hpx_info("C++ mode enforced: C++14")
elseif(HPX_WITH_CXX1Y)
set(CXX_FLAG -std:c++14)
hpx_add_config_cond_define(_HAS_AUTO_PTR_ETC 1)
hpx_info("C++ mode enforced: C++1y")
elseif(HPX_WITH_CXX11)
hpx_info("C++ mode enforced: C++11")
elseif(HPX_WITH_CXX0X)
hpx_info("C++ mode enforced: C++0x")
else()
hpx_info("C++ mode assumed: C++11")
endif()

else(MSVC)

# enable enforcing a particular C++ mode
if(HPX_WITH_CXX17)
set(CXX_FLAG -std=c++17)
hpx_add_config_cond_define(BOOST_NO_AUTO_PTR)
hpx_info("C++ mode enforced: C++17")
elseif(HPX_WITH_CXX1Z)
set(CXX_FLAG -std=c++1z)
hpx_add_config_cond_define(BOOST_NO_AUTO_PTR)
hpx_info("C++ mode enforced: C++1z")
elseif(HPX_WITH_CXX14)
set(CXX_FLAG -std=c++14)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
hpx_add_config_cond_define(BOOST_NO_CXX14_CONSTEXPR)
endif()
hpx_info("C++ mode enforced: C++14")
elseif(HPX_WITH_CXX1Y)
set(CXX_FLAG -std=c++1y)
hpx_info("C++ mode enforced: C++1y")
elseif(HPX_WITH_CXX11)
set(CXX_FLAG -std=c++11)
hpx_info("C++ mode enforced: C++11")
elseif(HPX_WITH_CXX0X)
set(CXX_FLAG -std=c++0x)
hpx_info("C++ mode enforced: C++0x")
else()
# if no C++ mode is enforced, try to detect which one to use
hpx_detect_cpp_dialect_non_msvc()
endif()

endif(MSVC)

if(CXX_FLAG)
hpx_add_target_compile_option(${CXX_FLAG})
endif()

endmacro()
11 changes: 6 additions & 5 deletions cmake/HPX_PerformCxxFeatureTests.cmake
Expand Up @@ -153,12 +153,10 @@ macro(hpx_perform_cxx_feature_tests)

hpx_check_for_cxx14_std_result_of_sfinae(
DEFINITIONS HPX_HAVE_CXX14_STD_RESULT_OF_SFINAE)
endif()

# check for experimental facilities
if(HPX_WITH_CXX1Y OR HPX_WITH_CXX14 OR HPX_WITH_CXX1Z OR HPX_WITH_CXX17)
# check for Library Fundamentals TS v2's experimental/optional only if in
# C++1y or C++14 mode.
# check for experimental facilities

# check for Library Fundamentals TS v2's experimental/optional
hpx_check_for_libfun_std_experimental_optional(
DEFINITIONS HPX_HAVE_LIBFUN_STD_EXPERIMENTAL_OPTIONAL)
endif()
Expand All @@ -167,6 +165,9 @@ macro(hpx_perform_cxx_feature_tests)
# Check the availability of certain C++17 language features
hpx_check_for_cxx17_fold_expressions(
DEFINITIONS HPX_HAVE_CXX17_FOLD_EXPRESSIONS)

hpx_check_for_cxx17_variable_templates(
DEFINITIONS HPX_HAVE_CXX17_VARIABLE_TEMPLATES)
endif()
endmacro()

17 changes: 17 additions & 0 deletions cmake/tests/cxx17_variable_templates.cpp
@@ -0,0 +1,17 @@
// Copyright (C) 2017 Hartmut Kaiser
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

struct true_type
{
enum { value = 1; }
}

template <typename T>
struct is_foobar : true_type {};

template <typename T>
constexpr bool is_foobar_v = is_foobar<T>::value;

int main() {}

0 comments on commit 30b37b9

Please sign in to comment.