/src/CMake/Source/cmIncludeDirectoryCommand.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst 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, std::string const& 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 | 0 | { |
25 | 0 | if (args.empty()) { |
26 | 0 | return true; |
27 | 0 | } |
28 | | |
29 | 0 | cmMakefile& mf = status.GetMakefile(); |
30 | |
|
31 | 0 | auto i = args.begin(); |
32 | |
|
33 | 0 | bool before = mf.IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE"); |
34 | 0 | bool system = false; |
35 | |
|
36 | 0 | if ((*i) == "BEFORE") { |
37 | 0 | before = true; |
38 | 0 | ++i; |
39 | 0 | } else if ((*i) == "AFTER") { |
40 | 0 | before = false; |
41 | 0 | ++i; |
42 | 0 | } |
43 | |
|
44 | 0 | std::vector<std::string> beforeIncludes; |
45 | 0 | std::vector<std::string> afterIncludes; |
46 | 0 | std::set<std::string> systemIncludes; |
47 | |
|
48 | 0 | for (; i != args.end(); ++i) { |
49 | 0 | if (*i == "SYSTEM") { |
50 | 0 | system = true; |
51 | 0 | continue; |
52 | 0 | } |
53 | 0 | if (i->empty()) { |
54 | 0 | status.SetError("given empty-string as include directory."); |
55 | 0 | return false; |
56 | 0 | } |
57 | | |
58 | 0 | std::vector<std::string> includes; |
59 | |
|
60 | 0 | GetIncludes(mf, *i, includes); |
61 | |
|
62 | 0 | if (before) { |
63 | 0 | cm::append(beforeIncludes, includes); |
64 | 0 | } else { |
65 | 0 | cm::append(afterIncludes, includes); |
66 | 0 | } |
67 | 0 | if (system) { |
68 | 0 | systemIncludes.insert(includes.begin(), includes.end()); |
69 | 0 | } |
70 | 0 | } |
71 | 0 | std::reverse(beforeIncludes.begin(), beforeIncludes.end()); |
72 | |
|
73 | 0 | mf.AddIncludeDirectories(afterIncludes); |
74 | 0 | mf.AddIncludeDirectories(beforeIncludes, before); |
75 | 0 | mf.AddSystemIncludeDirectories(systemIncludes); |
76 | |
|
77 | 0 | return true; |
78 | 0 | } |
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, std::string const& arg, |
93 | | std::vector<std::string>& incs) |
94 | 0 | { |
95 | | // break apart any line feed arguments |
96 | 0 | std::string::size_type pos = 0; |
97 | 0 | std::string::size_type lastPos = 0; |
98 | 0 | while ((pos = arg.find('\n', lastPos)) != std::string::npos) { |
99 | 0 | if (pos) { |
100 | 0 | std::string inc = arg.substr(lastPos, pos); |
101 | 0 | NormalizeInclude(mf, inc); |
102 | 0 | if (!inc.empty()) { |
103 | 0 | incs.push_back(std::move(inc)); |
104 | 0 | } |
105 | 0 | } |
106 | 0 | lastPos = pos + 1; |
107 | 0 | } |
108 | 0 | std::string inc = arg.substr(lastPos); |
109 | 0 | NormalizeInclude(mf, inc); |
110 | 0 | if (!inc.empty()) { |
111 | 0 | incs.push_back(std::move(inc)); |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | static void NormalizeInclude(cmMakefile& mf, std::string& inc) |
116 | 0 | { |
117 | 0 | std::string::size_type b = inc.find_first_not_of(" \r"); |
118 | 0 | std::string::size_type e = inc.find_last_not_of(" \r"); |
119 | 0 | if ((b != std::string::npos) && (e != std::string::npos)) { |
120 | 0 | inc.assign(inc, b, 1 + e - b); // copy the remaining substring |
121 | 0 | } else { |
122 | 0 | inc.clear(); |
123 | 0 | return; |
124 | 0 | } |
125 | | |
126 | 0 | if (!cmIsOff(inc)) { |
127 | 0 | cmSystemTools::ConvertToUnixSlashes(inc); |
128 | 0 | if (!cmSystemTools::FileIsFullPath(inc) && |
129 | 0 | !cmGeneratorExpression::StartsWithGeneratorExpression(inc)) { |
130 | 0 | inc = cmStrCat(mf.GetCurrentSourceDirectory(), '/', inc); |
131 | 0 | } |
132 | 0 | } |
133 | 0 | } |