cmOutputRequiredFilesCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 "cmOutputRequiredFilesCommand.h"
  4. #include "cmsys/FStream.hxx"
  5. #include "cmsys/RegularExpression.hxx"
  6. #include <map>
  7. #include <unordered_map>
  8. #include <utility>
  9. #include "cmAlgorithms.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmMakefile.h"
  12. #include "cmSourceFile.h"
  13. #include "cmSystemTools.h"
  14. #include "cmTarget.h"
  15. class cmExecutionStatus;
  16. /** \class cmDependInformation
  17. * \brief Store dependency information for a single source file.
  18. *
  19. * This structure stores the depend information for a single source file.
  20. */
  21. class cmDependInformation
  22. {
  23. public:
  24. /**
  25. * Construct with dependency generation marked not done; instance
  26. * not placed in cmMakefile's list.
  27. */
  28. cmDependInformation()
  29. : DependDone(false)
  30. , SourceFile(CM_NULLPTR)
  31. {
  32. }
  33. /**
  34. * The set of files on which this one depends.
  35. */
  36. typedef std::set<cmDependInformation*> DependencySetType;
  37. DependencySetType DependencySet;
  38. /**
  39. * This flag indicates whether dependency checking has been
  40. * performed for this file.
  41. */
  42. bool DependDone;
  43. /**
  44. * If this object corresponds to a cmSourceFile instance, this points
  45. * to it.
  46. */
  47. const cmSourceFile* SourceFile;
  48. /**
  49. * Full path to this file.
  50. */
  51. std::string FullPath;
  52. /**
  53. * Full path not including file name.
  54. */
  55. std::string PathOnly;
  56. /**
  57. * Name used to #include this file.
  58. */
  59. std::string IncludeName;
  60. /**
  61. * This method adds the dependencies of another file to this one.
  62. */
  63. void AddDependencies(cmDependInformation* info)
  64. {
  65. if (this != info) {
  66. this->DependencySet.insert(info);
  67. }
  68. }
  69. };
  70. class cmLBDepend
  71. {
  72. public:
  73. /**
  74. * Construct the object with verbose turned off.
  75. */
  76. cmLBDepend()
  77. {
  78. this->Verbose = false;
  79. this->IncludeFileRegularExpression.compile("^.*$");
  80. this->ComplainFileRegularExpression.compile("^$");
  81. }
  82. /**
  83. * Destructor.
  84. */
  85. ~cmLBDepend() { cmDeleteAll(this->DependInformationMap); }
  86. /**
  87. * Set the makefile that is used as a source of classes.
  88. */
  89. void SetMakefile(cmMakefile* makefile)
  90. {
  91. this->Makefile = makefile;
  92. // Now extract the include file regular expression from the makefile.
  93. this->IncludeFileRegularExpression.compile(
  94. this->Makefile->GetIncludeRegularExpression());
  95. this->ComplainFileRegularExpression.compile(
  96. this->Makefile->GetComplainRegularExpression());
  97. // Now extract any include paths from the targets
  98. std::set<std::string> uniqueIncludes;
  99. std::vector<std::string> orderedAndUniqueIncludes;
  100. cmTargets& targets = this->Makefile->GetTargets();
  101. for (cmTargets::iterator l = targets.begin(); l != targets.end(); ++l) {
  102. const char* incDirProp = l->second.GetProperty("INCLUDE_DIRECTORIES");
  103. if (!incDirProp) {
  104. continue;
  105. }
  106. std::string incDirs = cmGeneratorExpression::Preprocess(
  107. incDirProp, cmGeneratorExpression::StripAllGeneratorExpressions);
  108. std::vector<std::string> includes;
  109. cmSystemTools::ExpandListArgument(incDirs, includes);
  110. for (std::vector<std::string>::const_iterator j = includes.begin();
  111. j != includes.end(); ++j) {
  112. std::string path = *j;
  113. this->Makefile->ExpandVariablesInString(path);
  114. if (uniqueIncludes.insert(path).second) {
  115. orderedAndUniqueIncludes.push_back(path);
  116. }
  117. }
  118. }
  119. for (std::vector<std::string>::const_iterator it =
  120. orderedAndUniqueIncludes.begin();
  121. it != orderedAndUniqueIncludes.end(); ++it) {
  122. this->AddSearchPath(*it);
  123. }
  124. }
  125. /**
  126. * Add a directory to the search path for include files.
  127. */
  128. void AddSearchPath(const std::string& path)
  129. {
  130. this->IncludeDirectories.push_back(path);
  131. }
  132. /**
  133. * Generate dependencies for the file given. Returns a pointer to
  134. * the cmDependInformation object for the file.
  135. */
  136. const cmDependInformation* FindDependencies(const char* file)
  137. {
  138. cmDependInformation* info = this->GetDependInformation(file, CM_NULLPTR);
  139. this->GenerateDependInformation(info);
  140. return info;
  141. }
  142. protected:
  143. /**
  144. * Compute the depend information for this class.
  145. */
  146. void DependWalk(cmDependInformation* info)
  147. {
  148. cmsys::ifstream fin(info->FullPath.c_str());
  149. if (!fin) {
  150. cmSystemTools::Error("error can not open ", info->FullPath.c_str());
  151. return;
  152. }
  153. std::string line;
  154. while (cmSystemTools::GetLineFromStream(fin, line)) {
  155. if (cmHasLiteralPrefix(line.c_str(), "#include")) {
  156. // if it is an include line then create a string class
  157. size_t qstart = line.find('\"', 8);
  158. size_t qend;
  159. // if a quote is not found look for a <
  160. if (qstart == std::string::npos) {
  161. qstart = line.find('<', 8);
  162. // if a < is not found then move on
  163. if (qstart == std::string::npos) {
  164. cmSystemTools::Error("unknown include directive ", line.c_str());
  165. continue;
  166. }
  167. qend = line.find('>', qstart + 1);
  168. } else {
  169. qend = line.find('\"', qstart + 1);
  170. }
  171. // extract the file being included
  172. std::string includeFile = line.substr(qstart + 1, qend - qstart - 1);
  173. // see if the include matches the regular expression
  174. if (!this->IncludeFileRegularExpression.find(includeFile)) {
  175. if (this->Verbose) {
  176. std::string message = "Skipping ";
  177. message += includeFile;
  178. message += " for file ";
  179. message += info->FullPath;
  180. cmSystemTools::Error(message.c_str(), CM_NULLPTR);
  181. }
  182. continue;
  183. }
  184. // Add this file and all its dependencies.
  185. this->AddDependency(info, includeFile.c_str());
  186. /// add the cxx file if it exists
  187. std::string cxxFile = includeFile;
  188. std::string::size_type pos = cxxFile.rfind('.');
  189. if (pos != std::string::npos) {
  190. std::string root = cxxFile.substr(0, pos);
  191. cxxFile = root + ".cxx";
  192. bool found = false;
  193. // try jumping to .cxx .cpp and .c in order
  194. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  195. found = true;
  196. }
  197. for (std::vector<std::string>::iterator i =
  198. this->IncludeDirectories.begin();
  199. i != this->IncludeDirectories.end(); ++i) {
  200. std::string path = *i;
  201. path = path + "/";
  202. path = path + cxxFile;
  203. if (cmSystemTools::FileExists(path.c_str())) {
  204. found = true;
  205. }
  206. }
  207. if (!found) {
  208. cxxFile = root + ".cpp";
  209. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  210. found = true;
  211. }
  212. for (std::vector<std::string>::iterator i =
  213. this->IncludeDirectories.begin();
  214. i != this->IncludeDirectories.end(); ++i) {
  215. std::string path = *i;
  216. path = path + "/";
  217. path = path + cxxFile;
  218. if (cmSystemTools::FileExists(path.c_str())) {
  219. found = true;
  220. }
  221. }
  222. }
  223. if (!found) {
  224. cxxFile = root + ".c";
  225. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  226. found = true;
  227. }
  228. for (std::vector<std::string>::iterator i =
  229. this->IncludeDirectories.begin();
  230. i != this->IncludeDirectories.end(); ++i) {
  231. std::string path = *i;
  232. path = path + "/";
  233. path = path + cxxFile;
  234. if (cmSystemTools::FileExists(path.c_str())) {
  235. found = true;
  236. }
  237. }
  238. }
  239. if (!found) {
  240. cxxFile = root + ".txx";
  241. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  242. found = true;
  243. }
  244. for (std::vector<std::string>::iterator i =
  245. this->IncludeDirectories.begin();
  246. i != this->IncludeDirectories.end(); ++i) {
  247. std::string path = *i;
  248. path = path + "/";
  249. path = path + cxxFile;
  250. if (cmSystemTools::FileExists(path.c_str())) {
  251. found = true;
  252. }
  253. }
  254. }
  255. if (found) {
  256. this->AddDependency(info, cxxFile.c_str());
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /**
  263. * Add a dependency. Possibly walk it for more dependencies.
  264. */
  265. void AddDependency(cmDependInformation* info, const char* file)
  266. {
  267. cmDependInformation* dependInfo =
  268. this->GetDependInformation(file, info->PathOnly.c_str());
  269. this->GenerateDependInformation(dependInfo);
  270. info->AddDependencies(dependInfo);
  271. }
  272. /**
  273. * Fill in the given object with dependency information. If the
  274. * information is already complete, nothing is done.
  275. */
  276. void GenerateDependInformation(cmDependInformation* info)
  277. {
  278. // If dependencies are already done, stop now.
  279. if (info->DependDone) {
  280. return;
  281. }
  282. // Make sure we don't visit the same file more than once.
  283. info->DependDone = true;
  284. const char* path = info->FullPath.c_str();
  285. if (!path) {
  286. cmSystemTools::Error(
  287. "Attempt to find dependencies for file without path!");
  288. return;
  289. }
  290. bool found = false;
  291. // If the file exists, use it to find dependency information.
  292. if (cmSystemTools::FileExists(path, true)) {
  293. // Use the real file to find its dependencies.
  294. this->DependWalk(info);
  295. found = true;
  296. }
  297. // See if the cmSourceFile for it has any files specified as
  298. // dependency hints.
  299. if (info->SourceFile != CM_NULLPTR) {
  300. // Get the cmSourceFile corresponding to this.
  301. const cmSourceFile& cFile = *(info->SourceFile);
  302. // See if there are any hints for finding dependencies for the missing
  303. // file.
  304. if (!cFile.GetDepends().empty()) {
  305. // Dependency hints have been given. Use them to begin the
  306. // recursion.
  307. for (std::vector<std::string>::const_iterator file =
  308. cFile.GetDepends().begin();
  309. file != cFile.GetDepends().end(); ++file) {
  310. this->AddDependency(info, file->c_str());
  311. }
  312. // Found dependency information. We are done.
  313. found = true;
  314. }
  315. }
  316. if (!found) {
  317. // Try to find the file amongst the sources
  318. cmSourceFile* srcFile = this->Makefile->GetSource(
  319. cmSystemTools::GetFilenameWithoutExtension(path));
  320. if (srcFile) {
  321. if (srcFile->GetFullPath() == path) {
  322. found = true;
  323. } else {
  324. // try to guess which include path to use
  325. for (std::vector<std::string>::iterator t =
  326. this->IncludeDirectories.begin();
  327. t != this->IncludeDirectories.end(); ++t) {
  328. std::string incpath = *t;
  329. if (!incpath.empty() && incpath[incpath.size() - 1] != '/') {
  330. incpath = incpath + "/";
  331. }
  332. incpath = incpath + path;
  333. if (srcFile->GetFullPath() == incpath) {
  334. // set the path to the guessed path
  335. info->FullPath = incpath;
  336. found = true;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. if (!found) {
  343. // Couldn't find any dependency information.
  344. if (this->ComplainFileRegularExpression.find(
  345. info->IncludeName.c_str())) {
  346. cmSystemTools::Error("error cannot find dependencies for ", path);
  347. } else {
  348. // Destroy the name of the file so that it won't be output as a
  349. // dependency.
  350. info->FullPath = "";
  351. }
  352. }
  353. }
  354. /**
  355. * Get an instance of cmDependInformation corresponding to the given file
  356. * name.
  357. */
  358. cmDependInformation* GetDependInformation(const char* file,
  359. const char* extraPath)
  360. {
  361. // Get the full path for the file so that lookup is unambiguous.
  362. std::string fullPath = this->FullPath(file, extraPath);
  363. // Try to find the file's instance of cmDependInformation.
  364. DependInformationMapType::const_iterator result =
  365. this->DependInformationMap.find(fullPath);
  366. if (result != this->DependInformationMap.end()) {
  367. // Found an instance, return it.
  368. return result->second;
  369. }
  370. // Didn't find an instance. Create a new one and save it.
  371. cmDependInformation* info = new cmDependInformation;
  372. info->FullPath = fullPath;
  373. info->PathOnly = cmSystemTools::GetFilenamePath(fullPath);
  374. info->IncludeName = file;
  375. this->DependInformationMap[fullPath] = info;
  376. return info;
  377. }
  378. /**
  379. * Find the full path name for the given file name.
  380. * This uses the include directories.
  381. * TODO: Cache path conversions to reduce FileExists calls.
  382. */
  383. std::string FullPath(const char* fname, const char* extraPath)
  384. {
  385. DirectoryToFileToPathMapType::iterator m;
  386. if (extraPath) {
  387. m = this->DirectoryToFileToPathMap.find(extraPath);
  388. } else {
  389. m = this->DirectoryToFileToPathMap.find("");
  390. }
  391. if (m != this->DirectoryToFileToPathMap.end()) {
  392. FileToPathMapType& map = m->second;
  393. FileToPathMapType::iterator p = map.find(fname);
  394. if (p != map.end()) {
  395. return p->second;
  396. }
  397. }
  398. if (cmSystemTools::FileExists(fname, true)) {
  399. std::string fp = cmSystemTools::CollapseFullPath(fname);
  400. this->DirectoryToFileToPathMap[extraPath ? extraPath : ""][fname] = fp;
  401. return fp;
  402. }
  403. for (std::vector<std::string>::iterator i =
  404. this->IncludeDirectories.begin();
  405. i != this->IncludeDirectories.end(); ++i) {
  406. std::string path = *i;
  407. if (!path.empty() && path[path.size() - 1] != '/') {
  408. path = path + "/";
  409. }
  410. path = path + fname;
  411. if (cmSystemTools::FileExists(path.c_str(), true) &&
  412. !cmSystemTools::FileIsDirectory(path)) {
  413. std::string fp = cmSystemTools::CollapseFullPath(path);
  414. this->DirectoryToFileToPathMap[extraPath ? extraPath : ""][fname] = fp;
  415. return fp;
  416. }
  417. }
  418. if (extraPath) {
  419. std::string path = extraPath;
  420. if (!path.empty() && path[path.size() - 1] != '/') {
  421. path = path + "/";
  422. }
  423. path = path + fname;
  424. if (cmSystemTools::FileExists(path.c_str(), true) &&
  425. !cmSystemTools::FileIsDirectory(path)) {
  426. std::string fp = cmSystemTools::CollapseFullPath(path);
  427. this->DirectoryToFileToPathMap[extraPath][fname] = fp;
  428. return fp;
  429. }
  430. }
  431. // Couldn't find the file.
  432. return std::string(fname);
  433. }
  434. cmMakefile* Makefile;
  435. bool Verbose;
  436. cmsys::RegularExpression IncludeFileRegularExpression;
  437. cmsys::RegularExpression ComplainFileRegularExpression;
  438. std::vector<std::string> IncludeDirectories;
  439. typedef std::map<std::string, std::string> FileToPathMapType;
  440. typedef std::map<std::string, FileToPathMapType>
  441. DirectoryToFileToPathMapType;
  442. typedef std::map<std::string, cmDependInformation*> DependInformationMapType;
  443. DependInformationMapType DependInformationMap;
  444. DirectoryToFileToPathMapType DirectoryToFileToPathMap;
  445. };
  446. // cmOutputRequiredFilesCommand
  447. bool cmOutputRequiredFilesCommand::InitialPass(
  448. std::vector<std::string> const& args, cmExecutionStatus&)
  449. {
  450. if (args.size() != 2) {
  451. this->SetError("called with incorrect number of arguments");
  452. return false;
  453. }
  454. // store the arg for final pass
  455. this->File = args[0];
  456. this->OutputFile = args[1];
  457. // compute the list of files
  458. cmLBDepend md;
  459. md.SetMakefile(this->Makefile);
  460. md.AddSearchPath(this->Makefile->GetCurrentSourceDirectory());
  461. // find the depends for a file
  462. const cmDependInformation* info = md.FindDependencies(this->File.c_str());
  463. if (info) {
  464. // write them out
  465. FILE* fout = cmsys::SystemTools::Fopen(this->OutputFile, "w");
  466. if (!fout) {
  467. std::string err = "Can not open output file: ";
  468. err += this->OutputFile;
  469. this->SetError(err);
  470. return false;
  471. }
  472. std::set<cmDependInformation const*> visited;
  473. this->ListDependencies(info, fout, &visited);
  474. fclose(fout);
  475. }
  476. return true;
  477. }
  478. void cmOutputRequiredFilesCommand::ListDependencies(
  479. cmDependInformation const* info, FILE* fout,
  480. std::set<cmDependInformation const*>* visited)
  481. {
  482. // add info to the visited set
  483. visited->insert(info);
  484. // now recurse with info's dependencies
  485. for (cmDependInformation::DependencySetType::const_iterator d =
  486. info->DependencySet.begin();
  487. d != info->DependencySet.end(); ++d) {
  488. if (visited->find(*d) == visited->end()) {
  489. if (info->FullPath != "") {
  490. std::string tmp = (*d)->FullPath;
  491. std::string::size_type pos = tmp.rfind('.');
  492. if (pos != std::string::npos && (tmp.substr(pos) != ".h")) {
  493. tmp = tmp.substr(0, pos);
  494. fprintf(fout, "%s\n", (*d)->FullPath.c_str());
  495. }
  496. }
  497. this->ListDependencies(*d, fout, visited);
  498. }
  499. }
  500. }