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