[pacman-dev] [PATCH] Add link time optimization support to makepkg

Allan McRae allan at archlinux.org
Mon Mar 1 04:19:43 UTC 2021


Add the 'lto' option to enable building with link time optimization
by adding '-flto' to both CFLAGS and CXXFLAGS.  The 'lto' option can
be specificed both in the PKGBUILD or by setting the default in
makepkg.conf.

Signed-off-by: Allan McRae <allan at archlinux.org>
---

TODO:
 - strip LTO symbols from distributed .a/.o files.
   e.g. https://bugzilla.redhat.com/attachment.cgi?id=1650804&action=diff

 - provide helper script to adjust old autoconf files for LTO support(?)
   e.g. https://bugzilla.redhat.com/attachment.cgi?id=1650843&action=diff
   Not sure this should be part of makepkg or distribution tools.


 doc/PKGBUILD.5.asciidoc               |  4 +++
 doc/makepkg.conf.5.asciidoc           |  4 +++
 etc/makepkg.conf.in                   |  5 ++--
 scripts/libmakepkg/buildenv/lto.sh.in | 37 +++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 2 deletions(-)
 create mode 100644 scripts/libmakepkg/buildenv/lto.sh.in

diff --git a/doc/PKGBUILD.5.asciidoc b/doc/PKGBUILD.5.asciidoc
index 1db0b6c4..af615040 100644
--- a/doc/PKGBUILD.5.asciidoc
+++ b/doc/PKGBUILD.5.asciidoc
@@ -325,6 +325,10 @@ underscore and the architecture name e.g., 'replaces_x86_64=()'.
 		When used in combination with the `strip' option, a separate package
 		containing the debug symbols is created.
 
+	*lto*;;
+		Enable building packages using link time optimization. Adds '-flto'
+		to both CFLAGS and CXXFLAGS.
+
 
 Packaging Functions
 -------------------
diff --git a/doc/makepkg.conf.5.asciidoc b/doc/makepkg.conf.5.asciidoc
index 04cc5ea9..2c7a54db 100644
--- a/doc/makepkg.conf.5.asciidoc
+++ b/doc/makepkg.conf.5.asciidoc
@@ -189,6 +189,10 @@ Options
 		DEBUG_CXXFLAGS to their counterpart buildflags. Creates a separate
 		package containing the debug symbols when used with `strip'.
 
+	*lto*;;
+		Enable building packages using link time optimization. Adds '-flto'
+		to both CFLAGS and CXXFLAGS.
+
 **INTEGRITY_CHECK=(**check1 ...**)**::
 	File integrity checks to use. Multiple checks may be specified; this
 	affects both generation and checking. The current valid options are:
diff --git a/etc/makepkg.conf.in b/etc/makepkg.conf.in
index b13b1d5d..43a69df6 100644
--- a/etc/makepkg.conf.in
+++ b/etc/makepkg.conf.in
@@ -76,7 +76,7 @@ BUILDENV=(!distcc color !ccache check !sign)
 #   These are default values for the options=() settings
 #########################################################################
 #
-# Makepkg defaults: OPTIONS=(!strip docs libtool staticlibs emptydirs !zipman !purge !debug)
+# Makepkg defaults: OPTIONS=(!strip docs libtool staticlibs emptydirs !zipman !purge !debug !lto)
 #  A negated option will do the opposite of the comments below.
 #
 #-- strip:      Strip symbols from binaries/libraries
@@ -87,8 +87,9 @@ BUILDENV=(!distcc color !ccache check !sign)
 #-- zipman:     Compress manual (man and info) pages in MAN_DIRS with gzip
 #-- purge:      Remove files specified by PURGE_TARGETS
 #-- debug:      Add debugging flags as specified in DEBUG_* variables
+#-- lto:        Add compile flags for building with link time optimization
 #
-OPTIONS=(strip docs libtool staticlibs emptydirs zipman purge !debug)
+OPTIONS=(strip docs libtool staticlibs emptydirs zipman purge !debug !lto)
 
 #-- File integrity checks to use. Valid: ck, md5, sha1, sha224, sha256, sha384, sha512, b2
 INTEGRITY_CHECK=(ck)
diff --git a/scripts/libmakepkg/buildenv/lto.sh.in b/scripts/libmakepkg/buildenv/lto.sh.in
new file mode 100644
index 00000000..8e7be66a
--- /dev/null
+++ b/scripts/libmakepkg/buildenv/lto.sh.in
@@ -0,0 +1,37 @@
+#!/usr/bin/bash
+#
+#   lto.sh - Specify flags for building a package with link-time
+#            optimisation
+#
+#   Copyright (c) 2021 Pacman Development Team <pacman-dev at archlinux.org>
+#
+#   This program is free software; you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation; either version 2 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+[[ -n "$LIBMAKEPKG_BUILDENV_LTO_SH" ]] && return
+LIBMAKEPKG_BUILDENV_LTO_SH=1
+
+LIBRARY=${LIBRARY:-'@libmakepkgdir@'}
+
+source "$LIBRARY/util/option.sh"
+
+build_options+=('lto')
+buildenv_functions+=('buildenv_lto')
+
+buildenv_lto() {
+	if check_option "lto" "y"; then
+		CFLAGS+=" -flto"
+		CXXFLAGS+=" -flto"
+	fi
+}
-- 
2.30.0


More information about the pacman-dev mailing list