[U-Boot] [PATCH v4 0/8] Provide a mechanism to avoid using #ifdef everywhere

Simon Glass sjg at chromium.org
Sun Nov 10 05:24:51 CET 2013


Hi Wolfgang,

On Wed, Nov 6, 2013 at 1:24 AM, Wolfgang Denk <wd at denx.de> wrote:
> Dear Simon Glass,
>
> In message <1382800457-26608-1-git-send-email-sjg at chromium.org> you wrote:
>> Many parts of the U-Boot code base are sprinkled with #ifdefs. This makes
>> different boards compile different versions of the source code, meaning
>> that we must build all boards to check for failures. It is easy to
misspell
>> an #ifdef and there is not as much checking of this by the compiler.
Multiple
>> dependent #ifdefs are harder to do than with if..then..else. Variable
>> declarations must be #idefed as well as the code that uses them, often
much
>> later in the file/function. #ifdef indents don't match code indents and
>> have their own separate indent feature. Overall, excessive use of #idef
>> hurts readability and makes the code harder to modify and refactor. For
>> people coming newly into the code base, #ifdefs can be a big barrier.
>>
>> The use of #ifdef in U-Boot has possibly got a little out of hand. In an
>> attempt to turn the tide, this series includes a patch which provides a
way
>> to make CONFIG macros available to C code without using the preprocessor.
>> This makes it possible to use standard C conditional features such as
>> if/then instead of #ifdef. A README update exhorts compliance.
>
> As mentioned before, I'm really interested in seeing something like
> this going into mainline, but I have some doubts about the actual
> implementation.
>
> To summarize:  Your current proposal was to convert code snippets
> like this:
>
>         #ifdef CONFIG_VERSION_VARIABLE
>                 setenv("ver", version_string);  /* set version variable */
>         #endif
>
> into
>
>         if (autoconf_version_variable())
>                 setenv("ver", version_string);  /* set version variable */
>
> By chance I ran about "include/linux/kconfig.h" in the Linux kernel
> tree, which provides (among other things) the IS_ENABLED() macro that
> implements essentially the very same feature.  Using this, the same
> code would be written as:
>
>         if (IS_ENABLED(CONFIG_VERSION_VARIABLE))
>                 setenv("ver", version_string);  /* set version variable */
>
> I agree that this does not solve some of the isses that have been
> raised about this change (indentation level increses - which may in
> turn require reformatting of bigger parts of the code; code becomes
> less readable), but on the other hand it avoids the need for a new
> autoconf header file, and it should be possible to introduce this
> easily step by step.
>
> And I really like the idea of re-using existing code that is already
> known to Linux hackers, especially as we we are currently having our
> eyes on the Kconfig stuff anyway.
>
>
> What do you think?

Fair enough, sounds reasonable. The relevant kernel code is quite unusual...

/*
>  * Helper macros to use CONFIG_ options in C/CPP expressions. Note that
>  * these only work with boolean and tristate options.
>  */
> /*
>  * Getting something that works in C and CPP for an arg that may or may
>  * not be defined is tricky.  Here, if we have "#define CONFIG_BOOGER 1"
>  * we match on the placeholder define, insert the "0," for arg1 and
> generate
>  * the triplet (0, 1, 0).  Then the last step cherry picks the 2nd arg (a
> one).
>  * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and
> when
>  * the last step cherry picks the 2nd arg, we get a zero.
>  */
> #define __ARG_PLACEHOLDER_1 0,
> #define config_enabled(cfg) _config_enabled(cfg)
> #define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value)
> #define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0)
> #define ___config_enabled(__ignored, val, ...) val
> /*
>  * IS_ENABLED(CONFIG_FOO) evaluates to 1 if CONFIG_FOO is set to 'y' or
> 'm',
>  * 0 otherwise.
>  *
>  */
> #define IS_ENABLED(option) \
> (config_enabled(option) || config_enabled(option##_MODULE))



Many of U-Boot's options are not just yes/no, so I'm not sure what will
happen with those. Perhaps they just can't be used with this macro? Part of
my intent was to allow any option to be used.


So for example you can do this:

struct {
> const char *str;
> u_int len;
> int retry;
> const char *conf; /* Configuration value */
> }
> delaykey [] = {
> { str: getenv("bootdelaykey"),  retry: 1,
> conf: autoconf_autoboot_delay_str() },
> { str: getenv("bootdelaykey2"), retry: 1,
> conf: autoconf_autoboot_delay_str2() },
> { str: getenv("bootstopkey"),   retry: 0,
> conf: autoconf_autoboot_stop_str() },
> { str: getenv("bootstopkey2"),  retry: 0,
> conf: autoconf_autoboot_stop_str2() },
> };



or this:

/* don't retry auto boot? */
> if (autoconf_boot_retry_time() &&
>     !delaykey[i].retry)
> retry_time = -1;


Note that autoconf_boot_retry_time() will return 0 if the CONFIG is not
defined, or if its value is 0.

It seems to me we should provide the Linux feature in U-Boot as part of the
Kconfig stuff, and see where it takes us. Combined with a bit more
rationalisation of things like main.c it might be enough.

I like the simplicity and power of the autoconf feature, but I have similar
reservations to others.

>
> Best regards,
>
> Wolfgang Denk
>
> --
> DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
> Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
> PROGRAM - n.  A magic spell cast over a computer  allowing it to turn
> one's input into error messages.
> v. tr. - To engage in a pastime similar to banging one's head against
> a wall, but with fewer opportunities for reward.

Regards,
Simon


More information about the U-Boot mailing list