examples_compile.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/talloc/talloc.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <ctype.h>
  11. static const char *can_run(struct manifest *m)
  12. {
  13. if (safe_mode)
  14. return "Safe mode enabled";
  15. return NULL;
  16. }
  17. static char *obj_list(const struct manifest *m)
  18. {
  19. char *list;
  20. struct ccan_file *i;
  21. /* This CCAN module. */
  22. list = talloc_asprintf(m, " %s.o", m->dir);
  23. /* Other ccan modules we depend on. */
  24. list_for_each(&m->dep_dirs, i, list) {
  25. if (i->compiled)
  26. list = talloc_asprintf_append(list, " %s", i->compiled);
  27. }
  28. return list;
  29. }
  30. static char *lib_list(const struct manifest *m)
  31. {
  32. unsigned int i, num;
  33. char **libs = get_libs(m, ".", &num, &m->info_file->compiled);
  34. char *ret = talloc_strdup(m, "");
  35. for (i = 0; i < num; i++)
  36. ret = talloc_asprintf_append(ret, "-l%s ", libs[i]);
  37. return ret;
  38. }
  39. static char *compile(const void *ctx,
  40. struct manifest *m,
  41. struct ccan_file *file,
  42. bool keep)
  43. {
  44. char *errmsg;
  45. file->compiled = maybe_temp_file(ctx, "", keep, file->fullname);
  46. errmsg = compile_and_link(ctx, file->fullname, ccan_dir,
  47. obj_list(m), "", lib_list(m), file->compiled);
  48. if (errmsg) {
  49. talloc_free(file->compiled);
  50. return errmsg;
  51. }
  52. return NULL;
  53. }
  54. struct score {
  55. unsigned int score;
  56. char *errors;
  57. };
  58. static char *start_main(char *ret)
  59. {
  60. return talloc_asprintf_append(ret,
  61. "/* Fake function wrapper inserted */\n"
  62. "int main(int argc, char *argv[])\n"
  63. "{\n");
  64. }
  65. static char *mangle(struct manifest *m, struct ccan_file *example)
  66. {
  67. char **lines = get_ccan_file_lines(example);
  68. char *ret;
  69. bool in_function = false, fake_function = false;
  70. unsigned int i;
  71. ret = talloc_strdup(m, "/* Prepend a heap of headers. */\n"
  72. "#include <assert.h>\n"
  73. "#include <err.h>\n"
  74. "#include <fcntl.h>\n"
  75. "#include <stdbool.h>\n"
  76. "#include <stdint.h>\n"
  77. "#include <stdio.h>\n"
  78. "#include <stdlib.h>\n"
  79. "#include <string.h>\n"
  80. "#include <sys/stat.h>\n"
  81. "#include <sys/types.h>\n"
  82. "#include <unistd.h>\n");
  83. ret = talloc_asprintf_append(ret, "/* Include header from module. */\n"
  84. "#include <ccan/%s/%s.h>\n",
  85. m->basename, m->basename);
  86. /* Starts indented? Wrap it in a main() function. */
  87. if (lines[0] && isblank(lines[0][0])) {
  88. ret = start_main(ret);
  89. fake_function = true;
  90. in_function = true;
  91. }
  92. /* Primitive, very primitive. */
  93. for (i = 0; lines[i]; i++) {
  94. /* } at start of line ends a function. */
  95. if (in_function) {
  96. if (lines[i][0] == '}')
  97. in_function = false;
  98. } else {
  99. /* Character at start of line, with ( and no ;
  100. * == function start. */
  101. if (!isblank(lines[i][0])
  102. && strchr(lines[i], '(')
  103. && !strchr(lines[i], ';'))
  104. in_function = true;
  105. }
  106. /* ... means elided code. If followed by spaced line, means
  107. * next part is supposed to be inside a function. */
  108. if (strcmp(lines[i], "...") == 0) {
  109. if (!in_function
  110. && lines[i+1]
  111. && isblank(lines[i+1][0])) {
  112. /* This implies we start a function here. */
  113. ret = start_main(ret);
  114. fake_function = true;
  115. in_function = true;
  116. }
  117. ret = talloc_asprintf_append(ret,
  118. "/* ... removed */\n");
  119. continue;
  120. }
  121. ret = talloc_asprintf_append(ret, "%s\n", lines[i]);
  122. }
  123. if (fake_function)
  124. ret = talloc_asprintf_append(ret, "return 0;\n"
  125. "}\n");
  126. return ret;
  127. }
  128. static struct ccan_file *mangle_example(struct manifest *m,
  129. struct ccan_file *example, bool keep)
  130. {
  131. char *name, *contents;
  132. int fd;
  133. struct ccan_file *f;
  134. name = maybe_temp_file(example, ".c", keep,
  135. talloc_asprintf(m, "%s/mangled-%s",
  136. m->dir, example->name));
  137. f = new_ccan_file(example,
  138. talloc_dirname(example, name),
  139. talloc_basename(example, name));
  140. talloc_steal(f, name);
  141. fd = open(f->fullname, O_WRONLY | O_CREAT | O_EXCL, 0600);
  142. if (fd < 0)
  143. return NULL;
  144. contents = mangle(m, example);
  145. if (write(fd, contents, strlen(contents)) != strlen(contents)) {
  146. close(fd);
  147. return NULL;
  148. }
  149. close(fd);
  150. return f;
  151. }
  152. static void *build_examples(struct manifest *m, bool keep,
  153. unsigned int *timeleft)
  154. {
  155. struct ccan_file *i;
  156. struct score *score = talloc(m, struct score);
  157. score->score = 0;
  158. score->errors = NULL;
  159. list_for_each(&m->examples, i, list) {
  160. char *ret;
  161. examples_compile.total_score++;
  162. ret = compile(score, m, i, keep);
  163. if (!ret)
  164. score->score++;
  165. else {
  166. struct ccan_file *mangle = mangle_example(m, i, keep);
  167. talloc_free(ret);
  168. ret = compile(score, m, mangle, keep);
  169. if (!ret)
  170. score->score++;
  171. else {
  172. if (!score->errors)
  173. score->errors = ret;
  174. else {
  175. score->errors
  176. = talloc_append_string(score->errors,
  177. ret);
  178. talloc_free(ret);
  179. }
  180. }
  181. }
  182. }
  183. return score;
  184. }
  185. static unsigned int score_examples(struct manifest *m, void *check_result)
  186. {
  187. struct score *score = check_result;
  188. return score->score;
  189. }
  190. static const char *describe(struct manifest *m, void *check_result)
  191. {
  192. struct score *score = check_result;
  193. if (verbose >= 2 && score->errors)
  194. return talloc_asprintf(m, "Compile errors building examples:\n"
  195. "%s", score->errors);
  196. return NULL;
  197. }
  198. struct ccanlint examples_compile = {
  199. .key = "examples-compile",
  200. .name = "Module examples compile",
  201. .score = score_examples,
  202. .check = build_examples,
  203. .describe = describe,
  204. .can_run = can_run,
  205. };
  206. REGISTER_TEST(examples_compile, &has_examples, NULL);