build_objs.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. build_objs.total_score = 0;
  47. list_for_each(&m->c_files, i, list)
  48. build_objs.total_score++;
  49. list_for_each(&m->c_files, i, list) {
  50. char *objfile = talloc_strdup(m, i->name);
  51. objfile[strlen(objfile)-1] = 'o';
  52. if (compile_obj(i, objfile, &report))
  53. talloc_set_destructor(objfile, cleanup_obj);
  54. }
  55. return report;
  56. }
  57. static const char *describe_objs_build(struct manifest *m, void *check_result)
  58. {
  59. return talloc_asprintf(check_result,
  60. "%s", (char *)check_result);
  61. }
  62. struct ccanlint build_objs = {
  63. .name = "Module object files can be built",
  64. .total_score = 1,
  65. .check = check_objs_build,
  66. .describe = describe_objs_build,
  67. .can_run = can_build,
  68. };
  69. REGISTER_TEST(build_objs, &depends_exist, NULL);