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