resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmIncludeDirectoryCommand.cxx
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 "cmIncludeDirectoryCommand.h"
4
5 #include <algorithm>
6 #include <set>
7 #include <utility>
8
9 #include <cmext/algorithm>
10
11 #include "cmExecutionStatus.h"
12 #include "cmGeneratorExpression.h"
13 #include "cmMakefile.h"
14 #include "cmStringAlgorithms.h"
15 #include "cmSystemTools.h"
16 #include "cmValue.h"
17
18 static void GetIncludes(cmMakefile& mf, const std::string& arg,
19                         std::vector<std::string>& incs);
20 static void NormalizeInclude(cmMakefile& mf, std::string& inc);
21
22 bool cmIncludeDirectoryCommand(std::vector<std::string> const& args,
23                                cmExecutionStatus& status)
24 {
25   if (args.empty()) {
26     return true;
27   }
28
29   cmMakefile& mf = status.GetMakefile();
30
31   auto i = args.begin();
32
33   bool before = mf.IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");
34   bool system = false;
35
36   if ((*i) == "BEFORE") {
37     before = true;
38     ++i;
39   } else if ((*i) == "AFTER") {
40     before = false;
41     ++i;
42   }
43
44   std::vector<std::string> beforeIncludes;
45   std::vector<std::string> afterIncludes;
46   std::set<std::string> systemIncludes;
47
48   for (; i != args.end(); ++i) {
49     if (*i == "SYSTEM") {
50       system = true;
51       continue;
52     }
53     if (i->empty()) {
54       status.SetError("given empty-string as include directory.");
55       return false;
56     }
57
58     std::vector<std::string> includes;
59
60     GetIncludes(mf, *i, includes);
61
62     if (before) {
63       cm::append(beforeIncludes, includes);
64     } else {
65       cm::append(afterIncludes, includes);
66     }
67     if (system) {
68       systemIncludes.insert(includes.begin(), includes.end());
69     }
70   }
71   std::reverse(beforeIncludes.begin(), beforeIncludes.end());
72
73   mf.AddIncludeDirectories(afterIncludes);
74   mf.AddIncludeDirectories(beforeIncludes, before);
75   mf.AddSystemIncludeDirectories(systemIncludes);
76
77   return true;
78 }
79
80 // do a lot of cleanup on the arguments because this is one place where folks
81 // sometimes take the output of a program and pass it directly into this
82 // command not thinking that a single argument could be filled with spaces
83 // and newlines etc like below:
84 //
85 // "   /foo/bar
86 //    /boo/hoo /dingle/berry "
87 //
88 // ideally that should be three separate arguments but when sucking the
89 // output from a program and passing it into a command the cleanup doesn't
90 // always happen
91 //
92 static void GetIncludes(cmMakefile& mf, const std::string& arg,
93                         std::vector<std::string>& incs)
94 {
95   // break apart any line feed arguments
96   std::string::size_type pos = 0;
97   std::string::size_type lastPos = 0;
98   while ((pos = arg.find('\n', lastPos)) != std::string::npos) {
99     if (pos) {
100       std::string inc = arg.substr(lastPos, pos);
101       NormalizeInclude(mf, inc);
102       if (!inc.empty()) {
103         incs.push_back(std::move(inc));
104       }
105     }
106     lastPos = pos + 1;
107   }
108   std::string inc = arg.substr(lastPos);
109   NormalizeInclude(mf, inc);
110   if (!inc.empty()) {
111     incs.push_back(std::move(inc));
112   }
113 }
114
115 static void NormalizeInclude(cmMakefile& mf, std::string& inc)
116 {
117   std::string::size_type b = inc.find_first_not_of(" \r");
118   std::string::size_type e = inc.find_last_not_of(" \r");
119   if ((b != std::string::npos) && (e != std::string::npos)) {
120     inc.assign(inc, b, 1 + e - b); // copy the remaining substring
121   } else {
122     inc.clear();
123     return;
124   }
125
126   if (!cmIsOff(inc)) {
127     cmSystemTools::ConvertToUnixSlashes(inc);
128     if (!cmSystemTools::FileIsFullPath(inc) &&
129         !cmGeneratorExpression::StartsWithGeneratorExpression(inc)) {
130       inc = cmStrCat(mf.GetCurrentSourceDirectory(), '/', inc);
131     }
132   }
133 }