ltprune.eclass: add @SUPPORTED_EAPIS
[gentoo.git] / eclass / ltprune.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: ltprune.eclass
5 # @MAINTAINER:
6 # Michał Górny <mgorny@gentoo.org>
7 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6
8 # @BLURB: Smart .la file pruning
9 # @DESCRIPTION:
10 # A function to locate and remove unnecessary .la files.
11 #
12 # Discouraged. Whenever possible, please use much simpler:
13 # @CODE
14 # find "${D}" -name '*.la' -delete || die
15 # @CODE
16
17 if [[ -z ${_LTPRUNE_ECLASS} ]]; then
18
19 case ${EAPI:-0} in
20         0|1|2|3|4|5|6)
21                 ;;
22         *)
23                 die "${ECLASS}: banned in EAPI=${EAPI}; use 'find' instead";;
24 esac
25
26 inherit toolchain-funcs
27
28 # @FUNCTION: prune_libtool_files
29 # @USAGE: [--all|--modules]
30 # @DESCRIPTION:
31 # Locate unnecessary libtool files (.la) and libtool static archives
32 # (.a) and remove them from installation image.
33 #
34 # By default, .la files are removed whenever the static linkage can
35 # either be performed using pkg-config or doesn't introduce additional
36 # flags.
37 #
38 # If '--modules' argument is passed, .la files for modules (plugins) are
39 # removed as well. This is usually useful when the package installs
40 # plugins and the plugin loader does not use .la files.
41 #
42 # If '--all' argument is passed, all .la files are removed without
43 # performing any heuristic on them. You shouldn't ever use that,
44 # and instead report a bug in the algorithm instead.
45 #
46 # The .a files are only removed whenever corresponding .la files state
47 # that they should not be linked to, i.e. whenever these files
48 # correspond to plugins.
49 #
50 # Note: if your package installs both static libraries and .pc files
51 # which use variable substitution for -l flags, you need to add
52 # pkg-config to your DEPEND.
53 prune_libtool_files() {
54         debug-print-function ${FUNCNAME} "$@"
55
56         local removing_all removing_modules opt
57         for opt; do
58                 case "${opt}" in
59                         --all)
60                                 removing_all=1
61                                 removing_modules=1
62                                 ;;
63                         --modules)
64                                 removing_modules=1
65                                 ;;
66                         *)
67                                 die "Invalid argument to ${FUNCNAME}(): ${opt}"
68                 esac
69         done
70
71         local f
72         local queue=()
73         while IFS= read -r -d '' f; do # for all .la files
74                 local archivefile=${f/%.la/.a}
75
76                 # The following check is done by libtool itself.
77                 # It helps us avoid removing random files which match '*.la',
78                 # see bug #468380.
79                 if ! sed -n -e '/^# Generated by .*libtool/q0;4q1' "${f}"; then
80                         continue
81                 fi
82
83                 [[ ${f} != ${archivefile} ]] || die 'regex sanity check failed'
84                 local reason= pkgconfig_scanned=
85                 local snotlink=$(sed -n -e 's:^shouldnotlink=::p' "${f}")
86
87                 if [[ ${snotlink} == yes ]]; then
88
89                         # Remove static libs we're not supposed to link against.
90                         if [[ -f ${archivefile} ]]; then
91                                 einfo "Removing unnecessary ${archivefile#${D%/}} (static plugin)"
92                                 queue+=( "${archivefile}" )
93                         fi
94
95                         # The .la file may be used by a module loader, so avoid removing it
96                         # unless explicitly requested.
97                         if [[ ${removing_modules} ]]; then
98                                 reason='module'
99                         fi
100
101                 else
102
103                         # Remove .la files when:
104                         # - user explicitly wants us to remove all .la files,
105                         # - respective static archive doesn't exist,
106                         # - they are covered by a .pc file already,
107                         # - they don't provide any new information (no libs & no flags).
108
109                         if [[ ${removing_all} ]]; then
110                                 reason='requested'
111                         elif [[ ! -f ${archivefile} ]]; then
112                                 reason='no static archive'
113                         elif [[ ! $(sed -nre \
114                                         "s/^(dependency_libs|inherited_linker_flags)='(.*)'$/\2/p" \
115                                         "${f}") ]]; then
116                                 reason='no libs & flags'
117                         else
118                                 if [[ ! ${pkgconfig_scanned} ]]; then
119                                         # Create a list of all .pc-covered libs.
120                                         local pc_libs=()
121                                         if [[ ! ${removing_all} ]]; then
122                                                 local pc
123                                                 local tf=${T}/prune-lt-files.pc
124                                                 local pkgconf=$(tc-getPKG_CONFIG)
125
126                                                 while IFS= read -r -d '' pc; do # for all .pc files
127                                                         local arg libs
128
129                                                         # Use pkg-config if available (and works),
130                                                         # fallback to sed.
131                                                         if ${pkgconf} --exists "${pc}" &>/dev/null; then
132                                                                 sed -e '/^Requires:/d' "${pc}" > "${tf}"
133                                                                 libs=$(${pkgconf} --libs "${tf}")
134                                                         else
135                                                                 libs=$(sed -ne 's/^Libs://p' "${pc}")
136                                                         fi
137
138                                                         for arg in ${libs}; do
139                                                                 if [[ ${arg} == -l* ]]; then
140                                                                         if [[ ${arg} == '*$*' ]]; then
141                                                                                 eerror "${FUNCNAME}: variable substitution likely failed in ${pc}"
142                                                                                 eerror "(arg: ${arg})"
143                                                                                 eerror "Most likely, you need to add virtual/pkgconfig to DEPEND."
144                                                                                 die "${FUNCNAME}: unsubstituted variable found in .pc"
145                                                                         fi
146
147                                                                         pc_libs+=( lib${arg#-l}.la )
148                                                                 fi
149                                                         done
150                                                 done < <(find "${D}" -type f -name '*.pc' -print0)
151
152                                                 rm -f "${tf}"
153                                         fi
154
155                                         pkgconfig_scanned=1
156                                 fi # pkgconfig_scanned
157
158                                 has "${f##*/}" "${pc_libs[@]}" && reason='covered by .pc'
159                         fi # removal due to .pc
160
161                 fi # shouldnotlink==no
162
163                 if [[ ${reason} ]]; then
164                         einfo "Removing unnecessary ${f#${D%/}} (${reason})"
165                         queue+=( "${f}" )
166                 fi
167         done < <(find "${D}" -xtype f -name '*.la' -print0)
168
169         if [[ ${queue[@]} ]]; then
170                 rm -f "${queue[@]}"
171         fi
172 }
173
174 _LTPRUNE_ECLASS=1
175 fi #_LTPRUNE_ECLASS