check_build.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 char *obj_list(const struct manifest *m)
  23. {
  24. char *list = talloc_strdup(m, "");
  25. struct ccan_file *i;
  26. /* Other CCAN deps. */
  27. list_for_each(&m->dep_objs, i, list)
  28. list = talloc_asprintf_append(list, "%s ", i->name);
  29. return list;
  30. }
  31. static char *lib_list(const struct manifest *m)
  32. {
  33. unsigned int i, num;
  34. char **libs = get_libs(m, ".", ".", &num, &m->info_file->compiled);
  35. char *ret = talloc_strdup(m, "");
  36. for (i = 0; i < num; i++)
  37. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  38. return ret;
  39. }
  40. static void *check_use_build(struct manifest *m)
  41. {
  42. char *contents;
  43. char *tmpfile, *err;
  44. int fd;
  45. tmpfile = temp_file(m, ".c");
  46. fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
  47. if (fd < 0)
  48. return talloc_asprintf(m, "Creating temporary file: %s",
  49. strerror(errno));
  50. contents = talloc_asprintf(tmpfile,
  51. "#include <ccan/%s/%s.h>\n"
  52. "int main(void)\n"
  53. "{\n"
  54. " return 0;\n"
  55. "}\n",
  56. m->basename, m->basename);
  57. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  58. close(fd);
  59. return "Failure writing to temporary file";
  60. }
  61. close(fd);
  62. if (!compile_and_link(m, tmpfile, obj_list(m), "", lib_list(m), &err))
  63. return err;
  64. return NULL;
  65. }
  66. static const char *describe_use_build(struct manifest *m, void *check_result)
  67. {
  68. return talloc_asprintf(check_result,
  69. "Linking against module:\n"
  70. "%s", (char *)check_result);
  71. }
  72. struct ccanlint check_build = {
  73. .name = "Module can be used",
  74. .total_score = 1,
  75. .check = check_use_build,
  76. .describe = describe_use_build,
  77. .can_run = can_build,
  78. };
  79. REGISTER_TEST(check_build, &build, NULL);