1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmSetTargetPropertiesCommand.h"
4 
5 #include <algorithm>
6 #include <iterator>
7 
8 #include "cmExecutionStatus.h"
9 #include "cmMakefile.h"
10 #include "cmStringAlgorithms.h"
11 #include "cmTarget.h"
12 
cmSetTargetPropertiesCommand(std::vector<std::string> const & args,cmExecutionStatus & status)13 bool cmSetTargetPropertiesCommand(std::vector<std::string> const& args,
14                                   cmExecutionStatus& status)
15 {
16   if (args.size() < 2) {
17     status.SetError("called with incorrect number of arguments");
18     return false;
19   }
20 
21   // first identify the properties arguments
22   auto propsIter = std::find(args.begin(), args.end(), "PROPERTIES");
23   if (propsIter == args.end() || propsIter + 1 == args.end()) {
24     status.SetError("called with illegal arguments, maybe missing a "
25                     "PROPERTIES specifier?");
26     return false;
27   }
28 
29   if (std::distance(propsIter, args.end()) % 2 != 1) {
30     status.SetError("called with incorrect number of arguments.");
31     return false;
32   }
33 
34   cmMakefile& mf = status.GetMakefile();
35 
36   // loop over all the targets
37   for (const std::string& tname : cmStringRange{ args.begin(), propsIter }) {
38     if (mf.IsAlias(tname)) {
39       status.SetError("can not be used on an ALIAS target.");
40       return false;
41     }
42     if (cmTarget* target = mf.FindTargetToUse(tname)) {
43       // loop through all the props and set them
44       for (auto k = propsIter + 1; k != args.end(); k += 2) {
45         target->SetProperty(*k, *(k + 1));
46         target->CheckProperty(*k, &mf);
47       }
48     } else {
49       status.SetError(
50         cmStrCat("Can not find target to add properties to: ", tname));
51       return false;
52     }
53   }
54   return true;
55 }
56