cmOutputRequiredFilesCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <utility>
  8. #include "cmAlgorithms.h"
  9. #include "cmGeneratorExpression.h"
  10. #include "cmMakefile.h"
  11. #include "cmSourceFile.h"
  12. #include "cmSystemTools.h"
  13. #include "cmTarget.h"
  14. #include "cm_unordered_map.hxx"
  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. } else {
  167. qend = line.find('>', qstart + 1);
  168. }
  169. } else {
  170. qend = line.find('\"', qstart + 1);
  171. }
  172. // extract the file being included
  173. std::string includeFile = line.substr(qstart + 1, qend - qstart - 1);
  174. // see if the include matches the regular expression
  175. if (!this->IncludeFileRegularExpression.find(includeFile)) {
  176. if (this->Verbose) {
  177. std::string message = "Skipping ";
  178. message += includeFile;
  179. message += " for file ";
  180. message += info->FullPath;
  181. cmSystemTools::Error(message.c_str(), CM_NULLPTR);
  182. }
  183. continue;
  184. }
  185. // Add this file and all its dependencies.
  186. this->AddDependency(info, includeFile.c_str());
  187. /// add the cxx file if it exists
  188. std::string cxxFile = includeFile;
  189. std::string::size_type pos = cxxFile.rfind('.');
  190. if (pos != std::string::npos) {
  191. std::string root = cxxFile.substr(0, pos);
  192. cxxFile = root + ".cxx";
  193. bool found = false;
  194. // try jumping to .cxx .cpp and .c in order
  195. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  196. found = true;
  197. }
  198. for (std::vector<std::string>::iterator i =
  199. this->IncludeDirectories.begin();
  200. i != this->IncludeDirectories.end(); ++i) {
  201. std::string path = *i;
  202. path = path + "/";
  203. path = path + cxxFile;
  204. if (cmSystemTools::FileExists(path.c_str())) {
  205. found = true;
  206. }
  207. }
  208. if (!found) {
  209. cxxFile = root + ".cpp";
  210. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  211. found = true;
  212. }
  213. for (std::vector<std::string>::iterator i =
  214. this->IncludeDirectories.begin();
  215. i != this->IncludeDirectories.end(); ++i) {
  216. std::string path = *i;
  217. path = path + "/";
  218. path = path + cxxFile;
  219. if (cmSystemTools::FileExists(path.c_str())) {
  220. found = true;
  221. }
  222. }
  223. }
  224. if (!found) {
  225. cxxFile = root + ".c";
  226. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  227. found = true;
  228. }
  229. for (std::vector<std::string>::iterator i =
  230. this->IncludeDirectories.begin();
  231. i != this->IncludeDirectories.end(); ++i) {
  232. std::string path = *i;
  233. path = path + "/";
  234. path = path + cxxFile;
  235. if (cmSystemTools::FileExists(path.c_str())) {
  236. found = true;
  237. }
  238. }
  239. }
  240. if (!found) {
  241. cxxFile = root + ".txx";
  242. if (cmSystemTools::FileExists(cxxFile.c_str())) {
  243. found = true;
  244. }
  245. for (std::vector<std::string>::iterator i =
  246. this->IncludeDirectories.begin();
  247. i != this->IncludeDirectories.end(); ++i) {
  248. std::string path = *i;
  249. path = path + "/";
  250. path = path + cxxFile;
  251. if (cmSystemTools::FileExists(path.c_str())) {
  252. found = true;
  253. }
  254. }
  255. }
  256. if (found) {
  257. this->AddDependency(info, cxxFile.c_str());
  258. }
  259. }
  260. }
  261. }
  262. }
  263. /**
  264. * Add a dependency. Possibly walk it for more dependencies.
  265. */
  266. void AddDependency(cmDependInformation* info, const char* file)
  267. {
  268. cmDependInformation* dependInfo =
  269. this->GetDependInformation(file, info->PathOnly.c_str());
  270. this->GenerateDependInformation(dependInfo);
  271. info->AddDependencies(dependInfo);
  272. }
  273. /**
  274. * Fill in the given object with dependency information. If the
  275. * information is already complete, nothing is done.
  276. */
  277. void GenerateDependInformation(cmDependInformation* info)
  278. {
  279. // If dependencies are already done, stop now.
  280. if (info->DependDone) {
  281. return;
  282. }
  283. // Make sure we don't visit the same file more than once.
  284. info->DependDone = true;
  285. const char* path = info->FullPath.c_str();
  286. if (!path) {
  287. cmSystemTools::Error(
  288. "Attempt to find dependencies for file without path!");
  289. return;
  290. }
  291. bool found = false;
  292. // If the file exists, use it to find dependency information.
  293. if (cmSystemTools::FileExists(path, true)) {
  294. // Use the real file to find its dependencies.
  295. this->DependWalk(info);
  296. found = true;
  297. }
  298. // See if the cmSourceFile for it has any files specified as
  299. // dependency hints.
  300. if (info->SourceFile != CM_NULLPTR) {
  301. // Get the cmSourceFile corresponding to this.
  302. const cmSourceFile& cFile = *(info->SourceFile);
  303. // See if there are any hints for finding dependencies for the missing
  304. // file.
  305. if (!cFile.GetDepends().empty()) {
  306. // Dependency hints have been given. Use them to begin the
  307. // recursion.
  308. for (std::vector<std::string>::const_iterator file =
  309. cFile.GetDepends().begin();
  310. file != cFile.GetDepends().end(); ++file) {
  311. this->AddDependency(info, file->c_str());
  312. }
  313. // Found dependency information. We are done.
  314. found = true;
  315. }
  316. }
  317. if (!found) {
  318. // Try to find the file amongst the sources
  319. cmSourceFile* srcFile = this->Makefile->GetSource(
  320. cmSystemTools::GetFilenameWithoutExtension(path));
  321. if (srcFile) {
  322. if (srcFile->GetFullPath() == path) {
  323. found = true;
  324. } else {
  325. // try to guess which include path to use
  326. for (std::vector<std::string>::iterator t =
  327. this->IncludeDirectories.begin();
  328. t != this->IncludeDirectories.end(); ++t) {
  329. std::string incpath = *t;
  330. if (!incpath.empty() && incpath[incpath.size() - 1] != '/') {
  331. incpath = incpath + "/";
  332. }
  333. incpath = incpath + path;
  334. if (srcFile->GetFullPath() == incpath) {
  335. // set the path to the guessed path
  336. info->FullPath = incpath;
  337. found = true;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. if (!found) {
  344. // Couldn't find any dependency information.
  345. if (this->ComplainFileRegularExpression.find(
  346. info->IncludeName.c_str())) {
  347. cmSystemTools::Error("error cannot find dependencies for ", path);
  348. } else {
  349. // Destroy the name of the file so that it won't be output as a
  350. // dependency.
  351. info->FullPath = "";
  352. }
  353. }
  354. }
  355. /**
  356. * Get an instance of cmDependInformation corresponding to the given file
  357. * name.
  358. */
  359. cmDependInformation* GetDependInformation(const char* file,
  360. const char* extraPath)
  361. {
  362. // Get the full path for the file so that lookup is unambiguous.
  363. std::string fullPath = this->FullPath(file, extraPath);
  364. // Try to find the file's instance of cmDependInformation.
  365. DependInformationMapType::const_iterator result =
  366. this->DependInformationMap.find(fullPath);
  367. if (result != this->DependInformationMap.end()) {
  368. // Found an instance, return it.
  369. return result->second;
  370. }
  371. // Didn't find an instance. Create a new one and save it.
  372. cmDependInformation* info = new cmDependInformation;
  373. info->FullPath = fullPath;
  374. info->PathOnly = cmSystemTools::GetFilenamePath(fullPath);
  375. info->IncludeName = file;
  376. this->DependInformationMap[fullPath] = info;
  377. return info;
  378. }
  379. /**
  380. * Find the full path name for the given file name.
  381. * This uses the include directories.
  382. * TODO: Cache path conversions to reduce FileExists calls.
  383. */
  384. std::string FullPath(const char* fname, const char* extraPath)
  385. {
  386. DirectoryToFileToPathMapType::iterator m;
  387. if (extraPath) {
  388. m = this->DirectoryToFileToPathMap.find(extraPath);
  389. } else {
  390. m = this->DirectoryToFileToPathMap.find("");
  391. }
  392. if (m != this->DirectoryToFileToPathMap.end()) {
  393. FileToPathMapType& map = m->second;
  394. FileToPathMapType::iterator p = map.find(fname);
  395. if (p != map.end()) {
  396. return p->second;
  397. }
  398. }
  399. if (cmSystemTools::FileExists(fname, true)) {
  400. std::string fp = cmSystemTools::CollapseFullPath(fname);
  401. this->DirectoryToFileToPathMap[extraPath ? extraPath : ""][fname] = fp;
  402. return fp;
  403. }
  404. for (std::vector<std::string>::iterator i =
  405. this->IncludeDirectories.begin();
  406. i != this->IncludeDirectories.end(); ++i) {
  407. std::string path = *i;
  408. if (!path.empty() && path[path.size() - 1] != '/') {
  409. path = path + "/";
  410. }
  411. path = path + fname;
  412. if (cmSystemTools::FileExists(path.c_str(), true) &&
  413. !cmSystemTools::FileIsDirectory(path)) {
  414. std::string fp = cmSystemTools::CollapseFullPath(path);
  415. this->DirectoryToFileToPathMap[extraPath ? extraPath : ""][fname] = fp;
  416. return fp;
  417. }
  418. }
  419. if (extraPath) {
  420. std::string path = extraPath;
  421. if (!path.empty() && path[path.size() - 1] != '/') {
  422. path = path + "/";
  423. }
  424. path = path + fname;
  425. if (cmSystemTools::FileExists(path.c_str(), true) &&
  426. !cmSystemTools::FileIsDirectory(path)) {
  427. std::string fp = cmSystemTools::CollapseFullPath(path);
  428. this->DirectoryToFileToPathMap[extraPath][fname] = fp;
  429. return fp;
  430. }
  431. }
  432. // Couldn't find the file.
  433. return std::string(fname);
  434. }
  435. cmMakefile* Makefile;
  436. bool Verbose;
  437. cmsys::RegularExpression IncludeFileRegularExpression;
  438. cmsys::RegularExpression ComplainFileRegularExpression;
  439. std::vector<std::string> IncludeDirectories;
  440. typedef std::map<std::string, std::string> FileToPathMapType;
  441. typedef std::map<std::string, FileToPathMapType>
  442. DirectoryToFileToPathMapType;
  443. typedef std::map<std::string, cmDependInformation*> DependInformationMapType;
  444. DependInformationMapType DependInformationMap;
  445. DirectoryToFileToPathMapType DirectoryToFileToPathMap;
  446. };
  447. // cmOutputRequiredFilesCommand
  448. bool cmOutputRequiredFilesCommand::InitialPass(
  449. std::vector<std::string> const& args, cmExecutionStatus&)
  450. {
  451. if (args.size() != 2) {
  452. this->SetError("called with incorrect number of arguments");
  453. return false;
  454. }
  455. // store the arg for final pass
  456. this->File = args[0];
  457. this->OutputFile = args[1];
  458. // compute the list of files
  459. cmLBDepend md;
  460. md.SetMakefile(this->Makefile);
  461. md.AddSearchPath(this->Makefile->GetCurrentSourceDirectory());
  462. // find the depends for a file
  463. const cmDependInformation* info = md.FindDependencies(this->File.c_str());
  464. if (info) {
  465. // write them out
  466. FILE* fout = cmsys::SystemTools::Fopen(this->OutputFile, "w");
  467. if (!fout) {
  468. std::string err = "Can not open output file: ";
  469. err += this->OutputFile;
  470. this->SetError(err);
  471. return false;
  472. }
  473. std::set<cmDependInformation const*> visited;
  474. this->ListDependencies(info, fout, &visited);
  475. fclose(fout);
  476. }
  477. return true;
  478. }
  479. void cmOutputRequiredFilesCommand::ListDependencies(
  480. cmDependInformation const* info, FILE* fout,
  481. std::set<cmDependInformation const*>* visited)
  482. {
  483. // add info to the visited set
  484. visited->insert(info);
  485. // now recurse with info's dependencies
  486. for (cmDependInformation::DependencySetType::const_iterator d =
  487. info->DependencySet.begin();
  488. d != info->DependencySet.end(); ++d) {
  489. if (visited->find(*d) == visited->end()) {
  490. if (info->FullPath != "") {
  491. std::string tmp = (*d)->FullPath;
  492. std::string::size_type pos = tmp.rfind('.');
  493. if (pos != std::string::npos && (tmp.substr(pos) != ".h")) {
  494. tmp = tmp.substr(0, pos);
  495. fprintf(fout, "%s\n", (*d)->FullPath.c_str());
  496. }
  497. }
  498. this->ListDependencies(*d, fout, visited);
  499. }
  500. }
  501. }