module_builds.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "build.h"
  17. static const char *can_build(struct manifest *m)
  18. {
  19. if (safe_mode)
  20. return "Safe mode enabled";
  21. return NULL;
  22. }
  23. static char *obj_list(const struct manifest *m, enum compile_type ctype)
  24. {
  25. char *list = talloc_strdup(m, "");
  26. struct ccan_file *i;
  27. /* Objects from all the C files. */
  28. list_for_each(&m->c_files, i, list)
  29. list = talloc_asprintf_append(list, "%s ",
  30. i->compiled[ctype]);
  31. return list;
  32. }
  33. char *build_module(struct manifest *m, bool keep,
  34. enum compile_type ctype, char **errstr)
  35. {
  36. char *name = link_objects(m, m->basename, false, obj_list(m, ctype),
  37. errstr);
  38. if (name) {
  39. if (keep) {
  40. char *realname = talloc_asprintf(m, "%s.o", m->dir);
  41. assert(ctype == COMPILE_NORMAL);
  42. /* We leave this object file around, all built. */
  43. if (!move_file(name, realname))
  44. err(1, "Renaming %s to %s", name, realname);
  45. name = realname;
  46. }
  47. }
  48. return name;
  49. }
  50. static void do_build(struct manifest *m,
  51. bool keep,
  52. unsigned int *timeleft,
  53. struct score *score)
  54. {
  55. char *errstr;
  56. if (list_empty(&m->c_files)) {
  57. /* No files? No score, but we "pass". */
  58. score->total = 0;
  59. score->pass = true;
  60. return;
  61. }
  62. m->compiled[COMPILE_NORMAL]
  63. = build_module(m, keep, COMPILE_NORMAL, &errstr);
  64. if (!m->compiled[COMPILE_NORMAL]) {
  65. score_file_error(score, NULL, 0, "%s", errstr);
  66. return;
  67. }
  68. score->pass = true;
  69. score->score = score->total;
  70. }
  71. struct ccanlint module_builds = {
  72. .key = "module_builds",
  73. .name = "Module can be built from object files",
  74. .check = do_build,
  75. .can_run = can_build,
  76. .needs = "objects_build"
  77. };
  78. REGISTER_TEST(module_builds);