浏览代码

target_sources: New command to add sources to target.

Stephen Kelly 12 年之前
父节点
当前提交
9407174b1a

+ 28 - 0
Help/command/target_sources.rst

@@ -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.

+ 1 - 0
Help/manual/cmake-commands.7.rst

@@ -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

+ 5 - 0
Help/release/dev/target_sources-command.rst

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

+ 1 - 0
Source/CMakeLists.txt

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

+ 64 - 0
Source/cmTargetSourcesCommand.cxx

@@ -0,0 +1,64 @@
+/*============================================================================
+  CMake - Cross Platform Makefile Generator
+  Copyright 2014 Stephen Kelly <[email protected]>
+
+  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());
+}

+ 55 - 0
Source/cmTargetSourcesCommand.h

@@ -0,0 +1,55 @@
+/*============================================================================
+  CMake - Cross Platform Makefile Generator
+  Copyright 2014 Stephen Kelly <[email protected]>
+
+  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

+ 8 - 0
Tests/RunCMake/TargetSources/OriginDebug-stderr.txt

@@ -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\)
 +

+ 2 - 0
Tests/RunCMake/TargetSources/OriginDebug.cmake

@@ -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)

+ 9 - 0
Tests/RunCMake/TargetSources/OriginDebugIDE-stderr.txt

@@ -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\)

+ 7 - 0
Tests/RunCMake/TargetSources/empty_4.cpp

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