
On 9/6/07, Wolfgang Denk wd@denx.de wrote:
In message 20070905140015.29345.70726.stgit@trillian.cg.shawcable.net you wrote:
Use cpp and sed to postprocess config.h and import the defined values into include/config.mk. This is to support conditional compile of modules
Just a few comments....
+# +# Sed script to parse CPP macros and generate output usable by make
This will NOT work!
It WILL (but perhaps I should change the comment)! :-) See below.
+# Only process values prefixed with #define CONFIG_ or CFG_ +/^#define (CONFIG_|CFG_)[A-Za-z0-9_]+/ {
Optional white space between ^ and # and between # and define. White space after define may be any sequence of space and TAB chars.
cpp scrubs this for me (see below)
# Strip the #define prefix
s/#define *//;
Ditto.
cpp again (and again, see below)
# Change to form CONFIG_*=VALUE
s/ \+/=/;
One or more spaces or tabs...
cpp... (are you detecting a pattern yet?) :-)
# Drop trailing spaces
s/ *$//;
or tabs...
cpp...
# drop quoted string values
s/="\(.*\)"$/=\1/;
What about embedded escaped " chars?
Okay, I do need to comment on this one. The only (sane) possibility for embedded " chars is within a string. All embedded double quotes inside a string will *already* be escaped. (if not, the compile will break anyway)
# Concatenate string values
s/" *"//g;
# Wrap unknown with quotes
Ditto.
This should be safe also.
s/=\(.*\?[^0-9].*\)$/=\"\1\"/;
# Change empty properties and '1' properties to "y"
s/=$/=y/;
s/=1$/=y/;
# print the line
What about #defines between #if 0 / #endif pairs? Or
cpp...
/* #define CONFIG_FOO */
etc.
cpp...
Parsing C preprocessor code is not that simple, I guess...
Indeed! I'm not even attempting this. Instead, I run common.h through '$(CROSS_COMPILE}cpp -dM' and parse the output of that. By letting cpp do the heavy lifting, all I need to parse is what 'cpp -dM' produces with is scrubbed for formatting.