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