1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: ltprune.eclass
6 # Michał Górny <mgorny@gentoo.org>
7 # @BLURB: Smart .la file pruning
9 # A function to locate and remove unnecessary .la files.
11 # Discouraged. Whenever possible, please use much simpler:
13 # find "${D}" -name '*.la' -delete || die
16 if [[ -z ${_LTPRUNE_ECLASS} ]]; then
22 die "${ECLASS}: banned in EAPI=${EAPI}; use 'find' instead";;
25 inherit toolchain-funcs
27 # @FUNCTION: prune_libtool_files
28 # @USAGE: [--all|--modules]
30 # Locate unnecessary libtool files (.la) and libtool static archives
31 # (.a) and remove them from installation image.
33 # By default, .la files are removed whenever the static linkage can
34 # either be performed using pkg-config or doesn't introduce additional
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.
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.
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.
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} "$@"
55 local removing_all removing_modules opt
66 die "Invalid argument to ${FUNCNAME}(): ${opt}"
72 while IFS= read -r -d '' f; do # for all .la files
73 local archivefile=${f/%.la/.a}
75 # The following check is done by libtool itself.
76 # It helps us avoid removing random files which match '*.la',
78 if ! sed -n -e '/^# Generated by .*libtool/q0;4q1' "${f}"; then
82 [[ ${f} != ${archivefile} ]] || die 'regex sanity check failed'
83 local reason= pkgconfig_scanned=
84 local snotlink=$(sed -n -e 's:^shouldnotlink=::p' "${f}")
86 if [[ ${snotlink} == yes ]]; then
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}" )
94 # The .la file may be used by a module loader, so avoid removing it
95 # unless explicitly requested.
96 if [[ ${removing_modules} ]]; then
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).
108 if [[ ${removing_all} ]]; then
110 elif [[ ! -f ${archivefile} ]]; then
111 reason='no static archive'
112 elif [[ ! $(sed -nre \
113 "s/^(dependency_libs|inherited_linker_flags)='(.*)'$/\2/p" \
115 reason='no libs & flags'
117 if [[ ! ${pkgconfig_scanned} ]]; then
118 # Create a list of all .pc-covered libs.
120 if [[ ! ${removing_all} ]]; then
122 local tf=${T}/prune-lt-files.pc
123 local pkgconf=$(tc-getPKG_CONFIG)
125 while IFS= read -r -d '' pc; do # for all .pc files
128 # Use pkg-config if available (and works),
130 if ${pkgconf} --exists "${pc}" &>/dev/null; then
131 sed -e '/^Requires:/d' "${pc}" > "${tf}"
132 libs=$(${pkgconf} --libs "${tf}")
134 libs=$(sed -ne 's/^Libs://p' "${pc}")
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"
146 pc_libs+=( lib${arg#-l}.la )
149 done < <(find "${D}" -type f -name '*.pc' -print0)
155 fi # pkgconfig_scanned
157 has "${f##*/}" "${pc_libs[@]}" && reason='covered by .pc'
158 fi # removal due to .pc
160 fi # shouldnotlink==no
162 if [[ ${reason} ]]; then
163 einfo "Removing unnecessary ${f#${D%/}} (${reason})"
166 done < <(find "${D}" -xtype f -name '*.la' -print0)
168 if [[ ${queue[@]} ]]; then