ppDependableFile.cxx 14 KB

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