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 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6
8 # @BLURB: Smart .la file pruning
10 # A function to locate and remove unnecessary .la files.
12 # Discouraged. Whenever possible, please use much simpler:
14 # find "${D}" -name '*.la' -delete || die
17 if [[ -z ${_LTPRUNE_ECLASS} ]]; then
23 die "${ECLASS}: banned in EAPI=${EAPI}; use 'find' instead";;
26 inherit toolchain-funcs
28 # @FUNCTION: prune_libtool_files
29 # @USAGE: [--all|--modules]
31 # Locate unnecessary libtool files (.la) and libtool static archives
32 # (.a) and remove them from installation image.
34 # By default, .la files are removed whenever the static linkage can
35 # either be performed using pkg-config or doesn't introduce additional
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.
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.
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.
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} "$@"
56 local removing_all removing_modules opt
67 die "Invalid argument to ${FUNCNAME}(): ${opt}"
73 while IFS= read -r -d '' f; do # for all .la files
74 local archivefile=${f/%.la/.a}
76 # The following check is done by libtool itself.
77 # It helps us avoid removing random files which match '*.la',
79 if ! sed -n -e '/^# Generated by .*libtool/q0;4q1' "${f}"; then
83 [[ ${f} != ${archivefile} ]] || die 'regex sanity check failed'
84 local reason= pkgconfig_scanned=
85 local snotlink=$(sed -n -e 's:^shouldnotlink=::p' "${f}")
87 if [[ ${snotlink} == yes ]]; then
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}" )
95 # The .la file may be used by a module loader, so avoid removing it
96 # unless explicitly requested.
97 if [[ ${removing_modules} ]]; then
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).
109 if [[ ${removing_all} ]]; then
111 elif [[ ! -f ${archivefile} ]]; then
112 reason='no static archive'
113 elif [[ ! $(sed -nre \
114 "s/^(dependency_libs|inherited_linker_flags)='(.*)'$/\2/p" \
116 reason='no libs & flags'
118 if [[ ! ${pkgconfig_scanned} ]]; then
119 # Create a list of all .pc-covered libs.
121 if [[ ! ${removing_all} ]]; then
123 local tf=${T}/prune-lt-files.pc
124 local pkgconf=$(tc-getPKG_CONFIG)
126 while IFS= read -r -d '' pc; do # for all .pc files
129 # Use pkg-config if available (and works),
131 if ${pkgconf} --exists "${pc}" &>/dev/null; then
132 sed -e '/^Requires:/d' "${pc}" > "${tf}"
133 libs=$(${pkgconf} --libs "${tf}")
135 libs=$(sed -ne 's/^Libs://p' "${pc}")
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"
147 pc_libs+=( lib${arg#-l}.la )
150 done < <(find "${D}" -type f -name '*.pc' -print0)
156 fi # pkgconfig_scanned
158 has "${f##*/}" "${pc_libs[@]}" && reason='covered by .pc'
159 fi # removal due to .pc
161 fi # shouldnotlink==no
163 if [[ ${reason} ]]; then
164 einfo "Removing unnecessary ${f#${D%/}} (${reason})"
167 done < <(find "${D}" -xtype f -name '*.la' -print0)
169 if [[ ${queue[@]} ]]; then