treewide: add support for "lto" in PKG_BUILD_FLAGS

This reduces open coding and allows to easily add a knob to enable
it treewide, where chosen packages can still opt-out via "no-lto".

Some packages used LTO, but not the linker plugin. This unifies 'em
all to attempt to produce better code.
Quoting man gcc(1):
"This improves the quality of optimization by exposing more code to the
link-time optimizer."

Also use -flto=auto instead of -flto=jobserver, as it's not guaranteed
that every buildsystem uses +$(MAKE) correctly.

Signed-off-by: Andre Heider <a.heider@gmail.com>
This commit is contained in:
Andre Heider 2023-02-02 21:33:38 +01:00 committed by Christian Marangi
parent da3700988d
commit 07730ff346
No known key found for this signature in database
GPG Key ID: AC001D09ADBFEAD7
23 changed files with 46 additions and 67 deletions

View File

@ -30,7 +30,7 @@ ifneq ($(strip $(PKG_USE_MIPS16)),1)
PKG_BUILD_FLAGS+=no-mips16
endif
__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections,$(PKG_BUILD_FLAGS))
__unknown_flags=$(filter-out no-iremap no-mips16 gc-sections no-gc-sections lto no-lto,$(PKG_BUILD_FLAGS))
ifneq ($(__unknown_flags),)
$(error unknown PKG_BUILD_FLAGS: $(__unknown_flags))
endif
@ -56,6 +56,11 @@ ifeq ($(call pkg_build_flag,gc-sections,0),1)
TARGET_CXXFLAGS+= -ffunction-sections -fdata-sections
TARGET_LDFLAGS+= -Wl,--gc-sections
endif
ifeq ($(call pkg_build_flag,lto,0),1)
TARGET_CFLAGS+= -flto=auto -fno-fat-lto-objects
TARGET_CXXFLAGS+= -flto=auto -fno-fat-lto-objects
TARGET_LDFLAGS+= -flto=auto -fuse-linker-plugin
endif
include $(INCLUDE_DIR)/hardening.mk
include $(INCLUDE_DIR)/prereq.mk

View File

@ -19,6 +19,8 @@ PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE
PKG_CPE_ID:=cpe:/a:jansson_project:jansson
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
@ -41,9 +43,6 @@ define Package/jansson/description
Jansson is a C library for encoding, decoding and manipulating JSON data
endef
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto
define Package/jansson/install
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_INSTALL_DIR)/usr/lib/libjansson*so.* $(1)/usr/lib/

View File

@ -26,7 +26,7 @@ PKG_CONFIG_DEPENDS:= \
CONFIG_PACKAGE_libevent2-pthreads \
CONFIG_PACKAGE_libevent2-mbedtls
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
@ -114,8 +114,8 @@ define Package/libevent2-pthreads/description
threading & locking.
endef
TARGET_CFLAGS += $(FPIC) -flto
TARGET_LDFLAGS += -Wl,--as-needed -flto
TARGET_CFLAGS += $(FPIC)
TARGET_LDFLAGS += -Wl,--as-needed
CMAKE_OPTIONS += \
-DEVENT__DISABLE_BENCHMARK:BOOL=ON \

View File

@ -22,6 +22,7 @@ PKG_LICENSE_FILES:=COPYING
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
@ -41,8 +42,7 @@ define Package/libnftnl/description
programming interface (API) to the in-kernel nf_tables subsystem.
endef
TARGET_CFLAGS += $(FPIC) -flto
TARGET_LDFLAGS += -flto
TARGET_CFLAGS += $(FPIC)
CONFIGURE_ARGS += \
--enable-static \

View File

@ -17,7 +17,7 @@ PKG_HASH:=b7ee150e49def77c765bc02aac92ddeb0bebefd4cb12aa263d8f95e405221fb8
PKG_FIXUP:=libtool libtool-abiver
PKG_INSTALL:=1
PKG_BUILD_FLAGS:=no-mips16
PKG_BUILD_FLAGS:=no-mips16 lto
PKG_BUILD_PARALLEL:=1
PKG_LICENSE:=GPL-2.0-or-later
PKG_LICENSE_FILES:=LICENSING COPYING
@ -118,12 +118,9 @@ endef
TARGET_CFLAGS += \
$(FPIC) \
-fomit-frame-pointer \
-flto \
-DFP_MAX_BITS=8192 \
$(if $(CONFIG_WOLFSSL_ALT_NAMES),-DWOLFSSL_ALT_NAMES)
TARGET_LDFLAGS += -flto
# --enable-stunnel needed for OpenSSL API compatibility bits
CONFIGURE_ARGS += \
--enable-reproducible-build \

View File

@ -21,7 +21,7 @@ PKG_LICENSE:=ISC
PKG_CONFIG_DEPENDS := CONFIG_IPV6
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
@ -44,8 +44,6 @@ define Package/firewall/conffiles
/etc/firewall.user
endef
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto
CMAKE_OPTIONS += $(if $(CONFIG_IPV6),,-DDISABLE_IPV6=1)
define Package/firewall/install

View File

@ -13,6 +13,8 @@ PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
PKG_LICENSE:=GPL-2.0
PKG_LICENSE_FILES:=
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
@ -30,10 +32,7 @@ endef
TARGET_CFLAGS += \
-I$(STAGING_DIR)/usr/include/libnl-tiny \
-I$(STAGING_DIR)/usr/include \
-flto
TARGET_LDFLAGS += -flto -fuse-linker-plugin
-I$(STAGING_DIR)/usr/include
CMAKE_OPTIONS += \
-DLIBNL_LIBS=-lnl-tiny \

View File

@ -13,6 +13,8 @@ PKG_RELEASE:=12
PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
PKG_LICENSE:=GPL-2.0
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mk
@ -23,9 +25,6 @@ define Package/swconfig
TITLE:=Switch configuration utility
endef
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto=jobserver
TARGET_CPPFLAGS := \
-D_GNU_SOURCE \
-I$(STAGING_DIR)/usr/include/libnl-tiny \

View File

@ -24,6 +24,7 @@ PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/$(PKG_NAME)-$(PKG_UPSTR
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=lto
PKG_ASLR_PIE_REGULAR:=1
PKG_CONFIG_DEPENDS:= CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_dhcp \
CONFIG_PACKAGE_dnsmasq_$(BUILD_VARIANT)_dhcpv6 \
@ -133,9 +134,6 @@ endef
Package/dnsmasq-dhcpv6/conffiles = $(Package/dnsmasq/conffiles)
Package/dnsmasq-full/conffiles = $(Package/dnsmasq/conffiles)
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto=jobserver
COPTS = -DHAVE_UBUS -DHAVE_POLL_H \
$(if $(CONFIG_IPV6),,-DNO_IPV6)

View File

@ -23,7 +23,7 @@ PKG_CPE_ID:=cpe:/a:matt_johnston:dropbear_ssh_server
PKG_BUILD_PARALLEL:=1
PKG_ASLR_PIE_REGULAR:=1
PKG_BUILD_FLAGS:=no-mips16 gc-sections
PKG_BUILD_FLAGS:=no-mips16 gc-sections lto
PKG_FIXUP:=autoreconf
PKG_FLAGS:=nonshared
@ -142,8 +142,7 @@ DB_OPT_CONFIG = \
DROPBEAR_SVR_AGENTFWD|CONFIG_DROPBEAR_AGENTFORWARD|1|0 \
TARGET_CFLAGS += -DARGTYPE=3 -flto
TARGET_LDFLAGS += -flto=jobserver
TARGET_CFLAGS += -DARGTYPE=3
db_opt_add =echo '\#define $(1) $(2)' >> $(PKG_BUILD_DIR)/localoptions.h
db_opt_replace =$(ESED) 's,^(\#define $(1)) .*$$$$,\1 $(2),g' $(PKG_BUILD_DIR)/sysoptions.h

View File

@ -32,7 +32,7 @@ PKG_CONFIG_DEPENDS:= \
CONFIG_DRIVER_11AX_SUPPORT \
CONFIG_WPA_ENABLE_WEP
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
EAPOL_TEST_PROVIDERS:=eapol-test eapol-test-openssl eapol-test-wolfssl
@ -587,8 +587,7 @@ TARGET_CPPFLAGS := \
-D_GNU_SOURCE \
$(if $(CONFIG_WPA_MSG_MIN_PRIORITY),-DCONFIG_MSG_MIN_PRIORITY=$(CONFIG_WPA_MSG_MIN_PRIORITY))
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto=jobserver -fuse-linker-plugin -lubox -lubus
TARGET_LDFLAGS += -lubox -lubus
ifdef CONFIG_PACKAGE_kmod-cfg80211
TARGET_LDFLAGS += -lm -lnl-tiny

View File

@ -21,6 +21,7 @@ PKG_CPE_ID:=cpe:/a:lldpd_project:lldpd
PKG_FIXUP:=autoreconf
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=lto
PKG_INSTALL:=1
include $(INCLUDE_DIR)/package.mk
@ -111,7 +112,6 @@ CONFIGURE_ARGS += \
$(if $(CONFIG_LLDPD_WITH_SNMP),--with-snmp,) \
$(if $(CONFIG_USE_GLIBC),,--without-libbsd)
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto -Wl,--gc-sections,--as-needed
TARGET_LDFLAGS += -Wl,--gc-sections,--as-needed
$(eval $(call BuildPackage,lldpd))

View File

@ -26,7 +26,7 @@ PKG_VERSION:=$(PKG_RELEASE_VERSION).git-$(PKG_SOURCE_DATE)
PKG_BUILD_DEPENDS:=libpcap
PKG_ASLR_PIE_REGULAR:=1
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
PKG_BUILD_PARALLEL:=1
PKG_INSTALL:=1
@ -193,9 +193,6 @@ $(call Build/Configure/Default,, \
$(PKG_BUILD_DIR)/pppd/plugins/pppoatm/linux/
endef
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto -fuse-linker-plugin
MAKE_FLAGS += COPTS="$(TARGET_CFLAGS)" \
PRECOMPILED_FILTER=1 \
STAGING_DIR="$(STAGING_DIR)"

View File

@ -19,6 +19,8 @@ PKG_MIRROR_HASH:=0e96edc983cf437b95874e5715d743f30bb826d8757dc3771ff872ab9cf18f3
PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
PKG_LICENSE:=GPL-2.0
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
@ -29,8 +31,7 @@ define Package/ustp
DEPENDS:=+libubox +libubus
endef
TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include -flto
TARGET_LDFLAGS += -flto -fuse-linker-plugin
TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include
define Package/ustp/install
$(INSTALL_DIR) $(1)/sbin $(1)/etc/init.d

View File

@ -19,7 +19,7 @@ PKG_BUILD_DEPENDS:=iptables
PKG_LICENSE:=GPL-2.0
PKG_CPE_ID:=cpe:/a:iproute2_project:iproute2
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
include $(INCLUDE_DIR)/kernel.mk
include $(INCLUDE_DIR)/package.mk
@ -173,7 +173,6 @@ define Build/Configure
> $(PKG_BUILD_DIR)/include/SNAPSHOT.h
endef
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -Wl,--as-needed
TARGET_CPPFLAGS += -I$(STAGING_DIR)/usr/include/libnl-tiny

View File

@ -18,7 +18,7 @@ PKG_HASH:=f167bbe947dd53bb9ebc0c1dcef5db6ad73ac1d6084f2c6f9376c5c360cc4d4e
PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
PKG_LICENSE:=GPL-2.0
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
include $(INCLUDE_DIR)/package.mk
@ -48,8 +48,7 @@ TARGET_CPPFLAGS:= \
-I$(STAGING_DIR)/usr/include/libnl-tiny \
$(TARGET_CPPFLAGS) \
-DCONFIG_LIBNL20 \
-D_GNU_SOURCE \
-flto
-D_GNU_SOURCE
ifeq ($(BUILD_VARIANT),full)
TARGET_CPPFLAGS += -DIW_FULL
@ -58,7 +57,7 @@ endif
MAKE_FLAGS += \
CFLAGS="$(TARGET_CPPFLAGS) $(TARGET_CFLAGS)" \
LDFLAGS="$(TARGET_LDFLAGS) -flto" \
LDFLAGS="$(TARGET_LDFLAGS)" \
NL1FOUND="" NL2FOUND=Y \
NLLIBNAME="libnl-tiny" \
LIBS="-lm -lnl-tiny" \

View File

@ -20,6 +20,8 @@ PKG_LICENSE_FILES:=COPYING
PKG_FIXUP:=autoreconf
PKG_INSTALL:=1
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
DISABLE_NLS:=
@ -60,9 +62,6 @@ ifeq ($(BUILD_VARIANT),json)
CONFIGURE_ARGS += --with-json
endif
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto
define Build/InstallDev
$(INSTALL_DIR) $(1)/usr/lib $(1)/usr/include
$(CP) $(PKG_INSTALL_DIR)/usr/lib/*.so* $(1)/usr/lib/

View File

@ -18,6 +18,7 @@ PKG_LICENSE:=GPL-2.0+
PKG_LICENSE_FILES:=
PKG_FLAGS:=nonshared
PKG_BUILD_FLAGS:=lto
include $(INCLUDE_DIR)/package.mk
@ -36,8 +37,7 @@ endef
target=$(firstword $(subst -, ,$(BOARD)))
MAKE_FLAGS += TARGET="$(target)"
TARGET_CFLAGS += -Dtarget_$(target)=1 -Wall -flto
TARGET_LDFLAGS += -flto=jobserver
TARGET_CFLAGS += -Dtarget_$(target)=1 -Wall
ifdef CONFIG_MTD_REDBOOT_PARTS
MAKE_FLAGS += FIS_SUPPORT=1

View File

@ -22,6 +22,7 @@ PKG_LICENSE_FILES:=
PKG_MAINTAINER:=John Crispin <john@phrozen.org>
PKG_BUILD_FLAGS:=lto
PKG_ASLR_PIE_REGULAR:=1
PKG_CONFIG_DEPENDS:= \
CONFIG_TARGET_INIT_PATH CONFIG_KERNEL_SECCOMP CONFIG_PROCD_SHOW_BOOT \
@ -36,9 +37,6 @@ endif
CMAKE_OPTIONS += -DEARLY_PATH="$(TARGET_INIT_PATH)"
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto
define Package/procd/Default
SECTION:=base
CATEGORY:=Base system

View File

@ -15,6 +15,7 @@ PKG_LICENSE:=LGPL-2.1
PKG_LICENSE_FILES:=
PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
PKG_BUILD_FLAGS:=lto
PKG_ASLR_PIE_REGULAR:=1
include $(INCLUDE_DIR)/package.mk
@ -50,8 +51,7 @@ define Package/libubus-lua
TITLE:=Lua binding for the OpenWrt RPC client
endef
TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include -flto
TARGET_LDFLAGS += -flto
TARGET_CFLAGS += -I$(STAGING_DIR)/usr/include
CMAKE_OPTIONS += \
-DLUAPATH=/usr/lib/lua \

View File

@ -10,7 +10,7 @@ PKG_SOURCE_VERSION:=f1f65026a94137c91b5466b149ef3ea3f20091e9
PKG_MIRROR_HASH:=3f6569a5e63fdfd032976ac0f79d736d3935101ac1b97fb370514b013c5e6bb6
CMAKE_INSTALL:=1
PKG_CHECK_FORMAT_SECURITY:=1
PKG_BUILD_FLAGS:=no-mips16
PKG_BUILD_FLAGS:=no-mips16 lto
PKG_LICENSE:=ISC
PKG_LICENSE_FILES:=
@ -30,9 +30,6 @@ define Package/usign
TITLE:=OpenWrt signature verification utility
endef
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto=jobserver
CMAKE_OPTIONS += \
-DUSE_LIBUBOX=on

View File

@ -16,6 +16,7 @@ PKG_HASH:=542750c8af7cb2630e201780b4f99f3dcceeb06f505b479ec68241c1e6af61a5
PKG_BUILD_DEPENDS:=BUSYBOX_CONFIG_PAM:libpam
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=lto
PKG_CHECK_FORMAT_SECURITY:=0
PKG_LICENSE:=GPL-2.0
@ -103,9 +104,6 @@ ifeq ($(BUILD_VARIANT),selinux)
LDLIBS += selinux sepol
endif
TARGET_CFLAGS += -flto
TARGET_LDFLAGS += -flto=jobserver -fuse-linker-plugin
MAKE_VARS :=
MAKE_FLAGS += \
EXTRA_CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS)" \

View File

@ -23,7 +23,7 @@ PKG_BUILD_DEPENDS:=util-linux e2fsprogs/host
PKG_INSTALL:=1
PKG_BUILD_PARALLEL:=1
PKG_BUILD_FLAGS:=gc-sections
PKG_BUILD_FLAGS:=gc-sections lto
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/host-build.mk
@ -143,9 +143,7 @@ $(call Package/e2fsprogs)
DEPENDS:= +e2fsprogs
endef
TARGET_CFLAGS += $(FPIC) -flto
TARGET_LDFLAGS += -flto
TARGET_CFLAGS += $(FPIC)
CONFIGURE_ARGS += \
--disable-testio-debug \