Repository: openwrt/openwrt
Pull request #18659: [RFC] tools: add options to optimize host binaries
Author: login: rockdrilla
Created: 2025-04-30T19:59:30Z | Merged: 2025-05-04T16:25:11Z

Mains goals are:
- reduce binary size of host tools;
- reduce i/o load on build host;
- increase performance of host tools being built.


Diff:
diff --git a/config/Config-devel.in b/config/Config-devel.in
index cbac91c09dacf1..3fe78f325bcd5a 100644
--- a/config/Config-devel.in
+++ b/config/Config-devel.in
@@ -62,6 +62,60 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Host Tools compile options" if DEVEL
+
+		if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Host Tools optimization flags"
+			default "-O2"
+			help
+			  Compiler flags which are used to build host tools.
+
+			  E.g.: "-O2", "-O3 -fno-tree-vectorize".
+
+			  Default is "-O2".
+
+		config HOST_TOOLS_STRIP
+			bool "Strip Host Tools"
+			help
+			  Instructs compiler/linker to use flags from HOST_FLAGS_STRIP
+			  in order to reduce binary size of host tools.
+
+		config HOST_FLAGS_STRIP
+			string "Host Tools compiler/linker flags for stripping symbols"
+			depends on HOST_TOOLS_STRIP
+			default "-Wl,-s"
+			help
+			  Compiler flags which are used to strip symbols from host tools.
+
+			  Each flag should be prefixed with "-Wl," string
+			  because compiler (GCC) passes this value to linker.
+
+			  Default is "-Wl,-s" which means "strip all symbols" - specifically,
+			  debug symbols and other symbols not needed for relocation processing.
+
+		comment "Host Tools miscellaneous flags"
+
+		config HOST_EXTRA_CFLAGS
+			string "Host Tools extra CFLAGS"
+			default ""
+
+		config HOST_EXTRA_CXXFLAGS
+			string "Host Tools extra CXXFLAGS"
+			default ""
+
+		config HOST_EXTRA_CPPFLAGS
+			string "Host Tools extra CPPFLAGS"
+			default ""
+
+		config HOST_EXTRA_LDFLAGS
+			string "Host Tools extra LDFLAGS"
+			default ""
+
+		endif
+
 	config BUILD_SUFFIX
 		string "Build suffix to append to the target BUILD_DIR variable" if DEVEL
 		default ""
diff --git a/rules.mk b/rules.mk
index dbc448e1a432c9..ccf97fe7b7bb9b 100644
--- a/rules.mk
+++ b/rules.mk
@@ -278,12 +278,19 @@ PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
 
 export PKG_CONFIG
 
+HOST_FLAGS_OPT:=$(call qstrip,$(CONFIG_HOST_FLAGS_OPT))
+HOST_FLAGS_STRIP:=$(call qstrip,$(CONFIG_HOST_FLAGS_STRIP))
+HOST_EXTRA_CFLAGS:=$(call qstrip,$(CONFIG_HOST_EXTRA_CFLAGS))
+HOST_EXTRA_CXXFLAGS:=$(call qstrip,$(CONFIG_HOST_EXTRA_CXXFLAGS))
+HOST_EXTRA_CPPFLAGS:=$(call qstrip,$(CONFIG_HOST_EXTRA_CPPFLAGS))
+HOST_EXTRA_LDFLAGS:=$(call qstrip,$(CONFIG_HOST_EXTRA_LDFLAGS))
+
 HOSTCC:=$(STAGING_DIR_HOST)/bin/gcc
 HOSTCXX:=$(STAGING_DIR_HOST)/bin/g++
-HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include $(if $(IS_PACKAGE_BUILD),-I$(STAGING_DIR_HOSTPKG)/include -I$(STAGING_DIR)/host/include)
-HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
-HOST_CXXFLAGS:=$(HOST_CFLAGS)
-HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib $(if $(IS_PACKAGE_BUILD),-L$(STAGING_DIR_HOSTPKG)/lib -L$(STAGING_DIR)/host/lib)
+HOST_CPPFLAGS:=$(strip -I$(STAGING_DIR_HOST)/include $(if $(IS_PACKAGE_BUILD),-I$(STAGING_DIR_HOSTPKG)/include -I$(STAGING_DIR)/host/include) $(HOST_EXTRA_CPPFLAGS))
+HOST_CFLAGS:=$(strip $(HOST_FLAGS_OPT) $(HOST_EXTRA_CFLAGS) $(HOST_CPPFLAGS) $(HOST_FLAGS_STRIP))
+HOST_CXXFLAGS:=$(strip $(HOST_CFLAGS) $(HOST_EXTRA_CXXFLAGS))
+HOST_LDFLAGS:=$(strip -L$(STAGING_DIR_HOST)/lib $(if $(IS_PACKAGE_BUILD),-L$(STAGING_DIR_HOSTPKG)/lib -L$(STAGING_DIR)/host/lib) $(HOST_EXTRA_LDFLAGS) $(HOST_FLAGS_STRIP))
 
 BUILD_KEY=$(TOPDIR)/key-build
 BUILD_KEY_APK_SEC=$(TOPDIR)/private-key.pem


Reviews:
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T17:30:07Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T17:30:44Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T17:31:08Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T17:31:38Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T17:32:09Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T21:34:08Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T21:35:43Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-01T21:36:16Z
user: login: rockdrilla
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-02T09:58:54Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-02T10:00:19Z
user: login: rockdrilla
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-02T10:18:42Z
user: login: rockdrilla
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-02T10:39:24Z
user: login: Ansuel
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-05-02T15:25:13Z

Review comments:
diff_hunk: @@ -62,6 +62,61 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Optimize host tools" if DEVEL
path: config/Config-devel.in
user: login: Ansuel
body: lets use "Host Tools Compile Options"
created_at: 2025-05-01T17:30:07Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,61 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Optimize host tools" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Generic host optimization flags"
path: config/Config-devel.in
user: login: Ansuel
body: "Host Tools optimization flags"
created_at: 2025-05-01T17:30:44Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,61 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Optimize host tools" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Generic host optimization flags"
+			depends on OPTIMIZE_HOST_TOOLS
+			default "-O2"
+			help
+			  Compiler optimization flags (e.g. "-O2", "-O3" and so on).
+
+			  Default is "-O2". If not set or empty, defaults to "-O2".
+
+		config HOST_TOOLS_STRIP
+			bool "Strip host tools"
path: config/Config-devel.in
user: login: Ansuel
body: capital Host Tools ?
created_at: 2025-05-01T17:31:08Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,61 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Optimize host tools" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Generic host optimization flags"
+			depends on OPTIMIZE_HOST_TOOLS
+			default "-O2"
+			help
+			  Compiler optimization flags (e.g. "-O2", "-O3" and so on).
+
+			  Default is "-O2". If not set or empty, defaults to "-O2".
+
+		config HOST_TOOLS_STRIP
+			bool "Strip host tools"
+			depends on OPTIMIZE_HOST_TOOLS
+			help
+			  Instructs compiler/linker to use flags from HOST_FLAGS_STRIP
+			  in order to reduce binary size.
+
+		config HOST_FLAGS_STRIP
+			string "Linker strip flags"
+			depends on HOST_TOOLS_STRIP
+			default "-Wl,-s"
+			help
+			  Compiler flags for linker (prefixed with "-Wl," in terms of GCC).
+
+			  Default is "-Wl,-s" which means "strip all symbols" - specifically,
+			  debug and all unneeded symbols not needed for relocation processing.
+
+		comment "Miscellaneous flags"
+
+		config HOST_EXTRA_CFLAGS
+			string "Extra host CFLAGS"
path: config/Config-devel.in
user: login: Ansuel
body: Host Tools, same for the other 3.
created_at: 2025-05-01T17:31:38Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,61 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Optimize host tools" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Generic host optimization flags"
+			depends on OPTIMIZE_HOST_TOOLS
+			default "-O2"
+			help
+			  Compiler optimization flags (e.g. "-O2", "-O3" and so on).
+
+			  Default is "-O2". If not set or empty, defaults to "-O2".
+
+		config HOST_TOOLS_STRIP
+			bool "Strip host tools"
+			depends on OPTIMIZE_HOST_TOOLS
+			help
+			  Instructs compiler/linker to use flags from HOST_FLAGS_STRIP
+			  in order to reduce binary size.
+
+		config HOST_FLAGS_STRIP
+			string "Linker strip flags"
path: config/Config-devel.in
user: login: Ansuel
body: Host Tool Linker ... ? 
created_at: 2025-05-01T17:32:09Z
author_association: MEMBER
diff_hunk: @@ -278,12 +278,20 @@ PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
 
 export PKG_CONFIG
 
+HOST_FLAGS_OPT:=$(if $(CONFIG_OPTIMIZE_HOST_TOOLS),$(call qstrip,$(CONFIG_HOST_FLAGS_OPT)))
+HOST_FLAGS_OPT:=$(if $(HOST_FLAGS_OPT),$(HOST_FLAGS_OPT),-O2)
path: rules.mk
user: login: Ansuel
body: Pack this under a single condition. 
created_at: 2025-05-01T21:34:07Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,68 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Host Tools Compile Options" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
path: config/Config-devel.in
user: login: Ansuel
body: Indent everything of one additional block since it's part of the menuconfig.
created_at: 2025-05-01T21:35:43Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,68 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Host Tools Compile Options" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Host Tools optimization flags"
+			depends on OPTIMIZE_HOST_TOOLS
path: config/Config-devel.in
user: login: Ansuel
body: drop depends on OPTIMIZE_HOST_TOOLS as we already have the if for everything.
created_at: 2025-05-01T21:36:15Z
author_association: MEMBER
diff_hunk: @@ -278,12 +278,20 @@ PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
 
 export PKG_CONFIG
 
+HOST_FLAGS_OPT:=$(if $(CONFIG_OPTIMIZE_HOST_TOOLS),$(call qstrip,$(CONFIG_HOST_FLAGS_OPT)))
+HOST_FLAGS_OPT:=$(if $(HOST_FLAGS_OPT),$(HOST_FLAGS_OPT),-O2)
path: rules.mk
user: login: rockdrilla
body: It's not possible due to promise "`If not set or empty, defaults to "-O2".`"
created_at: 2025-05-02T09:58:54Z
author_association: CONTRIBUTOR
diff_hunk: @@ -278,12 +278,20 @@ PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
 
 export PKG_CONFIG
 
+HOST_FLAGS_OPT:=$(if $(CONFIG_OPTIMIZE_HOST_TOOLS),$(call qstrip,$(CONFIG_HOST_FLAGS_OPT)))
+HOST_FLAGS_OPT:=$(if $(HOST_FLAGS_OPT),$(HOST_FLAGS_OPT),-O2)
path: rules.mk
user: login: Ansuel
body: `HOST_FLAGS_OPT:=$(if $(CONFIG_OPTIMIZE_HOST_TOOLS),$(call qstrip,$(CONFIG_HOST_FLAGS_OPT)),-O2)` ?

it's ok to set it empty if the user requested it.
created_at: 2025-05-02T10:00:18Z
author_association: MEMBER
diff_hunk: @@ -62,6 +62,68 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Host Tools Compile Options" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Host Tools optimization flags"
+			depends on OPTIMIZE_HOST_TOOLS
path: config/Config-devel.in
user: login: rockdrilla
body: Clause "`depends on OPTIMIZE_HOST_TOOLS`" ensures that options from this submenu doesn't appear in config if `CONFIG_OPTIMIZE_HOST_TOOLS` is not set. Same for `CONFIG_HOST_TOOLS_STRIP` and `CONFIG_HOST_FLAGS_STRIP`.
created_at: 2025-05-02T10:18:41Z
author_association: CONTRIBUTOR
diff_hunk: @@ -278,12 +278,20 @@ PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
 
 export PKG_CONFIG
 
+HOST_FLAGS_OPT:=$(if $(CONFIG_OPTIMIZE_HOST_TOOLS),$(call qstrip,$(CONFIG_HOST_FLAGS_OPT)))
+HOST_FLAGS_OPT:=$(if $(HOST_FLAGS_OPT),$(HOST_FLAGS_OPT),-O2)
path: rules.mk
user: login: rockdrilla
body: Seems legit.
created_at: 2025-05-02T10:39:24Z
author_association: CONTRIBUTOR
diff_hunk: @@ -62,6 +62,68 @@ menuconfig DEVEL
 		  Compile all host host tools even if not needed. This is needed to prepare a
 		  universal precompiled host tools archive to use in another buildroot.
 
+	menuconfig OPTIMIZE_HOST_TOOLS
+		bool "Host Tools Compile Options" if DEVEL
+
+	if OPTIMIZE_HOST_TOOLS
+
+		config HOST_FLAGS_OPT
+			string "Host Tools optimization flags"
+			depends on OPTIMIZE_HOST_TOOLS
path: config/Config-devel.in
user: login: Ansuel
body: Nono I verified. The "if OPTIMIZE_HOST_TOOLS" is enough to make the option not appear in the final .config

Indeed from the kernel kconfig documentation is precisely said that either if CONFIG block or depends on CONFIG should be used as they do the same exact thing. (I just verified that to make sure)
created_at: 2025-05-02T15:25:13Z
author_association: MEMBER

PR comments:
user: login: rockdrilla
created_at: 2025-04-30T20:02:36Z
body: @Ansuel, @blogic, @hauke, @nbd168, friendly ping and PTAL :)
author_association: CONTRIBUTOR
user: login: rockdrilla
created_at: 2025-04-30T23:41:16Z
body: I've encountered bug with `tools/make-ext4fs`, MR with fix: openwrt/make_ext4fs#3
author_association: CONTRIBUTOR
user: login: Ansuel
created_at: 2025-05-01T10:59:17Z
body: @rockdrilla lovely! I wonder if you make some stats about size and time difference for this.

Also why RFC? The changes are so simple I guess this can be merged right away. Maybe the submenu and using "Optimize tool" might be confusing. I don't remember what we use for kernel build option but we should stick to that naming scheme.

Maybe a better name for the category might be "Advanced Host build Options" ?
author_association: MEMBER
user: login: rockdrilla
created_at: 2025-05-01T14:29:56Z
body: > Also why RFC? The changes are so simple I guess this can be merged right away.

I usually tag my MRs with RFC when I'm not sure that upstream wants this. %)

> Maybe the submenu and using "Optimize tool" might be confusing.

I didn't want to choke this submenu so there's another submenu. :)

> I don't remember what we use for kernel build option but we should stick to that naming scheme.

Kernel submenu doesn't contain less or more similar configuration.

> Maybe a better name for the category might be "Advanced Host build Options"

Naming and spelling proposals are welcome - sorry, English isn't my native language.
author_association: CONTRIBUTOR
user: login: rockdrilla
created_at: 2025-05-01T19:43:02Z
body: @Ansuel I've changed titles / help messages, PTAL =)
author_association: CONTRIBUTOR
user: login: rockdrilla
created_at: 2025-05-01T19:46:21Z
body: Now it looks fine from my point of view:

![изображение](https://github.com/user-attachments/assets/eb0e7f7f-2d1c-4cdf-a425-68a93f766e12)

author_association: CONTRIBUTOR