util.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
  3. * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
  4. *
  5. * Released under the terms of the GNU GPL v2.0.
  6. */
  7. #include <string.h>
  8. #include "lkc.h"
  9. /* file already present in list? If not add it */
  10. struct file *file_lookup(const char *name)
  11. {
  12. struct file *file;
  13. for (file = file_list; file; file = file->next) {
  14. if (!strcmp(name, file->name))
  15. return file;
  16. }
  17. file = malloc(sizeof(*file));
  18. memset(file, 0, sizeof(*file));
  19. file->name = strdup(name);
  20. file->next = file_list;
  21. file_list = file;
  22. return file;
  23. }
  24. static char* br2_symbol_printer(const char * const in)
  25. {
  26. ssize_t i, j, len = strlen(in);
  27. char *ret;
  28. if (len < 1)
  29. return NULL;
  30. ret = malloc(len);
  31. if (!ret) {
  32. printf("Out of memory!");
  33. exit(1);
  34. }
  35. memset(ret, 0, len);
  36. i = j = 0;
  37. if (strncmp("BR2_", in, 4) == 0)
  38. i += 4;
  39. if (strncmp("PACKAGE_", in + i, 8) == 0)
  40. i += 8;
  41. else if (strncmp("TARGET_", in + i, 7) == 0)
  42. i += 7;
  43. while (i <= len)
  44. ret[j++] = tolower(in[i++]);
  45. return ret;
  46. }
  47. /* write dependencies of the infividual config-symbols */
  48. static int write_make_deps(const char *name)
  49. {
  50. struct menu *menu;
  51. struct symbol *sym;
  52. struct property *prop, *p;
  53. unsigned done;
  54. const char * const name_tmp = "..make.deps.tmp";
  55. FILE *out;
  56. if (!name)
  57. name = ".auto.deps";
  58. out = fopen(name_tmp, "w");
  59. if (!out)
  60. return 1;
  61. fprintf(out, "# ATTENTION! This does not handle 'depends', just 'select'! \n"
  62. "# See package/config/util.c write_make_deps()\n#\n");
  63. menu = &rootmenu;//rootmenu.list;
  64. while (menu) {
  65. sym = menu->sym;
  66. if (!sym) {
  67. if (!menu_is_visible(menu))
  68. goto next;
  69. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  70. sym_calc_value(sym);
  71. if (sym->type == S_BOOLEAN
  72. && sym_get_tristate_value(sym) != no) {
  73. done = 0;
  74. for_all_prompts(sym, prop) {
  75. struct expr *e;
  76. //printf("\nname=%s\n", sym->name);
  77. for_all_properties(sym, p, P_SELECT) {
  78. e = p->expr;
  79. if (e && e->left.sym->name) {
  80. if (!done) {
  81. fprintf(out, "%s: $(BASE_TARGETS)", br2_symbol_printer(sym->name));
  82. done = 1;
  83. }
  84. //printf("SELECTS %s\n",e->left.sym->name);
  85. fprintf(out, " %s",br2_symbol_printer(e->left.sym->name));
  86. }
  87. }
  88. if (done)
  89. fprintf(out, "\n");
  90. #if 0
  91. e = sym->rev_dep.expr;
  92. if (e && e->type == E_SYMBOL
  93. && e->left.sym->name) {
  94. fprintf(out, "%s: %s", br2_symbol_printer(e->left.sym->name),
  95. br2_symbol_printer(sym->name));
  96. printf("%s is Selected BY: %s", sym->name, e->left.sym->name);
  97. }
  98. #endif
  99. }
  100. }
  101. }
  102. next:
  103. if (menu->list) {
  104. menu = menu->list;
  105. continue;
  106. }
  107. if (menu->next)
  108. menu = menu->next;
  109. else while ((menu = menu->parent)) {
  110. if (menu->next) {
  111. menu = menu->next;
  112. break;
  113. }
  114. }
  115. }
  116. fclose(out);
  117. rename(name_tmp, name);
  118. printf(_("#\n"
  119. "# make dependencies written to %s\n"
  120. "# ATTENTION buildroot devels!\n"
  121. "# See top of this file before playing with this auto-preprequisites!\n"
  122. "#\n"), name);
  123. return 0;
  124. }
  125. /* write a dependency file as used by kbuild to track dependencies */
  126. int file_write_dep(const char *name)
  127. {
  128. struct file *file;
  129. FILE *out;
  130. if (!name)
  131. name = ".kconfig.d";
  132. out = fopen("..config.tmp", "w");
  133. if (!out)
  134. return 1;
  135. fprintf(out, "deps_config := \\\n");
  136. for (file = file_list; file; file = file->next) {
  137. if (file->next)
  138. fprintf(out, "\t%s \\\n", file->name);
  139. else
  140. fprintf(out, "\t%s\n", file->name);
  141. }
  142. fprintf(out, "\n$(BR2_DEPENDS_DIR)/config/auto.conf: \\\n"
  143. "\t$(deps_config)\n\n"
  144. "$(deps_config): ;\n");
  145. fclose(out);
  146. rename("..config.tmp", name);
  147. return write_make_deps(NULL);
  148. }
  149. /* Allocate initial growable sting */
  150. struct gstr str_new(void)
  151. {
  152. struct gstr gs;
  153. gs.s = malloc(sizeof(char) * 64);
  154. gs.len = 16;
  155. strcpy(gs.s, "\0");
  156. return gs;
  157. }
  158. /* Allocate and assign growable string */
  159. struct gstr str_assign(const char *s)
  160. {
  161. struct gstr gs;
  162. gs.s = strdup(s);
  163. gs.len = strlen(s) + 1;
  164. return gs;
  165. }
  166. /* Free storage for growable string */
  167. void str_free(struct gstr *gs)
  168. {
  169. if (gs->s)
  170. free(gs->s);
  171. gs->s = NULL;
  172. gs->len = 0;
  173. }
  174. /* Append to growable string */
  175. void str_append(struct gstr *gs, const char *s)
  176. {
  177. size_t l;
  178. if (s) {
  179. l = strlen(gs->s) + strlen(s) + 1;
  180. if (l > gs->len) {
  181. gs->s = realloc(gs->s, l);
  182. gs->len = l;
  183. }
  184. strcat(gs->s, s);
  185. }
  186. }
  187. /* Append printf formatted string to growable string */
  188. void str_printf(struct gstr *gs, const char *fmt, ...)
  189. {
  190. va_list ap;
  191. char s[10000]; /* big enough... */
  192. va_start(ap, fmt);
  193. vsnprintf(s, sizeof(s), fmt, ap);
  194. str_append(gs, s);
  195. va_end(ap);
  196. }
  197. /* Retrieve value of growable string */
  198. const char *str_get(struct gstr *gs)
  199. {
  200. return gs->s;
  201. }