build_objs.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. static bool compile_obj(struct ccan_file *c_file, char *objfile, char **report)
  23. {
  24. char *contents;
  25. contents = run_command(objfile, "cc " CFLAGS " -o %s -c %s",
  26. objfile, c_file->name);
  27. if (contents) {
  28. if (*report)
  29. *report = talloc_append_string(*report, contents);
  30. else
  31. *report = contents;
  32. return false;
  33. }
  34. return true;
  35. }
  36. static int cleanup_obj(char *objfile)
  37. {
  38. unlink(objfile);
  39. return 0;
  40. }
  41. static void *check_objs_build(struct manifest *m)
  42. {
  43. char *report = NULL;
  44. struct ccan_file *i;
  45. /* One point for each obj file. */
  46. list_for_each(&m->c_files, i, list)
  47. build_objs.total_score++;
  48. list_for_each(&m->c_files, i, list) {
  49. char *objfile = talloc_strdup(m, i->name);
  50. objfile[strlen(objfile)-1] = 'o';
  51. if (compile_obj(i, objfile, &report))
  52. talloc_set_destructor(objfile, cleanup_obj);
  53. }
  54. return report;
  55. }
  56. static const char *describe_objs_build(struct manifest *m, void *check_result)
  57. {
  58. return talloc_asprintf(check_result,
  59. "%s", (char *)check_result);
  60. }
  61. struct ccanlint build_objs = {
  62. .name = "Module object files can be built",
  63. .check = check_objs_build,
  64. .describe = describe_objs_build,
  65. .can_run = can_build,
  66. };
  67. REGISTER_TEST(build_objs, &depends_exist, NULL);