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
18 inherit toolchain-funcs
20 # @FUNCTION: prune_libtool_files
21 # @USAGE: [--all|--modules]
23 # Locate unnecessary libtool files (.la) and libtool static archives
24 # (.a) and remove them from installation image.
26 # By default, .la files are removed whenever the static linkage can
27 # either be performed using pkg-config or doesn't introduce additional
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.
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.
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.
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} "$@"
48 local removing_all removing_modules opt
59 die "Invalid argument to ${FUNCNAME}(): ${opt}"
65 while IFS= read -r -d '' f; do # for all .la files
66 local archivefile=${f/%.la/.a}
68 # The following check is done by libtool itself.
69 # It helps us avoid removing random files which match '*.la',
71 if ! sed -n -e '/^# Generated by .*libtool/q0;4q1' "${f}"; then
75 [[ ${f} != ${archivefile} ]] || die 'regex sanity check failed'
76 local reason= pkgconfig_scanned=
77 local snotlink=$(sed -n -e 's:^shouldnotlink=::p' "${f}")
79 if [[ ${snotlink} == yes ]]; then
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}" )
87 # The .la file may be used by a module loader, so avoid removing it
88 # unless explicitly requested.
89 if [[ ${removing_modules} ]]; then
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).
101 if [[ ${removing_all} ]]; then
103 elif [[ ! -f ${archivefile} ]]; then
104 reason='no static archive'
105 elif [[ ! $(sed -nre \
106 "s/^(dependency_libs|inherited_linker_flags)='(.*)'$/\2/p" \
108 reason='no libs & flags'
110 if [[ ! ${pkgconfig_scanned} ]]; then
111 # Create a list of all .pc-covered libs.
113 if [[ ! ${removing_all} ]]; then
115 local tf=${T}/prune-lt-files.pc
116 local pkgconf=$(tc-getPKG_CONFIG)
118 while IFS= read -r -d '' pc; do # for all .pc files
121 # Use pkg-config if available (and works),
123 if ${pkgconf} --exists "${pc}" &>/dev/null; then
124 sed -e '/^Requires:/d' "${pc}" > "${tf}"
125 libs=$(${pkgconf} --libs "${tf}")
127 libs=$(sed -ne 's/^Libs://p' "${pc}")
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"
139 pc_libs+=( lib${arg#-l}.la )
142 done < <(find "${D}" -type f -name '*.pc' -print0)
148 fi # pkgconfig_scanned
150 has "${f##*/}" "${pc_libs[@]}" && reason='covered by .pc'
151 fi # removal due to .pc
153 fi # shouldnotlink==no
155 if [[ ${reason} ]]; then
156 einfo "Removing unnecessary ${f#${D%/}} (${reason})"
159 done < <(find "${D}" -xtype f -name '*.la' -print0)
161 if [[ ${queue[@]} ]]; then