target_sources: New command to add sources to target.

This commit is contained in:
Stephen Kelly 2013-07-08 20:18:42 +02:00
parent 81ad69e056
commit 9407174b1a
10 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,28 @@
target_sources
--------------
Add sources to a target.
::
target_sources(<target>
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
Specify sources to use when compiling a given target. The
named ``<target>`` must have been created by a command such as
:command:`add_executable` or :command:`add_library` and must not be an
:prop_tgt:`IMPORTED Target`.
The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
specify the scope of the following arguments. ``PRIVATE`` and ``PUBLIC``
items will populate the :prop_tgt:`SOURCES` property of
``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
:prop_tgt:`INTERFACE_SOURCES` property of ``<target>``. The
following arguments specify sources. Repeated calls for the same
``<target>`` append items in the order called.
Arguments to ``target_sources`` may use "generator expressions"
with the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
manual for available expressions. See the :manual:`cmake-buildsystem(7)`
manual for more on defining buildsystem properties.

View File

@ -94,6 +94,7 @@ These commands may be used freely in CMake projects.
/command/target_compile_options
/command/target_include_directories
/command/target_link_libraries
/command/target_sources
/command/try_compile
/command/try_run
/command/unset

View File

@ -0,0 +1,5 @@
target_sources-command
----------------------
* The :command:`target_sources` command was added to add to the
:prop_tgt:`SOURCES` target property.

View File

@ -348,6 +348,7 @@ foreach(command_file
cmTargetCompileDefinitionsCommand
cmTargetCompileOptionsCommand
cmTargetIncludeDirectoriesCommand
cmTargetSourcesCommand
cmUseMangledMesaCommand
cmUtilitySourceCommand
cmVariableRequiresCommand

View File

@ -0,0 +1,64 @@
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2014 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 "cmTargetSourcesCommand.h"
#include "cmGeneratorExpression.h"
//----------------------------------------------------------------------------
bool cmTargetSourcesCommand
::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
{
return this->HandleArguments(args, "SOURCES");
}
//----------------------------------------------------------------------------
void cmTargetSourcesCommand
::HandleImportedTarget(const std::string &tgt)
{
cmOStringStream e;
e << "Cannot specify sources for imported target \""
<< tgt << "\".";
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
}
//----------------------------------------------------------------------------
void cmTargetSourcesCommand
::HandleMissingTarget(const std::string &name)
{
cmOStringStream e;
e << "Cannot specify sources for target \"" << name << "\" "
"which is not built by this project.";
this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
}
//----------------------------------------------------------------------------
std::string cmTargetSourcesCommand
::Join(const std::vector<std::string> &content)
{
std::string srcs;
std::string sep;
for(std::vector<std::string>::const_iterator it = content.begin();
it != content.end(); ++it)
{
srcs += sep + *it;
sep = ";";
}
return srcs;
}
//----------------------------------------------------------------------------
void cmTargetSourcesCommand
::HandleDirectContent(cmTarget *tgt, const std::vector<std::string> &content,
bool, bool)
{
tgt->AppendProperty("SOURCES", this->Join(content).c_str());
}

View File

@ -0,0 +1,55 @@
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2014 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 cmTargetSourcesCommand_h
#define cmTargetSourcesCommand_h
#include "cmTargetPropCommandBase.h"
//----------------------------------------------------------------------------
class cmTargetSourcesCommand : public cmTargetPropCommandBase
{
public:
/**
* This is a virtual constructor for the command.
*/
virtual cmCommand* Clone()
{
return new cmTargetSourcesCommand;
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
virtual bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus &status);
/**
* The name of the command as specified in CMakeList.txt.
*/
virtual std::string GetName() const { return "target_sources";}
cmTypeMacro(cmTargetSourcesCommand, cmTargetPropCommandBase);
private:
virtual void HandleImportedTarget(const std::string &tgt);
virtual void HandleMissingTarget(const std::string &name);
virtual void HandleDirectContent(cmTarget *tgt,
const std::vector<std::string> &content,
bool prepend, bool system);
virtual std::string Join(const std::vector<std::string> &content);
};
#endif

View File

@ -11,6 +11,14 @@ CMake Debug Log at OriginDebug.cmake:16 \(set_property\):
\* .*Tests/RunCMake/TargetSources/empty_3.cpp
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
+
CMake Debug Log at OriginDebug.cmake:20 \(target_sources\):
Used sources for target OriginDebug:
\* .*Tests/RunCMake/TargetSources/empty_4.cpp
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)
+

View File

@ -16,3 +16,5 @@ target_link_libraries(OriginDebug iface)
set_property(TARGET OriginDebug APPEND PROPERTY SOURCES
empty_3.cpp
)
target_sources(OriginDebug PRIVATE empty_4.cpp)

View File

@ -12,6 +12,15 @@ CMake Debug Log at OriginDebug.cmake:16 \(set_property\):
\* .*Tests/RunCMake/TargetSources/empty_3.cpp
Call Stack \(most recent call first\):
OriginDebugIDE.cmake:4 \(include\)
CMakeLists.txt:3 \(include\)
+
CMake Debug Log at OriginDebug.cmake:20 \(target_sources\):
Used sources for target OriginDebug:
\* .*Tests/RunCMake/TargetSources/empty_4.cpp
Call Stack \(most recent call first\):
OriginDebugIDE.cmake:4 \(include\)
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,7 @@
#ifdef _WIN32
__declspec(dllexport)
#endif
int empty()
{
return 0;
}