portability.eclass 3.8 KB

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