module_links.c 2.5 KB

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