module_links.c 2.2 KB

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