depcheck.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* depcheck.cpp - Dependency checker for NMake Makefiles
  2. * Copyright (c) 2024 Mika T. Lindqvist
  3. */
  4. #include <fstream>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. int main(int argc, char* argv[]) {
  10. if (argc != 3) {
  11. printf("Usage: depcheck Makefile <top_directory>\n");
  12. return -1;
  13. }
  14. std::filebuf fb;
  15. if (fb.open (argv[1],std::ios::in)) {
  16. std::istream is(&fb);
  17. std::string makefile = argv[1];
  18. std::string l, tmp, tmp2;
  19. while (is) {
  20. std::getline(is, l);
  21. while (l.back() == '\\') {
  22. std::getline(is, tmp);
  23. l.replace(l.length() - 1, 1, tmp);
  24. }
  25. size_t pos = l.find("obj:");
  26. if (pos != std::string::npos) {
  27. std::string objfile = l.substr(0, pos+3);
  28. printf("File: %s\n", objfile.c_str());
  29. std::vector<std::string> files;
  30. std::stringstream ss(l.substr(pos+4));
  31. while(getline(ss, tmp, ' ')){
  32. if (tmp != "" && tmp != "/") {
  33. files.push_back(tmp);
  34. }
  35. }
  36. for (auto it = files.begin(); it != files.end(); ++it) {
  37. printf("Dependency: %s\n", (*it).c_str());
  38. }
  39. if (!files.empty()) {
  40. std::filebuf fb2;
  41. std::string src = files[0];
  42. size_t pos2 = src.find("$(TOP)");
  43. if (pos2 != std::string::npos) {
  44. src.replace(pos2, 6, argv[2]);
  45. }
  46. printf("Source: %s\n", src.c_str());
  47. if (fb2.open(src.c_str(),std::ios::in)) {
  48. std::istream is2(&fb2);
  49. std::vector<std::string> includes;
  50. while (is2) {
  51. std::getline(is2, l);
  52. pos = l.find("#");
  53. if (pos != std::string::npos) {
  54. pos2 = l.find("include");
  55. size_t pos3 = l.find("\"");
  56. if (pos2 != std::string::npos && pos3 != std::string::npos && pos2 > pos && pos3 > pos2) {
  57. tmp = l.substr(pos3 + 1);
  58. pos2 = tmp.find("\"");
  59. if (pos2 != std::string::npos) {
  60. tmp = tmp.substr(0, pos2);
  61. }
  62. pos2 = tmp.find("../");
  63. if (pos2 != std::string::npos) {
  64. tmp = tmp.substr(3);
  65. }
  66. printf("Line: %s\n", tmp.c_str());
  67. int found = 0;
  68. for (size_t i = 1; i < files.size(); i++) {
  69. pos3 = files[i].find("$(SUFFIX)");
  70. if (pos3 != std::string::npos) {
  71. tmp2 = files[i].substr(0, pos3).append(files[i].substr(pos3 + 9));
  72. printf("Comparing dependency \"%s\" and \"%s\"\n", tmp2.c_str(), tmp.c_str());
  73. if (tmp2 == tmp) {
  74. printf("Dependency %s OK\n", tmp.c_str());
  75. found = 1;
  76. includes.push_back(tmp);
  77. break;
  78. }
  79. printf("Comparing dependency \"%s\" and \"$(TOP)/%s\"\n", tmp2.c_str(), tmp.c_str());
  80. if (tmp2 == std::string("$(TOP)/").append(tmp)) {
  81. printf("Dependency %s OK\n", tmp.c_str());
  82. found = 1;
  83. includes.push_back(tmp);
  84. break;
  85. }
  86. tmp2 = files[i].substr(0, pos3).append("-ng").append(files[i].substr(pos3 + 9));
  87. printf("Comparing dependency \"%s\" and \"%s\"\n", tmp2.c_str(), tmp.c_str());
  88. if (tmp2 == tmp) {
  89. printf("Dependency %s OK\n", tmp.c_str());
  90. found = 1;
  91. includes.push_back(tmp);
  92. break;
  93. }
  94. printf("Comparing dependency \"%s\" and \"$(TOP)/%s\"\n", tmp2.c_str(), tmp.c_str());
  95. if (tmp2 == std::string("$(TOP)/").append(tmp)) {
  96. printf("Dependency %s OK\n", tmp.c_str());
  97. found = 1;
  98. includes.push_back(tmp);
  99. break;
  100. }
  101. } else {
  102. printf("Comparing dependency \"%s\" and \"%s\"\n", files[i].c_str(), tmp.c_str());
  103. if (files[i] == tmp) {
  104. printf("Dependency %s OK\n", tmp.c_str());
  105. found = 1;
  106. includes.push_back(tmp);
  107. break;
  108. }
  109. printf("Comparing dependency \"%s\" and \"$(TOP)/%s\"\n", files[i].c_str(), tmp.c_str());
  110. if (files[i] == std::string("$(TOP)/").append(tmp)) {
  111. printf("Dependency %s OK\n", tmp.c_str());
  112. found = 1;
  113. includes.push_back(tmp);
  114. break;
  115. }
  116. printf("Comparing dependency \"%s\" and \"$(TOP)/arch/%s\"\n", files[i].c_str(), tmp.c_str());
  117. if (files[i] == std::string("$(TOP)/arch/").append(tmp)) {
  118. printf("Dependency %s OK\n", tmp.c_str());
  119. found = 1;
  120. includes.push_back(tmp);
  121. break;
  122. }
  123. printf("Comparing dependency \"%s\" and \"$(TOP)/arch/generic/%s\"\n", files[i].c_str(), tmp.c_str());
  124. if (files[i] == std::string("$(TOP)/arch/generic/").append(tmp)) {
  125. printf("Dependency %s OK\n", tmp.c_str());
  126. found = 1;
  127. includes.push_back(tmp);
  128. break;
  129. }
  130. printf("Comparing dependency \"%s\" and \"$(TOP)/arch/arm/%s\"\n", files[i].c_str(), tmp.c_str());
  131. if (files[i] == std::string("$(TOP)/arch/arm/").append(tmp)) {
  132. printf("Dependency %s OK\n", tmp.c_str());
  133. found = 1;
  134. includes.push_back(tmp);
  135. break;
  136. }
  137. printf("Comparing dependency \"%s\" and \"$(TOP)/arch/x86/%s\"\n", files[i].c_str(), tmp.c_str());
  138. if (files[i] == std::string("$(TOP)/arch/x86/").append(tmp)) {
  139. printf("Dependency %s OK\n", tmp.c_str());
  140. found = 1;
  141. includes.push_back(tmp);
  142. break;
  143. }
  144. printf("Comparing dependency \"%s\" and \"$(TOP)/test/%s\"\n", files[i].c_str(), tmp.c_str());
  145. if (files[i] == std::string("$(TOP)/test/").append(tmp)) {
  146. printf("Dependency %s OK\n", tmp.c_str());
  147. found = 1;
  148. includes.push_back(tmp);
  149. break;
  150. }
  151. }
  152. }
  153. // Skip irrelevant dependencies
  154. if (tmp.substr(0, 9) == "arch/s390") found = 1;
  155. if (tmp == "zlib-ng.h" && std::find(includes.begin(), includes.end(), "zlib.h") != includes.end()) found = 1;
  156. if (found == 0) {
  157. printf("%s: Dependency %s missing for %s!\n", makefile.c_str(), tmp.c_str(), objfile.c_str());
  158. return -1;
  159. }
  160. }
  161. }
  162. }
  163. for (size_t i = 1; i < files.size(); i++) {
  164. int found = 0;
  165. tmp = files[i];
  166. printf("Dependency: %s\n", tmp.c_str());
  167. pos2 = tmp.find("$(TOP)");
  168. if (pos2 != std::string::npos) {
  169. tmp = tmp.substr(7);
  170. }
  171. for (size_t j = 0; j < includes.size(); j++) {
  172. pos2 = tmp.find("$(SUFFIX)");
  173. if (pos2 != std::string::npos) {
  174. std::string tmp1 = tmp.substr(0, pos2).append(tmp.substr(pos2 + 9));
  175. printf("[%zd/%zd] Comparing dependency \"%s\" and \"%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  176. if (tmp1 == includes[j]) {
  177. printf("Dependency %s OK\n", files[i].c_str());
  178. found = 1;
  179. break;
  180. }
  181. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  182. if (tmp1 == std::string("arch/").append(includes[j])) {
  183. printf("Dependency %s OK\n", files[i].c_str());
  184. found = 1;
  185. break;
  186. }
  187. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/generic/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  188. if (tmp1 == std::string("arch/generic/").append(includes[j])) {
  189. printf("Dependency %s OK\n", files[i].c_str());
  190. found = 1;
  191. break;
  192. }
  193. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/arm/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  194. if (tmp1 == std::string("arch/arm/").append(includes[j])) {
  195. printf("Dependency %s OK\n", files[i].c_str());
  196. found = 1;
  197. break;
  198. }
  199. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/x86/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  200. if (tmp1 == std::string("arch/x86/").append(includes[j])) {
  201. printf("Dependency %s OK\n", files[i].c_str());
  202. found = 1;
  203. break;
  204. }
  205. printf("[%zd/%zd] Comparing dependency \"%s\" and \"test/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  206. if (tmp1 == std::string("test/").append(includes[j])) {
  207. printf("Dependency %s OK\n", files[i].c_str());
  208. found = 1;
  209. break;
  210. }
  211. tmp1 = tmp.substr(0, pos2).append("-ng").append(tmp.substr(pos2 + 9));
  212. printf("[%zd/%zd] Comparing dependency \"%s\" and \"%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  213. if (tmp1 == includes[j]) {
  214. printf("Dependency %s OK\n", files[i].c_str());
  215. found = 1;
  216. break;
  217. }
  218. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  219. if (tmp1 == std::string("arch/").append(includes[j])) {
  220. printf("Dependency %s OK\n", files[i].c_str());
  221. found = 1;
  222. break;
  223. }
  224. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/generic/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  225. if (tmp1 == std::string("arch/generic/").append(includes[j])) {
  226. printf("Dependency %s OK\n", files[i].c_str());
  227. found = 1;
  228. break;
  229. }
  230. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/arm/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  231. if (tmp1 == std::string("arch/arm/").append(includes[j])) {
  232. printf("Dependency %s OK\n", files[i].c_str());
  233. found = 1;
  234. break;
  235. }
  236. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/x86/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  237. if (tmp1 == std::string("arch/x86/").append(includes[j])) {
  238. printf("Dependency %s OK\n", files[i].c_str());
  239. found = 1;
  240. break;
  241. }
  242. printf("[%zd/%zd] Comparing dependency \"%s\" and \"test/%s\"\n", j, includes.size(), tmp1.c_str(), includes[j].c_str());
  243. if (tmp1 == std::string("test/").append(includes[j])) {
  244. printf("Dependency %s OK\n", files[i].c_str());
  245. found = 1;
  246. break;
  247. }
  248. } else {
  249. printf("[%zd/%zd] Comparing dependency \"%s\" and \"%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
  250. if (tmp == includes[j]) {
  251. printf("Dependency %s OK\n", files[i].c_str());
  252. found = 1;
  253. break;
  254. }
  255. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
  256. if (tmp == std::string("arch/").append(includes[j])) {
  257. printf("Dependency %s OK\n", files[i].c_str());
  258. found = 1;
  259. break;
  260. }
  261. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/generic/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
  262. if (tmp == std::string("arch/generic/").append(includes[j])) {
  263. printf("Dependency %s OK\n", files[i].c_str());
  264. found = 1;
  265. break;
  266. }
  267. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/arm/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
  268. if (tmp == std::string("arch/arm/").append(includes[j])) {
  269. printf("Dependency %s OK\n", files[i].c_str());
  270. found = 1;
  271. break;
  272. }
  273. printf("[%zd/%zd] Comparing dependency \"%s\" and \"arch/x86/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
  274. if (tmp == std::string("arch/x86/").append(includes[j])) {
  275. printf("Dependency %s OK\n", files[i].c_str());
  276. found = 1;
  277. break;
  278. }
  279. printf("[%zd/%zd] Comparing dependency \"%s\" and \"test/%s\"\n", j, includes.size(), tmp.c_str(), includes[j].c_str());
  280. if (tmp == std::string("test/").append(includes[j])) {
  281. printf("Dependency %s OK\n", files[i].c_str());
  282. found = 1;
  283. break;
  284. }
  285. }
  286. }
  287. // Skip indirect dependencies
  288. if (tmp.find("arm_features.h") != std::string::npos
  289. && std::find(includes.begin(), includes.end(), "cpu_features.h") != includes.end()
  290. && (makefile.find(".arm") != std::string::npos
  291. || makefile.find(".a64") != std::string::npos)) found = 1;
  292. if (tmp.find("x86_features.h") != std::string::npos
  293. && std::find(includes.begin(), includes.end(), "cpu_features.h") != includes.end()
  294. && makefile.find(".msc") != std::string::npos) found = 1;
  295. //
  296. if (tmp.find("generic_functions.h") != std::string::npos
  297. && std::find(includes.begin(), includes.end(), "arch_functions.h") != includes.end()) found = 1;
  298. if (tmp.find("arm_functions.h") != std::string::npos
  299. && std::find(includes.begin(), includes.end(), "arch_functions.h") != includes.end()
  300. && (makefile.find(".arm") != std::string::npos
  301. || makefile.find(".a64") != std::string::npos)) found = 1;
  302. if (tmp.find("x86_functions.h") != std::string::npos
  303. && std::find(includes.begin(), includes.end(), "arch_functions.h") != includes.end()
  304. && makefile.find(".msc") != std::string::npos) found = 1;
  305. if (found == 0) {
  306. printf("%s: Dependency %s not needed for %s\n", makefile.c_str(), files[i].c_str(), objfile.c_str());
  307. return -1;
  308. }
  309. }
  310. fb2.close();
  311. }
  312. }
  313. }
  314. }
  315. fb.close();
  316. }
  317. return 0;
  318. }