cmOutputRequiredFilesCommand.cxx 15 KB

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