From 913394af249d6b1892a6e609d2abfed001fa1dc4 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sun, 13 Oct 2013 02:00:24 +0200 Subject: cmTarget: Add CXX_STANDARD and CXX_EXTENSION target properties. These are used to determine whether to add -std=c++11, -std=gnu++11 etc flags on the compile line. --- Help/manual/cmake-properties.7.rst | 2 ++ Help/manual/cmake-variables.7.rst | 2 ++ Help/prop_tgt/CXX_EXTENSIONS.rst | 8 +++++++ Help/prop_tgt/CXX_STANDARD.rst | 14 +++++++++++++ Help/release/dev/compile-language-features.rst | 8 +++++++ Help/variable/CMAKE_CXX_EXTENSIONS.rst | 8 +++++++ Help/variable/CMAKE_CXX_STANDARD.rst | 8 +++++++ Source/cmLocalGenerator.cxx | 29 ++++++++++++++++++++++++++ Source/cmLocalGenerator.h | 2 ++ Source/cmTarget.cxx | 2 ++ Tests/CMakeLists.txt | 13 ++++++++++++ Tests/CxxDialect/CMakeLists.txt | 14 +++++++++++++ Tests/CxxDialect/use_constexpr.cxx | 10 +++++++++ Tests/CxxDialect/use_constexpr_and_typeof.cxx | 11 ++++++++++ Tests/CxxDialect/use_typeof.cxx | 6 ++++++ 15 files changed, 137 insertions(+) create mode 100644 Help/prop_tgt/CXX_EXTENSIONS.rst create mode 100644 Help/prop_tgt/CXX_STANDARD.rst create mode 100644 Help/release/dev/compile-language-features.rst create mode 100644 Help/variable/CMAKE_CXX_EXTENSIONS.rst create mode 100644 Help/variable/CMAKE_CXX_STANDARD.rst create mode 100644 Tests/CxxDialect/CMakeLists.txt create mode 100644 Tests/CxxDialect/use_constexpr.cxx create mode 100644 Tests/CxxDialect/use_constexpr_and_typeof.cxx create mode 100644 Tests/CxxDialect/use_typeof.cxx diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst index fd16eb9..9ea50de 100644 --- a/Help/manual/cmake-properties.7.rst +++ b/Help/manual/cmake-properties.7.rst @@ -106,6 +106,8 @@ Properties on Targets /prop_tgt/COMPILE_PDB_OUTPUT_DIRECTORY_CONFIG /prop_tgt/CONFIG_OUTPUT_NAME /prop_tgt/CONFIG_POSTFIX + /prop_tgt/CXX_STANDARD + /prop_tgt/CXX_EXTENSIONS /prop_tgt/DEBUG_POSTFIX /prop_tgt/DEFINE_SYMBOL /prop_tgt/EchoString diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index 4fac6a5..62a7cce 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -257,6 +257,8 @@ Variables for Languages :maxdepth: 1 /variable/CMAKE_COMPILER_IS_GNULANG + /variable/CMAKE_CXX_STANDARD + /variable/CMAKE_CXX_EXTENSIONS /variable/CMAKE_Fortran_MODDIR_DEFAULT /variable/CMAKE_Fortran_MODDIR_FLAG /variable/CMAKE_Fortran_MODOUT_FLAG diff --git a/Help/prop_tgt/CXX_EXTENSIONS.rst b/Help/prop_tgt/CXX_EXTENSIONS.rst new file mode 100644 index 0000000..b9c9931 --- /dev/null +++ b/Help/prop_tgt/CXX_EXTENSIONS.rst @@ -0,0 +1,8 @@ +CXX_EXTENSIONS +-------------- + +Boolean specifying whether compiler specific extensions are requested. + +This property specifies whether compiler specific extensions should be +used. For some compilers, this results in adding a flag such +as ``-std=gnu++11`` instead of ``-std=c++11`` to the compile line. diff --git a/Help/prop_tgt/CXX_STANDARD.rst b/Help/prop_tgt/CXX_STANDARD.rst new file mode 100644 index 0000000..e1b6e78 --- /dev/null +++ b/Help/prop_tgt/CXX_STANDARD.rst @@ -0,0 +1,14 @@ +CXX_STANDARD +------------ + +The C++ standard whose features are required to build this target. + +This property specifies the C++ standard whose features are required +to build this target. For some compilers, this results in adding a +flag such as ``-std=c++11`` to the compile line. + +Supported values are ``98`` and ``11``. + +This property is initialized by the value of +the :variable:`CMAKE_CXX_STANDARD` variable if it is set when a target +is created. diff --git a/Help/release/dev/compile-language-features.rst b/Help/release/dev/compile-language-features.rst new file mode 100644 index 0000000..f03e1a3 --- /dev/null +++ b/Help/release/dev/compile-language-features.rst @@ -0,0 +1,8 @@ +target-language-features +------------------------ + +* New :prop_tgt:`CXX_STANDARD` and :prop_tgt:`CXX_EXTENSIONS` target + properties may specify values which CMake uses to compute required + compile options such as ``-std=c++11`` or ``-std=gnu++11``. The + :variable:`CMAKE_CXX_STANDARD` and :variable:`CMAKE_CXX_EXTENSIONS` + variables may be set to initialize the target properties. diff --git a/Help/variable/CMAKE_CXX_EXTENSIONS.rst b/Help/variable/CMAKE_CXX_EXTENSIONS.rst new file mode 100644 index 0000000..734d508 --- /dev/null +++ b/Help/variable/CMAKE_CXX_EXTENSIONS.rst @@ -0,0 +1,8 @@ +CMAKE_CXX_EXTENSIONS +-------------------- + +Default value for ``CXX_EXTENSIONS`` property of targets. + +This variable is used to initialize the :prop_tgt:`CXX_EXTENSIONS` +property on all targets. See that target property for additional +information. diff --git a/Help/variable/CMAKE_CXX_STANDARD.rst b/Help/variable/CMAKE_CXX_STANDARD.rst new file mode 100644 index 0000000..5fd4138 --- /dev/null +++ b/Help/variable/CMAKE_CXX_STANDARD.rst @@ -0,0 +1,8 @@ +CMAKE_CXX_STANDARD +------------------ + +Default value for ``CXX_STANDARD`` property of targets. + +This variable is used to initialize the :prop_tgt:`CXX_STANDARD` +property on all targets. See that target property for additional +information. diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 1e65a02..779a2b8 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1459,6 +1459,7 @@ void cmLocalGenerator::AddCompileOptions( this->AppendFlagEscape(flags, *i); } } + this->AddCompilerRequirementFlag(flags, target, lang); } //---------------------------------------------------------------------------- @@ -2128,6 +2129,34 @@ void cmLocalGenerator::AddSharedFlags(std::string& flags, } } +//---------------------------------------------------------------------------- +void cmLocalGenerator:: +AddCompilerRequirementFlag(std::string &flags, cmTarget* target, + const std::string& lang) +{ + if (lang.empty()) + { + return; + } + std::string stdProp = lang + "_STANDARD"; + const char *standard = target->GetProperty(stdProp); + if (!standard) + { + return; + } + std::string extProp = lang + "_EXTENSIONS"; + bool ext = target->GetPropertyAsBool(extProp); + std::string type = ext ? "EXTENSION" : "STANDARD"; + + std::string compile_option = + "CMAKE_" + lang + std::string(standard) + + "_" + type + "_COMPILE_OPTION"; + if (const char *opt = target->GetMakefile()->GetDefinition(compile_option)) + { + this->AppendFlags(flags, opt); + } +} + static void AddVisibilityCompileOption(std::string &flags, cmTarget* target, cmLocalGenerator *lg, const std::string& lang) diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 8f30149..12a94e4 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -149,6 +149,8 @@ public: const std::string& lang); void AddConfigVariableFlags(std::string& flags, const std::string& var, const std::string& config); + void AddCompilerRequirementFlag(std::string &flags, cmTarget* target, + const std::string& lang); ///! Append flags to a string. virtual void AppendFlags(std::string& flags, const char* newFlags); virtual void AppendFlagEscape(std::string& flags, diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index d28ac3f..53c0205 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -307,6 +307,8 @@ void cmTarget::SetMakefile(cmMakefile* mf) this->SetPropertyDefault("MACOSX_BUNDLE", 0); this->SetPropertyDefault("MACOSX_RPATH", 0); this->SetPropertyDefault("NO_SYSTEM_FROM_IMPORTED", 0); + this->SetPropertyDefault("CXX_STANDARD", 0); + this->SetPropertyDefault("CXX_EXTENSIONS", 0); } // Collect the set of configuration types. diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 0e60ed1..aaa24c7 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -283,6 +283,19 @@ if(BUILD_TESTING) ADD_TEST_MACRO(ConfigSources ConfigSources) endif() ADD_TEST_MACRO(SourcesProperty SourcesProperty) + if(CMAKE_CXX_COMPILER_ID STREQUAL GNU + AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) + set(runCxxDialectTest 1) + endif() + if(CMAKE_CXX_COMPILER_ID STREQUAL Clang + AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 2.9) + if(NOT APPLE OR POLICY CMP0025) + set(runCxxDialectTest 1) + endif() + endif() + if(runCxxDialectTest) + ADD_TEST_MACRO(CxxDialect CxxDialect) + endif() set_tests_properties(EmptyLibrary PROPERTIES PASS_REGULAR_EXPRESSION "CMake Error: CMake can not determine linker language for target: test") ADD_TEST_MACRO(CrossCompile CrossCompile) diff --git a/Tests/CxxDialect/CMakeLists.txt b/Tests/CxxDialect/CMakeLists.txt new file mode 100644 index 0000000..0eb6f8f --- /dev/null +++ b/Tests/CxxDialect/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.8.12) +cmake_policy(SET CMP0025 NEW) +project(CxxDialect) + +add_executable(use_typeof use_typeof.cxx) +set_property(TARGET use_typeof PROPERTY CXX_STANDARD 98) +set_property(TARGET use_typeof PROPERTY CXX_EXTENSIONS ON) + +add_executable(use_constexpr use_constexpr.cxx) +set_property(TARGET use_constexpr PROPERTY CXX_STANDARD 11) + +add_executable(CxxDialect use_constexpr_and_typeof.cxx) +set_property(TARGET CxxDialect PROPERTY CXX_STANDARD 11) +set_property(TARGET CxxDialect PROPERTY CXX_EXTENSIONS ON) diff --git a/Tests/CxxDialect/use_constexpr.cxx b/Tests/CxxDialect/use_constexpr.cxx new file mode 100644 index 0000000..30ccc4c --- /dev/null +++ b/Tests/CxxDialect/use_constexpr.cxx @@ -0,0 +1,10 @@ + +constexpr int foo() +{ + return 0; +} + +int main(int argc, char**) +{ + return foo(); +} diff --git a/Tests/CxxDialect/use_constexpr_and_typeof.cxx b/Tests/CxxDialect/use_constexpr_and_typeof.cxx new file mode 100644 index 0000000..af217b6 --- /dev/null +++ b/Tests/CxxDialect/use_constexpr_and_typeof.cxx @@ -0,0 +1,11 @@ + +constexpr int foo() +{ + return 0; +} + +int main(int argc, char**) +{ + typeof(argc) ret = foo(); + return ret; +} diff --git a/Tests/CxxDialect/use_typeof.cxx b/Tests/CxxDialect/use_typeof.cxx new file mode 100644 index 0000000..dabb61f --- /dev/null +++ b/Tests/CxxDialect/use_typeof.cxx @@ -0,0 +1,6 @@ + +int main(int argc, char**) +{ + typeof(argc) ret = 0; + return ret; +} -- cgit v0.12