2
0

ppDependableFile.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // Filename: ppDependableFile.cxx
  2. // Created by: drose (15Oct00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "ppDependableFile.h"
  6. #include "ppDirectory.h"
  7. #include "ppDirectoryTree.h"
  8. #include "check_include.h"
  9. #include <assert.h>
  10. #include <unistd.h>
  11. #include <sys/stat.h>
  12. #include <algorithm>
  13. class SortDependableFilesByName {
  14. public:
  15. bool operator () (PPDependableFile *a, PPDependableFile *b) const {
  16. return a->get_filename() < b->get_filename();
  17. }
  18. };
  19. ////////////////////////////////////////////////////////////////////
  20. // Function: PPDependableFile::Ordering Operator
  21. // Access: Public
  22. // Description: We provide this function so we can sort the
  23. // dependency list into a consistent ordering, so that
  24. // the makefiles won't get randomly regenerated between
  25. // different sessions.
  26. ////////////////////////////////////////////////////////////////////
  27. bool PPDependableFile::Dependency::
  28. operator < (const Dependency &other) const {
  29. return _file->get_filename() < other._file->get_filename();
  30. }
  31. ////////////////////////////////////////////////////////////////////
  32. // Function: PPDependableFile::Constructor
  33. // Access: Public
  34. // Description:
  35. ////////////////////////////////////////////////////////////////////
  36. PPDependableFile::
  37. PPDependableFile(PPDirectory *directory, const string &filename) :
  38. _directory(directory),
  39. _filename(filename)
  40. {
  41. _flags = 0;
  42. _mtime = 0;
  43. }
  44. ////////////////////////////////////////////////////////////////////
  45. // Function: PPDependableFile::update_from_cache
  46. // Access: Public
  47. // Description: Called as the dependency cache file is being read,
  48. // this asks the file to update its information from the
  49. // cache file if appropriate. This means comparing the
  50. // cached modification time against the file's actual
  51. // modification time, and storing the cached
  52. // dependencies if they match.
  53. ////////////////////////////////////////////////////////////////////
  54. void PPDependableFile::
  55. update_from_cache(const vector<string> &words) {
  56. // Shouldn't call this once the file has actually been read.
  57. assert((_flags & F_updated) == 0);
  58. assert((_flags & F_updating) == 0);
  59. assert((_flags & F_from_cache) == 0);
  60. assert(words.size() >= 2);
  61. // The second parameter is the cached modification time.
  62. time_t mtime = strtol(words[1].c_str(), (char **)NULL, 10);
  63. if (mtime == get_mtime()) {
  64. // The modification matches; preserve the cache information.
  65. PPDirectoryTree *tree = _directory->get_tree();
  66. _dependencies.clear();
  67. vector<string>::const_iterator wi;
  68. for (wi = words.begin() + 2; wi != words.end(); ++wi) {
  69. string dirpath = (*wi);
  70. Dependency dep;
  71. dep._okcircular = false;
  72. if (dirpath.length() > 1 && dirpath[0] == '/') {
  73. // If the first character is '/', it means that the file has
  74. // been marked okcircular.
  75. dep._okcircular = true;
  76. dirpath = dirpath.substr(1);
  77. }
  78. if (dirpath.length() > 2 && dirpath.substr(0, 2) == "*/") {
  79. // This is an extra include file, not a file in this source
  80. // tree.
  81. _extra_includes.push_back(dirpath.substr(2));
  82. } else {
  83. dep._file =
  84. tree->get_dependable_file_by_dirpath(dirpath, false);
  85. if (dep._file != (PPDependableFile *)NULL) {
  86. _dependencies.push_back(dep);
  87. }
  88. }
  89. }
  90. _flags |= F_from_cache;
  91. sort(_dependencies.begin(), _dependencies.end());
  92. }
  93. }
  94. ////////////////////////////////////////////////////////////////////
  95. // Function: PPDependableFile::write_cache
  96. // Access: Public
  97. // Description: Writes the dependency information out as a single
  98. // line to the indicated dependency cache file.
  99. ////////////////////////////////////////////////////////////////////
  100. void PPDependableFile::
  101. write_cache(ostream &out) {
  102. out << _filename << " " << get_mtime();
  103. Dependencies::const_iterator di;
  104. for (di = _dependencies.begin(); di != _dependencies.end(); ++di) {
  105. out << " ";
  106. if ((*di)._okcircular) {
  107. out << "/";
  108. }
  109. out << (*di)._file->get_dirpath();
  110. }
  111. // Also write out the extra includes--those #include directives
  112. // which do not reference a file within this source tree. We need
  113. // those just for comparison's sake later, so we can tell whether
  114. // the cache line is still current (without having to know which
  115. // files are part of the tree).
  116. ExtraIncludes::const_iterator ei;
  117. for (ei = _extra_includes.begin(); ei != _extra_includes.end(); ++ei) {
  118. out << " */" << (*ei);
  119. }
  120. out << "\n";
  121. }
  122. ////////////////////////////////////////////////////////////////////
  123. // Function: PPDependableFile::get_directory
  124. // Access: Public
  125. // Description: Returns the directory that this file can be found in.
  126. ////////////////////////////////////////////////////////////////////
  127. PPDirectory *PPDependableFile::
  128. get_directory() const {
  129. return _directory;
  130. }
  131. ////////////////////////////////////////////////////////////////////
  132. // Function: PPDependableFile::get_filename
  133. // Access: Public
  134. // Description: Returns the local filename of this particular file
  135. // within the directory.
  136. ////////////////////////////////////////////////////////////////////
  137. const string &PPDependableFile::
  138. get_filename() const {
  139. return _filename;
  140. }
  141. ////////////////////////////////////////////////////////////////////
  142. // Function: PPDependableFile::get_pathname
  143. // Access: Public
  144. // Description: Returns the relative pathname from the root of the
  145. // source tree to this particular filename.
  146. ////////////////////////////////////////////////////////////////////
  147. string PPDependableFile::
  148. get_pathname() const {
  149. return _directory->get_path() + "/" + _filename;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: PPDependableFile::get_dirpath
  153. // Access: Public
  154. // Description: Returns an abbreviated pathname to this file, in the
  155. // form dirname/filename.
  156. ////////////////////////////////////////////////////////////////////
  157. string PPDependableFile::
  158. get_dirpath() const {
  159. return _directory->get_dirname() + "/" + _filename;
  160. }
  161. ////////////////////////////////////////////////////////////////////
  162. // Function: PPDependableFile::exists
  163. // Access: Public
  164. // Description: Returns true if the file exists, false if it does
  165. // not.
  166. ////////////////////////////////////////////////////////////////////
  167. bool PPDependableFile::
  168. exists() {
  169. stat_file();
  170. return ((_flags & F_exists) != 0);
  171. }
  172. ////////////////////////////////////////////////////////////////////
  173. // Function: PPDependableFile::get_mtime
  174. // Access: Public
  175. // Description: Returns the last modification time of the file.
  176. ////////////////////////////////////////////////////////////////////
  177. time_t PPDependableFile::
  178. get_mtime() {
  179. stat_file();
  180. return _mtime;
  181. }
  182. ////////////////////////////////////////////////////////////////////
  183. // Function: PPDependableFile::get_num_dependencies
  184. // Access: Public
  185. // Description: Returns the number of files this file depends on.
  186. ////////////////////////////////////////////////////////////////////
  187. int PPDependableFile::
  188. get_num_dependencies() {
  189. update_dependencies();
  190. return _dependencies.size();
  191. }
  192. ////////////////////////////////////////////////////////////////////
  193. // Function: PPDependableFile::get_dependency
  194. // Access: Public
  195. // Description: Returns the nth file this file depends on.
  196. ////////////////////////////////////////////////////////////////////
  197. PPDependableFile *PPDependableFile::
  198. get_dependency(int n) {
  199. assert((_flags & F_updated) != 0);
  200. assert(n >= 0 && n < (int)_dependencies.size());
  201. return _dependencies[n]._file;
  202. }
  203. ////////////////////////////////////////////////////////////////////
  204. // Function: PPDependableFile::get_complete_dependencies
  205. // Access: Public
  206. // Description: This flavor of get_complete_dependencies() works like
  207. // the one below, except it returns the results in a
  208. // consistently-ordered vector. This allows us to keep
  209. // the dependencies in the same order between sessions
  210. // and prevent makefiles from being arbitrarily
  211. // regenerated.
  212. ////////////////////////////////////////////////////////////////////
  213. void PPDependableFile::
  214. get_complete_dependencies(vector<PPDependableFile *> &files) {
  215. set<PPDependableFile *> files_set;
  216. get_complete_dependencies(files_set);
  217. copy(files_set.begin(), files_set.end(), back_inserter(files));
  218. sort(files.begin(), files.end(), SortDependableFilesByName());
  219. }
  220. ////////////////////////////////////////////////////////////////////
  221. // Function: PPDependableFile::get_complete_dependencies
  222. // Access: Public
  223. // Description: Recursively determines the complete set of files this
  224. // file depends on. It is the user's responsibility to
  225. // empty the set before calling this function; the
  226. // results will simply be added to the existing set.
  227. ////////////////////////////////////////////////////////////////////
  228. void PPDependableFile::
  229. get_complete_dependencies(set<PPDependableFile *> &files) {
  230. update_dependencies();
  231. Dependencies::const_iterator di;
  232. for (di = _dependencies.begin(); di != _dependencies.end(); ++di) {
  233. PPDependableFile *file = (*di)._file;
  234. if (files.insert(file).second) {
  235. file->get_complete_dependencies(files);
  236. }
  237. }
  238. }
  239. ////////////////////////////////////////////////////////////////////
  240. // Function: PPDependableFile::is_circularity
  241. // Access: Public
  242. // Description: Returns true if a circular dependency exists between
  243. // this file and one or more other files.
  244. ////////////////////////////////////////////////////////////////////
  245. bool PPDependableFile::
  246. is_circularity() {
  247. update_dependencies();
  248. return (_flags & F_circularity) != 0;
  249. }
  250. ////////////////////////////////////////////////////////////////////
  251. // Function: PPDependableFile::get_circularity
  252. // Access: Public
  253. // Description: If is_circularity() returns true, returns a string
  254. // describing the circular dependency path for the user.
  255. ////////////////////////////////////////////////////////////////////
  256. string PPDependableFile::
  257. get_circularity() {
  258. update_dependencies();
  259. return _circularity;
  260. }
  261. ////////////////////////////////////////////////////////////////////
  262. // Function: PPDependableFile::was_examined
  263. // Access: Public
  264. // Description: Returns true if anyone ever asked this file for its
  265. // list of dependencies, or false otherwise.
  266. ////////////////////////////////////////////////////////////////////
  267. bool PPDependableFile::
  268. was_examined() const {
  269. return ((_flags & F_updated) != 0);
  270. }
  271. ////////////////////////////////////////////////////////////////////
  272. // Function: PPDependableFile::update_dependencies
  273. // Access: Private
  274. // Description: Builds up the dependency list--the list of files this
  275. // file depends on--if it hasn't already been built. If
  276. // a circular dependency is detected during this
  277. // process, _circularity and _circularity_detected will
  278. // be updated accordingly.
  279. ////////////////////////////////////////////////////////////////////
  280. void PPDependableFile::
  281. update_dependencies() {
  282. if ((_flags & F_updated) != 0) {
  283. return;
  284. }
  285. assert((_flags & F_updating) == 0);
  286. string circularity;
  287. compute_dependencies(circularity);
  288. }
  289. ////////////////////////////////////////////////////////////////////
  290. // Function: PPDependableFile::compute_dependencies
  291. // Access: Private
  292. // Description: Builds up the dependency list--the list of files this
  293. // file depends on--if it hasn't already been built.
  294. //
  295. // If a circularity is detected, e.g. two files depend
  296. // on each other, a pointer to the offending file is
  297. // returned and the string is updated to indicate the
  298. // circularity. Otherwise, if there is no circularity,
  299. // NULL is returned.
  300. ////////////////////////////////////////////////////////////////////
  301. PPDependableFile *PPDependableFile::
  302. compute_dependencies(string &circularity) {
  303. if ((_flags & F_updated) != 0) {
  304. return (PPDependableFile *)NULL;
  305. } else if ((_flags & F_updating) != 0) {
  306. // Oh oh, a circular dependency!
  307. circularity = get_dirpath();
  308. return this;
  309. }
  310. _flags |= F_updating;
  311. if ((_flags & F_from_cache) == 0) {
  312. // Now open the file and scan it for #include statements.
  313. ifstream in(get_pathname().c_str());
  314. if (!in) {
  315. // Can't read the file, or the file doesn't exist. Interesting.
  316. if (exists()) {
  317. cerr << "Warning: dependent file " << get_pathname()
  318. << " exists but cannot be read.\n";
  319. } else {
  320. cerr << "Warning: dependent file " << get_pathname()
  321. << " does not exist.\n";
  322. }
  323. } else {
  324. PPDirectoryTree *tree = _directory->get_tree();
  325. bool okcircular = false;
  326. string line;
  327. getline(in, line);
  328. while (!in.fail() && !in.eof()) {
  329. if (line.substr(0, 16) == "/* okcircular */") {
  330. okcircular = true;
  331. } else {
  332. string filename = check_include(line);
  333. if (!filename.empty() && filename.find('/') == string::npos) {
  334. Dependency dep;
  335. dep._okcircular = okcircular;
  336. dep._file = tree->find_dependable_file(filename);
  337. if (dep._file != (PPDependableFile *)NULL) {
  338. // All right! Here's a file we depend on. Add it to the
  339. // list.
  340. _dependencies.push_back(dep);
  341. } else {
  342. // It's an include file from somewhere else, not from within
  343. // our source tree. We don't care about it, but we do need
  344. // to record it so we can easily check later if the cache
  345. // file has gone stale.
  346. _extra_includes.push_back(filename);
  347. }
  348. }
  349. okcircular = false;
  350. }
  351. getline(in, line);
  352. }
  353. }
  354. }
  355. // Now recursively expand all our dependent files, so we can check
  356. // for circularities.
  357. PPDependableFile *circ = (PPDependableFile *)NULL;
  358. Dependencies::iterator di;
  359. for (di = _dependencies.begin();
  360. di != _dependencies.end() && circ == (PPDependableFile *)NULL;
  361. ++di) {
  362. // Skip this file if the user specifically marked it
  363. // with an "okcircular" comment.
  364. if (!(*di)._okcircular) {
  365. circ = (*di)._file->compute_dependencies(circularity);
  366. if (circ != (PPDependableFile *)NULL) {
  367. // Oops, a circularity. Silly user.
  368. circularity = get_dirpath() + " => " + circularity;
  369. if (circ == this) {
  370. _flags |= F_circularity;
  371. _circularity = circularity;
  372. }
  373. }
  374. }
  375. }
  376. _flags = (_flags & ~F_updating) | F_updated;
  377. sort(_dependencies.begin(), _dependencies.end());
  378. return circ;
  379. }
  380. ////////////////////////////////////////////////////////////////////
  381. // Function: PPDependableFile::stat_file
  382. // Access: Private
  383. // Description: Performs a stat() on the file, if it has not already
  384. // been performed, to check whether the file exists and
  385. // to get its last-modification time.
  386. ////////////////////////////////////////////////////////////////////
  387. void PPDependableFile::
  388. stat_file() {
  389. if ((_flags & F_statted) != 0) {
  390. // Already done.
  391. return;
  392. }
  393. _flags |= F_statted;
  394. struct stat st;
  395. if (stat(get_pathname().c_str(), &st) < 0) {
  396. // The file doesn't exist!
  397. return;
  398. }
  399. if (!S_ISREG(st.st_mode)) {
  400. // The file exists, but it's not a regular file--we consider that
  401. // not existing.
  402. return;
  403. }
  404. _flags |= F_exists;
  405. _mtime = st.st_mtime;
  406. }