media-fonts/noto-emoji: add ~ppc64 keyword
[gentoo.git] / eclass / portability.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: portability.eclass
5 # @MAINTAINER:
6 # base-system@gentoo.org
7 # @AUTHOR:
8 # Diego Pettenò <flameeyes@gentoo.org>
9 # @BLURB: This eclass is created to avoid using non-portable GNUisms inside ebuilds
10
11 if [[ -z ${_PORTABILITY_ECLASS} ]]; then
12 _PORTABILITY_ECLASS=1
13
14 # @FUNCTION: treecopy
15 # @USAGE: <orig1> [orig2 orig3 ....] <dest>
16 # @RETURN:
17 # @DESCRIPTION:
18 # mimic cp --parents copy, but working on BSD userland as well
19 treecopy() {
20         local dest=${!#}
21         local files_count=$#
22
23         while (( $# > 1 )); do
24                 local dirstruct=$(dirname "$1")
25                 mkdir -p "${dest}/${dirstruct}" || die
26                 cp -pPR "$1" "${dest}/${dirstruct}" || die
27
28                 shift
29         done
30 }
31
32 # @FUNCTION: seq
33 # @USAGE: [min] <max> [step]
34 # @RETURN: sequence from min to max regardless of seq command being present on system
35 # @DESCRIPTION:
36 # compatibility function that mimes seq command if not available
37 seq() {
38         # First try `seq`
39         local p=$(type -P seq)
40         if [[ -n ${p} ]] ; then
41                 "${p}" "$@" || die
42                 return $?
43         fi
44
45         local min max step
46         case $# in
47                 1) min=1  max=$1 step=1  ;;
48                 2) min=$1 max=$2 step=1  ;;
49                 3) min=$1 max=$3 step=$2 ;;
50                 *) die "seq called with wrong number of arguments" ;;
51         esac
52
53         # Then try `jot`
54         p=$(type -P jot)
55         if [[ -n ${p} ]] ; then
56                 local reps
57                 # BSD userland
58                 if [[ ${step} != 0 ]] ; then
59                         reps=$(( (max - min) / step + 1 ))
60                 else
61                         reps=0
62                 fi
63
64                 jot $reps $min $max $step || die
65                 return $?
66         fi
67
68         # Screw it, do the output ourselves
69         while :; do
70                 [[ $max -lt $min && $step -gt 0 ]] && break
71                 [[ $min -lt $max && $step -gt 0 ]] && break
72                 echo $min
73                 : $(( min += step ))
74         done
75         return 0
76 }
77
78 # @FUNCTION: dlopen_lib
79 # @USAGE:
80 # @RETURN: linker flag if needed
81 # @DESCRIPTION:
82 # Gets the linker flag to link to dlopen() function
83 dlopen_lib() {
84         # - Solaris needs nothing
85         # - Darwin needs nothing
86         # - *BSD needs nothing
87         # - Linux needs -ldl (glibc and uclibc)
88         # - Interix needs -ldl
89         case "${CHOST}" in
90                 *-linux-gnu*|*-linux-uclibc|*-interix*)
91                         echo "-ldl"
92                 ;;
93         esac
94 }
95
96 # @FUNCTION: get_bmake
97 # @USAGE:
98 # @RETURN: system version of make
99 # @DESCRIPTION:
100 # Gets the name of the BSD-ish make command (bmake from NetBSD)
101 #
102 # This will return make (provided by system packages) for BSD userlands,
103 # or bsdmake for Darwin userlands and pmake for the rest of userlands,
104 # both of which are provided by sys-devel/pmake package.
105 #
106 # Note: the bsdmake for Darwin userland is with compatibility with MacOSX
107 # default name.
108 get_bmake() {
109         if [[ ${CBUILD:-${CHOST}} == *bsd* ]]; then
110                 echo make
111         elif [[ ${CBUILD:-${CHOST}} == *darwin* ]]; then
112                 echo bsdmake
113         else
114                 echo bmake
115         fi
116 }
117
118 # @FUNCTION: get_mounts
119 # @USAGE:
120 # @RETURN: table of mounts in form "point node fs opts"
121 # @MAINTAINER:
122 # @DESCRIPTION:
123 # Portable method of getting mount names and points.
124 # Returns as "point node fs options"
125 # Remember to convert 040 back to a space.
126 get_mounts() {
127         local point= node= fs= opts= foo=
128
129         # Linux has /proc/mounts which should always exist
130         if [[ $(uname -s) == "Linux" ]] ; then
131                 while read node point fs opts foo ; do
132                         echo "${point} ${node} ${fs} ${opts}"
133                 done < /proc/mounts
134                 return
135         fi
136
137         # OK, pray we have a -p option that outputs mounts in fstab format
138         # using tabs as the seperator.
139         # Then pray that there are no tabs in the either.
140         # Currently only FreeBSD supports this and the other BSDs will
141         # have to be patched.
142         # Athough the BSD's may support /proc, they do NOT put \040 in place
143         # of the spaces and we should not force a /proc either.
144         local IFS=$'\t'
145         LC_ALL=C mount -p | while read node point fs foo ; do
146                 opts=${fs#* }
147                 fs=${fs%% *}
148                 echo "${point// /\040} ${node// /\040} ${fs%% *} ${opts// /\040}"
149         done
150 }
151
152 _dead_portability_user_funcs() { die "if you really need this, please file a bug for base-system@gentoo.org"; }
153 is-login-disabled() { _dead_portability_user_funcs; }
154
155 fi