summaryrefslogtreecommitdiffstats
path: root/Source/cmSetSourceFilesPropertiesCommand.cxx
blob: 7a53a1dc04df2f471dc0f88b1e4cf376dbafb1c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmSetSourceFilesPropertiesCommand.h"

#include <algorithm>
#include <iterator>

#include <cm/string_view>
#include <cmext/algorithm>

#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmStringAlgorithms.h"

bool cmSetSourceFilesPropertiesCommand(std::vector<std::string> const& args,
                                       cmExecutionStatus& status)
{
  if (args.size() < 2) {
    status.SetError("called with incorrect number of arguments");
    return false;
  }

  // break the arguments into source file names and properties
  // old style allows for specifier before PROPERTIES keyword
  static const cm::string_view propNames[] = {
    "ABSTRACT",      "GENERATED",      "WRAP_EXCLUDE",
    "COMPILE_FLAGS", "OBJECT_DEPENDS", "PROPERTIES"
  };
  auto propsBegin = std::find_first_of(
    args.begin(), args.end(), std::begin(propNames), std::end(propNames));

  std::vector<std::string> propertyPairs;
  // build the property pairs
  for (auto j = propsBegin; j != args.end(); ++j) {
    // consume old style options
    if (*j == "ABSTRACT" || *j == "GENERATED" || *j == "WRAP_EXCLUDE") {
      propertyPairs.emplace_back(*j);
      propertyPairs.emplace_back("1");
    } else if (*j == "COMPILE_FLAGS") {
      propertyPairs.emplace_back("COMPILE_FLAGS");
      ++j;
      if (j == args.end()) {
        status.SetError("called with incorrect number of arguments "
                        "COMPILE_FLAGS with no flags");
        return false;
      }
      propertyPairs.push_back(*j);
    } else if (*j == "OBJECT_DEPENDS") {
      propertyPairs.emplace_back("OBJECT_DEPENDS");
      ++j;
      if (j == args.end()) {
        status.SetError("called with incorrect number of arguments "
                        "OBJECT_DEPENDS with no dependencies");
        return false;
      }
      propertyPairs.push_back(*j);
    } else if (*j == "PROPERTIES") {
      // PROPERTIES is followed by new style prop value pairs
      cmStringRange newStyleProps{ j + 1, args.end() };
      if (newStyleProps.size() % 2 != 0) {
        status.SetError("called with incorrect number of arguments.");
        return false;
      }
      // set newStyleProps as is.
      cm::append(propertyPairs, newStyleProps);
      // break out of the loop.
      break;
    } else {
      status.SetError("called with illegal arguments, maybe missing a "
                      "PROPERTIES specifier?");
      return false;
    }
  }

  // loop over all the files
  for (const std::string& sfname : cmStringRange{ args.begin(), propsBegin }) {
    // get the source file
    if (cmSourceFile* sf = status.GetMakefile().GetOrCreateSource(sfname)) {
      // loop through the props and set them
      for (auto k = propertyPairs.begin(); k != propertyPairs.end(); k += 2) {
        sf->SetProperty(*k, (k + 1)->c_str());
      }
    }
  }
  return true;
}