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"
9 #include <cmext/algorithm>
11 #include "cmExecutionStatus.h"
12 #include "cmGeneratorExpression.h"
13 #include "cmMakefile.h"
14 #include "cmStringAlgorithms.h"
15 #include "cmSystemTools.h"
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);
22 bool cmIncludeDirectoryCommand(std::vector<std::string> const& args,
23 cmExecutionStatus& status)
29 cmMakefile& mf = status.GetMakefile();
31 auto i = args.begin();
33 bool before = mf.IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");
36 if ((*i) == "BEFORE") {
39 } else if ((*i) == "AFTER") {
44 std::vector<std::string> beforeIncludes;
45 std::vector<std::string> afterIncludes;
46 std::set<std::string> systemIncludes;
48 for (; i != args.end(); ++i) {
54 status.SetError("given empty-string as include directory.");
58 std::vector<std::string> includes;
60 GetIncludes(mf, *i, includes);
63 cm::append(beforeIncludes, includes);
65 cm::append(afterIncludes, includes);
68 systemIncludes.insert(includes.begin(), includes.end());
71 std::reverse(beforeIncludes.begin(), beforeIncludes.end());
73 mf.AddIncludeDirectories(afterIncludes);
74 mf.AddIncludeDirectories(beforeIncludes, before);
75 mf.AddSystemIncludeDirectories(systemIncludes);
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:
86 // /boo/hoo /dingle/berry "
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
92 static void GetIncludes(cmMakefile& mf, const std::string& arg,
93 std::vector<std::string>& incs)
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) {
100 std::string inc = arg.substr(lastPos, pos);
101 NormalizeInclude(mf, inc);
103 incs.push_back(std::move(inc));
108 std::string inc = arg.substr(lastPos);
109 NormalizeInclude(mf, inc);
111 incs.push_back(std::move(inc));
115 static void NormalizeInclude(cmMakefile& mf, std::string& inc)
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
127 cmSystemTools::ConvertToUnixSlashes(inc);
128 if (!cmSystemTools::FileIsFullPath(inc) &&
129 !cmGeneratorExpression::StartsWithGeneratorExpression(inc)) {
130 inc = cmStrCat(mf.GetCurrentSourceDirectory(), '/', inc);