util.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. char *str;
  51. char dir[PATH_MAX+1], buf[PATH_MAX+1], buf2[PATH_MAX+1];
  52. struct menu *menu;
  53. struct symbol *sym;
  54. struct property *prop, *p;
  55. unsigned done;
  56. const char * const name_tmp = "..make.deps.tmp";
  57. FILE *out;
  58. if (!name)
  59. name = ".auto.deps";
  60. strcpy(dir, conf_get_configname());
  61. str = strrchr(dir, '/');
  62. if (str)
  63. str[1] = 0;
  64. else
  65. dir[0] = 0;
  66. sprintf(buf, "%s%s", dir, name_tmp);
  67. out = fopen(buf, "w");
  68. if (!out)
  69. return 1;
  70. fprintf(out, "# ATTENTION! This does not handle 'depends', just 'select'! \n"
  71. "# See package/config/util.c write_make_deps()\n#\n");
  72. menu = &rootmenu;//rootmenu.list;
  73. while (menu) {
  74. sym = menu->sym;
  75. if (!sym) {
  76. if (!menu_is_visible(menu))
  77. goto next;
  78. } else if (!(sym->flags & SYMBOL_CHOICE)) {
  79. sym_calc_value(sym);
  80. if (sym->type == S_BOOLEAN
  81. && sym_get_tristate_value(sym) != no) {
  82. done = 0;
  83. for_all_prompts(sym, prop) {
  84. struct expr *e;
  85. //printf("\nname=%s\n", sym->name);
  86. for_all_properties(sym, p, P_SELECT) {
  87. e = p->expr;
  88. if (e && e->left.sym->name) {
  89. if (!done) {
  90. fprintf(out, "%s: $(BASE_TARGETS)", br2_symbol_printer(sym->name));
  91. done = 1;
  92. }
  93. //printf("SELECTS %s\n",e->left.sym->name);
  94. fprintf(out, " %s",br2_symbol_printer(e->left.sym->name));
  95. }
  96. }
  97. if (done)
  98. fprintf(out, "\n");
  99. #if 0
  100. e = sym->rev_dep.expr;
  101. if (e && e->type == E_SYMBOL
  102. && e->left.sym->name) {
  103. fprintf(out, "%s: %s", br2_symbol_printer(e->left.sym->name),
  104. br2_symbol_printer(sym->name));
  105. printf("%s is Selected BY: %s", sym->name, e->left.sym->name);
  106. }
  107. #endif
  108. }
  109. }
  110. }
  111. next:
  112. if (menu->list) {
  113. menu = menu->list;
  114. continue;
  115. }
  116. if (menu->next)
  117. menu = menu->next;
  118. else while ((menu = menu->parent)) {
  119. if (menu->next) {
  120. menu = menu->next;
  121. break;
  122. }
  123. }
  124. }
  125. fclose(out);
  126. sprintf(buf2, "%s%s", dir, name);
  127. rename(buf, buf2);
  128. printf(_("#\n"
  129. "# make dependencies written to %s\n"
  130. "# ATTENTION buildroot devels!\n"
  131. "# See top of this file before playing with this auto-preprequisites!\n"
  132. "#\n"), name);
  133. return 0;
  134. }
  135. /* write a dependency file as used by kbuild to track dependencies */
  136. int file_write_dep(const char *name)
  137. {
  138. char *str;
  139. char buf[PATH_MAX+1], buf2[PATH_MAX+1], dir[PATH_MAX+1];
  140. struct symbol *sym, *env_sym;
  141. struct expr *e;
  142. struct file *file;
  143. FILE *out;
  144. if (!name)
  145. name = ".kconfig.d";
  146. strcpy(dir, conf_get_configname());
  147. str = strrchr(dir, '/');
  148. if (str)
  149. str[1] = 0;
  150. else
  151. dir[0] = 0;
  152. sprintf(buf, "%s..config.tmp", dir);
  153. out = fopen(buf, "w");
  154. if (!out)
  155. return 1;
  156. fprintf(out, "deps_config := \\\n");
  157. for (file = file_list; file; file = file->next) {
  158. if (file->next)
  159. fprintf(out, "\t%s \\\n", file->name);
  160. else
  161. fprintf(out, "\t%s\n", file->name);
  162. }
  163. fprintf(out, "\n%s: \\\n"
  164. "\t$(deps_config)\n\n", conf_get_autoconfig_name());
  165. expr_list_for_each_sym(sym_env_list, e, sym) {
  166. struct property *prop;
  167. const char *value;
  168. prop = sym_get_env_prop(sym);
  169. env_sym = prop_get_symbol(prop);
  170. if (!env_sym)
  171. continue;
  172. value = getenv(env_sym->name);
  173. if (!value)
  174. value = "";
  175. fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
  176. fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
  177. fprintf(out, "endif\n");
  178. }
  179. fprintf(out, "\n$(deps_config): ;\n");
  180. fclose(out);
  181. sprintf(buf2, "%s%s", dir, name);
  182. rename(buf, buf2);
  183. return write_make_deps(NULL);
  184. }
  185. /* Allocate initial growable string */
  186. struct gstr str_new(void)
  187. {
  188. struct gstr gs;
  189. gs.s = malloc(sizeof(char) * 64);
  190. gs.len = 64;
  191. gs.max_width = 0;
  192. strcpy(gs.s, "\0");
  193. return gs;
  194. }
  195. /* Allocate and assign growable string */
  196. struct gstr str_assign(const char *s)
  197. {
  198. struct gstr gs;
  199. gs.s = strdup(s);
  200. gs.len = strlen(s) + 1;
  201. gs.max_width = 0;
  202. return gs;
  203. }
  204. /* Free storage for growable string */
  205. void str_free(struct gstr *gs)
  206. {
  207. if (gs->s)
  208. free(gs->s);
  209. gs->s = NULL;
  210. gs->len = 0;
  211. }
  212. /* Append to growable string */
  213. void str_append(struct gstr *gs, const char *s)
  214. {
  215. size_t l;
  216. if (s) {
  217. l = strlen(gs->s) + strlen(s) + 1;
  218. if (l > gs->len) {
  219. gs->s = realloc(gs->s, l);
  220. gs->len = l;
  221. }
  222. strcat(gs->s, s);
  223. }
  224. }
  225. /* Append printf formatted string to growable string */
  226. void str_printf(struct gstr *gs, const char *fmt, ...)
  227. {
  228. va_list ap;
  229. char s[10000]; /* big enough... */
  230. va_start(ap, fmt);
  231. vsnprintf(s, sizeof(s), fmt, ap);
  232. str_append(gs, s);
  233. va_end(ap);
  234. }
  235. /* Retrieve value of growable string */
  236. const char *str_get(struct gstr *gs)
  237. {
  238. return gs->s;
  239. }