summarylogtreecommitdiffstats
path: root/lto.sh.in
blob: 126a9fc175d1cdfa45935ca23ca310f8c96d9dcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/bash
#
#   lto.sh - Compile with link-time optimization
#

[[ -n "$LIBMAKEPKG_BUILDENV_LTO_SH" ]] && return
LIBMAKEPKG_BUILDENV_LTO_SH=1

LIBRARY=${LIBRARY:-'@libmakepkgdir@'}

source "$LIBRARY/util/option.sh"

build_options+=('lto' 'lto-thin')
buildenv_functions+=('buildenv_lto')

buildenv_lto() {
	if check_buildoption "lto" "y" || check_buildoption "lto-thin" "y" && \
	[[ -f "$(gcc -print-search-dirs | grep install | awk '{print $2 "liblto_plugin.so"}')" ]]; then
		[[ "$CC" == "clang" ]] && \
			CFLAGS+=" -flto" || \
			CFLAGS+=" -flto=$(getconf _NPROCESSORS_ONLN) -flto-partition=none -fno-fat-lto-objects"
		[[ "$CXX" == "clang++" ]] && \
			CXXFLAGS+=" -flto" || \
			CXXFLAGS+=" -flto=$(getconf _NPROCESSORS_ONLN) -flto-partition=none -fno-fat-lto-objects"

		if check_buildoption "lto-thin" "y"; then
			[[ "$CC" == "clang" ]] && \
				CFLAGS="${CFLAGS// -flto / -flto=thin }" || \
				CFLAGS="${CFLAGS// -flto-partition=none / }"
			[[ "$CXX" == "clang++" ]] && \
				CXXFLAGS="${CXXFLAGS// -flto /  -flto=thin }" || \
				CXXFLAGS="${CXXFLAGS// -flto-partition=none / }"
		fi

		LDFLAGS+=" -fuse-linker-plugin $CFLAGS $CXXFLAGS"
		LTOPLUGIN="$(gcc -print-search-dirs | grep install | awk '{print $2 "liblto_plugin.so"}')"
		ARFLAGS+=" --plugin $LTOPLUGIN"
		RANLIBFLAGS+=" --plugin $LTOPLUGIN"
		NMFLAGS+=" --plugin $LTOPLUGIN"
	fi
}