2
1

util.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 symbol *sym, *env_sym;
  129. struct expr *e;
  130. struct file *file;
  131. FILE *out;
  132. if (!name)
  133. name = ".kconfig.d";
  134. out = fopen("..config.tmp", "w");
  135. if (!out)
  136. return 1;
  137. fprintf(out, "deps_config := \\\n");
  138. for (file = file_list; file; file = file->next) {
  139. if (file->next)
  140. fprintf(out, "\t%s \\\n", file->name);
  141. else
  142. fprintf(out, "\t%s\n", file->name);
  143. }
  144. fprintf(out, "\n%s: \\\n"
  145. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  146. expr_list_for_each_sym(sym_env_list, e, sym) {
  147. struct property *prop;
  148. const char *value;
  149. prop = sym_get_env_prop(sym);
  150. env_sym = prop_get_symbol(prop);
  151. if (!env_sym)
  152. continue;
  153. value = getenv(env_sym->name);
  154. if (!value)
  155. value = "";
  156. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  157. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  158. fprintf(out, "endif\n");
  159. }
  160. fprintf(out, "\n$(deps_config): ;\n");
  161. fclose(out);
  162. rename("..config.tmp", name);
  163. return write_make_deps(NULL);
  164. }
  165. /* Allocate initial growable sting */
  166. struct gstr str_new(void)
  167. {
  168. struct gstr gs;
  169. gs.s = malloc(sizeof(char) * 64);
  170. gs.len = 64;
  171. strcpy(gs.s, "\0");
  172. return gs;
  173. }
  174. /* Allocate and assign growable string */
  175. struct gstr str_assign(const char *s)
  176. {
  177. struct gstr gs;
  178. gs.s = strdup(s);
  179. gs.len = strlen(s) + 1;
  180. return gs;
  181. }
  182. /* Free storage for growable string */
  183. void str_free(struct gstr *gs)
  184. {
  185. if (gs->s)
  186. free(gs->s);
  187. gs->s = NULL;
  188. gs->len = 0;
  189. }
  190. /* Append to growable string */
  191. void str_append(struct gstr *gs, const char *s)
  192. {
  193. size_t l;
  194. if (s) {
  195. l = strlen(gs->s) + strlen(s) + 1;
  196. if (l > gs->len) {
  197. gs->s = realloc(gs->s, l);
  198. gs->len = l;
  199. }
  200. strcat(gs->s, s);
  201. }
  202. }
  203. /* Append printf formatted string to growable string */
  204. void str_printf(struct gstr *gs, const char *fmt, ...)
  205. {
  206. va_list ap;
  207. char s[10000]; /* big enough... */
  208. va_start(ap, fmt);
  209. vsnprintf(s, sizeof(s), fmt, ap);
  210. str_append(gs, s);
  211. va_end(ap);
  212. }
  213. /* Retrieve value of growable string */
  214. const char *str_get(struct gstr *gs)
  215. {
  216. return gs->s;
  217. }