cmSetSourceFilesPropertiesCommand.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "cmSetSourceFilesPropertiesCommand.h"
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <cm/string_view>
  7. #include <cmext/algorithm>
  8. #include "cmExecutionStatus.h"
  9. #include "cmMakefile.h"
  10. #include "cmSourceFile.h"
  11. #include "cmStringAlgorithms.h"
  12. bool cmSetSourceFilesPropertiesCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (args.size() < 2) {
  16. status.SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. // break the arguments into source file names and properties
  20. // old style allows for specifier before PROPERTIES keyword
  21. static const cm::string_view propNames[] = {
  22. "ABSTRACT", "GENERATED", "WRAP_EXCLUDE",
  23. "COMPILE_FLAGS", "OBJECT_DEPENDS", "PROPERTIES"
  24. };
  25. auto propsBegin = std::find_first_of(
  26. args.begin(), args.end(), std::begin(propNames), std::end(propNames));
  27. std::vector<std::string> propertyPairs;
  28. // build the property pairs
  29. for (auto j = propsBegin; j != args.end(); ++j) {
  30. // consume old style options
  31. if (*j == "ABSTRACT" || *j == "GENERATED" || *j == "WRAP_EXCLUDE") {
  32. propertyPairs.emplace_back(*j);
  33. propertyPairs.emplace_back("1");
  34. } else if (*j == "COMPILE_FLAGS") {
  35. propertyPairs.emplace_back("COMPILE_FLAGS");
  36. ++j;
  37. if (j == args.end()) {
  38. status.SetError("called with incorrect number of arguments "
  39. "COMPILE_FLAGS with no flags");
  40. return false;
  41. }
  42. propertyPairs.push_back(*j);
  43. } else if (*j == "OBJECT_DEPENDS") {
  44. propertyPairs.emplace_back("OBJECT_DEPENDS");
  45. ++j;
  46. if (j == args.end()) {
  47. status.SetError("called with incorrect number of arguments "
  48. "OBJECT_DEPENDS with no dependencies");
  49. return false;
  50. }
  51. propertyPairs.push_back(*j);
  52. } else if (*j == "PROPERTIES") {
  53. // PROPERTIES is followed by new style prop value pairs
  54. cmStringRange newStyleProps{ j + 1, args.end() };
  55. if (newStyleProps.size() % 2 != 0) {
  56. status.SetError("called with incorrect number of arguments.");
  57. return false;
  58. }
  59. // set newStyleProps as is.
  60. cm::append(propertyPairs, newStyleProps);
  61. // break out of the loop.
  62. break;
  63. } else {
  64. status.SetError("called with illegal arguments, maybe missing a "
  65. "PROPERTIES specifier?");
  66. return false;
  67. }
  68. }
  69. // loop over all the files
  70. for (const std::string& sfname : cmStringRange{ args.begin(), propsBegin }) {
  71. // get the source file
  72. if (cmSourceFile* sf = status.GetMakefile().GetOrCreateSource(sfname)) {
  73. // loop through the props and set them
  74. for (auto k = propertyPairs.begin(); k != propertyPairs.end(); k += 2) {
  75. sf->SetProperty(*k, (k + 1)->c_str());
  76. }
  77. }
  78. }
  79. return true;
  80. }