cmSetTestsPropertiesCommand.cxx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "cmSetTestsPropertiesCommand.h"
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <cmext/string_view>
  7. #include "cmArgumentParser.h"
  8. #include "cmExecutionStatus.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmRange.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cmTest.h"
  15. bool cmSetTestsPropertiesCommand(std::vector<std::string> const& args,
  16. cmExecutionStatus& status)
  17. {
  18. if (args.empty()) {
  19. status.SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. // first identify the properties arguments
  23. auto propsIter = std::find(args.begin(), args.end(), "PROPERTIES");
  24. if (propsIter == args.end() || propsIter + 1 == args.end()) {
  25. status.SetError("called with illegal arguments, maybe missing a "
  26. "PROPERTIES specifier?");
  27. return false;
  28. }
  29. if (std::distance(propsIter, args.end()) % 2 != 1) {
  30. status.SetError("called with incorrect number of arguments.");
  31. return false;
  32. }
  33. std::vector<std::string> tests;
  34. std::string directory;
  35. cmArgumentParser<void> parser;
  36. parser.Bind("DIRECTORY"_s, directory);
  37. auto result = parser.Parse(cmStringRange{ args.begin(), propsIter }, &tests);
  38. cmMakefile* mf = &status.GetMakefile();
  39. if (result.MaybeReportError(*mf)) {
  40. return false;
  41. }
  42. if (!directory.empty()) {
  43. std::string absDirectory = cmSystemTools::CollapseFullPath(
  44. directory, mf->GetCurrentSourceDirectory());
  45. mf = mf->GetGlobalGenerator()->FindMakefile(absDirectory);
  46. if (!mf) {
  47. status.SetError(cmStrCat("given non-existent DIRECTORY ", directory));
  48. return false;
  49. }
  50. }
  51. // loop over all the tests
  52. for (const std::string& tname : tests) {
  53. if (cmTest* test = mf->GetTest(tname)) {
  54. // loop through all the props and set them
  55. for (auto k = propsIter + 1; k != args.end(); k += 2) {
  56. if (!k->empty()) {
  57. test->SetProperty(*k, *(k + 1));
  58. }
  59. }
  60. } else {
  61. status.SetError(
  62. cmStrCat("Can not find test to add properties to: ", tname));
  63. return false;
  64. }
  65. }
  66. return true;
  67. }