From 9407174b1a0f1b4f8edf7fe08cc85ab6e990d59f Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Mon, 8 Jul 2013 20:18:42 +0200 Subject: target_sources: New command to add sources to target. --- Help/command/target_sources.rst | 28 ++++++++++ Help/manual/cmake-commands.7.rst | 1 + Help/release/dev/target_sources-command.rst | 5 ++ Source/CMakeLists.txt | 1 + Source/cmTargetSourcesCommand.cxx | 64 ++++++++++++++++++++++ Source/cmTargetSourcesCommand.h | 55 +++++++++++++++++++ .../RunCMake/TargetSources/OriginDebug-stderr.txt | 8 +++ Tests/RunCMake/TargetSources/OriginDebug.cmake | 2 + .../TargetSources/OriginDebugIDE-stderr.txt | 9 +++ Tests/RunCMake/TargetSources/empty_4.cpp | 7 +++ 10 files changed, 180 insertions(+) create mode 100644 Help/command/target_sources.rst create mode 100644 Help/release/dev/target_sources-command.rst create mode 100644 Source/cmTargetSourcesCommand.cxx create mode 100644 Source/cmTargetSourcesCommand.h create mode 100644 Tests/RunCMake/TargetSources/empty_4.cpp diff --git a/Help/command/target_sources.rst b/Help/command/target_sources.rst new file mode 100644 index 0000000..ff756b4 --- /dev/null +++ b/Help/command/target_sources.rst @@ -0,0 +1,28 @@ +target_sources +-------------- + +Add sources to a target. + +:: + + target_sources( + [items1...] + [ [items2...] ...]) + +Specify sources to use when compiling a given target. The +named ```` 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 +````. ``PUBLIC`` and ``INTERFACE`` items will populate the +:prop_tgt:`INTERFACE_SOURCES` property of ````. The +following arguments specify sources. Repeated calls for the same +```` 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. diff --git a/Help/manual/cmake-commands.7.rst b/Help/manual/cmake-commands.7.rst index fb0d2b5..4b1dbed 100644 --- a/Help/manual/cmake-commands.7.rst +++ b/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 diff --git a/Help/release/dev/target_sources-command.rst b/Help/release/dev/target_sources-command.rst new file mode 100644 index 0000000..abfb303 --- /dev/null +++ b/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. diff --git a/Source/CMakeLists.txt b/Source/CMakeLists.txt index 27d099d..4c678d8 100644 --- a/Source/CMakeLists.txt +++ b/Source/CMakeLists.txt @@ -348,6 +348,7 @@ foreach(command_file cmTargetCompileDefinitionsCommand cmTargetCompileOptionsCommand cmTargetIncludeDirectoriesCommand + cmTargetSourcesCommand cmUseMangledMesaCommand cmUtilitySourceCommand cmVariableRequiresCommand diff --git a/Source/cmTargetSourcesCommand.cxx b/Source/cmTargetSourcesCommand.cxx new file mode 100644 index 0000000..e82b36d --- /dev/null +++ b/Source/cmTargetSourcesCommand.cxx @@ -0,0 +1,64 @@ +/*============================================================================ + CMake - Cross Platform Makefile Generator + Copyright 2014 Stephen Kelly + + 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 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 &content) +{ + std::string srcs; + std::string sep; + for(std::vector::const_iterator it = content.begin(); + it != content.end(); ++it) + { + srcs += sep + *it; + sep = ";"; + } + return srcs; +} + +//---------------------------------------------------------------------------- +void cmTargetSourcesCommand +::HandleDirectContent(cmTarget *tgt, const std::vector &content, + bool, bool) +{ + tgt->AppendProperty("SOURCES", this->Join(content).c_str()); +} diff --git a/Source/cmTargetSourcesCommand.h b/Source/cmTargetSourcesCommand.h new file mode 100644 index 0000000..dae78c4 --- /dev/null +++ b/Source/cmTargetSourcesCommand.h @@ -0,0 +1,55 @@ +/*============================================================================ + CMake - Cross Platform Makefile Generator + Copyright 2014 Stephen Kelly + + 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 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 &content, + bool prepend, bool system); + + virtual std::string Join(const std::vector &content); +}; + +#endif diff --git a/Tests/RunCMake/TargetSources/OriginDebug-stderr.txt b/Tests/RunCMake/TargetSources/OriginDebug-stderr.txt index 6b7453f..0200dcb 100644 --- a/Tests/RunCMake/TargetSources/OriginDebug-stderr.txt +++ b/Tests/RunCMake/TargetSources/OriginDebug-stderr.txt @@ -14,6 +14,14 @@ CMake Debug Log at OriginDebug.cmake:16 \(set_property\): 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\) ++ CMake Debug Log at OriginDebug.cmake:14 \(target_link_libraries\): Used sources for target OriginDebug: diff --git a/Tests/RunCMake/TargetSources/OriginDebug.cmake b/Tests/RunCMake/TargetSources/OriginDebug.cmake index 3fa5858..5fe9ba7 100644 --- a/Tests/RunCMake/TargetSources/OriginDebug.cmake +++ b/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) diff --git a/Tests/RunCMake/TargetSources/OriginDebugIDE-stderr.txt b/Tests/RunCMake/TargetSources/OriginDebugIDE-stderr.txt index 683c2e9..fad7073 100644 --- a/Tests/RunCMake/TargetSources/OriginDebugIDE-stderr.txt +++ b/Tests/RunCMake/TargetSources/OriginDebugIDE-stderr.txt @@ -16,6 +16,15 @@ 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\) ++ CMake Debug Log: Used sources for target OriginDebug: diff --git a/Tests/RunCMake/TargetSources/empty_4.cpp b/Tests/RunCMake/TargetSources/empty_4.cpp new file mode 100644 index 0000000..bfbbdde --- /dev/null +++ b/Tests/RunCMake/TargetSources/empty_4.cpp @@ -0,0 +1,7 @@ +#ifdef _WIN32 +__declspec(dllexport) +#endif +int empty() +{ + return 0; +} -- cgit v0.12