cmOutputRequiredFilesCommand.cxx 17 KB

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