cmOutputRequiredFilesCommand.cxx 17 KB

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