module_builds.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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)
  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 ", i->compiled);
  30. return list;
  31. }
  32. char *build_module(struct manifest *m, bool keep, char **errstr)
  33. {
  34. char *name = link_objects(m, m->basename, false, obj_list(m), errstr);
  35. if (name) {
  36. if (keep) {
  37. char *realname = talloc_asprintf(m, "%s.o", m->dir);
  38. /* We leave this object file around, all built. */
  39. if (!move_file(name, realname))
  40. err(1, "Renaming %s to %s", name, realname);
  41. name = realname;
  42. }
  43. }
  44. return name;
  45. }
  46. static void do_build(struct manifest *m,
  47. bool keep,
  48. unsigned int *timeleft,
  49. struct score *score)
  50. {
  51. char *errstr;
  52. if (list_empty(&m->c_files)) {
  53. /* No files? No score, but we "pass". */
  54. score->total = 0;
  55. score->pass = true;
  56. return;
  57. }
  58. m->compiled = build_module(m, keep, &errstr);
  59. if (!m->compiled) {
  60. score_file_error(score, NULL, 0, errstr);
  61. return;
  62. }
  63. score->pass = true;
  64. score->score = score->total;
  65. }
  66. struct ccanlint module_builds = {
  67. .key = "module_builds",
  68. .name = "Module can be built from object files",
  69. .check = do_build,
  70. .can_run = can_build,
  71. .needs = "objects_build"
  72. };
  73. REGISTER_TEST(module_builds);