cmSetTargetPropertiesCommand.cxx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include <iterator>
  5. #include "cmAlgorithms.h"
  6. #include "cmExecutionStatus.h"
  7. #include "cmMakefile.h"
  8. #include "cmStringAlgorithms.h"
  9. #include "cmTarget.h"
  10. static bool SetOneTarget(const std::string& tname,
  11. std::vector<std::string>& propertyPairs,
  12. cmMakefile* mf);
  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. // first collect up the list of files
  21. std::vector<std::string> propertyPairs;
  22. int numFiles = 0;
  23. for (auto j = args.begin(); j != args.end(); ++j) {
  24. if (*j == "PROPERTIES") {
  25. // now loop through the rest of the arguments, new style
  26. ++j;
  27. if (std::distance(j, args.end()) % 2 != 0) {
  28. status.SetError("called with incorrect number of arguments.");
  29. return false;
  30. }
  31. cmAppend(propertyPairs, j, args.end());
  32. break;
  33. }
  34. numFiles++;
  35. }
  36. if (propertyPairs.empty()) {
  37. status.SetError("called with illegal arguments, maybe missing "
  38. "a PROPERTIES specifier?");
  39. return false;
  40. }
  41. cmMakefile& mf = status.GetMakefile();
  42. // now loop over all the targets
  43. for (int i = 0; i < numFiles; ++i) {
  44. if (mf.IsAlias(args[i])) {
  45. status.SetError("can not be used on an ALIAS target.");
  46. return false;
  47. }
  48. bool ret = SetOneTarget(args[i], propertyPairs, &mf);
  49. if (!ret) {
  50. status.SetError(
  51. cmStrCat("Can not find target to add properties to: ", args[i]));
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. static bool SetOneTarget(const std::string& tname,
  58. std::vector<std::string>& propertyPairs,
  59. cmMakefile* mf)
  60. {
  61. if (cmTarget* target = mf->FindTargetToUse(tname)) {
  62. // now loop through all the props and set them
  63. unsigned int k;
  64. for (k = 0; k < propertyPairs.size(); k = k + 2) {
  65. target->SetProperty(propertyPairs[k], propertyPairs[k + 1]);
  66. target->CheckProperty(propertyPairs[k], mf);
  67. }
  68. }
  69. // if file is not already in the makefile, then add it
  70. else {
  71. return false;
  72. }
  73. return true;
  74. }