diff mbox series

[v5,3/4] sandbox: Detect the host bit size automatically

Message ID 20220123195514.3152022-4-sjg@chromium.org
State Changes Requested
Delegated to: Tom Rini
Headers show
Series Fix compiler warnings for 32-bit ARM | expand

Commit Message

Simon Glass Jan. 23, 2022, 7:55 p.m. UTC
At present if you build sandbox on a 32-bit host a lot of errors are
produced. This is because CONFIG_HOST_64BIT is enabled by default.

It is quite annoying to have to change that manually before building
sandbox. It is also quite confusing for new users.

Add a way to detect the setting and add the appropriate
CONFIG_HOST_64BIT=y or CONFIG_HOST_32BIT=y to the defconfig, to avoid
this issue.

Tidy up the Kconfig help for the above two options while we are here.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

(no changes since v4)

Changes in v4:
- Use $(HOSTCC) instead of gcc
- Add proper Kconfig help

Changes in v3:
- Use 'bitness' instead of 'bit'

Changes in v2:
- Drop patches previously applied
- Put all the packages in gcc.rst

 Makefile                 | 11 ++++++++++-
 arch/sandbox/Kconfig     | 33 ++++++++++++++++++++++++++++-----
 scripts/kconfig/Makefile | 14 +++++++++++++-
 3 files changed, 51 insertions(+), 7 deletions(-)

Comments

Tom Rini Feb. 3, 2022, 5:12 p.m. UTC | #1
On Sun, Jan 23, 2022 at 12:55:13PM -0700, Simon Glass wrote:

> At present if you build sandbox on a 32-bit host a lot of errors are
> produced. This is because CONFIG_HOST_64BIT is enabled by default.
> 
> It is quite annoying to have to change that manually before building
> sandbox. It is also quite confusing for new users.
> 
> Add a way to detect the setting and add the appropriate
> CONFIG_HOST_64BIT=y or CONFIG_HOST_32BIT=y to the defconfig, to avoid
> this issue.
> 
> Tidy up the Kconfig help for the above two options while we are here.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>

This pretty reliably fails to build for me with
CONFIG_HOST_DETECT_BITNESS=y resulting in CONFIG_SANDBOX_BITS_PER_LONG
not being set.  It's as easy to trigger as "make O=/tmp/fail
sandbox_config all" for me (over on bill-the-cat if it works for you
locally).
Heinrich Schuchardt Feb. 3, 2022, 6:51 p.m. UTC | #2
On 1/23/22 20:55, Simon Glass wrote:
> At present if you build sandbox on a 32-bit host a lot of errors are
> produced. This is because CONFIG_HOST_64BIT is enabled by default.
>
> It is quite annoying to have to change that manually before building
> sandbox. It is also quite confusing for new users.
>
> Add a way to detect the setting and add the appropriate
> CONFIG_HOST_64BIT=y or CONFIG_HOST_32BIT=y to the defconfig, to avoid
> this issue.
>
> Tidy up the Kconfig help for the above two options while we are here.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
> (no changes since v4)
>
> Changes in v4:
> - Use $(HOSTCC) instead of gcc
> - Add proper Kconfig help
>
> Changes in v3:
> - Use 'bitness' instead of 'bit'
>
> Changes in v2:
> - Drop patches previously applied
> - Put all the packages in gcc.rst
>
>   Makefile                 | 11 ++++++++++-
>   arch/sandbox/Kconfig     | 33 ++++++++++++++++++++++++++++-----
>   scripts/kconfig/Makefile | 14 +++++++++++++-
>   3 files changed, 51 insertions(+), 7 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index b08bad48738..da9b5e494f8 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -570,8 +570,17 @@ export KBUILD_DEFCONFIG KBUILD_KCONFIG
>   config: scripts_basic outputmakefile FORCE
>   	$(Q)$(MAKE) $(build)=scripts/kconfig $@
>
> +# If nothing is specified explicitly, detect the bit size for sandbox,
> +# referred to from arch/sandbox/Kconfig
> +# Add it to the end of the defconfig file
>   %config: scripts_basic outputmakefile FORCE
> -	$(Q)$(MAKE) $(build)=scripts/kconfig $@
> +	$(Q)if test -f $(srctree)/configs/$@ && \
> +		! grep -Eq "CONFIG_HOST_(32|64)BIT=y" $(srctree)/configs/$@; then \
> +		echo '#include <stdio.h>\nint main(void) { printf("CONFIG_HOST_%dBIT=y\\n", __WORDSIZE); return 0; }' \

This does not compile on Alpine Linux:

test.c: In function 'main':
test.c:2:50: error: '__WORDSIZE' undeclared (first use in this function)
     2 | int main(void) { printf("CONFIG_HOST_%dBIT=y\n", __WORDSIZE);
return 0; }

> +			 | $(HOSTCC) -x c - -o get_word_size; \
> +		extra=$$(./get_word_size); \
> +	fi; \
> +	$(MAKE) $(build)=scripts/kconfig EXTRA_DEFCONFIG=$$extra $@
>
>   else
>   # ===========================================================================
> diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
> index 477c51960da..08d7f3bded6 100644
> --- a/arch/sandbox/Kconfig
> +++ b/arch/sandbox/Kconfig
> @@ -35,19 +35,42 @@ config SYS_CONFIG_NAME
>
>   choice
>   	prompt "Run sandbox on 32/64-bit host"
> -	default HOST_64BIT
> +	default HOST_DETECT_BITNESS
>   	help
> -	  Sandbox can be built on 32-bit and 64-bit hosts.
> -	  The default is to build on a 64-bit host and run
> -	  on a 64-bit host. If you want to run sandbox on
> -	  a 32-bit host, change it here.
> +	  Sandbox can be built on 32-bit and 64-bit hosts. This is generally
> +	  auto-detected but you can force a particular word size here. If you
> +	  see strange warnings about SANDBOX_BITS_PER_LONG then you may have



> +	  selected the wrong value.
>
>   config HOST_32BIT
>   	bool "32-bit host"
>   	depends on !PHYS_64BIT
> +	help
> +	  Select this if the host is a 32-bit machine. This adjusts various

This text is really confusing me.

Does HOST_32BIT relate to the bitness of HOSTCC that we use to compile
tools? I would not know why I should have to set HOSTCC manually.

The bitness of the binary that we build is orthogonal to the bitness of
the system that we are building on.

You can build a 64bit ARM binary on a 32bit machine and vice versa.

An i386 binary can be run on a x86_64 host. Same with ARMv7 on an ARMv8
host.

Normally I would call the machine I am building on host. But that does
not imply the bitness of the target machine nor does it imply the
bitness of the binary.

What does your usage of HOST relate to?

Best regards

Heinrich

> +	  tests and other features to work correctly in this environment. If
> +	  this option is enabled on 64-bit machines you may get build warnings
> +	  and/or errors.
>
>   config HOST_64BIT
>   	bool "64-bit host"
> +	help
> +	  Select this if the host is a 64-bit machine. This adjusts various
> +	  tests and other features to work correctly in this environment. If
> +	  this option is enabled on 32-bit machines you may get build warnings
> +	  and/or errors.
> +
> +config HOST_DETECT_BITNESS
> +	bool "Auto-detect host bitness"
> +	help
> +	  Select this if you want the build system to determine the bitness
> +	  automatically. This compiles a small program during the build, then
> +	  runs it to determine the bitness using __WORDSIZE (32 or 64 bits).
> +	  Then it adds one of the above options (CONFIG_HOST_32 or
> +	  CONFIG_HOST_64) to the configuration when creating it.
> +
> +	  In rare situations this may fail, e.g. on an unsupported Operating
> +	  System or toolchain, in which case you will likely get build warnings
> +	  and errors.
>
>   endchoice
>
> diff --git a/scripts/kconfig/Makefile b/scripts/kconfig/Makefile
> index 12e525ee31f..4e28a7e5973 100644
> --- a/scripts/kconfig/Makefile
> +++ b/scripts/kconfig/Makefile
> @@ -12,6 +12,9 @@ PHONY += xconfig gconfig menuconfig config localmodconfig localyesconfig \
>   #  Set SRCARCH to .. fake this Makefile.
>   SRCARCH := ..
>
> +# For U-Boot, EXTRA_DEFCONFIG contains a line to be added to the defconfig
> +# before processing. If empty, no line is added.
> +
>   ifdef KBUILD_KCONFIG
>   Kconfig := $(KBUILD_KCONFIG)
>   else
> @@ -92,8 +95,17 @@ else
>   endif
>   endif
>
> +# If EXTRA_DEFCONFIG is defined, add it to the end of the defconfig, before
> +# processing. This allows the caller to change a setting on the fly
>   %_defconfig: $(obj)/conf
> -	$(Q)$< $(silent) --defconfig=arch/$(SRCARCH)/configs/$@ $(Kconfig)
> +	$(Q)defconfig="$(srctree)/arch/$(SRCARCH)/configs/$@"; \
> +	if [ -n "$(EXTRA_DEFCONFIG)" ]; then \
> +		cat $$defconfig >defconfig_tmp; \
> +		echo $(EXTRA_DEFCONFIG) >>defconfig_tmp; \
> +		$< $(silent) --defconfig=defconfig_tmp $(Kconfig); \
> +	else \
> +		$< $(silent) --defconfig=$$defconfig $(Kconfig); \
> +	fi
>
>   # Added for U-Boot (backward compatibility)
>   %_config: %_defconfig
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index b08bad48738..da9b5e494f8 100644
--- a/Makefile
+++ b/Makefile
@@ -570,8 +570,17 @@  export KBUILD_DEFCONFIG KBUILD_KCONFIG
 config: scripts_basic outputmakefile FORCE
 	$(Q)$(MAKE) $(build)=scripts/kconfig $@
 
+# If nothing is specified explicitly, detect the bit size for sandbox,
+# referred to from arch/sandbox/Kconfig
+# Add it to the end of the defconfig file
 %config: scripts_basic outputmakefile FORCE
-	$(Q)$(MAKE) $(build)=scripts/kconfig $@
+	$(Q)if test -f $(srctree)/configs/$@ && \
+		! grep -Eq "CONFIG_HOST_(32|64)BIT=y" $(srctree)/configs/$@; then \
+		echo '#include <stdio.h>\nint main(void) { printf("CONFIG_HOST_%dBIT=y\\n", __WORDSIZE); return 0; }' \
+			 | $(HOSTCC) -x c - -o get_word_size; \
+		extra=$$(./get_word_size); \
+	fi; \
+	$(MAKE) $(build)=scripts/kconfig EXTRA_DEFCONFIG=$$extra $@
 
 else
 # ===========================================================================
diff --git a/arch/sandbox/Kconfig b/arch/sandbox/Kconfig
index 477c51960da..08d7f3bded6 100644
--- a/arch/sandbox/Kconfig
+++ b/arch/sandbox/Kconfig
@@ -35,19 +35,42 @@  config SYS_CONFIG_NAME
 
 choice
 	prompt "Run sandbox on 32/64-bit host"
-	default HOST_64BIT
+	default HOST_DETECT_BITNESS
 	help
-	  Sandbox can be built on 32-bit and 64-bit hosts.
-	  The default is to build on a 64-bit host and run
-	  on a 64-bit host. If you want to run sandbox on
-	  a 32-bit host, change it here.
+	  Sandbox can be built on 32-bit and 64-bit hosts. This is generally
+	  auto-detected but you can force a particular word size here. If you
+	  see strange warnings about SANDBOX_BITS_PER_LONG then you may have
+	  selected the wrong value.
 
 config HOST_32BIT
 	bool "32-bit host"
 	depends on !PHYS_64BIT
+	help
+	  Select this if the host is a 32-bit machine. This adjusts various
+	  tests and other features to work correctly in this environment. If
+	  this option is enabled on 64-bit machines you may get build warnings
+	  and/or errors.
 
 config HOST_64BIT
 	bool "64-bit host"
+	help
+	  Select this if the host is a 64-bit machine. This adjusts various
+	  tests and other features to work correctly in this environment. If
+	  this option is enabled on 32-bit machines you may get build warnings
+	  and/or errors.
+
+config HOST_DETECT_BITNESS
+	bool "Auto-detect host bitness"
+	help
+	  Select this if you want the build system to determine the bitness
+	  automatically. This compiles a small program during the build, then
+	  runs it to determine the bitness using __WORDSIZE (32 or 64 bits).
+	  Then it adds one of the above options (CONFIG_HOST_32 or
+	  CONFIG_HOST_64) to the configuration when creating it.
+
+	  In rare situations this may fail, e.g. on an unsupported Operating
+	  System or toolchain, in which case you will likely get build warnings
+	  and errors.
 
 endchoice
 
diff --git a/scripts/kconfig/Makefile b/scripts/kconfig/Makefile
index 12e525ee31f..4e28a7e5973 100644
--- a/scripts/kconfig/Makefile
+++ b/scripts/kconfig/Makefile
@@ -12,6 +12,9 @@  PHONY += xconfig gconfig menuconfig config localmodconfig localyesconfig \
 #  Set SRCARCH to .. fake this Makefile.
 SRCARCH := ..
 
+# For U-Boot, EXTRA_DEFCONFIG contains a line to be added to the defconfig
+# before processing. If empty, no line is added.
+
 ifdef KBUILD_KCONFIG
 Kconfig := $(KBUILD_KCONFIG)
 else
@@ -92,8 +95,17 @@  else
 endif
 endif
 
+# If EXTRA_DEFCONFIG is defined, add it to the end of the defconfig, before
+# processing. This allows the caller to change a setting on the fly
 %_defconfig: $(obj)/conf
-	$(Q)$< $(silent) --defconfig=arch/$(SRCARCH)/configs/$@ $(Kconfig)
+	$(Q)defconfig="$(srctree)/arch/$(SRCARCH)/configs/$@"; \
+	if [ -n "$(EXTRA_DEFCONFIG)" ]; then \
+		cat $$defconfig >defconfig_tmp; \
+		echo $(EXTRA_DEFCONFIG) >>defconfig_tmp; \
+		$< $(silent) --defconfig=defconfig_tmp $(Kconfig); \
+	else \
+		$< $(silent) --defconfig=$$defconfig $(Kconfig); \
+	fi
 
 # Added for U-Boot (backward compatibility)
 %_config: %_defconfig