Skip to content
This repository has been archived by the owner on Feb 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #6 from Nava2/add-cmake-features
Browse files Browse the repository at this point in the history
Add cmake features
  • Loading branch information
foonathan committed Aug 24, 2016
2 parents 09152a5 + 214bd2d commit 99ed74b
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 62 deletions.
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
build*/

CMakeCache.txt
CMakeFiles/

#vscode
.vscode/

# Jetbrains
.idea/
144 changes: 82 additions & 62 deletions comp_base.cmake
Expand Up @@ -12,10 +12,13 @@ include(CMakeParseArguments)
# the current API version
set(COMP_API_VERSION 1.1 CACHE STRING "compatibility api version" FORCE)

set(COMP_REMOTE_REPO "foonathan/compatibility" CACHE STRING "Github Repository to pull from")
set(COMP_REMOTE_BRANCH "master" CACHE STRING "Git branch on COMP_REMOTE_REPO")

# EXTERNAL
# download location for feature files, the feature file name will be appended
# to circumvent download process, manually place the files at the CMAKE_PATH
set(COMP_REMOTE_URL "https://raw.githubusercontent.com/foonathan/compatibility/master/" CACHE STRING "url of remote repository to be used for downloading the feature files")
set(COMP_REMOTE_URL "https://raw.githubusercontent.com/${COMP_REMOTE_REPO}/${COMP_REMOTE_BRANCH}/" CACHE STRING "url of remote repository to be used for downloading the feature files")

# EXTERNAL; feature module
# requires a certain API version
Expand Down Expand Up @@ -105,57 +108,66 @@ endmacro()
# INTERNAL
# translates feature names
function(_comp_translate_feature feature)
set(_cxx_alias_templates cpp11_lang/alias_template)
set(_cxx_alignas cpp11_lang/alignas)
set(_cxx_alignof cpp11_lang/alignof)
set(_cxx_attribute_deprecated cpp14_lang/deprecated)
set(_cxx_constexpr cpp11_lang/constexpr)
set(_cxx_decltype cpp11_lang/decltype)
set(_cxx_deleted_functions cpp11_lang/delete_fnc)
set(_cxx_explicit_conversions cpp11_lang/explicit_conversion_op)
set(_cxx_final cpp11_lang/final)
set(_cxx_noexcept cpp11_lang/noexcept)
set(_cxx_nullptr cpp11_lang/nullptr)
set(_cxx_override cpp11_lang/override)
set(_cxx_relaxed_constexpr cpp14_lang/general_constexpr)
set(_cxx_rvalue_references cpp11_lang/rvalue_ref)
set(_cxx_static_assert cpp11_lang/static_assert)
set(_cxx_thread_local cpp11_lang/thread_local)
set(_cxx_user_literals cpp11_lang/literal_op)
set(_cxx_variable_templates cpp14_lang/variable_template)
set(_cxx_alias_templates cpp11_lang/alias_template CACHE INTERNAL "")
set(_cxx_alignas cpp11_lang/alignas CACHE INTERNAL "")
set(_cxx_alignof cpp11_lang/alignof CACHE INTERNAL "")
set(_cxx_attribute_deprecated cpp14_lang/deprecated CACHE INTERNAL "")
set(_cxx_auto_type cpp11_lang/auto_type CACHE INTERNAL "")
set(_cxx_constexpr cpp11_lang/constexpr CACHE INTERNAL "")
set(_cxx_decltype cpp11_lang/decltype CACHE INTERNAL "")
set(_cxx_decltype_auto cpp14_lang/return_type_deduction CACHE INTERNAL "")
set(_cxx_deleted_functions cpp11_lang/delete_fnc CACHE INTERNAL "")
set(_cxx_default_function_template_args cpp11_lang/default_function_template_args CACHE INTERNAL "")
set(_cxx_explicit_conversions cpp11_lang/explicit_conversion_op CACHE INTERNAL "")
set(_cxx_final cpp11_lang/final CACHE INTERNAL "")
set(_cxx_lambdas cpp11_lang/lambdas CACHE INTERNAL "")
set(_cxx_noexcept cpp11_lang/noexcept CACHE INTERNAL "")
set(_cxx_nullptr cpp11_lang/nullptr CACHE INTERNAL "")
set(_cxx_override cpp11_lang/override CACHE INTERNAL "")
set(_cxx_relaxed_constexpr cpp14_lang/general_constexpr CACHE INTERNAL "")
set(_cxx_range_for cpp11_lang/range_for CACHE INTERNAL "")
set(_cxx_return_type_deduction cpp14_lang/return_type_deduction CACHE INTERNAL "")
set(_cxx_right_angle_brackets cpp11_lang/right_angle_brackets CACHE INTERNAL "")
set(_cxx_rvalue_references cpp11_lang/rvalue_ref CACHE INTERNAL "")
set(_cxx_static_assert cpp11_lang/static_assert CACHE INTERNAL "")
set(_cxx_strong_enums cpp11_lang/enum_class CACHE INTERNAL "")
set(_cxx_thread_local cpp11_lang/thread_local CACHE INTERNAL "")
set(_cxx_user_literals cpp11_lang/literal_op CACHE INTERNAL "")
set(_cxx_variable_templates cpp14_lang/variable_template CACHE INTERNAL "")
set(_cxx_variadic_templates cpp11_lang/parameter_pack CACHE INTERNAL "")

# note: triple underscore at beginning!
set(___cpp_alias_templates cpp11_lang/alias_template)
set(___cpp_constexpr cpp11_lang/constexpr)
set(___cpp_decltype cpp11_lang/decltype)
set(___cpp_user_defined_literals cpp11_lang/literal_op)
set(___cpp_noexcept cpp11_lang/noexcept)
set(___cpp_rvalue_references cpp11_lang/rvalue_ref)
set(___cpp_static_assert cpp11_lang/static_assert)

set(___cpp_return_type_deduction cpp14_lang/return_type_deduction)
set(___cpp_sized_deallocation cpp14_lang/sized_deallocation)
set(___cpp_variable_templates cpp14_lang/variable_template)

set(___cpp_lib_exchange_function cpp14_lib/exchange)
set(___cpp_lib_transparent_operators cpp14_lib/generic_operator_functors)
set(___cpp_lib_integer_sequence cpp14_lib/integer_sequence)
set(___cpp_lib_make_unique cpp14_lib/make_unique)
set(___cpp_lib_quoted_string_io cpp14_lib/quoted)
set(___cpp_lib_shared_timed_mutex cpp14_lib/shared_lock)
set(___cpp_lib_robust_nonmodifying_seq_ops cpp14_lib/two_range_algorithms)

set(___cpp_fold_expressions cpp17_lang/fold_expressions)
set(___cpp_unicode_characters cpp17_lang/utf8_char_literal)

set(___cpp_lib_nonmember_container_access cpp17_lib/container_access)
set(___cpp_lib_invoke cpp17_lib/invoke)
set(___cpp_lib_map_insertion cpp17_lib/map_insertion)
set(___cpp_lib_unordered_map_insertion cpp17_lib/map_insertion)
set(___cpp_lib_map_try_emplace cpp17_lib/map_insertion)
set(___cpp_lib_unordered_map_try_emplace cpp17_lib/map_insertion)
set(___cpp_lib_uncaught_exceptions cpp17_lib/uncaught_exceptions)
set(___cpp_lib_void_t cpp17_lib/void_t)
set(___cpp_alias_templates cpp11_lang/alias_template CACHE INTERNAL "")
set(___cpp_constexpr cpp11_lang/constexpr CACHE INTERNAL "")
set(___cpp_decltype cpp11_lang/decltype CACHE INTERNAL "")
set(___cpp_user_defined_literals cpp11_lang/literal_op CACHE INTERNAL "")
set(___cpp_noexcept cpp11_lang/noexcept CACHE INTERNAL "")
set(___cpp_rvalue_references cpp11_lang/rvalue_ref CACHE INTERNAL "")
set(___cpp_static_assert cpp11_lang/static_assert CACHE INTERNAL "")

set(___cpp_return_type_deduction cpp14_lang/return_type_deduction CACHE INTERNAL "")
set(___cpp_sized_deallocation cpp14_lang/sized_deallocation CACHE INTERNAL "")
set(___cpp_variable_templates cpp14_lang/variable_template CACHE INTERNAL "")

set(___cpp_lib_exchange_function cpp14_lib/exchange CACHE INTERNAL "")
set(___cpp_lib_transparent_operators cpp14_lib/generic_operator_functors CACHE INTERNAL "")
set(___cpp_lib_integer_sequence cpp14_lib/integer_sequence CACHE INTERNAL "")
set(___cpp_lib_make_unique cpp14_lib/make_unique CACHE INTERNAL "")
set(___cpp_lib_quoted_string_io cpp14_lib/quoted CACHE INTERNAL "")
set(___cpp_lib_shared_timed_mutex cpp14_lib/shared_lock CACHE INTERNAL "")
set(___cpp_lib_robust_nonmodifying_seq_ops cpp14_lib/two_range_algorithms CACHE INTERNAL "")

set(___cpp_fold_expressions cpp17_lang/fold_expressions CACHE INTERNAL "")
set(___cpp_unicode_characters cpp17_lang/utf8_char_literal CACHE INTERNAL "")

set(___cpp_lib_nonmember_container_access cpp17_lib/container_access CACHE INTERNAL "")
set(___cpp_lib_invoke cpp17_lib/invoke CACHE INTERNAL "")
set(___cpp_lib_map_insertion cpp17_lib/map_insertion CACHE INTERNAL "")
set(___cpp_lib_unordered_map_insertion cpp17_lib/map_insertion CACHE INTERNAL "")
set(___cpp_lib_map_try_emplace cpp17_lib/map_insertion CACHE INTERNAL "")
set(___cpp_lib_unordered_map_try_emplace cpp17_lib/map_insertion CACHE INTERNAL "")
set(___cpp_lib_uncaught_exceptions cpp17_lib/uncaught_exceptions CACHE INTERNAL "")
set(___cpp_lib_void_t cpp17_lib/void_t CACHE INTERNAL "")

if(DEFINED _${feature})
set(feature "${_${feature}}" PARENT_SCOPE)
Expand Down Expand Up @@ -195,7 +207,8 @@ function(_comp_gen_files feature)
set(result "0")
endif()

set(headers ${headers} ${COMP_PREFIX}${macro_name}_HEADER="${COMP_INCLUDE_PATH}/${COMP_ID}/${name}.hpp" PARENT_SCOPE)
list(APPEND _COMP_HEADERS ${COMP_PREFIX}${macro_name}_HEADER="${COMP_INCLUDE_PATH}/${COMP_ID}/${name}.hpp")
set(_COMP_HEADERS "${_COMP_HEADERS}" PARENT_SCOPE)
file(WRITE ${COMP_INCLUDE_PATH}/${COMP_ID}/${name}.hpp
"// This file was autogenerated using foonathan/compatibility.
// See https://github.com/foonathan/compatibility for further information.
Expand Down Expand Up @@ -233,12 +246,15 @@ endfunction()

# INTERNAL
# handles a feature file
macro(_comp_handle_feature feature)
function(_comp_handle_feature feature)
_comp_translate_feature(${feature})
_comp_fetch_feature(${COMP_CMAKE_PATH} ${feature})
include(${COMP_CMAKE_PATH}/${feature}.cmake)
_comp_gen_files(${feature})
endmacro()

# propagate variables up
set(_COMP_HEADERS "${_COMP_HEADERS}" PARENT_SCOPE)
endfunction()

# EXTERNAL; user
# setups certain features for a target
Expand All @@ -254,28 +270,32 @@ function(comp_target_features target include_policy)
foreach(feature ${COMP_UNPARSED_ARGUMENTS})
_comp_handle_feature(${feature})
endforeach()
target_include_directories(${target} ${include_policy} ${COMP_INCLUDE_PATH})
target_compile_definitions(${target} ${include_policy} ${headers})

target_include_directories(${target} ${include_policy} $<BUILD_INTERFACE:${COMP_INCLUDE_PATH}>)

if(NOT COMP_SINGLE_HEADER)
target_compile_definitions(${target} ${include_policy} ${_COMP_HEADERS})
endif()

# first explicit option, then implicit; 17 over 14 over 11
if(COMP_CPP17)
set(${target}_COMP_COMPILE_OPTIONS ${COMP_CPP17_FLAG}
CACHE STRING "required compile options for target")
CACHE STRING "required compile options for target")
elseif(COMP_CPP14)
set(${target}_COMP_COMPILE_OPTIONS ${COMP_CPP14_FLAG}
CACHE STRING "required compile options for target")
CACHE STRING "required compile options for target")
elseif(COMP_CPP11)
set(${target}_COMP_COMPILE_OPTIONS ${COMP_CPP14_FLAG}
CACHE STRING "required compile options for target")
CACHE STRING "required compile options for target")
elseif(need_COMP_CPP17_FLAG)
set(${target}_COMP_COMPILE_OPTIONS ${COMP_CPP17_FLAG}
CACHE STRING "required compile options for target")
CACHE STRING "required compile options for target")
elseif(need_COMP_CPP14_FLAG)
set(${target}_COMP_COMPILE_OPTIONS ${COMP_CPP14_FLAG}
CACHE STRING "required compile options for target")
CACHE STRING "required compile options for target")
elseif(need_COMP_CPP11_FLAG)
set(${target}_COMP_COMPILE_OPTIONS ${COMP_CPP11_FLAG}
CACHE STRING "required compile options for target")
CACHE STRING "required compile options for target")
endif()

# actually set option
Expand Down Expand Up @@ -346,7 +366,7 @@ function(comp_workaround name workaround standard)
_comp_handle_feature(${feature})
endforeach()
set(${name}_requires ${requires} PARENT_SCOPE)
set(headers "${headers}" PARENT_SCOPE)
set(_COMP_HEADERS "${_COMP_HEADERS}" PARENT_SCOPE)
endfunction()

# EXTERNAL; feature module
Expand Down
7 changes: 7 additions & 0 deletions cpp11_lang.cmake
Expand Up @@ -5,17 +5,24 @@
comp_fetch_include(cpp11_lang/alias_template)
comp_fetch_include(cpp11_lang/alignas)
comp_fetch_include(cpp11_lang/alignof)
comp_fetch_include(cpp11_lang/auto_type)
comp_fetch_include(cpp11_lang/constexpr)
comp_fetch_include(cpp11_lang/decltype)
comp_fetch_include(cpp11_lang/default_function_template_args)
comp_fetch_include(cpp11_lang/delete_fnc)
comp_fetch_include(cpp11_lang/explicit_conversion_op)
comp_fetch_include(cpp11_lang/final)
comp_fetch_include(cpp11_lang/inline_namespace)
comp_fetch_include(cpp11_lang/lambdas)
comp_fetch_include(cpp11_lang/literal_op)
comp_fetch_include(cpp11_lang/noexcept)
comp_fetch_include(cpp11_lang/noreturn)
comp_fetch_include(cpp11_lang/nullptr)
comp_fetch_include(cpp11_lang/override)
comp_fetch_include(cpp11_lang/parameter_pack)
comp_fetch_include(cpp11_lang/range_for)
comp_fetch_include(cpp11_lang/right_angle_brackets)
comp_fetch_include(cpp11_lang/rvalue_ref)
comp_fetch_include(cpp11_lang/static_assert)
comp_fetch_include(cpp11_lang/strong_enum)
comp_fetch_include(cpp11_lang/thread_local)
10 changes: 10 additions & 0 deletions cpp11_lang/auto_type.cmake
@@ -0,0 +1,10 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

comp_feature(auto_type "int main() {auto i = 0.f;}" COMP_CPP11_FLAG)
25 changes: 25 additions & 0 deletions cpp11_lang/default_function_template_args.cmake
@@ -0,0 +1,25 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

# Taken from:
# http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226
comp_feature(default_function_template_args
"template <class T, class U = double>
void f(T t = 0, U u = 0)
{ (void)t; (void) u; }
int main()
{
f(1, 'c'); // f<int,char>(1,'c')
f(1); // f<int,double>(1,0)
// f(); // error: T cannot be deduced
f<int>(); // f<int,double>(0,0)
f<int,char>(); // f<int,char>(0,0)
return 0;
}" COMP_CPP11_FLAG)
11 changes: 11 additions & 0 deletions cpp11_lang/enum_class.cmake
@@ -0,0 +1,11 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

comp_feature(enum_class "enum class Colour: unsigned int { Red = 1, Blue = 3 };
int main() { const Colour c = Colour::Red; }" COMP_CPP11_FLAG)
27 changes: 27 additions & 0 deletions cpp11_lang/lambdas.cmake
@@ -0,0 +1,27 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

# Taken from:
# http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226
comp_feature(lambdas
"#include <algorithm>
#include <cmath>
void abssort(float *x, unsigned N) {
std::sort(x, x+N,
[](float a, float b) {
return std::abs(a) < std::abs(b);
});
}
int main()
{
float* arr = new float[20];
abssort(arr, 20);
}
" COMP_CPP11_FLAG)
24 changes: 24 additions & 0 deletions cpp11_lang/parameter_pack.cmake
@@ -0,0 +1,24 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

comp_feature(parameter_pack "template <typename... Tn>
void f(const Tn&... args);
template <typename T, typename... Tn>
void f(const T& t, const Tn&... rest)
{ (void)t; f(rest...); }
template <>
void f()
{ }
int main()
{ f(1, 2); f(\"one\", 2); f(1); f(); }
" COMP_CPP11_FLAG)

16 changes: 16 additions & 0 deletions cpp11_lang/range_for.cmake
@@ -0,0 +1,16 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

comp_feature(range_for "int main()
{
int array[3] = { 1, 2, 3 };
for (int& x : array)
x *= 2;
}" COMP_CPP11_FLAG)

14 changes: 14 additions & 0 deletions cpp11_lang/right_angle_brackets.cmake
@@ -0,0 +1,14 @@
# Copyright (C) 2015-2016 Jonathan Müller <jonathanmueller.dev@gmail.com>
# This file is subject to the license terms in the LICENSE file
# found in the top-level directory of this distribution.

if(NOT COMP_API_VERSION)
message(FATAL_ERROR "needs newer comp_base.cmake version")
endif()
comp_api_version(1)

comp_feature(right_angle_brackets "#include <vector>
typedef std::vector<std::vector<int> > Table; // OK
typedef std::vector<std::vector<bool>> Flags; // OK (Error < CPP11)
int main(){}" COMP_CPP11_FLAG)

0 comments on commit 99ed74b

Please sign in to comment.