check_depends_built.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <ccan/str/str.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <err.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. static const char *can_build(struct manifest *m)
  17. {
  18. if (safe_mode)
  19. return "Safe mode enabled";
  20. return NULL;
  21. }
  22. /* FIXME: recursive ccanlint if they ask for it. */
  23. static bool expect_obj_file(const char *dir)
  24. {
  25. char *olddir;
  26. struct manifest *dep_man;
  27. bool has_c_files;
  28. olddir = talloc_getcwd(dir);
  29. if (!olddir)
  30. err(1, "Getting current directory");
  31. /* We will fail below if this doesn't exist. */
  32. if (chdir(dir) != 0)
  33. return false;
  34. dep_man = get_manifest(dir);
  35. if (chdir(olddir) != 0)
  36. err(1, "Returning to original directory '%s'", olddir);
  37. talloc_free(olddir);
  38. /* If it has C files, we expect an object file built from them. */
  39. has_c_files = !list_empty(&dep_man->c_files);
  40. talloc_free(dep_man);
  41. return has_c_files;
  42. }
  43. static void *check_depends_built(struct manifest *m)
  44. {
  45. struct ccan_file *i;
  46. char *report = NULL;
  47. list_for_each(&m->dep_dirs, i, list) {
  48. char *objfile;
  49. struct stat st;
  50. if (!expect_obj_file(i->name))
  51. continue;
  52. objfile = talloc_asprintf(m, "%s.o", i->name);
  53. if (stat(objfile, &st) != 0) {
  54. report = talloc_asprintf_append(report,
  55. "object file %s\n",
  56. objfile);
  57. } else {
  58. struct ccan_file *f = new_ccan_file(m, objfile);
  59. list_add_tail(&m->dep_objs, &f->list);
  60. }
  61. }
  62. return talloc_steal(m, report);
  63. }
  64. static const char *describe_depends_built(struct manifest *m,
  65. void *check_result)
  66. {
  67. return talloc_asprintf(check_result,
  68. "The following dependencies are not built:\n"
  69. "%s", (char *)check_result);
  70. }
  71. struct ccanlint depends_built = {
  72. .name = "CCAN dependencies are built",
  73. .total_score = 1,
  74. .check = check_depends_built,
  75. .describe = describe_depends_built,
  76. .can_run = can_build,
  77. };
  78. REGISTER_TEST(depends_built, &depends_exist, NULL);