build_objs.c 1.7 KB

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