module_links.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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;
  25. struct manifest *i;
  26. if (m->compiled[COMPILE_NORMAL])
  27. list = talloc_strdup(m, m->compiled[COMPILE_NORMAL]);
  28. else
  29. list = talloc_strdup(m, "");
  30. /* Other CCAN deps. */
  31. list_for_each(&m->deps, i, list) {
  32. if (i->compiled[COMPILE_NORMAL])
  33. list = talloc_asprintf_append(list, " %s",
  34. i->compiled
  35. [COMPILE_NORMAL]);
  36. }
  37. return list;
  38. }
  39. static char *lib_list(const struct manifest *m)
  40. {
  41. unsigned int i;
  42. char **libs;
  43. char *ret = talloc_strdup(m, "");
  44. libs = get_libs(m, m->dir, "depends", get_or_compile_info);
  45. for (i = 0; libs[i]; i++)
  46. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  47. return ret;
  48. }
  49. static void check_use_build(struct manifest *m,
  50. unsigned int *timeleft, struct score *score)
  51. {
  52. char *contents;
  53. char *tmpfile, *cmdout;
  54. int fd;
  55. tmpfile = temp_file(m, ".c", "example.c");
  56. fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0600);
  57. if (fd < 0)
  58. err(1, "Creating temporary file %s", tmpfile);
  59. contents = talloc_asprintf(tmpfile,
  60. "#include <ccan/%s/%s.h>\n"
  61. "int main(void)\n"
  62. "{\n"
  63. " return 0;\n"
  64. "}\n",
  65. m->modname, m->basename);
  66. if (write(fd, contents, strlen(contents)) != strlen(contents))
  67. err(1, "Failure writing to temporary file %s", tmpfile);
  68. close(fd);
  69. if (compile_and_link(score, tmpfile, ccan_dir, obj_list(m),
  70. compiler, cflags, lib_list(m),
  71. temp_file(m, "", tmpfile),
  72. &cmdout)) {
  73. score->pass = true;
  74. score->score = score->total;
  75. } else {
  76. score->error = cmdout;
  77. }
  78. }
  79. struct ccanlint module_links = {
  80. .key = "module_links",
  81. .name = "Module can be linked against trivial program",
  82. .check = check_use_build,
  83. .can_run = can_build,
  84. .needs = "module_builds depends_build"
  85. };
  86. REGISTER_TEST(module_links);