kconfig/util.c
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Aug 23 21:11:26 2011 +0200 (2011-08-23)
changeset 2891 f176fee535a0
parent 943 1cca90ce0481
permissions -rw-r--r--
cc/gcc: add 'cflags' paramater to the core backend

As the core backend is used to generate the bare-metal compiler,
we need to pass it the host CFLAGS.

Signed-off-by: "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
yann@1
     1
/*
yann@1
     2
 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
yann@1
     3
 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
yann@1
     4
 *
yann@1
     5
 * Released under the terms of the GNU GPL v2.0.
yann@1
     6
 */
yann@1
     7
yann@1
     8
#include <string.h>
yann@1
     9
#include "lkc.h"
yann@1
    10
yann@1
    11
/* file already present in list? If not add it */
yann@1
    12
struct file *file_lookup(const char *name)
yann@1
    13
{
yann@1
    14
	struct file *file;
yann@2448
    15
	const char *file_name = sym_expand_string_value(name);
yann@1
    16
yann@1
    17
	for (file = file_list; file; file = file->next) {
yann@2448
    18
		if (!strcmp(name, file->name)) {
yann@2448
    19
			free((void *)file_name);
yann@1
    20
			return file;
yann@2448
    21
		}
yann@1
    22
	}
yann@1
    23
yann@1
    24
	file = malloc(sizeof(*file));
yann@1
    25
	memset(file, 0, sizeof(*file));
yann@2448
    26
	file->name = file_name;
yann@1
    27
	file->next = file_list;
yann@1
    28
	file_list = file;
yann@1
    29
	return file;
yann@1
    30
}
yann@1
    31
yann@1
    32
/* write a dependency file as used by kbuild to track dependencies */
yann@1
    33
int file_write_dep(const char *name)
yann@1
    34
{
yann@943
    35
	struct symbol *sym, *env_sym;
yann@943
    36
	struct expr *e;
yann@1
    37
	struct file *file;
yann@1
    38
	FILE *out;
yann@1
    39
yann@1
    40
	if (!name)
yann@1
    41
		name = ".kconfig.d";
yann@1
    42
	out = fopen("..config.tmp", "w");
yann@1
    43
	if (!out)
yann@1
    44
		return 1;
yann@1
    45
	fprintf(out, "deps_config := \\\n");
yann@1
    46
	for (file = file_list; file; file = file->next) {
yann@1
    47
		if (file->next)
yann@1
    48
			fprintf(out, "\t%s \\\n", file->name);
yann@1
    49
		else
yann@1
    50
			fprintf(out, "\t%s\n", file->name);
yann@1
    51
	}
yann@2448
    52
	fprintf(out, "\n%s: \\\n"
yann@2448
    53
		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
yann@943
    54
yann@943
    55
	expr_list_for_each_sym(sym_env_list, e, sym) {
yann@943
    56
		struct property *prop;
yann@943
    57
		const char *value;
yann@943
    58
yann@943
    59
		prop = sym_get_env_prop(sym);
yann@943
    60
		env_sym = prop_get_symbol(prop);
yann@943
    61
		if (!env_sym)
yann@943
    62
			continue;
yann@943
    63
		value = getenv(env_sym->name);
yann@943
    64
		if (!value)
yann@943
    65
			value = "";
yann@943
    66
		fprintf(out, "ifneq \"$(%s)\" \"%s\"\n", env_sym->name, value);
yann@2448
    67
		fprintf(out, "%s: FORCE\n", conf_get_autoconfig_name());
yann@943
    68
		fprintf(out, "endif\n");
yann@943
    69
	}
yann@943
    70
yann@943
    71
	fprintf(out, "\n$(deps_config): ;\n");
yann@1
    72
	fclose(out);
yann@1
    73
	rename("..config.tmp", name);
yann@1
    74
	return 0;
yann@1
    75
}
yann@1
    76
yann@1
    77
yann@2448
    78
/* Allocate initial growable string */
yann@1
    79
struct gstr str_new(void)
yann@1
    80
{
yann@1
    81
	struct gstr gs;
yann@1
    82
	gs.s = malloc(sizeof(char) * 64);
yann@943
    83
	gs.len = 64;
yann@2448
    84
	gs.max_width = 0;
yann@1
    85
	strcpy(gs.s, "\0");
yann@1
    86
	return gs;
yann@1
    87
}
yann@1
    88
yann@1
    89
/* Allocate and assign growable string */
yann@1
    90
struct gstr str_assign(const char *s)
yann@1
    91
{
yann@1
    92
	struct gstr gs;
yann@1
    93
	gs.s = strdup(s);
yann@1
    94
	gs.len = strlen(s) + 1;
yann@2448
    95
	gs.max_width = 0;
yann@1
    96
	return gs;
yann@1
    97
}
yann@1
    98
yann@1
    99
/* Free storage for growable string */
yann@1
   100
void str_free(struct gstr *gs)
yann@1
   101
{
yann@1
   102
	if (gs->s)
yann@1
   103
		free(gs->s);
yann@1
   104
	gs->s = NULL;
yann@1
   105
	gs->len = 0;
yann@1
   106
}
yann@1
   107
yann@1
   108
/* Append to growable string */
yann@1
   109
void str_append(struct gstr *gs, const char *s)
yann@1
   110
{
yann@943
   111
	size_t l;
yann@943
   112
	if (s) {
yann@943
   113
		l = strlen(gs->s) + strlen(s) + 1;
yann@943
   114
		if (l > gs->len) {
yann@943
   115
			gs->s   = realloc(gs->s, l);
yann@943
   116
			gs->len = l;
yann@943
   117
		}
yann@943
   118
		strcat(gs->s, s);
yann@1
   119
	}
yann@1
   120
}
yann@1
   121
yann@1
   122
/* Append printf formatted string to growable string */
yann@1
   123
void str_printf(struct gstr *gs, const char *fmt, ...)
yann@1
   124
{
yann@1
   125
	va_list ap;
yann@1
   126
	char s[10000]; /* big enough... */
yann@1
   127
	va_start(ap, fmt);
yann@1
   128
	vsnprintf(s, sizeof(s), fmt, ap);
yann@1
   129
	str_append(gs, s);
yann@1
   130
	va_end(ap);
yann@1
   131
}
yann@1
   132
yann@1
   133
/* Retrieve value of growable string */
yann@1
   134
const char *str_get(struct gstr *gs)
yann@1
   135
{
yann@1
   136
	return gs->s;
yann@1
   137
}
yann@1
   138