module_links.c 2.2 KB

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