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