sys-libs/libnih: stable 1.0.3-r4 for hppa, bug #724174
[gentoo.git] / eclass / prefix.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: prefix.eclass
5 # @MAINTAINER:
6 # Feel free to contact the Prefix team through <prefix@gentoo.org> if
7 # you have problems, suggestions or questions.
8 # @BLURB: Eclass to provide Prefix functionality
9 # @DESCRIPTION:
10 # Gentoo Prefix allows users to install into a self defined offset
11 # located somewhere in the filesystem.  Prefix ebuilds require
12 # additional functions and variables which are defined by this eclass.
13
14 # @ECLASS-VARIABLE: EPREFIX
15 # @DESCRIPTION:
16 # The offset prefix of a Gentoo Prefix installation.  When Gentoo Prefix
17 # is not used, ${EPREFIX} should be "".  Prefix Portage sets EPREFIX,
18 # hence this eclass has nothing to do here in that case.
19 # Note that setting EPREFIX in the environment with Prefix Portage sets
20 # Portage into cross-prefix mode.
21 if [[ ! ${EPREFIX+set} ]]; then
22         export EPREFIX=''
23 fi
24
25
26 # @FUNCTION: eprefixify
27 # @USAGE: <list of to be eprefixified files>
28 # @DESCRIPTION:
29 # replaces @GENTOO_PORTAGE_EPREFIX@ with ${EPREFIX} for the given files,
30 # dies if no arguments are given, a file does not exist, or changing a
31 # file failed.
32 eprefixify() {
33         [[ $# -lt 1 ]] && die "at least one argument required"
34
35         einfo "Adjusting to prefix ${EPREFIX:-/}"
36         local x
37         for x in "$@" ; do
38                 if [[ -e ${x} ]] ; then
39                         ebegin "  ${x##*/}"
40                         sed -i -e "s|@GENTOO_PORTAGE_EPREFIX@|${EPREFIX}|g" "${x}"
41                         eend $? || die "failed to eprefixify ${x}"
42                 else
43                         die "${x} does not exist"
44                 fi
45         done
46
47         return 0
48 }
49
50 # @FUNCTION: hprefixify
51 # @USAGE: [ -w <line match> ] [ -e <extended regex> ] [ -q <quotation char> ] <list of files>
52 # @DESCRIPTION:
53 # Tries a set of heuristics to prefixify the given files. Dies if no
54 # arguments are given, a file does not exist, or changing a file failed.
55 #
56 # Additional extended regular expression can be passed by -e or
57 # environment variable PREFIX_EXTRA_REGEX.  The default heuristics can
58 # be constrained to lines that match a sed expression passed by -w or
59 # environment variable PREFIX_LINE_MATCH.  Quotation characters can be
60 # specified by -q or environment variable PREFIX_QUOTE_CHAR, unless
61 # EPREFIX is empty.
62 #
63 # @EXAMPLE:
64 # Only prefixify the 30th line,
65 #   hprefixify -w 30 configure
66 # Only prefixify lines that contain "PATH",
67 #   hprefixify -w "/PATH/" configure
68 # Also delete all the /opt/gnu search paths,
69 #   hprefixify -e "/\/opt\/gnu/d" configure
70 # Quote the inserted EPREFIX
71 #   hprefixify -q '"' etc/profile
72 hprefixify() {
73         use prefix || return 0
74
75         local xl=() x
76         while [[ $# -gt 0 ]]; do
77                 case $1 in
78                         -e) local PREFIX_EXTRA_REGEX="$2"
79                                 shift
80                                 ;;
81                         -w) local PREFIX_LINE_MATCH="$2"
82                                 shift
83                                 ;;
84                         -q) local PREFIX_QUOTE_CHAR="${EPREFIX:+$2}"
85                                 shift
86                                 ;;
87                         *)
88                                 xl+=( "$1" )
89                                 ;;
90                 esac
91                 shift
92         done
93         local dirs="/(usr|lib(|[onx]?32|n?64)|etc|bin|sbin|var|opt|run)" \
94                   eprefix="${PREFIX_QUOTE_CHAR}${EPREFIX}${PREFIX_QUOTE_CHAR}"
95
96         [[ ${#xl[@]} -lt 1 ]] && die "at least one file operand is required"
97         einfo "Adjusting to prefix ${EPREFIX:-/}"
98         for x in "${xl[@]}" ; do
99                 if [[ -e ${x} ]] ; then
100                         ebegin "  ${x##*/}"
101                         sed -r \
102                                 -e "${PREFIX_LINE_MATCH}s,([^[:alnum:]}\)\.])${dirs},\1${eprefix}/\2,g" \
103                                 -e "${PREFIX_LINE_MATCH}s,^${dirs},${eprefix}/\1," \
104                                 -e "${PREFIX_EXTRA_REGEX}" \
105                                 -i "${x}"
106                         eend $? || die "failed to prefixify ${x}"
107                 else
108                         die "${x} does not exist"
109                 fi
110         done
111 }
112
113 # @FUNCTION: prefixify_ro
114 # @USAGE: <file>
115 # @DESCRIPTION:
116 # prefixify a read-only file.
117 # copies the files to ${T}, prefixies it, echos the new file.
118 # @EXAMPLE:
119 # doexe "$(prefixify_ro "${FILESDIR}"/fix_libtool_files.sh)"
120 # epatch "$(prefixify_ro "${FILESDIR}"/${PN}-4.0.2-path.patch)"
121 prefixify_ro() {
122         if [[ -e $1 ]] ; then
123                 local f=${1##*/}
124                 cp "$1" "${T}" || die "failed to copy file"
125                 local x="${T}"/${f}
126                 # redirect to stderr because stdout is used to
127                 # return the prefixified file.
128                 if grep -qs @GENTOO_PORTAGE_EPREFIX@ "${x}" ; then
129                         eprefixify "${T}"/${f} 1>&2
130                 else
131                         hprefixify "${T}"/${f} 1>&2
132                 fi
133                 echo "${x}"
134         else
135                 die "$1 does not exist"
136         fi
137 }
138 # vim: tw=72: