cmDependsC.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 "cmDependsC.h"
  4. #include "cmsys/FStream.hxx"
  5. #include <utility>
  6. #include "cmAlgorithms.h"
  7. #include "cmFileTimeComparison.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmSystemTools.h"
  11. #define INCLUDE_REGEX_LINE \
  12. "^[ \t]*[#%][ \t]*(include|import)[ \t]*[<\"]([^\">]+)([\">])"
  13. #define INCLUDE_REGEX_LINE_MARKER "#IncludeRegexLine: "
  14. #define INCLUDE_REGEX_SCAN_MARKER "#IncludeRegexScan: "
  15. #define INCLUDE_REGEX_COMPLAIN_MARKER "#IncludeRegexComplain: "
  16. #define INCLUDE_REGEX_TRANSFORM_MARKER "#IncludeRegexTransform: "
  17. cmDependsC::cmDependsC()
  18. : ValidDeps(nullptr)
  19. {
  20. }
  21. cmDependsC::cmDependsC(
  22. cmLocalGenerator* lg, const char* targetDir, const std::string& lang,
  23. const std::map<std::string, DependencyVector>* validDeps)
  24. : cmDepends(lg, targetDir)
  25. , ValidDeps(validDeps)
  26. {
  27. cmMakefile* mf = lg->GetMakefile();
  28. // Configure the include file search path.
  29. this->SetIncludePathFromLanguage(lang);
  30. // Configure regular expressions.
  31. std::string scanRegex = "^.*$";
  32. std::string complainRegex = "^$";
  33. {
  34. std::string scanRegexVar = "CMAKE_";
  35. scanRegexVar += lang;
  36. scanRegexVar += "_INCLUDE_REGEX_SCAN";
  37. if (const char* sr = mf->GetDefinition(scanRegexVar)) {
  38. scanRegex = sr;
  39. }
  40. std::string complainRegexVar = "CMAKE_";
  41. complainRegexVar += lang;
  42. complainRegexVar += "_INCLUDE_REGEX_COMPLAIN";
  43. if (const char* cr = mf->GetDefinition(complainRegexVar)) {
  44. complainRegex = cr;
  45. }
  46. }
  47. this->IncludeRegexLine.compile(INCLUDE_REGEX_LINE);
  48. this->IncludeRegexScan.compile(scanRegex.c_str());
  49. this->IncludeRegexComplain.compile(complainRegex.c_str());
  50. this->IncludeRegexLineString = INCLUDE_REGEX_LINE_MARKER INCLUDE_REGEX_LINE;
  51. this->IncludeRegexScanString = INCLUDE_REGEX_SCAN_MARKER;
  52. this->IncludeRegexScanString += scanRegex;
  53. this->IncludeRegexComplainString = INCLUDE_REGEX_COMPLAIN_MARKER;
  54. this->IncludeRegexComplainString += complainRegex;
  55. this->SetupTransforms();
  56. this->CacheFileName = this->TargetDirectory;
  57. this->CacheFileName += "/";
  58. this->CacheFileName += lang;
  59. this->CacheFileName += ".includecache";
  60. this->ReadCacheFile();
  61. }
  62. cmDependsC::~cmDependsC()
  63. {
  64. this->WriteCacheFile();
  65. cmDeleteAll(this->FileCache);
  66. }
  67. bool cmDependsC::WriteDependencies(const std::set<std::string>& sources,
  68. const std::string& obj,
  69. std::ostream& makeDepends,
  70. std::ostream& internalDepends)
  71. {
  72. // Make sure this is a scanning instance.
  73. if (sources.empty() || sources.begin()->empty()) {
  74. cmSystemTools::Error("Cannot scan dependencies without a source file.");
  75. return false;
  76. }
  77. if (obj.empty()) {
  78. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  79. return false;
  80. }
  81. std::set<std::string> dependencies;
  82. bool haveDeps = false;
  83. if (this->ValidDeps != nullptr) {
  84. std::map<std::string, DependencyVector>::const_iterator tmpIt =
  85. this->ValidDeps->find(obj);
  86. if (tmpIt != this->ValidDeps->end()) {
  87. dependencies.insert(tmpIt->second.begin(), tmpIt->second.end());
  88. haveDeps = true;
  89. }
  90. }
  91. if (!haveDeps) {
  92. // Walk the dependency graph starting with the source file.
  93. int srcFiles = static_cast<int>(sources.size());
  94. this->Encountered.clear();
  95. for (std::string const& src : sources) {
  96. UnscannedEntry root;
  97. root.FileName = src;
  98. this->Unscanned.push(root);
  99. this->Encountered.insert(src);
  100. }
  101. std::set<std::string> scanned;
  102. // Use reserve to allocate enough memory for tempPathStr
  103. // so that during the loops no memory is allocated or freed
  104. std::string tempPathStr;
  105. tempPathStr.reserve(4 * 1024);
  106. while (!this->Unscanned.empty()) {
  107. // Get the next file to scan.
  108. UnscannedEntry current = this->Unscanned.front();
  109. this->Unscanned.pop();
  110. // If not a full path, find the file in the include path.
  111. std::string fullName;
  112. if ((srcFiles > 0) || cmSystemTools::FileIsFullPath(current.FileName)) {
  113. if (cmSystemTools::FileExists(current.FileName, true)) {
  114. fullName = current.FileName;
  115. }
  116. } else if (!current.QuotedLocation.empty() &&
  117. cmSystemTools::FileExists(current.QuotedLocation, true)) {
  118. // The include statement producing this entry was a double-quote
  119. // include and the included file is present in the directory of
  120. // the source containing the include statement.
  121. fullName = current.QuotedLocation;
  122. } else {
  123. std::map<std::string, std::string>::iterator headerLocationIt =
  124. this->HeaderLocationCache.find(current.FileName);
  125. if (headerLocationIt != this->HeaderLocationCache.end()) {
  126. fullName = headerLocationIt->second;
  127. } else {
  128. for (std::string const& i : this->IncludePath) {
  129. // Construct the name of the file as if it were in the current
  130. // include directory. Avoid using a leading "./".
  131. tempPathStr =
  132. cmSystemTools::CollapseCombinedPath(i, current.FileName);
  133. // Look for the file in this location.
  134. if (cmSystemTools::FileExists(tempPathStr, true)) {
  135. fullName = tempPathStr;
  136. HeaderLocationCache[current.FileName] = fullName;
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. // Complain if the file cannot be found and matches the complain
  143. // regex.
  144. if (fullName.empty() &&
  145. this->IncludeRegexComplain.find(current.FileName.c_str())) {
  146. cmSystemTools::Error("Cannot find file \"", current.FileName.c_str(),
  147. "\".");
  148. return false;
  149. }
  150. // Scan the file if it was found and has not been scanned already.
  151. if (!fullName.empty() && (scanned.find(fullName) == scanned.end())) {
  152. // Record scanned files.
  153. scanned.insert(fullName);
  154. // Check whether this file is already in the cache
  155. std::map<std::string, cmIncludeLines*>::iterator fileIt =
  156. this->FileCache.find(fullName);
  157. if (fileIt != this->FileCache.end()) {
  158. fileIt->second->Used = true;
  159. dependencies.insert(fullName);
  160. for (UnscannedEntry const& inc : fileIt->second->UnscannedEntries) {
  161. if (this->Encountered.find(inc.FileName) ==
  162. this->Encountered.end()) {
  163. this->Encountered.insert(inc.FileName);
  164. this->Unscanned.push(inc);
  165. }
  166. }
  167. } else {
  168. // Try to scan the file. Just leave it out if we cannot find
  169. // it.
  170. cmsys::ifstream fin(fullName.c_str());
  171. if (fin) {
  172. cmsys::FStream::BOM bom = cmsys::FStream::ReadBOM(fin);
  173. if (bom == cmsys::FStream::BOM_None ||
  174. bom == cmsys::FStream::BOM_UTF8) {
  175. // Add this file as a dependency.
  176. dependencies.insert(fullName);
  177. // Scan this file for new dependencies. Pass the directory
  178. // containing the file to handle double-quote includes.
  179. std::string dir = cmSystemTools::GetFilenamePath(fullName);
  180. this->Scan(fin, dir.c_str(), fullName);
  181. } else {
  182. // Skip file with encoding we do not implement.
  183. }
  184. }
  185. }
  186. }
  187. srcFiles--;
  188. }
  189. }
  190. // Write the dependencies to the output stream. Makefile rules
  191. // written by the original local generator for this directory
  192. // convert the dependencies to paths relative to the home output
  193. // directory. We must do the same here.
  194. std::string binDir = this->LocalGenerator->GetBinaryDirectory();
  195. std::string obj_i = this->LocalGenerator->ConvertToRelativePath(binDir, obj);
  196. std::string obj_m = cmSystemTools::ConvertToOutputPath(obj_i);
  197. internalDepends << obj_i << std::endl;
  198. for (std::string const& dep : dependencies) {
  199. makeDepends << obj_m << ": "
  200. << cmSystemTools::ConvertToOutputPath(
  201. this->LocalGenerator->ConvertToRelativePath(binDir, dep))
  202. << std::endl;
  203. internalDepends << " " << dep << std::endl;
  204. }
  205. makeDepends << std::endl;
  206. return true;
  207. }
  208. void cmDependsC::ReadCacheFile()
  209. {
  210. if (this->CacheFileName.empty()) {
  211. return;
  212. }
  213. cmsys::ifstream fin(this->CacheFileName.c_str());
  214. if (!fin) {
  215. return;
  216. }
  217. std::string line;
  218. cmIncludeLines* cacheEntry = nullptr;
  219. bool haveFileName = false;
  220. while (cmSystemTools::GetLineFromStream(fin, line)) {
  221. if (line.empty()) {
  222. cacheEntry = nullptr;
  223. haveFileName = false;
  224. continue;
  225. }
  226. // the first line after an empty line is the name of the parsed file
  227. if (!haveFileName) {
  228. haveFileName = true;
  229. int newer = 0;
  230. cmFileTimeComparison comp;
  231. bool res = comp.FileTimeCompare(this->CacheFileName.c_str(),
  232. line.c_str(), &newer);
  233. if (res && newer == 1) // cache is newer than the parsed file
  234. {
  235. cacheEntry = new cmIncludeLines;
  236. this->FileCache[line] = cacheEntry;
  237. }
  238. // file doesn't exist, check that the regular expressions
  239. // haven't changed
  240. else if (!res) {
  241. if (line.find(INCLUDE_REGEX_LINE_MARKER) == 0) {
  242. if (line != this->IncludeRegexLineString) {
  243. return;
  244. }
  245. } else if (line.find(INCLUDE_REGEX_SCAN_MARKER) == 0) {
  246. if (line != this->IncludeRegexScanString) {
  247. return;
  248. }
  249. } else if (line.find(INCLUDE_REGEX_COMPLAIN_MARKER) == 0) {
  250. if (line != this->IncludeRegexComplainString) {
  251. return;
  252. }
  253. } else if (line.find(INCLUDE_REGEX_TRANSFORM_MARKER) == 0) {
  254. if (line != this->IncludeRegexTransformString) {
  255. return;
  256. }
  257. }
  258. }
  259. } else if (cacheEntry != nullptr) {
  260. UnscannedEntry entry;
  261. entry.FileName = line;
  262. if (cmSystemTools::GetLineFromStream(fin, line)) {
  263. if (line != "-") {
  264. entry.QuotedLocation = line;
  265. }
  266. cacheEntry->UnscannedEntries.push_back(std::move(entry));
  267. }
  268. }
  269. }
  270. }
  271. void cmDependsC::WriteCacheFile() const
  272. {
  273. if (this->CacheFileName.empty()) {
  274. return;
  275. }
  276. cmsys::ofstream cacheOut(this->CacheFileName.c_str());
  277. if (!cacheOut) {
  278. return;
  279. }
  280. cacheOut << this->IncludeRegexLineString << "\n\n";
  281. cacheOut << this->IncludeRegexScanString << "\n\n";
  282. cacheOut << this->IncludeRegexComplainString << "\n\n";
  283. cacheOut << this->IncludeRegexTransformString << "\n\n";
  284. for (auto const& fileIt : this->FileCache) {
  285. if (fileIt.second->Used) {
  286. cacheOut << fileIt.first << std::endl;
  287. for (UnscannedEntry const& inc : fileIt.second->UnscannedEntries) {
  288. cacheOut << inc.FileName << std::endl;
  289. if (inc.QuotedLocation.empty()) {
  290. cacheOut << "-" << std::endl;
  291. } else {
  292. cacheOut << inc.QuotedLocation << std::endl;
  293. }
  294. }
  295. cacheOut << std::endl;
  296. }
  297. }
  298. }
  299. void cmDependsC::Scan(std::istream& is, const char* directory,
  300. const std::string& fullName)
  301. {
  302. cmIncludeLines* newCacheEntry = new cmIncludeLines;
  303. newCacheEntry->Used = true;
  304. this->FileCache[fullName] = newCacheEntry;
  305. // Read one line at a time.
  306. std::string line;
  307. while (cmSystemTools::GetLineFromStream(is, line)) {
  308. // Transform the line content first.
  309. if (!this->TransformRules.empty()) {
  310. this->TransformLine(line);
  311. }
  312. // Match include directives.
  313. if (this->IncludeRegexLine.find(line.c_str())) {
  314. // Get the file being included.
  315. UnscannedEntry entry;
  316. entry.FileName = this->IncludeRegexLine.match(2);
  317. cmSystemTools::ConvertToUnixSlashes(entry.FileName);
  318. if (this->IncludeRegexLine.match(3) == "\"" &&
  319. !cmSystemTools::FileIsFullPath(entry.FileName)) {
  320. // This was a double-quoted include with a relative path. We
  321. // must check for the file in the directory containing the
  322. // file we are scanning.
  323. entry.QuotedLocation =
  324. cmSystemTools::CollapseCombinedPath(directory, entry.FileName);
  325. }
  326. // Queue the file if it has not yet been encountered and it
  327. // matches the regular expression for recursive scanning. Note
  328. // that this check does not account for the possibility of two
  329. // headers with the same name in different directories when one
  330. // is included by double-quotes and the other by angle brackets.
  331. // It also does not work properly if two header files with the same
  332. // name exist in different directories, and both are included from a
  333. // file their own directory by simply using "filename.h" (#12619)
  334. // This kind of problem will be fixed when a more
  335. // preprocessor-like implementation of this scanner is created.
  336. if (this->IncludeRegexScan.find(entry.FileName.c_str())) {
  337. newCacheEntry->UnscannedEntries.push_back(entry);
  338. if (this->Encountered.find(entry.FileName) ==
  339. this->Encountered.end()) {
  340. this->Encountered.insert(entry.FileName);
  341. this->Unscanned.push(entry);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. void cmDependsC::SetupTransforms()
  348. {
  349. // Get the transformation rules.
  350. std::vector<std::string> transformRules;
  351. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  352. if (const char* xform = mf->GetDefinition("CMAKE_INCLUDE_TRANSFORMS")) {
  353. cmSystemTools::ExpandListArgument(xform, transformRules, true);
  354. }
  355. for (std::string const& tr : transformRules) {
  356. this->ParseTransform(tr);
  357. }
  358. this->IncludeRegexTransformString = INCLUDE_REGEX_TRANSFORM_MARKER;
  359. if (!this->TransformRules.empty()) {
  360. // Construct the regular expression to match lines to be
  361. // transformed.
  362. std::string xform = "^([ \t]*[#%][ \t]*(include|import)[ \t]*)(";
  363. const char* sep = "";
  364. for (auto const& tr : this->TransformRules) {
  365. xform += sep;
  366. xform += tr.first;
  367. sep = "|";
  368. }
  369. xform += ")[ \t]*\\(([^),]*)\\)";
  370. this->IncludeRegexTransform.compile(xform.c_str());
  371. // Build a string that encodes all transformation rules and will
  372. // change when rules are changed.
  373. this->IncludeRegexTransformString += xform;
  374. for (auto const& tr : this->TransformRules) {
  375. this->IncludeRegexTransformString += " ";
  376. this->IncludeRegexTransformString += tr.first;
  377. this->IncludeRegexTransformString += "(%)=";
  378. this->IncludeRegexTransformString += tr.second;
  379. }
  380. }
  381. }
  382. void cmDependsC::ParseTransform(std::string const& xform)
  383. {
  384. // A transform rule is of the form SOME_MACRO(%)=value-with-%
  385. // We can simply separate with "(%)=".
  386. std::string::size_type pos = xform.find("(%)=");
  387. if (pos == std::string::npos || pos == 0) {
  388. return;
  389. }
  390. std::string name = xform.substr(0, pos);
  391. std::string value = xform.substr(pos + 4);
  392. this->TransformRules[name] = value;
  393. }
  394. void cmDependsC::TransformLine(std::string& line)
  395. {
  396. // Check for a transform rule match. Return if none.
  397. if (!this->IncludeRegexTransform.find(line.c_str())) {
  398. return;
  399. }
  400. TransformRulesType::const_iterator tri =
  401. this->TransformRules.find(this->IncludeRegexTransform.match(3));
  402. if (tri == this->TransformRules.end()) {
  403. return;
  404. }
  405. // Construct the transformed line.
  406. std::string newline = this->IncludeRegexTransform.match(1);
  407. std::string arg = this->IncludeRegexTransform.match(4);
  408. for (const char* c = tri->second.c_str(); *c; ++c) {
  409. if (*c == '%') {
  410. newline += arg;
  411. } else {
  412. newline += *c;
  413. }
  414. }
  415. // Return the transformed line.
  416. line = newline;
  417. }