cmOutputRequiredFilesCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 "cmAlgorithms.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmMakefile.h"
  9. #include "cmPolicies.h"
  10. #include "cmSourceFile.h"
  11. #include "cmSystemTools.h"
  12. #include "cmTarget.h"
  13. class cmExecutionStatus;
  14. /** \class cmDependInformation
  15. * \brief Store dependency information for a single source file.
  16. *
  17. * This structure stores the depend information for a single source file.
  18. */
  19. class cmDependInformation
  20. {
  21. public:
  22. /**
  23. * Construct with dependency generation marked not done; instance
  24. * not placed in cmMakefile's list.
  25. */
  26. cmDependInformation()
  27. : DependDone(false)
  28. , SourceFile(CM_NULLPTR)
  29. {
  30. }
  31. /**
  32. * The set of files on which this one depends.
  33. */
  34. typedef std::set<cmDependInformation*> DependencySetType;
  35. DependencySetType DependencySet;
  36. /**
  37. * This flag indicates whether dependency checking has been
  38. * performed for this file.
  39. */
  40. bool DependDone;
  41. /**
  42. * If this object corresponds to a cmSourceFile instance, this points
  43. * to it.
  44. */
  45. const cmSourceFile* SourceFile;
  46. /**
  47. * Full path to this file.
  48. */
  49. std::string FullPath;
  50. /**
  51. * Full path not including file name.
  52. */
  53. std::string PathOnly;
  54. /**
  55. * Name used to #include this file.
  56. */
  57. std::string IncludeName;
  58. /**
  59. * This method adds the dependencies of another file to this one.
  60. */
  61. void AddDependencies(cmDependInformation* info)
  62. {
  63. if (this != info) {
  64. this->DependencySet.insert(info);
  65. }
  66. }
  67. };
  68. class cmLBDepend
  69. {
  70. public:
  71. /**
  72. * Construct the object with verbose turned off.
  73. */
  74. cmLBDepend()
  75. {
  76. this->Verbose = false;
  77. this->IncludeFileRegularExpression.compile("^.*$");
  78. this->ComplainFileRegularExpression.compile("^$");
  79. }
  80. /**
  81. * Destructor.
  82. */
  83. ~cmLBDepend() { cmDeleteAll(this->DependInformationMap); }
  84. /**
  85. * Set the makefile that is used as a source of classes.
  86. */
  87. void SetMakefile(cmMakefile* makefile)
  88. {
  89. this->Makefile = makefile;
  90. // Now extract the include file regular expression from the makefile.
  91. this->IncludeFileRegularExpression.compile(
  92. this->Makefile->GetIncludeRegularExpression());
  93. this->ComplainFileRegularExpression.compile(
  94. this->Makefile->GetComplainRegularExpression());
  95. // Now extract any include paths from the targets
  96. std::set<std::string> uniqueIncludes;
  97. std::vector<std::string> orderedAndUniqueIncludes;
  98. cmTargets& targets = this->Makefile->GetTargets();
  99. for (cmTargets::iterator l = targets.begin(); l != targets.end(); ++l) {
  100. const char* incDirProp = l->second.GetProperty("INCLUDE_DIRECTORIES");
  101. if (!incDirProp) {
  102. continue;
  103. }
  104. std::string incDirs = cmGeneratorExpression::Preprocess(
  105. incDirProp, cmGeneratorExpression::StripAllGeneratorExpressions);
  106. std::vector<std::string> includes;
  107. cmSystemTools::ExpandListArgument(incDirs, includes);
  108. for (std::vector<std::string>::const_iterator j = includes.begin();
  109. j != includes.end(); ++j) {
  110. std::string path = *j;
  111. this->Makefile->ExpandVariablesInString(path);
  112. if (uniqueIncludes.insert(path).second) {
  113. orderedAndUniqueIncludes.push_back(path);
  114. }
  115. }
  116. }
  117. for (std::vector<std::string>::const_iterator it =
  118. orderedAndUniqueIncludes.begin();
  119. it != orderedAndUniqueIncludes.end(); ++it) {
  120. this->AddSearchPath(*it);
  121. }
  122. }
  123. /**
  124. * Add a directory to the search path for include files.
  125. */
  126. void AddSearchPath(const std::string& path)
  127. {
  128. this->IncludeDirectories.push_back(path);
  129. }
  130. /**
  131. * Generate dependencies for the file given. Returns a pointer to
  132. * the cmDependInformation object for the file.
  133. */
  134. const cmDependInformation* FindDependencies(const char* file)
  135. {
  136. cmDependInformation* info = this->GetDependInformation(file, CM_NULLPTR);
  137. this->GenerateDependInformation(info);
  138. return info;
  139. }
  140. protected:
  141. /**
  142. * Compute the depend information for this class.
  143. */
  144. void DependWalk(cmDependInformation* info)
  145. {
  146. cmsys::ifstream fin(info->FullPath.c_str());
  147. if (!fin) {
  148. cmSystemTools::Error("error can not open ", info->FullPath.c_str());
  149. return;
  150. }
  151. std::string line;
  152. while (cmSystemTools::GetLineFromStream(fin, line)) {
  153. if (cmHasLiteralPrefix(line.c_str(), "#include")) {
  154. // if it is an include line then create a string class
  155. std::string currentline = line;
  156. size_t qstart = currentline.find('\"', 8);
  157. size_t qend;
  158. // if a quote is not found look for a <
  159. if (qstart == std::string::npos) {
  160. qstart = currentline.find('<', 8);
  161. // if a < is not found then move on
  162. if (qstart == std::string::npos) {
  163. cmSystemTools::Error("unknown include directive ",
  164. currentline.c_str());
  165. continue;
  166. } else {
  167. qend = currentline.find('>', qstart + 1);
  168. }
  169. } else {
  170. qend = currentline.find('\"', qstart + 1);
  171. }
  172. // extract the file being included
  173. std::string includeFile =
  174. currentline.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.c_str();
  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 (this->Disallowed(cmPolicies::CMP0032, "The output_required_files "
  453. "command should not be called; "
  454. "see CMP0032.")) {
  455. return true;
  456. }
  457. if (args.size() != 2) {
  458. this->SetError("called with incorrect number of arguments");
  459. return false;
  460. }
  461. // store the arg for final pass
  462. this->File = args[0];
  463. this->OutputFile = args[1];
  464. // compute the list of files
  465. cmLBDepend md;
  466. md.SetMakefile(this->Makefile);
  467. md.AddSearchPath(this->Makefile->GetCurrentSourceDirectory());
  468. // find the depends for a file
  469. const cmDependInformation* info = md.FindDependencies(this->File.c_str());
  470. if (info) {
  471. // write them out
  472. FILE* fout = cmsys::SystemTools::Fopen(this->OutputFile.c_str(), "w");
  473. if (!fout) {
  474. std::string err = "Can not open output file: ";
  475. err += this->OutputFile;
  476. this->SetError(err);
  477. return false;
  478. }
  479. std::set<cmDependInformation const*> visited;
  480. this->ListDependencies(info, fout, &visited);
  481. fclose(fout);
  482. }
  483. return true;
  484. }
  485. void cmOutputRequiredFilesCommand::ListDependencies(
  486. cmDependInformation const* info, FILE* fout,
  487. std::set<cmDependInformation const*>* visited)
  488. {
  489. // add info to the visited set
  490. visited->insert(info);
  491. // now recurse with info's dependencies
  492. for (cmDependInformation::DependencySetType::const_iterator d =
  493. info->DependencySet.begin();
  494. d != info->DependencySet.end(); ++d) {
  495. if (visited->find(*d) == visited->end()) {
  496. if (info->FullPath != "") {
  497. std::string tmp = (*d)->FullPath;
  498. std::string::size_type pos = tmp.rfind('.');
  499. if (pos != std::string::npos && (tmp.substr(pos) != ".h")) {
  500. tmp = tmp.substr(0, pos);
  501. fprintf(fout, "%s\n", (*d)->FullPath.c_str());
  502. }
  503. }
  504. this->ListDependencies(*d, fout, visited);
  505. }
  506. }
  507. }