check_build.c 2.3 KB

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