Add target_compile_features command.
This can be used to set the compiler features required by particular targets. An error is issued at CMake time if the compiler does not support the required feature. If a language dialect flag is required by the features used, that will be added automatically. Base the target_compile_features command on cmTargetPropCommandBase. This gives us 'free' handling of IMPORTED, ALIAS, INTERFACE, non-compilable and missing targets.
This commit is contained in:
parent
4e6ca50445
commit
8ed59fc207
|
@ -0,0 +1,30 @@
|
|||
target_compile_features
|
||||
-----------------------
|
||||
|
||||
Add expected compiler features to a target.
|
||||
|
||||
::
|
||||
|
||||
target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> <feature> [...])
|
||||
|
||||
Specify compiler features required when compiling a given target. If the
|
||||
feature is not listed in the :variable:`CMAKE_CXX_COMPILE_FEATURES` variable,
|
||||
then an error will be reported by CMake. If the use of the feature requires
|
||||
an additional compiler flag, such as ``-std=c++11``, the flag will be added
|
||||
automatically.
|
||||
|
||||
The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
|
||||
specify the scope of the features. ``PRIVATE`` and ``PUBLIC`` items will
|
||||
populate the :prop_tgt:`COMPILE_FEATURES` property of ``<target>``.
|
||||
``PUBLIC`` and ``INTERFACE`` items will populate the
|
||||
:prop_tgt:`INTERFACE_COMPILE_FEATURES` property of ``<target>``. Repeated
|
||||
calls for the same ``<target>`` append items.
|
||||
|
||||
The named ``<target>`` must have been created by a command such as
|
||||
:command:`add_executable` or :command:`add_library` and must not be
|
||||
an ``IMPORTED`` target.
|
||||
|
||||
Arguments to ``target_compile_features`` may use "generator expressions"
|
||||
with the syntax ``$<...>``.
|
||||
See the :manual:`cmake-generator-expressions(7)` manual for available
|
||||
expressions.
|
|
@ -91,6 +91,7 @@ These commands may be used freely in CMake projects.
|
|||
/command/source_group
|
||||
/command/string
|
||||
/command/target_compile_definitions
|
||||
/command/target_compile_features
|
||||
/command/target_compile_options
|
||||
/command/target_include_directories
|
||||
/command/target_link_libraries
|
||||
|
|
|
@ -12,3 +12,7 @@ target-language-features
|
|||
information to ensure that the compiler in use is capable of building
|
||||
the target, and to add any necessary compile flags to support language
|
||||
features.
|
||||
|
||||
* New :command:`target_compile_features` command allows populating the
|
||||
:prop_tgt:`COMPILE_FEATURES` target property, just like any other
|
||||
build variable.
|
||||
|
|
|
@ -7,6 +7,9 @@ The features listed in this variable may be known to be available to the
|
|||
C++ compiler. If the feature is available with the C++ compiler, it will
|
||||
be listed in the :variable:`CMAKE_CXX_COMPILE_FEATURES` variable.
|
||||
|
||||
The features listed here may be used with the :command:`target_compile_features`
|
||||
command.
|
||||
|
||||
The features known to this version of CMake are:
|
||||
|
||||
``cxx_auto_type``
|
||||
|
|
|
@ -346,6 +346,7 @@ foreach(command_file
|
|||
cmSourceGroupCommand
|
||||
cmSubdirDependsCommand
|
||||
cmTargetCompileDefinitionsCommand
|
||||
cmTargetCompileFeaturesCommand
|
||||
cmTargetCompileOptionsCommand
|
||||
cmTargetIncludeDirectoriesCommand
|
||||
cmTargetSourcesCommand
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
/*============================================================================
|
||||
CMake - Cross Platform Makefile Generator
|
||||
Copyright 2013 Stephen Kelly <steveire@gmail.com>
|
||||
|
||||
Distributed under the OSI-approved BSD License (the "License");
|
||||
see accompanying file Copyright.txt for details.
|
||||
|
||||
This software is distributed WITHOUT ANY WARRANTY; without even the
|
||||
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the License for more information.
|
||||
============================================================================*/
|
||||
#include "cmTargetCompileFeaturesCommand.h"
|
||||
|
||||
bool cmTargetCompileFeaturesCommand::InitialPass(
|
||||
std::vector<std::string> const& args,
|
||||
cmExecutionStatus &)
|
||||
{
|
||||
return this->HandleArguments(args, "COMPILE_FEATURES", NO_FLAGS);
|
||||
}
|
||||
|
||||
void cmTargetCompileFeaturesCommand
|
||||
::HandleImportedTarget(const std::string &tgt)
|
||||
{
|
||||
cmOStringStream e;
|
||||
e << "Cannot specify compile features for imported target \""
|
||||
<< tgt << "\".";
|
||||
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
|
||||
}
|
||||
|
||||
void cmTargetCompileFeaturesCommand
|
||||
::HandleMissingTarget(const std::string &name)
|
||||
{
|
||||
cmOStringStream e;
|
||||
e << "Cannot specify compile features for target \"" << name << "\" "
|
||||
"which is not built by this project.";
|
||||
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
std::string cmTargetCompileFeaturesCommand
|
||||
::Join(const std::vector<std::string> &content)
|
||||
{
|
||||
std::string defs;
|
||||
std::string sep;
|
||||
for(std::vector<std::string>::const_iterator it = content.begin();
|
||||
it != content.end(); ++it)
|
||||
{
|
||||
defs += sep + *it;
|
||||
sep = ";";
|
||||
}
|
||||
return defs;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool cmTargetCompileFeaturesCommand
|
||||
::HandleDirectContent(cmTarget *tgt, const std::vector<std::string> &content,
|
||||
bool, bool)
|
||||
{
|
||||
for(std::vector<std::string>::const_iterator it = content.begin();
|
||||
it != content.end(); ++it)
|
||||
{
|
||||
std::string error;
|
||||
if(!this->Makefile->AddRequiredTargetFeature(tgt, *it, &error))
|
||||
{
|
||||
this->SetError(error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
/*============================================================================
|
||||
CMake - Cross Platform Makefile Generator
|
||||
Copyright 2013 Stephen Kelly <steveire@gmail.com>
|
||||
|
||||
Distributed under the OSI-approved BSD License (the "License");
|
||||
see accompanying file Copyright.txt for details.
|
||||
|
||||
This software is distributed WITHOUT ANY WARRANTY; without even the
|
||||
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
See the License for more information.
|
||||
============================================================================*/
|
||||
#ifndef cmTargetCompileFeaturesCommand_h
|
||||
#define cmTargetCompileFeaturesCommand_h
|
||||
|
||||
#include "cmTargetPropCommandBase.h"
|
||||
|
||||
class cmTargetCompileFeaturesCommand : public cmTargetPropCommandBase
|
||||
{
|
||||
virtual cmCommand* Clone()
|
||||
{
|
||||
return new cmTargetCompileFeaturesCommand;
|
||||
}
|
||||
|
||||
virtual bool InitialPass(std::vector<std::string> const& args,
|
||||
cmExecutionStatus &status);
|
||||
|
||||
virtual std::string GetName() const { return "target_compile_features";}
|
||||
|
||||
cmTypeMacro(cmTargetCompileFeaturesCommand, cmTargetPropCommandBase);
|
||||
|
||||
private:
|
||||
virtual void HandleImportedTarget(const std::string &tgt);
|
||||
virtual void HandleMissingTarget(const std::string &name);
|
||||
|
||||
virtual bool HandleDirectContent(cmTarget *tgt,
|
||||
const std::vector<std::string> &content,
|
||||
bool prepend, bool system);
|
||||
virtual std::string Join(const std::vector<std::string> &content);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
cmake_minimum_required(VERSION 3.0)
|
||||
project(target_compile_features)
|
||||
|
||||
set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||
|
||||
add_executable(target_compile_features main.cpp)
|
||||
target_compile_features(target_compile_features
|
||||
PRIVATE cxx_auto_type
|
||||
)
|
||||
|
||||
add_library(lib_auto_type lib_auto_type.cpp)
|
||||
target_compile_features(lib_auto_type
|
||||
PUBLIC cxx_auto_type
|
||||
)
|
||||
|
||||
add_executable(lib_user lib_user.cpp)
|
||||
target_link_libraries(lib_user lib_auto_type)
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
int main(int, char **)
|
||||
{
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
int getAutoTypeImpl()
|
||||
{
|
||||
auto i = 0;
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
int getAutoTypeImpl();
|
||||
|
||||
int getAutoType()
|
||||
{
|
||||
auto i = getAutoTypeImpl();
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
|
||||
#include "lib_auto_type.h"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return getAutoType();
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
int main(int, char **)
|
||||
{
|
||||
auto i = 0;
|
||||
return i;
|
||||
}
|
|
@ -200,6 +200,7 @@ if(BUILD_TESTING)
|
|||
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU
|
||||
AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
|
||||
ADD_TEST_MACRO(CompileFeatures CompileFeatures)
|
||||
ADD_TEST_MACRO(CMakeCommands.target_compile_features target_compile_features)
|
||||
endif()
|
||||
# assume no resources building to test
|
||||
set(TEST_RESOURCES FALSE)
|
||||
|
|
|
@ -125,6 +125,10 @@ endif()
|
|||
add_RunCMake_test(File_Generate)
|
||||
add_RunCMake_test(ExportWithoutLanguage)
|
||||
add_RunCMake_test(target_link_libraries)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL GNU AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
|
||||
add_RunCMake_test(target_compile_features)
|
||||
endif()
|
||||
add_RunCMake_test(CheckModules)
|
||||
add_RunCMake_test(CommandLine)
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,5 @@
|
|||
CMake Error at NotAFeature_OriginDebug_target_compile_features.cmake:4 \(target_compile_features\):
|
||||
target_compile_features specified unknown feature "not_a_feature" for
|
||||
target "somelib".
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
set(CMAKE_DEBUG_TARGET_PROPERTIES COMPILE_FEATURES)
|
||||
add_library(somelib STATIC empty.cpp)
|
||||
target_compile_features(somelib PRIVATE not_a_feature)
|
|
@ -6,3 +6,4 @@ run_cmake(NotAFeatureTransitive)
|
|||
run_cmake(NotAFeature_OriginDebug)
|
||||
run_cmake(NotAFeature_OriginDebugGenex)
|
||||
run_cmake(NotAFeature_OriginDebugTransitive)
|
||||
run_cmake(NotAFeature_OriginDebug_target_compile_features)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
cmake_minimum_required(VERSION 3.0)
|
||||
project(${RunCMake_TEST} CXX)
|
||||
include(${RunCMake_TEST}.cmake)
|
|
@ -0,0 +1,11 @@
|
|||
include(RunCMake)
|
||||
|
||||
run_cmake(not_enough_args)
|
||||
run_cmake(alias_target)
|
||||
run_cmake(utility_target)
|
||||
run_cmake(invalid_args)
|
||||
run_cmake(invalid_args_on_interface)
|
||||
run_cmake(imported_target)
|
||||
run_cmake(no_target)
|
||||
run_cmake(not_a_cxx_feature)
|
||||
run_cmake(no_matching_cxx_feature)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,4 @@
|
|||
CMake Error at alias_target.cmake:4 \(target_compile_features\):
|
||||
target_compile_features can not be used on an ALIAS target.
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
add_executable(main empty.cpp)
|
||||
add_executable(Alias::Main ALIAS main)
|
||||
target_compile_features(Alias::Main PRIVATE cxx_delegating_constructors)
|
|
@ -0,0 +1,7 @@
|
|||
#ifdef _WIN32
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
int empty()
|
||||
{
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,4 @@
|
|||
CMake Error at imported_target.cmake:3 \(target_compile_features\):
|
||||
Cannot specify compile features for imported target "main".
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
add_library(main INTERFACE IMPORTED)
|
||||
target_compile_features(main INTERFACE cxx_delegating_constructors)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,4 @@
|
|||
CMake Error at invalid_args.cmake:3 \(target_compile_features\):
|
||||
target_compile_features called with invalid arguments
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
add_executable(main empty.cpp)
|
||||
target_compile_features(main INVALID cxx_delegating_constructors)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,5 @@
|
|||
CMake Error at invalid_args_on_interface.cmake:3 \(target_compile_features\):
|
||||
target_compile_features may only be set INTERFACE properties on INTERFACE
|
||||
targets
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
add_library(main INTERFACE)
|
||||
target_compile_features(main PRIVATE cxx_delegating_constructors)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,8 @@
|
|||
CMake Error at no_matching_cxx_feature.cmake:[0-9][0-9]? \((target_compile_features|message)\):
|
||||
The compiler feature "[^"]+" is not known to compiler
|
||||
|
||||
"[^"]*"
|
||||
|
||||
version *[.0-9]+\.
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,26 @@
|
|||
|
||||
if (NOT ";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";gnu_cxx_typeof;"
|
||||
AND NOT ";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";msvc_cxx_sealed;" )
|
||||
# Simulate passing the test.
|
||||
message(SEND_ERROR
|
||||
"The compiler feature \"gnu_cxx_dummy\" is not known to compiler\n\"GNU\"\nversion 4.8.1."
|
||||
)
|
||||
return()
|
||||
endif()
|
||||
|
||||
if (";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";gnu_cxx_typeof;")
|
||||
set(feature msvc_cxx_sealed)
|
||||
if (";${CMAKE_CXX_COMPILE_FEATURES};" MATCHES ";msvc_cxx_sealed;")
|
||||
# If a compiler supports both extensions, remove one of them.
|
||||
list(REMOVE_ITEM CMAKE_CXX_COMPILE_FEATURES msvc_cxx_sealed)
|
||||
endif()
|
||||
else()
|
||||
set(feature gnu_cxx_typeof)
|
||||
endif()
|
||||
|
||||
add_executable(main empty.cpp)
|
||||
|
||||
target_compile_features(main
|
||||
PRIVATE
|
||||
${feature}
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,5 @@
|
|||
CMake Error at no_target.cmake:2 \(target_compile_features\):
|
||||
Cannot specify compile features for target "main" which is not built by
|
||||
this project.
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
target_compile_features(main INTERFACE cxx_delegating_constructors)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,5 @@
|
|||
CMake Error at not_a_cxx_feature.cmake:3 \(target_compile_features\):
|
||||
target_compile_features specified unknown feature "cxx_not_a_feature" for
|
||||
target "main".
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,6 @@
|
|||
|
||||
add_executable(main empty.cpp)
|
||||
target_compile_features(main
|
||||
PRIVATE
|
||||
cxx_not_a_feature
|
||||
)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,4 @@
|
|||
CMake Error at not_enough_args.cmake:3 \(target_compile_features\):
|
||||
target_compile_features called with incorrect number of arguments
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,3 @@
|
|||
|
||||
add_executable(main empty.cpp)
|
||||
target_compile_features(main)
|
|
@ -0,0 +1 @@
|
|||
1
|
|
@ -0,0 +1,4 @@
|
|||
CMake Error at utility_target.cmake:4 \(target_compile_features\):
|
||||
target_compile_features called with non-compilable target type
|
||||
Call Stack \(most recent call first\):
|
||||
CMakeLists.txt:3 \(include\)
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
add_custom_target(utility)
|
||||
|
||||
target_compile_features(utility PRIVATE cxx_delegating_constructors)
|
Loading…
Reference in New Issue