check_build.c 2.3 KB

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