Skip to contentSkip to conversation

Provide a mechanism to avoid using #ifdef everywhere

RFC PATCH New Simon Glass 4 participants 10 messages Feb 18, 2013 – Feb 21, 2013 Kbuild

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

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 patch 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 an example of how to use this, this patch replaces all but two #ifdefs from the main code body of common/main.c, which has the dubious distinction of having the most #ifdefs by at least one measure:

$ for f in $(find . -name *.c); do echo $(grep -c "ifdef" $f) $f; done \
	|sort -nr |head
57 ./common/main.c
57 ./arch/powerpc/cpu/mpc83xx/cpu_init.c
48 ./arch/powerpc/lib/board.c
46 ./drivers/video/cfb_console.c
40 ./drivers/mtd/cfi_flash.c
38 ./net/tftp.c
38 ./common/cmd_bootm.c
37 ./drivers/usb/host/ohci-hcd.c
36 ./drivers/fpga/ivm_core.c
35 ./drivers/usb/gadget/ether.c

Code size for this patch seems to be roughly neutral (below numbers are average change in byte size for each region:

       x86: (3 boards)   text -1.3   data +1.3
   sandbox: (1 boards)   bss +16.0
      m68k: (50 boards)   text -4.8
   powerpc: (634 boards)   text +7.4   data +0.0   bss +2.1
        sh: (21 boards)   text -9.1   bss +2.5
     nios2: (3 boards)   text +24.0
       arm: (287 boards)   spl/u-boot-spl:text -0.3   text -2.3   bss +7.2
     nds32: (3 boards)   text -16.0
---
 Makefile                      |  41 ++-
 README                        |  87 ++++-
 common/cmd_fitupd.c           |   1 +
 common/main.c                 | 794 ++++++++++++++++++------------------------
 include/command.h             |   2 -
 include/common.h              |   9 +-
 include/config_drop.h         |  17 +
 include/configs/pm9263.h      |   2 +-
 include/fdt_support.h         |   4 +-
 include/hush.h                |   2 -
 include/menu.h                |   2 -
 include/net.h                 |   2 +
 tools/scripts/define2conf.sed |  36 ++
 tools/scripts/define2list.sed |  31 ++
 14 files changed, 563 insertions(+), 467 deletions(-)
 create mode 100644 include/config_drop.h
 create mode 100644 tools/scripts/define2conf.sed
 create mode 100644 tools/scripts/define2list.sed
Makefile +40 -1
index fc18dd4..5ca3a57 100644
@@ -614,6 +614,7 @@ updater:
 # parallel sub-makes creating .depend files simultaneously.
 depend dep:	$(TIMESTAMP_FILE) $(VERSION_FILE) \
 		$(obj)include/autoconf.mk \
+		$(obj)include/generated/autoconf.h \
 		$(obj)include/generated/generic-asm-offsets.h \
 		$(obj)include/generated/asm-offsets.h
 		for dir in $(SUBDIRS) $(CPUDIR) $(LDSCRIPT_MAKEFILE_DIR) ; do \
@@ -688,6 +689,43 @@ $(obj)include/autoconf.mk: $(obj)include/config.h
 		sed -n -f tools/scripts/define2mk.sed > [email protected] && \
 	mv [email protected] $@
 
+# Create a C header file where every '#define CONFIG_XXX value' becomes
+# '#define config_xxx() value', or '#define config_xxx() 0' where the CONFIG
+# is not used by this board configuration. This allows C code to do things
+# like 'if (config_xxx())' and have the compiler remove the dead code,
+# instead of using '#ifdef CONFIG_XXX...#endif'. Note that in most cases
+# if the config_...() returns 0 then the option is not enabled. In some rare
+# cases such as CONFIG_BOOTDELAY, the config can be enabled but still have a
+# a value of 0. So in addition we a #define config_xxx_enabled(), setting the
+# value to 0 if the option is disabled, 1 if enabled. This last feature will
+# hopefully be deprecated soon.
+# The file is regenerated when any U-Boot header file changes.
+$(obj)include/generated/autoconf.h: $(obj)include/config.h
+	@$(XECHO) Generating $@ ; \
+	set -e ; \
+	: Extract the config macros to a C header file ; \
+	$(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | \
+		sed -n -f tools/scripts/define2conf.sed > [email protected]; \
+	: Regenerate our list of all config macros if neeed ; \
+	if [ ! -f [email protected] ] || \
+		find $(src) -name '*.h' -type f -newer [email protected] | \
+			egrep -qv 'include/(autoconf.h|generated|config.h)'; \
+			then \
+		: Extract all config macros from all C header files ; \
+		( \
+			find ${src} -name "*.h" -type f | xargs \
+			cat | \
+			sed -n -f tools/scripts/define2list.sed \
+		) | sort | uniq > [email protected]; \
+	fi; \
+	: Extract the enabled config macros to a C header file ; \
+	$(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | \
+		sed -n -f tools/scripts/define2list.sed | sort > [email protected]; \
+	set -e ; \
+	: Find CONFIGs that are not enabled ; \
+	comm -13 [email protected] [email protected] >>[email protected] && \
+	mv [email protected] $@
+
 $(obj)include/generated/generic-asm-offsets.h:	$(obj)include/autoconf.mk.dep \
 	$(obj)lib/asm-offsets.s
 	@$(XECHO) Generating $@
@@ -770,7 +808,8 @@ include/license.h: tools/bin2header COPYING
 unconfig:
 	@rm -f $(obj)include/config.h $(obj)include/config.mk \
 		$(obj)board/*/config.tmp $(obj)board/*/*/config.tmp \
-		$(obj)include/autoconf.mk $(obj)include/autoconf.mk.dep
+		$(obj)include/autoconf.mk $(obj)include/autoconf.mk.dep \
+		$(obj)include/generated/autoconf.h
 
 %_config::	unconfig
 	@$(MKCONFIG) -A $(@:_config=)
README +84 -3

95 lines not on this page · raw message

common/cmd_fitupd.c +1 -0
index 7a3789e..3f62d83 100644
@@ -8,6 +8,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <net.h>
 
 #if !defined(CONFIG_UPDATE_TFTP)
 #error "CONFIG_UPDATE_TFTP required"
common/main.c +345 -449

1,188 lines not on this page · raw message

include/command.h +0 -2
index 3785eb9..80da938 100644
@@ -75,10 +75,8 @@ cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
 
 extern int cmd_usage(const cmd_tbl_t *cmdtp);
 
-#ifdef CONFIG_AUTO_COMPLETE
 extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
-#endif
 
 /*
  * Monitor Command
include/common.h +4 -5
index 4ad17ea..e5eb882 100644
@@ -35,6 +35,9 @@ typedef volatile unsigned short vu_short;
 typedef volatile unsigned char	vu_char;
 
 #include <config.h>
+#ifndef DO_DEPS_ONLY
+#include <generated/autoconf.h>
+#endif
 #include <asm-offsets.h>
 #include <linux/bitops.h>
 #include <linux/types.h>
@@ -294,9 +297,6 @@ int	readline_into_buffer(const char *const prompt, char *buffer,
 int	parse_line (char *, char *[]);
 void	init_cmd_timeout(void);
 void	reset_cmd_timeout(void);
-#ifdef CONFIG_MENU
-int	abortboot(int bootdelay);
-#endif
 extern char console_buffer[];
 
 /* arch/$(ARCH)/lib/board.c */
@@ -310,6 +310,7 @@ extern ulong monitor_flash_len;
 int mac_read_from_eeprom(void);
 extern u8 _binary_dt_dtb_start[];	/* embedded device tree blob */
 int set_cpu_clk_info(void);
+extern int mdm_init(void); /* defined in board.c */
 
 /**
  * Show the DRAM size in a board-specific way
@@ -857,9 +858,7 @@ int	pcmcia_init (void);
 
 #include <bootstage.h>
 
-#ifdef CONFIG_SHOW_ACTIVITY
 void show_activity(int arg);
-#endif
 
 /* Multicore arch functions */
 #ifdef CONFIG_MP
include/config_drop.h new file +17 -0
new file mode 100644
index 0000000..bf2beaa
@@ -0,0 +1,17 @@
+/*
+ * Copyright 2013 Google, Inc
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License Version 2. This file is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#ifndef _CONFIG_DROP_H
+#define _CONFIG_DROP_H
+
+/* Options which don't seem to be referred to anywhere in U-Boot */
+#define CONFIG_MENUPROMPT	"Auto-boot prompt"
+#define CONFIG_MENUKEY
+#define CONFIG_UPDATE_TFTP
+
+#endif
include/configs/pm9263.h +1 -1
index b60a9ad..6f6ddfa 100644
@@ -355,7 +355,7 @@
 
 #define CONFIG_BOOTCOMMAND		"run flashboot"
 #define CONFIG_ROOTPATH			"/ronetix/rootfs"
-#define CONFIG_AUTOBOOT_PROMPT		"autoboot in %d seconds\n"
+#define CONFIG_AUTOBOOT_PROMPT		"autoboot in %d seconds\n", bootdelay
 
 #define CONFIG_CON_ROT			"fbcon=rotate:3 "
 #define CONFIG_BOOTARGS			"root=/dev/mtdblock4 rootfstype=jffs2 "\
include/fdt_support.h +2 -2
index 2cccc35..cf8f5e0 100644
@@ -24,7 +24,7 @@
 #ifndef __FDT_SUPPORT_H
 #define __FDT_SUPPORT_H
 
-#ifdef CONFIG_OF_LIBFDT
+#ifndef USE_HOSTCC
 
 #include <libfdt.h>
 
@@ -132,5 +132,5 @@ static inline int fdt_status_disabled_by_alias(void *fdt, const char* alias)
 	return fdt_set_status_by_alias(fdt, alias, FDT_STATUS_DISABLED, 0);
 }
 
-#endif /* ifdef CONFIG_OF_LIBFDT */
+#endif /* ifdef USE_HOSTCC */
 #endif /* ifndef __FDT_SUPPORT_H */
include/hush.h +0 -2
index ecf9222..12c55f4 100644
@@ -36,7 +36,5 @@ int set_local_var(const char *s, int flg_export);
 void unset_local_var(const char *name);
 char *get_local_var(const char *s);
 
-#if defined(CONFIG_HUSH_INIT_VAR)
 extern int hush_init_var (void);
 #endif
-#endif
include/menu.h +0 -2
index 7af5fdb..1cdcb88 100644
@@ -28,7 +28,5 @@ int menu_item_add(struct menu *m, char *item_key, void *item_data);
 int menu_destroy(struct menu *m);
 void menu_display_statusline(struct menu *m);
 
-#if defined(CONFIG_MENU_SHOW)
 int menu_show(int bootdelay);
-#endif
 #endif /* __MENU_H__ */
include/net.h +2 -0
index 970d4d1..1b56b7d 100644
@@ -695,6 +695,8 @@ extern void copy_filename(char *dst, const char *src, int size);
 /* get a random source port */
 extern unsigned int random_port(void);
 
+extern int update_tftp(ulong addr);
+
 /**********************************************************************/
 
 #endif /* __NET_H__ */
tools/scripts/define2conf.sed new file +36 -0
new file mode 100644
index 0000000..269eaba
@@ -0,0 +1,36 @@
+#
+# Sed script to parse CPP macros and generate a list of CONFIG macros
+#
+# This converts:
+#	#define CONFIG_XXX value
+#into:
+#	#define config_xxx() value
+#	#define config_xxx_enabled() 1
+#
+
+# Macros with parameters are ignored.
+/^#define CONFIG_[A-Za-z0-9_][A-Za-z0-9_]*(/ {
+	d
+}
+
+# Only process values prefixed with #define CONFIG_
+/^#define CONFIG_[A-Za-z0-9_][A-Za-z0-9_]*/ {
+	# Strip the #define prefix
+	s/#define[ \t]*//;
+	# Change to form CONFIG_*=VALUE
+	s/[\t ][\t ]*/=/;
+	# Handle lines with no value
+	s/^\([^=]*\)$/\1=/;
+	# Drop trailing spaces
+	s/ *$//;
+	# Change empty values to '1'
+	s/=$/=1/;
+	# Add #define at the start
+	s/^\([^=]*\)=/#define \L\1() /
+	# print the line
+	p
+	# Create a version suffixed with _enabled, value 1
+	s/().*/_enabled() 1/
+	# print the line
+	p
+}
tools/scripts/define2list.sed new file +31 -0
new file mode 100644
index 0000000..8df8aa0
@@ -0,0 +1,31 @@
+#
+# Sed script to parse CPP macros and generate a list of CONFIG macros
+#
+# This converts:
+#	#define CONFIG_XXX value
+#into:
+#	#define config_xxx() 0
+#	#define config_xxx_enabled() 0
+
+# Macros with parameters are ignored.
+/^#define CONFIG_[A-Za-z0-9_][A-Za-z0-9_]*(/ {
+	s/.*//
+}
+
+# Only process values prefixed with #define CONFIG_
+/^#define CONFIG_[A-Za-z0-9_][A-Za-z0-9_]*/ {
+	# Strip the #define prefix
+	s/#define *//;
+	# Remove the value
+	s/[ \t].*//;
+	# Convert to lower case, prepend #define
+	s/\(.*\)/#define \L\1/
+	# Append 0
+	s/$/() 0/
+	# print the line
+	p
+	# Create a version suffixed with _enabled, value 0
+	s/().*/_enabled() 0/
+	# print the line
+	p
+}
Signature · 1 line
1.8.1.3
#1 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

Dear Simon,

Quoted 3 lines · In message <[email protected]> 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

...

Makefile $(obj)include/autoconf.mk: $(obj)include/config.h · line 701

Quoted 10 lines
Makefile +10 -0
+# Create a C header file where every '#define CONFIG_XXX value' becomes
+# '#define config_xxx() value', or '#define config_xxx() 0' where the CONFIG
+# is not used by this board configuration. This allows C code to do things
+# like 'if (config_xxx())' and have the compiler remove the dead code,
+# instead of using '#ifdef CONFIG_XXX...#endif'. Note that in most cases
+# if the config_...() returns 0 then the option is not enabled. In some rare
+# cases such as CONFIG_BOOTDELAY, the config can be enabled but still have a
+# a value of 0. So in addition we a #define config_xxx_enabled(), setting the
+# value to 0 if the option is disabled, 1 if enabled. This last feature will
+# hopefully be deprecated soon.

I think config_* is not a good name to use here - it has never been a reserved prefix so far, so it is IMO a bad idea to turn it into one now . We already have quite a number such variable names in the code all over the place (not sure I caught them all):

config_2 config_io_ctrl
config_8260_ioports config_kbc_gpio
config_8560_ioports config_list
config_address config_list_t
config_backside_crossbar_mux config_mpc8xx_ioports
config_base config_nfc_clk
config_bit config_node
config_buf config_on_ebc_cs4_is_small_flash
config_byte config_pci_bridge
config_change config_periph_clk
config_clock config_pin
config_cmd_ctrl config_pll_clk
config_core_clk config_qe_ioports
config_data config_reg
config_data_eye_leveling_samples config_reg_helper
config_ddr config_s
config_ddr_clk config_sdram
config_ddr_data config_select_P
config_ddr_phy config_selection
config_desc config_selection_t
config_device config_status
config_fifo config_table
config_file_size config_val
config_frontside_crossbar_vsc3316 config_val_P
config_genmii_advert config_validity
config_hub_port config_validity_t
config_id config_vtp
config_instance

README Notes: · line 5488

Quoted 2 lines
README +2 -0
+The compiler will see that config_xxx() evalutes to a constant and will
+eliminate the dead code. The resulting code (and code size) is the same.

Did you measure the impact on compile time?

README Notes: · line 5514

Quoted 13 lines
README +13 -0
+This takes care of almost all CONFIG macros. Unfortunately there are a few
+cases where a value of 0 does not mean the option is disabled. For example
+CONFIG_BOOTDELAY can be defined to 0, which means that the bootdelay
+code should be used, but with a value of 0. To get around this and other
+sticky cases, an addition macro with an '_enabled' suffix is provided, where
+the value is always either 0 or 1:
+
+	// Will work even if boaard config has '#define CONFIG_BOOTDELAY 0'
+	if (config_bootdelay_enabled())
+		do_something;
+
+(Probably such config options should be deprecated and then we can remove
+this feature)

These are perfectly valid cases, I think - there are quite a number of these, including defines for console index, PHY address, etc. etc.

common/cmd_fitupd.c line 14

Quoted 10 lines
common/cmd_fitupd.c +1 -0
@@ -8,6 +8,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <net.h>
 
 #if !defined(CONFIG_UPDATE_TFTP)
 #error "CONFIG_UPDATE_TFTP required"

This seems to be an unrelated change?

Quoted 4 lines
common/main.c +0 -0
index e2d2e09..cd42b67 100644

...

Quoted 1 line

-#include <malloc.h> /* for free() prototype */

...

Quoted 1 line

+#include <malloc.h>

Why dropping the comment?

common/main.c DECLARE_GLOBAL_DATA_PTR; · line 62

Quoted 12 lines
common/main.c +11 -1
-#undef DEBUG_PARSER
+#define DEBUG_PARSER	0	/* set to 1 to debug */
+
+
+#define debug_parser(fmt, args...)		\
+	debug_cond(DEBUG_PARSER, fmt, ##args)
+
+#ifndef DEBUG_BOOTKEYS
+#define DEBUG_BOOTKEYS 0
+#endif
+#define debug_bootkeys(fmt, args...)		\
+	debug_cond(DEBUG_BOOTKEYS, fmt, ##args)

This is also a different type of changes. Please keep such separate.

common/main.c int abortboot(int bootdelay) · line 102

Quoted 3 lines
common/main.c +1 -2
-#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
-	if (bootdelay == 0)
+	if (config_zero_bootdelay_check() && bootdelay == 0)

This appears to be plain wrong. That was a "#ifndef" before...

common/main.c int abortboot(int bootdelay) · line 109

Quoted 2 lines
common/main.c +2 -0
+	if (config_autoboot_delay_str() && delaykey[0].str == NULL)
+		delaykey[0].str = config_autoboot_delay_str();

Hm.... constructs like these make me think about side effects. As is, your implementation does not pretect against any. This may be dangerous - you are evaluating the macro multiple times now, while before it was only a defined() test, folowed by a single evaluation.

And to be honest - I find the old code easier to read.

common/main.c int abortboot(int bootdelay) · line 147

Quoted 2 lines
common/main.c +1 -1
-		for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
+		for (i = 0; i < ARRAY_SIZE(delaykey); i++) {

Once more, a totally unrelated code change. Please split into independent commits.

Quoted 4 lines
common/main.c +0 -1
-#if defined CONFIG_ZERO_BOOTDELAY_CHECK
 	/*
 	 * Check if key already pressed
 	 * Don't check if bootdelay < 0

common/main.c int abortboot(int bootdelay) · line 249

Quoted 10 lines
common/main.c +1 -2
 	 */
-	if (bootdelay >= 0) {
+	if (config_zero_bootdelay_check() && bootdelay >= 0) {
 		if (tstc()) {	/* we got a key press	*/
 			(void) getc();  /* consume input	*/
 			puts ("\b\b\b 0");
 			abort = 1;	/* don't auto boot	*/
 		}
 	}
-#endif

I think code like this needs more careful editing.

With the #ifdef, it was clear that the comment only applies if CONFIG_ZERO_BOOTDELAY_CHECK is defined, but now it suddenly becomes valid _always_, which is pretty much misleading to someone trying to
understand this code. I think it would be necessary to rephrase the
commend, and move it partially into the if() block.

Quoted 67 lines
common/main.c +1 -63
-void main_loop (void)
+static void process_boot_delay(void)
 {
-#ifndef CONFIG_SYS_HUSH_PARSER
-	static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
-	int len;
-	int rc = 1;
-	int flag;
-#endif
-#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
-		defined(CONFIG_OF_CONTROL)
-	char *env;
-#endif
-#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
-	char *s;
-	int bootdelay;
-#endif
-#ifdef CONFIG_PREBOOT
-	char *p;
-#endif
-#ifdef CONFIG_BOOTCOUNT_LIMIT
 	unsigned long bootcount = 0;
 	unsigned long bootlimit = 0;
-	char *bcs;
-	char bcs_set[16];
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-
-	bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
-
-#ifdef CONFIG_BOOTCOUNT_LIMIT
-	bootcount = bootcount_load();
-	bootcount++;
-	bootcount_store (bootcount);
-	sprintf (bcs_set, "%lu", bootcount);
-	setenv ("bootcount", bcs_set);
-	bcs = getenv ("bootlimit");
-	bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-
-#ifdef CONFIG_MODEM_SUPPORT
-	debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
-	if (do_mdm_init) {
-		char *str = strdup(getenv("mdm_cmd"));
-		setenv ("preboot", str);  /* set or delete definition */
-		if (str != NULL)
-			free (str);
-		mdm_init(); /* wait for modem connection */
-	}
-#endif  /* CONFIG_MODEM_SUPPORT */
-
-#ifdef CONFIG_VERSION_VARIABLE
-	{
-		setenv ("ver", version_string);  /* set version variable */
-	}
-#endif /* CONFIG_VERSION_VARIABLE */
-
-#ifdef CONFIG_SYS_HUSH_PARSER
-	u_boot_hush_start ();
-#endif
-
-#if defined(CONFIG_HUSH_INIT_VAR)
-	hush_init_var ();
-#endif
-
-#ifdef CONFIG_PREBOOT
-	if ((p = getenv ("preboot")) != NULL) {
-# ifdef CONFIG_AUTOBOOT_KEYED

common/main.c static void process_fdt_options(const void *blob) · line 426

Quoted 10 lines
common/main.c +2 -7
-		int prev = disable_ctrlc(1);	/* disable Control C checking */
-# endif
-
-		run_command_list(p, -1, 0);
+	const char *s;
+	int bootdelay;
 
-# ifdef CONFIG_AUTOBOOT_KEYED
-		disable_ctrlc(prev);	/* restore Control C checking */
-# endif

I'm afraid here the patch becomes unreadable, at least to me - I give up.

I find the idea interesting and definitely worth to carry on, but it appears we're still a pretty long way off from usable code.

Best regards,

Wolfgang Denk

Signature · 5 lines
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
Too bad that all the people who know how to run the country are  busy
driving taxicabs and cutting hair.                     - George Burns
#2 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 22 lines · Wolfgang Denk

Dear Simon,

In message <[email protected]> you

Quoted 4 lines · 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

...

Quoted 4 lines

+# Create a C header file where every '#define CONFIG_XXX value' becomes +# '#define config_xxx() value', or '#define config_xxx() 0' where the CONFIG +# is not used by this board configuration. This allows C code to do things +# like 'if (config_xxx())' and

Quoted 8 lines

have the compiler remove the dead code, +# instead of using '#ifdef CONFIG_XXX...#endif'. Note that in most cases +# if the config_...() returns 0 then the option is not enabled. In some rare +# cases such as CONFIG_BOOTDELAY, the config can be enabled but still have a +# a value of 0. So in addition we a #define config_xxx_enabled(), setting the +# value to 0 if the option is disabled, 1 if enabled. This last feature will +# hopefully be deprecated soon.

Quoted 4 lines

I think config_* is not a good name to use here - it has never been a reserved prefix so far, so it is IMO a bad idea to turn it into one now . We already have quite a number such variable names in the code all over the place (not sure I caught them all):

Indeed. It's not a good choice to suddenly reserve now either. build_has_xxx() ? I'm not sure.

[snip]

Quoted 5 lines
Quoted 3 lines

+The compiler will see that config_xxx() evalutes to a constant and will +eliminate the dead code. The resulting code (and code size) is the same.

Did you measure the impact on compile time?

Probably won't have a good handle on this without converting a whole build target's needs (just about).

[snip]

Quoted 7 lines
Quoted 6 lines

-#if defined CONFIG_ZERO_BOOTDELAY_CHECK /* * Check if key
already pressed * Don't check if bootdelay < 0 */ - if (bootdelay

Quoted 1 line

= 0) { + if (config_zero_bootdelay_check() && bootdelay >= 0) {

if (tstc()) { /* we got a key press */ (void) getc(); /* consume
input */ puts ("\b\b\b 0"); abort = 1; /* don't auto boot */ } }
-#endif

Quoted 7 lines

I think code like this needs more careful editing.

With the #ifdef, it was clear that the comment only applies if CONFIG_ZERO_BOOTDELAY_CHECK is defined, but now it suddenly becomes valid _always_, which is pretty much misleading to someone
trying to understand this code. I think it would be necessary to
rephrase the commend, and move it partially into the if() block.

Exactly. This type of change will require a lot of re-commenting to make it clear what's going on now, and after the change even more-so.

- --
Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRIp7WAAoJENk4IS6UOR1W1S0QAKzMt8KkcVdRTFElSjlze35P PxFkO/W8YchPkzwMdhpU4UxHNYoFziLk5pLfj8hhh9uyQ7Lp0I411PZtJ+mkt3I5 RQf8xPHF9PDN2w0ZsxYKd0JE9LgFB/b9EmOOpzxy7Bge3aEGrfnhqgjNgnPGgVgO eCcLGZLrGYlbcL9SOJNxBdFjgCxJvRfrNtsaLOJc5SveeqMNGISp6xc5WDWnmf1f JAHNg7d9ik5d782AC76jbNUVOIp+85N0dMjhCdLR4YZBdNTC5grAW6x7gEGj+vYZ xUE2/Y20FG1Ie3vQjbbW1gUhYtxCxjFLl+UkUOn5bf6F0eDgyUvaSt17nrO3GSbQ hgunWaw9fZoVKMhb1WNnRHmLU5gS9rVlYGGsGibiMs1VPuiYpTLM/kuxfVitxJO0 Ysgkyotgfj2RbnKuBCkMVmvxBzIC3S2bAtxY97TwVrpEh2ZB7r2YwEKak8WhVxyQ nMyMulgpZoMJLM3fJEcF/kQPIzsKtz1Fl/YQXGZlI2lQpohE03kAPVQDyVeTQpGA FzGOUwVZY4WbcKrpcCV4tJOEnHRVyDR8ntQx0udMjtChaLE40fAmmUlWmWrnE4yV SVBM+PS2d7NCXt85QpS0eMc/UdFI0v1i6R24KEHEfqQe1WEdQb1wC7XXYblutZ8z ySlnbeEMfeDg5i5FHX46
=CF0y
-----END PGP SIGNATURE-----

#3 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

Hi Tom,

Quoted 26 lines · Tom Rini

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 22 lines · Wolfgang Denk

Dear Simon,

In message <[email protected]> you

Quoted 4 lines · 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

...

Quoted 12 lines

+# Create a C header file where every '#define CONFIG_XXX value' becomes +# '#define config_xxx() value', or '#define config_xxx() 0' where the CONFIG +# is not used by this board configuration. This allows C code to do things +# like 'if (config_xxx())' and have the compiler remove the dead code, +# instead of using '#ifdef CONFIG_XXX...#endif'. Note that in most cases +# if the config_...() returns 0 then the option is not enabled. In some rare +# cases such as CONFIG_BOOTDELAY, the config can be enabled but still have a +# a value of 0. So in addition we a #define config_xxx_enabled(), setting the +# value to 0 if the option is disabled, 1 if enabled. This last feature will +# hopefully be deprecated soon.

Quoted 7 lines
Quoted 4 lines

I think config_* is not a good name to use here - it has never been a reserved prefix so far, so it is IMO a bad idea to turn it into one now . We already have quite a number such variable names in the code all over the place (not sure I caught them all):

Indeed. It's not a good choice to suddenly reserve now either. build_has_xxx() ? I'm not sure.

So config_bootdelay(), or cfg_bootdelay() for the value, and build_has_bootdelay() for the enable?

Quoted 10 lines

[snip]

Quoted 5 lines
Quoted 3 lines

+The compiler will see that config_xxx() evalutes to a constant and will +eliminate the dead code. The resulting code (and code size) is the same.

Did you measure the impact on compile time?

Probably won't have a good handle on this without converting a whole build target's needs (just about).

Possibly, but it seems like the biggest impact is the slower reconfigure - sed runs for several seconds.

Quoted 11 lines

[snip]

Quoted 9 lines
Quoted 6 lines

-#if defined CONFIG_ZERO_BOOTDELAY_CHECK /* * Check if key already pressed * Don't check if bootdelay < 0 */ - if (bootdelay

Quoted 1 line

= 0) { + if (config_zero_bootdelay_check() && bootdelay >= 0) {

if (tstc()) { /* we got a key press */ (void) getc(); /* consume
input */ puts ("\b\b\b 0"); abort = 1; /* don't auto boot */ } }
-#endif

I think code like this needs more careful editing.

Quoted 8 lines
Quoted 5 lines

With the #ifdef, it was clear that the comment only applies if CONFIG_ZERO_BOOTDELAY_CHECK is defined, but now it suddenly becomes valid _always_, which is pretty much misleading to someone
trying to understand this code. I think it would be necessary to
rephrase the commend, and move it partially into the if() block.

Exactly. This type of change will require a lot of re-commenting to make it clear what's going on now, and after the change even more-so.

I suspect cleaning up main.c would involve a number of patches in the end.

BTW this patch also is interesting from the point of view of the generic board series. At the moment I am using a list of function pointers, which is quite nice, but does defeat the compiler optimisations, so adds about 1KB of code size. Also it's hard to imagine removing the #ifdefs from a list of function pointers using this mechanism.

Regards,
Simon

Quoted 21 lines

- --
Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRIp7WAAoJENk4IS6UOR1W1S0QAKzMt8KkcVdRTFElSjlze35P PxFkO/W8YchPkzwMdhpU4UxHNYoFziLk5pLfj8hhh9uyQ7Lp0I411PZtJ+mkt3I5 RQf8xPHF9PDN2w0ZsxYKd0JE9LgFB/b9EmOOpzxy7Bge3aEGrfnhqgjNgnPGgVgO eCcLGZLrGYlbcL9SOJNxBdFjgCxJvRfrNtsaLOJc5SveeqMNGISp6xc5WDWnmf1f JAHNg7d9ik5d782AC76jbNUVOIp+85N0dMjhCdLR4YZBdNTC5grAW6x7gEGj+vYZ xUE2/Y20FG1Ie3vQjbbW1gUhYtxCxjFLl+UkUOn5bf6F0eDgyUvaSt17nrO3GSbQ hgunWaw9fZoVKMhb1WNnRHmLU5gS9rVlYGGsGibiMs1VPuiYpTLM/kuxfVitxJO0 Ysgkyotgfj2RbnKuBCkMVmvxBzIC3S2bAtxY97TwVrpEh2ZB7r2YwEKak8WhVxyQ nMyMulgpZoMJLM3fJEcF/kQPIzsKtz1Fl/YQXGZlI2lQpohE03kAPVQDyVeTQpGA FzGOUwVZY4WbcKrpcCV4tJOEnHRVyDR8ntQx0udMjtChaLE40fAmmUlWmWrnE4yV SVBM+PS2d7NCXt85QpS0eMc/UdFI0v1i6R24KEHEfqQe1WEdQb1wC7XXYblutZ8z ySlnbeEMfeDg5i5FHX46
=CF0y
-----END PGP SIGNATURE-----

#4 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

In reply to Wolfgang Denk

Hi Wolfgang,

Quoted 7 lines · Wolfgang Denk

Dear Simon,

Quoted 3 lines · In message <[email protected]> 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

...

Quoted 10 lines

Makefile $(obj)include/autoconf.mk: $(obj)include/config.h · line 701

Quoted 10 lines
Makefile +10 -0
+# Create a C header file where every '#define CONFIG_XXX value' becomes
+# '#define config_xxx() value', or '#define config_xxx() 0' where the CONFIG
+# is not used by this board configuration. This allows C code to do things
+# like 'if (config_xxx())' and have the compiler remove the dead code,
+# instead of using '#ifdef CONFIG_XXX...#endif'. Note that in most cases
+# if the config_...() returns 0 then the option is not enabled. In some rare
+# cases such as CONFIG_BOOTDELAY, the config can be enabled but still have a
+# a value of 0. So in addition we a #define config_xxx_enabled(), setting the
+# value to 0 if the option is disabled, 1 if enabled. This last feature will
+# hopefully be deprecated soon.

Thanks for looking at this so closely - it's just an RFC at this stage, but I think it has promise.

Quoted 33 lines

I think config_* is not a good name to use here - it has never been a reserved prefix so far, so it is IMO a bad idea to turn it into one now . We already have quite a number such variable names in the code all over the place (not sure I caught them all):

config_2                            config_io_ctrl
config_8260_ioports                 config_kbc_gpio
config_8560_ioports                 config_list
config_address                      config_list_t
config_backside_crossbar_mux        config_mpc8xx_ioports
config_base                         config_nfc_clk
config_bit                          config_node
config_buf                          config_on_ebc_cs4_is_small_flash
config_byte                         config_pci_bridge
config_change                       config_periph_clk
config_clock                        config_pin
config_cmd_ctrl                     config_pll_clk
config_core_clk                     config_qe_ioports
config_data                         config_reg
config_data_eye_leveling_samples    config_reg_helper
config_ddr                          config_s
config_ddr_clk                      config_sdram
config_ddr_data                     config_select_P
config_ddr_phy                      config_selection
config_desc                         config_selection_t
config_device                       config_status
config_fifo                         config_table
config_file_size                    config_val
config_frontside_crossbar_vsc3316   config_val_P
config_genmii_advert                config_validity
config_hub_port                     config_validity_t
config_id                           config_vtp
Quoted 2 lines

config_instance

These are variables, so won't conflict with the function macros used by this patch. But maybe we should try for something like cfg_...()?

Quoted 5 lines

README Notes: · line 5488

Quoted 3 lines
README +2 -0
+The compiler will see that config_xxx() evalutes to a constant and will
+eliminate the dead code. The resulting code (and code size) is the same.

Did you measure the impact on compile time?

I did for the 'sed' step - it was a 2-3 seconds to regenerate the header files.

Full reconfig/recompile goes from about about 30s to 34s. Incremental build doesn't change noticeably.

I also tried recompiling both the mainline U-Boot's main.c 100 times, and then this one. I could not see any time difference, which is a little surprising. Maybe the C compiler's DCE is pretty early in the the process?

Quoted 17 lines
Quoted 4 lines
README +3 -0
+This takes care of almost all CONFIG macros. Unfortunately there are a few
+cases where a value of 0 does not mean the option is disabled. For example
+CONFIG_BOOTDELAY can be defined to 0, which means that the bootdelay

README Notes: · line 5514

Quoted 10 lines
README +10 -0
+code should be used, but with a value of 0. To get around this and other
+sticky cases, an addition macro with an '_enabled' suffix is provided, where
+the value is always either 0 or 1:
+
+     // Will work even if boaard config has '#define CONFIG_BOOTDELAY 0'
+     if (config_bootdelay_enabled())
+             do_something;
+
+(Probably such config options should be deprecated and then we can remove
+this feature)

These are perfectly valid cases, I think - there are quite a number of these, including defines for console index, PHY address, etc. etc.

Well the issue is not so much the value, but whether the value enables something. So for example:

config_console_index()

is perfectly fine if it just specifies the console index. But if it also enables are particular feature (e.g. turning the console on), then causes problems. I actually think such cases are quite rare, and probably cause other confusion also.

Quoted 5 lines
Quoted 5 lines
common/cmd_fitupd.c +0 -0
@@ -8,6 +8,7 @@
 
Quoted 8 lines

common/cmd_fitupd.c line 14

Quoted 6 lines
common/cmd_fitupd.c +1 -0
 #include <common.h>
 #include <command.h>
+#include <net.h>
 
 #if !defined(CONFIG_UPDATE_TFTP)
 #error "CONFIG_UPDATE_TFTP required"

This seems to be an unrelated change?

Yes - just to make it fail here rather than die with an obscure message later.

Quoted 7 lines
Quoted 6 lines
common/main.c +0 -0
index e2d2e09..cd42b67 100644

...

Quoted 5 lines
Quoted 1 line

-#include <malloc.h> /* for free() prototype */

...

Quoted 1 line

+#include <malloc.h>

Why dropping the comment?

Actually main.c includes this file twice - I dropped the one with the comment. There are far to many changes in main.c for the diff to be very useful unfortunately. It's just an RFC, but my intent with main.c was to take the concept as far as I could.

Quoted 15 lines

common/main.c DECLARE_GLOBAL_DATA_PTR; · line 62

Quoted 13 lines
common/main.c +11 -1
-#undef DEBUG_PARSER
+#define DEBUG_PARSER 0       /* set to 1 to debug */
+
+
+#define debug_parser(fmt, args...)           \
+     debug_cond(DEBUG_PARSER, fmt, ##args)
+
+#ifndef DEBUG_BOOTKEYS
+#define DEBUG_BOOTKEYS 0
+#endif
+#define debug_bootkeys(fmt, args...)         \
+     debug_cond(DEBUG_BOOTKEYS, fmt, ##args)

This is also a different type of changes. Please keep such separate.

Yes

Quoted 15 lines

common/main.c int abortboot(int bootdelay) · line 102

Quoted 4 lines
common/main.c +1 -2
-#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
-     if (bootdelay == 0)
+     if (config_zero_bootdelay_check() && bootdelay == 0)

This appears to be plain wrong. That was a "#ifndef" before...

common/main.c int abortboot(int bootdelay) · line 109

Quoted 2 lines
common/main.c +2 -0
+     if (config_autoboot_delay_str() && delaykey[0].str == NULL)
+             delaykey[0].str = config_autoboot_delay_str();

Hm.... constructs like these make me think about side effects. As is, your implementation does not pretect against any. This may be dangerous - you are evaluating the macro multiple times now, while before it was only a defined() test, folowed by a single evaluation.

Quoted 8 lines

And to be honest - I find the old code easier to read.

common/main.c int abortboot(int bootdelay) · line 147

Quoted 2 lines
common/main.c +1 -1
-             for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
+             for (i = 0; i < ARRAY_SIZE(delaykey); i++) {

Once more, a totally unrelated code change. Please split into independent commits.

I will sort out these comments when I put together a proper series for this.

Quoted 18 lines
Quoted 5 lines
common/main.c +0 -1
-#if defined CONFIG_ZERO_BOOTDELAY_CHECK
      /*
       * Check if key already pressed
       * Don't check if bootdelay < 0

common/main.c int abortboot(int bootdelay) · line 249

Quoted 10 lines
common/main.c +1 -2
       */
-     if (bootdelay >= 0) {
+     if (config_zero_bootdelay_check() && bootdelay >= 0) {
              if (tstc()) {   /* we got a key press   */
                      (void) getc();  /* consume input        */
                      puts ("\b\b\b 0");
                      abort = 1;      /* don't auto boot      */
              }
      }
-#endif

I think code like this needs more careful editing.

Quoted 5 lines

With the #ifdef, it was clear that the comment only applies if CONFIG_ZERO_BOOTDELAY_CHECK is defined, but now it suddenly becomes valid _always_, which is pretty much misleading to someone trying to
understand this code. I think it would be necessary to rephrase the
commend, and move it partially into the if() block.

Yes I see.

Quoted 76 lines
Quoted 76 lines
common/main.c +3 -67
-void main_loop (void)
+static void process_boot_delay(void)
 {
-#ifndef CONFIG_SYS_HUSH_PARSER
-     static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
-     int len;
-     int rc = 1;
-     int flag;
-#endif
-#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
-             defined(CONFIG_OF_CONTROL)
-     char *env;
-#endif
-#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
-     char *s;
-     int bootdelay;
-#endif
-#ifdef CONFIG_PREBOOT
-     char *p;
-#endif
-#ifdef CONFIG_BOOTCOUNT_LIMIT
      unsigned long bootcount = 0;
      unsigned long bootlimit = 0;
-     char *bcs;
-     char bcs_set[16];
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-
-     bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
-
-#ifdef CONFIG_BOOTCOUNT_LIMIT
-     bootcount = bootcount_load();
-     bootcount++;
-     bootcount_store (bootcount);
-     sprintf (bcs_set, "%lu", bootcount);
-     setenv ("bootcount", bcs_set);
-     bcs = getenv ("bootlimit");
-     bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
-#endif /* CONFIG_BOOTCOUNT_LIMIT */
-
-#ifdef CONFIG_MODEM_SUPPORT
-     debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
-     if (do_mdm_init) {
-             char *str = strdup(getenv("mdm_cmd"));
-             setenv ("preboot", str);  /* set or delete definition */
-             if (str != NULL)
-                     free (str);
-             mdm_init(); /* wait for modem connection */
-     }
-#endif  /* CONFIG_MODEM_SUPPORT */
-
-#ifdef CONFIG_VERSION_VARIABLE
-     {
-             setenv ("ver", version_string);  /* set version variable */
-     }
-#endif /* CONFIG_VERSION_VARIABLE */
-
-#ifdef CONFIG_SYS_HUSH_PARSER
-     u_boot_hush_start ();
-#endif
-
-#if defined(CONFIG_HUSH_INIT_VAR)
-     hush_init_var ();
-#endif
-
-#ifdef CONFIG_PREBOOT
-     if ((p = getenv ("preboot")) != NULL) {
-# ifdef CONFIG_AUTOBOOT_KEYED
-             int prev = disable_ctrlc(1);    /* disable Control C checking */
-# endif
-
-             run_command_list(p, -1, 0);
+     const char *s;
+     int bootdelay;
Quoted 7 lines

common/main.c static void process_fdt_options(const void *blob) · line 426

Quoted 3 lines
common/main.c +0 -3
-# ifdef CONFIG_AUTOBOOT_KEYED
-             disable_ctrlc(prev);    /* restore Control C checking */
-# endif

I'm afraid here the patch becomes unreadable, at least to me - I give up.

Agreed, it's just too hard to follow. in this form. What is happening here is that I split the boot delay code into two functions, one for each of the available options. It really needs a number of smaller patches to be useful.

But if you have time, please take a look at the sed scripts and the Makefile.

Quoted 3 lines

I find the idea interesting and definitely worth to carry on, but it appears we're still a pretty long way off from usable code.

OK good.

The background here is that I have been wondering about this for a while, but have never really come up with a good way (in the absence of a unified Kconfig) of getting a complete list of CONFIG variables. There was a mailing list post about this a few weeks ago. But then David Hendrix complained about main.c which spurred me into action, and I thought maybe we could just live with a brute force 'find/grep/sed' approach for now, rather than trying to be too clever. That's what I have done here.

If you look at the image series I did, for example
http://patchwork.ozlabs.org/patch/209601/, you will see that I was trying to avoid adding back #ifdefs into the cleaned-up code. That was a point solution, not particularly elegant, and I would much prefer that we get a built-in solution.

I will come back to this when I have time - will be a week or two.

Regards,
Simon

Quoted 11 lines

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 Too bad that all the people who know how to run the country are busy
driving taxicabs and cutting hair. - George Burns

#5 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

Dear Simon,

Quoted 3 lines · In message <CAPnjgZ38HPmOf07CMYssa1Q167sRjzDpbBUbCVMaoGdRoWESog@mail.gmail.com>…

Thanks for looking at this so closely - it's just an RFC at this stage, but I think it has promise.

Agreed.

Quoted 4 lines
Quoted 4 lines

I think config_* is not a good name to use here - it has never been a reserved prefix so far, so it is IMO a bad idea to turn it into one now . We already have quite a number such variable names in the code all over the place (not sure I caught them all):

...

Quoted 2 lines

These are variables, so won't conflict with the function macros used by this patch. But maybe we should try for something like cfg_...()?

You are wrong. This includes a number of functions, and macros, too, for example:

	void config_sdram(const struct emif_regs *regs);
	void config_ddr_phy(const struct emif_regs *regs);
	void config_cmd_ctrl(const struct cmd_control *cmd);
	void config_ddr_data(int data_macrono, ...)
	void config_io_ctrl(unsigned long val);
	void config_ddr(unsigned int pll, unsigned int ioctrl,
	void config_data_eye_leveling_samples(u32 emif_base);
	static int config_pll_clk(enum pll_clocks index, ...)
	static int config_core_clk(u32 ref, u32 freq)
	# define config_fifo(dir, idx, addr)

And cfg_ is not much better:

	#define cfg_read(val, addr, type, op)
	#define cfg_write(val, addr, type, op)
	u16 cfg_type = 0;
	unsigned cfg_val;
	u32 *cfg_reg;
	uint    cfg_addr;
	uint    cfg_data;
	...
Quoted 4 lines

I did for the 'sed' step - it was a 2-3 seconds to regenerate the header files.

Full reconfig/recompile goes from about about 30s to 34s. Incremental build doesn't change noticeably.

I'm mostly concerned about build time for the autobuilder, which does full config / build cycles for all boards. Here more than 10% increase hurt...

Quoted 4 lines

I also tried recompiling both the mainline U-Boot's main.c 100 times, and then this one. I could not see any time difference, which is a little surprising. Maybe the C compiler's DCE is pretty early in the the process?

This is a surprising result, indeed.

Quoted 7 lines
Quoted 7 lines

common/main.c int abortboot(int bootdelay) · line 109

Quoted 2 lines
common/main.c +2 -0
+     if (config_autoboot_delay_str() && delaykey[0].str == NULL)
+             delaykey[0].str = config_autoboot_delay_str();

Hm.... constructs like these make me think about side effects. As is, your implementation does not pretect against any. This may be dangerous - you are evaluating the macro multiple times now, while before it was only a defined() test, folowed by a single evaluation.

You did not comment on this remark. I think it is something to keep in mind!

Quoted 1 line
Quoted 1 line

And to be honest - I find the old code easier to read.

:-)

Quoted 1 line

But if you have time, please take a look at the sed scripts and the Makefile.

Sorry, but I can't find the audacity to bend my mind around these currently ;-)

But you might have a look at "tools/scripts/define2mk.sed" and either use this as is, or base your code on this. I would find it good to use the same code for the same (or so very similar) purposes.

Quoted 3 lines

The background here is that I have been wondering about this for a while, but have never really come up with a good way (in the absence of a unified Kconfig) of getting a complete list of CONFIG variables.

Does not the already existing "include/autoconf.mk" contain this information? In any case, please check "tools/scripts/define2mk.sed"

Quoted 5 lines

There was a mailing list post about this a few weeks ago. But then David Hendrix complained about main.c which spurred me into action, and I thought maybe we could just live with a brute force 'find/grep/sed' approach for now, rather than trying to be too clever. That's what I have done here.

Understood, and appreciated.

Quoted 1 line

I will come back to this when I have time - will be a week or two.

Thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
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
If you think the problem is bad now, just wait until we've solved it.
                                                        Epstein's Law
#6 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

I'm not sure if this has been discussed, but I've found as a new u-boot user there are often features that require enabling other CONFIG macros that I think should just be auto-enabled as dependencies. Please keep this in mind for any future designs. For example, when enabling CONFIG_CMD_UBI and CONFIG_CMD_UBIFS, my builds failed with linker errors because I didn't know that I needed to also define CONFIG_RBTREE and CONFIG_LZO.

Thanks,
Harvey

#7 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

In reply to Wolfgang Denk

Hi Wolfgang,

Quoted 20 lines · Wolfgang Denk

Dear Simon,

Quoted 3 lines · In message <CAPnjgZ38HPmOf07CMYssa1Q167sRjzDpbBUbCVMaoGdRoWESog@mail.gmail.com>…

Thanks for looking at this so closely - it's just an RFC at this stage, but I think it has promise.

Agreed.

Quoted 4 lines
Quoted 4 lines

I think config_* is not a good name to use here - it has never been a reserved prefix so far, so it is IMO a bad idea to turn it into one now . We already have quite a number such variable names in the code all over the place (not sure I caught them all):

...

Quoted 2 lines

These are variables, so won't conflict with the function macros used by this patch. But maybe we should try for something like cfg_...()?

You are wrong. This includes a number of functions, and macros, too, for example:

Quoted 10 lines

void config_sdram(const struct emif_regs *regs); void config_ddr_phy(const struct emif_regs *regs); void config_cmd_ctrl(const struct cmd_control *cmd); void config_ddr_data(int data_macrono, ...) void config_io_ctrl(unsigned long val);
void config_ddr(unsigned int pll, unsigned int ioctrl, void config_data_eye_leveling_samples(u32 emif_base); static int config_pll_clk(enum pll_clocks index, ...) static int config_core_clk(u32 ref, u32 freq) # define config_fifo(dir, idx, addr)

I will see if I can think of some other sane and obvious name that is little used in the source. Ideas welcome.

But this isn't a very large number. It seems to be that we could easily adjust these symbols if we wanted to (and bear in mind that there are currently no conflicts, it is just confusing) - here are the files:

for str in $(find . -name *.c -or -name *.h |xargs sed -n -f /tmp/s |sort |uniq); do echo $str; find . -name *.c -or -name *.h |xargs grep "[^a-zA-Z0-9_<]$str"; done
config_aneg(
./drivers/qe/uec.c: err = curphy->config_aneg(uec->mii_info);
config_buf(
./drivers/usb/gadget/ether.c: value = config_buf(gadget, req->buf,
./drivers/usb/gadget/composite.c:static int config_buf(struct usb_configuration *config,
./drivers/usb/gadget/composite.c: return config_buf(c, speed,
cdev->req->buf, type);
config_clock(
./arch/arm/cpu/armv7/tegra20/usb.c:static void config_clock(const u32 timing[])
./arch/arm/cpu/armv7/tegra20/usb.c: config_clock(usb_pll[freq]);
config_ddr(
./board/phytec/pcm051/board.c: config_ddr(DDR_CLK_MHZ,
MT41J256M8HX15E_IOCTRL_VALUE, &ddr3_data,
./board/ti/am335x/board.c: config_ddr(303,
MT41J128MJT125_IOCTRL_VALUE, &ddr3_data,
./board/ti/am335x/board.c: config_ddr(303,
MT41J512M8RH125_IOCTRL_VALUE, &ddr3_evm_data,
./board/ti/am335x/board.c: config_ddr(266,
MT47H128M16RT25E_IOCTRL_VALUE, &ddr2_data, ./arch/arm/include/asm/arch-am33xx/ddr_defs.h:void config_ddr(unsigned int pll, unsigned int ioctrl, ./arch/arm/cpu/armv7/am33xx/emif4.c:void config_ddr(unsigned int pll, unsigned int ioctrl,
config_desc(
./drivers/usb/gadget/composite.c:static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
./drivers/usb/gadget/composite.c: value = config_desc(cdev, w_value);
config_device(
./drivers/pci/pci.c: cfg->config_device(hose, dev, cfg);
config_fifo(
./drivers/usb/musb/musb_core.c:# define config_fifo(dir, idx, addr) ./drivers/usb/musb/musb_core.c:# define config_fifo(dir, idx, addr) \
./drivers/usb/musb/musb_core.c: config_fifo(tx, idx, fifoaddr);
./drivers/usb/musb/musb_core.c: config_fifo(rx, idx, fifoaddr);
config_pin(
./drivers/gpio/db8500_gpio.c:static void config_pin(unsigned long cfg) ./drivers/gpio/db8500_gpio.c: * Configures several pins using config_pin(). Refer to that function for
./drivers/gpio/db8500_gpio.c: config_pin(cfgs[i]);
config_sdram(
./arch/arm/include/asm/arch-am33xx/ddr_defs.h:void config_sdram(const struct emif_regs *regs);
./arch/arm/cpu/armv7/am33xx/emif4.c: config_sdram(regs);
./arch/arm/cpu/armv7/am33xx/ddr.c:void config_sdram(const struct emif_regs *regs)
config_vtp(
./arch/arm/cpu/armv7/am33xx/emif4.c:static void config_vtp(void)
./arch/arm/cpu/armv7/am33xx/emif4.c: config_vtp();

Quoted 11 lines

And cfg_ is not much better:

        #define cfg_read(val, addr, type, op)
        #define cfg_write(val, addr, type, op)
        u16 cfg_type = 0;
        unsigned cfg_val;
        u32 *cfg_reg;
        uint    cfg_addr;
        uint    cfg_data;
        ...

./drivers/video/exynos_fb.c: vid->cfg_gpio();
cfg_ldo(
./drivers/video/exynos_fb.c: vid->cfg_ldo();
cfg_oc(
./board/netta/pcmcia.c:static void cfg_oc(void)
./board/netta/pcmcia.c: cfg_oc();
cfg_read(
./drivers/pci/pci_indirect.c:#define cfg_read(val, addr, type,
op) *val = op((type)(addr))
./arch/x86/lib/pci_type1.c:#define cfg_read(val, addr, op) (*val =
op((int)(addr)))
./arch/powerpc/cpu/mpc83xx/pcie.c:#define cfg_read(val, addr, type, op) \
./arch/powerpc/cpu/mpc8220/pci.c:#define cfg_read(val, addr, type,
op) *val = op((type)(addr));
./arch/m68k/cpu/mcf547x_8x/pci.c:#define cfg_read(val, addr, type,
op) *val = op((type)(addr));
./arch/m68k/cpu/mcf5445x/pci.c:#define cfg_read(val, addr, type,
op) *val = op((type)(addr));
cfg_shdn(
./board/netta/pcmcia.c:static void cfg_shdn(void)
./board/netta/pcmcia.c: cfg_shdn();
cfg_vccd(
./board/netta/pcmcia.c:static void cfg_vccd(int no)
./board/netta/pcmcia.c: cfg_vccd(0); cfg_vccd(1); /* 3V and 5V off */
cfg_vppd(
./board/netta/pcmcia.c:static void cfg_vppd(int no)
./board/netta/pcmcia.c: cfg_vppd(0); cfg_vppd(1); /* VPPD0,VPPD1 VAVPP
=> Hi-Z */
cfg_write(
./drivers/pci/pci_indirect.c:#define cfg_write(val, addr, type,
op) op((type *)(addr), (val))
./arch/x86/lib/pci_type1.c:#define cfg_write(val, addr, op) op((val),
(int)(addr))
./arch/powerpc/cpu/mpc83xx/pcie.c:#define cfg_write(val, addr, type, op) \
./arch/powerpc/cpu/mpc8220/pci.c:#define cfg_write(val, addr, type,
op) op((type *)(addr), (val));
./arch/m68k/cpu/mcf547x_8x/pci.c:#define cfg_write(val, addr, type,
op) op((type *)(addr), (val));
./arch/m68k/cpu/mcf5445x/pci.c:#define cfg_write(val, addr, type,
op) op((type *)(addr), (val));

That's a very manageable and small series of patches I think if we want to use either. I do like an obvious name, and we already have CONFIG_...

Quoted 9 lines
Quoted 5 lines

I did for the 'sed' step - it was a 2-3 seconds to regenerate the header files.

Full reconfig/recompile goes from about about 30s to 34s. Incremental build doesn't change noticeably.

I'm mostly concerned about build time for the autobuilder, which does full config / build cycles for all boards. Here more than 10% increase hurt...

Yes this will definitely increase the time. The current brute force 'sed' of all headers isn't very efficient. How impossible would it be to regenerate this only when someone adds a new CONFIG, and then check it into the source?

Quoted 11 lines
Quoted 5 lines

I also tried recompiling both the mainline U-Boot's main.c 100 times, and then this one. I could not see any time difference, which is a little surprising. Maybe the C compiler's DCE is pretty early in the the process?

This is a surprising result, indeed.

Quoted 3 lines
Quoted 3 lines
Quoted 2 lines
common/main.c +2 -0
+     if (config_autoboot_delay_str() && delaykey[0].str == NULL)
+             delaykey[0].str = config_autoboot_delay_str();
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines

Hm.... constructs like these make me think about side effects. As is, your implementation does not pretect against any. This may be dangerous - you are evaluating the macro multiple times now, while before it was only a defined() test, folowed by a single evaluation.

You did not comment on this remark. I think it is something to keep in mind!

Yes it is.

You could use the ..._enabled() form to get around this, but it may be reasonable to state that CONFIGs are not allowed side effects. I have previously tried to add function calls into a few CONFIGs and just got compile errors. So it isn't a new problem.

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

And to be honest - I find the old code easier to read.

:-)

I prefer the one without #ifdefs, but I suppose this is not the worst example of #ifdef stuff in U-Boot.

# ifdef CONFIG_AUTOBOOT_DELAY_STR

	if (delaykey[0].str == NULL)
		delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
#  endif
#  ifdef CONFIG_AUTOBOOT_DELAY_STR2
	if (delaykey[1].str == NULL)
		delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
#  endif
#  ifdef CONFIG_AUTOBOOT_STOP_STR
	if (delaykey[2].str == NULL)
		delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
#  endif
#  ifdef CONFIG_AUTOBOOT_STOP_STR2
	if (delaykey[3].str == NULL)
		delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;

# endif

	if (config_autoboot_delay_str() && delaykey[0].str == NULL)
		delaykey[0].str = config_autoboot_delay_str();
	if (config_autoboot_delay_str2() && delaykey[1].str == NULL)
		delaykey[1].str = config_autoboot_delay_str2();
	if (config_autoboot_stop_str() && delaykey[2].str == NULL)
		delaykey[2].str = config_autoboot_stop_str();
	if (config_autoboot_stop_str2() && delaykey[3].str == NULL)
		delaykey[3].str = config_autoboot_stop_str2();
Quoted 9 lines
Quoted 2 lines

But if you have time, please take a look at the sed scripts and the Makefile.

Sorry, but I can't find the audacity to bend my mind around these currently ;-)

But you might have a look at "tools/scripts/define2mk.sed" and either use this as is, or base your code on this. I would find it good to use the same code for the same (or so very similar) purposes.

Yes found it, copied it :-)

Quoted 7 lines
Quoted 4 lines

The background here is that I have been wondering about this for a while, but have never really come up with a good way (in the absence of a unified Kconfig) of getting a complete list of CONFIG variables.

Does not the already existing "include/autoconf.mk" contain this information? In any case, please check "tools/scripts/define2mk.sed"

It only has a list of CONFIG variables that are enabled for the board. The C code will then get compile errors if it uses a config that is not enabled. So we need to define all the others to be 0 so that the code still compiles.

Quoted 9 lines
Quoted 6 lines

There was a mailing list post about this a few weeks ago. But then David Hendrix complained about main.c which spurred me into action, and I thought maybe we could just live with a brute force 'find/grep/sed' approach for now, rather than trying to be too clever. That's what I have done here.

Understood, and appreciated.

Quoted 7 lines
Quoted 1 line

I will come back to this when I have time - will be a week or two.

Thanks.

Best regards,

Wolfgang Denk

Regards,
Simon

Quoted 7 lines

--
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 If you think the problem is bad now, just wait until we've solved it.
Epstein's Law

#8 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

Dear Simon Glass,

Quoted 3 lines · In message <CAPnjgZ2NvwAB0t4v=41BaVxqMaCgAU7XBTTbM1ZRT0fTA_40tA@mail.gmail.com>…
Quoted 3 lines

You are wrong. This includes a number of functions, and macros, too, for example:

...

Quoted 3 lines

That's a very manageable and small series of patches I think if we want to use either. I do like an obvious name, and we already have CONFIG_...

I think we really need to define a new, so far unused name space for these, and reserve it for such purpose.

Quoted 4 lines

Yes this will definitely increase the time. The current brute force 'sed' of all headers isn't very efficient. How impossible would it be to regenerate this only when someone adds a new CONFIG, and then check it into the source?

Doesn't work - assume you are hacking on your new code (without checking in) - and if runs haywire because the needed re-scan is not done...

Quoted 7 lines
Quoted 2 lines

Does not the already existing "include/autoconf.mk" contain this information? In any case, please check "tools/scripts/define2mk.sed"

It only has a list of CONFIG variables that are enabled for the board. The C code will then get compile errors if it uses a config that is not enabled. So we need to define all the others to be 0 so that the code still compiles.

I see.

Thanks.

Wolfgang Denk

Signature · 5 lines
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
The human race is faced with a cruel choice: work  or  daytime  tele-
vision.
#9 lore raw

RFC PATCH 1/1 Provide a mechanism to avoid using #ifdef everywhere

Hi Wolfgang,

Quoted 6 lines · Wolfgang Denk

Dear Simon Glass,

Quoted 3 lines · In message <CAPnjgZ2NvwAB0t4v=41BaVxqMaCgAU7XBTTbM1ZRT0fTA_40tA@mail.gmail.com>…
Quoted 3 lines

You are wrong. This includes a number of functions, and macros, too, for example:

Quoted 7 lines

...

Quoted 3 lines

That's a very manageable and small series of patches I think if we want to use either. I do like an obvious name, and we already have CONFIG_...

I think we really need to define a new, so far unused name space for these, and reserve it for such purpose.

What about:

autoconf_...(): value of CONFIG (or 0 if not defined) autoconf_has_...(): 1 if the CONFIG is defined, 0 if not defined (rarely needed I think)

This doesn't seem to be used currently.

Quoted 9 lines
Quoted 5 lines

Yes this will definitely increase the time. The current brute force 'sed' of all headers isn't very efficient. How impossible would it be to regenerate this only when someone adds a new CONFIG, and then check it into the source?

Doesn't work - assume you are hacking on your new code (without checking in) - and if runs haywire because the needed re-scan is not done...

You would get compile errors in this case. I'm not sure how we can optimise this then.

Quoted 13 lines
Quoted 8 lines
Quoted 3 lines

Does not the already existing "include/autoconf.mk" contain this information? In any case, please check "tools/scripts/define2mk.sed"

It only has a list of CONFIG variables that are enabled for the board. The C code will then get compile errors if it uses a config that is not enabled. So we need to define all the others to be 0 so that the code still compiles.

I see.

Thanks.

Quoted 8 lines

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 The human race is faced with a cruel choice: work or daytime tele- vision.

Regards,
Simon