public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] kconfig: introduce KCONFIG_* symbols for .c files
@ 2008-05-24 19:25 Sam Ravnborg
  2008-05-24 19:53 ` Andrew Morton
                   ` (4 more replies)
  0 siblings, 5 replies; 25+ messages in thread
From: Sam Ravnborg @ 2008-05-24 19:25 UTC (permalink / raw)
  To: linux-kbuild, LKML, Linus Torvalds, Andrew Morton, Roman Zippel
  Cc: Jeremy Fitzhardinge

We have many places in the kernel that looks like
the following:

#ifdef CONFIG_FOO
	...
#endif

Which has the disadvantage that the code denoted '...'
are not even built if CONFIG_FOO is not selected in
the current configuration.

We know that gcc do simple code-elimination for
conditionals which is always true/false and
thus the above code could be turned into:

	if (CONFIG_FOO)
		...

One line smaller and we follow the normal flow in the program.
The code is always build but we do not waste space as gcc will
do proper code-elimination for us.

Today this is not possible because kconfig will only
define CONFIG_FOO if selected and FOO is not a module.

The following patch implement a new set of defines in
the KCONFIG_* namespace.

For a tristate symbol the following are defined:

FOO not selected: 
#define KCONFIG_FOO        0
#define KCONFIG_FOO_MODULE 0

FOO is built-in ('y')
#define KCONFIG_FOO        1
#define KCONFIG_FOO_MODULE 0

FOO is a module ('m'):
#define KCONFIG_FOO        1
#define KCONFIG_FOO_MODULE 1

In other words KCONFIG_FOO will say if the
symbol is selected and KCONFIG_FOO_MODULE
will say if it is a module.

With the above included we can now do:

	if (KCONFIG_FOO)
		...

This is not a replacement for the CONFIG_*
defines but a pleasant supplement.
Using KCONFIG_FOO will also give us a nice
error message the day that FOO is no longer part
of the configuration.

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
---
diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
index ee5fe94..011a5ec 100644
--- a/scripts/kconfig/confdata.c
+++ b/scripts/kconfig/confdata.c
@@ -666,6 +666,31 @@ out:
 	return res;
 }
 
+static void write_tristate(FILE *m, FILE *h, struct symbol *sym)
+{
+	switch (sym_get_tristate_value(sym)) {
+	case mod:
+		fprintf(m, "CONFIG_%s=m\n", sym->name);
+		fprintf(h, "#define CONFIG_%s_MODULE 1\n", sym->name);
+		fprintf(h, "#define KCONFIG_%s 1\n", sym->name);
+		if (sym->type == S_TRISTATE)
+			fprintf(h, "#define KCONFIG_%s_MODULE 1\n", sym->name);
+		break;
+	case yes:
+		fprintf(m, "CONFIG_%s=y\n", sym->name);
+		fprintf(h, "#define CONFIG_%s 1\n", sym->name);
+		fprintf(h, "#define KCONFIG_%s 1\n", sym->name);
+		if (sym->type == S_TRISTATE)
+			fprintf(h, "#define KCONFIG_%s_MODULE 0\n", sym->name);
+		break;
+	case no:
+		fprintf(h, "#define KCONFIG_%s 0\n", sym->name);
+		if (sym->type == S_TRISTATE)
+			fprintf(h, "#define KCONFIG_%s_MODULE 0\n", sym->name);
+		break;
+	}
+}
+
 int conf_write_autoconf(void)
 {
 	struct symbol *sym;
@@ -716,18 +741,7 @@ int conf_write_autoconf(void)
 		switch (sym->type) {
 		case S_BOOLEAN:
 		case S_TRISTATE:
-			switch (sym_get_tristate_value(sym)) {
-			case no:
-				break;
-			case mod:
-				fprintf(out, "CONFIG_%s=m\n", sym->name);
-				fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
-				break;
-			case yes:
-				fprintf(out, "CONFIG_%s=y\n", sym->name);
-				fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
-				break;
-			}
+			write_tristate(out, out_h, sym);
 			break;
 		case S_STRING:
 			str = sym_get_string_value(sym);

^ permalink raw reply related	[flat|nested] 25+ messages in thread
* Re: [RFC PATCH] kconfig: introduce KCONFIG_* symbols for .c files
@ 2008-05-24 23:36 Steven Fuerst
  2008-05-25  0:00 ` Tom Spink
  0 siblings, 1 reply; 25+ messages in thread
From: Steven Fuerst @ 2008-05-24 23:36 UTC (permalink / raw)
  To: linux-kernel


> On Sat, May 24, 2008 at 09:58:47PM +0100, Jeremy Fitzhardinge wrote:
> >
> > Would
> >
> > #define KCONFIG(x) (CONFIG_##x - 0)
> >
> > if (KCONFIG(PREEMPT)) {
> > ...
> > }
> >
> > work?
> 
> $ cat test.c
> #define KCONFIG(x) (CONFIG_##x - 0)
> 
> int main()
> {
> if (KCONFIG(PREEMPT))
> ;
> 
> return 0;
> }
> $ gcc -O2 -Wall test.c
> test.c: In function âmainâ:
> test.c:5: error: âCONFIG_PREEMPTâ undeclared (first use in this function)
> test.c:5: error: (Each undeclared identifier is reported only once
> test.c:5: error: for each function it appears in.)
> $ gcc --version
> gcc (Debian 4.3.0-5) 4.3.1 20080523 (prerelease)
>
> > J
>
> cu
> Adrian

How about this?

#include <stdio.h>

#define CONFIG_FOO	1
/* #define CONFIG_BAR	1 */

#define STR2(x) #x
#define STR(x) STR2(x)

#define PASTE2(x, y) x##y
#define PASTE(x, y) PASTE2(x, y)

#define KCONFIG2(x, y) STR(PASTE(CONFIG_, x))[y]
#define KCONFIG(x) (!((KCONFIG2(x, 0) == 'C') && \
					(KCONFIG2(x, 1) == 'O') && \
					(KCONFIG2(x, 2) == 'N') && \
					(KCONFIG2(x, 3) == 'F') && \
					(KCONFIG2(x, 4) == 'I') && \
					(KCONFIG2(x, 5) == 'G') && \
					(KCONFIG2(x, 6) == '_')))

int main()
{
	if (KCONFIG(FOO)) printf("FOO\n");
	if (KCONFIG(BAR)) printf("BAR\n");
	return 0;
}


It of course assumes that the result of all defined macros you want to test 
for do not start with "CONFIG_", so this doesn't work generally.  However, 
nearly all of the time the arguement to KCONFIG() will be defined to be "1" 
an integer, or some other known string that doesn't begin with "CONFIG_", so 
this trick is fairly robust.

Steven


^ permalink raw reply	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2008-05-25  6:22 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-24 19:25 [RFC PATCH] kconfig: introduce KCONFIG_* symbols for .c files Sam Ravnborg
2008-05-24 19:53 ` Andrew Morton
2008-05-24 20:14   ` Jeremy Fitzhardinge
2008-05-24 20:46     ` Sam Ravnborg
2008-05-24 20:56       ` Jeremy Fitzhardinge
2008-05-24 21:03         ` Sam Ravnborg
2008-05-24 20:24   ` Sam Ravnborg
2008-05-24 20:48     ` Andrew Morton
2008-05-24 21:00       ` Sam Ravnborg
2008-05-24 20:05 ` Adrian Bunk
2008-05-24 20:44   ` Sam Ravnborg
2008-05-24 20:57     ` Adrian Bunk
2008-05-24 20:20 ` Linus Torvalds
2008-05-24 20:37   ` [PATCH] x86: use defconfig as last resort Sam Ravnborg
2008-05-25  1:30     ` Linus Torvalds
2008-05-25  6:15       ` Sam Ravnborg
2008-05-25  6:22       ` Sam Ravnborg
2008-05-24 20:48 ` [RFC PATCH] kconfig: introduce KCONFIG_* symbols for .c files Jeremy Fitzhardinge
2008-05-24 20:58   ` Jeremy Fitzhardinge
2008-05-24 21:03     ` Adrian Bunk
2008-05-24 21:13       ` Jeremy Fitzhardinge
2008-05-24 21:26 ` Pavel Machek
  -- strict thread matches above, loose matches on Subject: below --
2008-05-24 23:36 Steven Fuerst
2008-05-25  0:00 ` Tom Spink
2008-05-25  0:20   ` Steven Fuerst

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox