media-sound/snapcast: USE flag avahi > zeroconf
[gentoo.git] / eclass / freebsd.eclass
1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 #
4 # @MAINTAINER:
5 # maintainer-needed@gentoo.org
6 # @AUTHOR:
7 # Diego Pettenò <flameeyes@gentoo.org>
8
9 inherit versionator eutils flag-o-matic bsdmk
10
11 # Drop patch level from ${PV}
12 MY_PV=${PV/_p*}
13 PLEVEL=${PV##*_p}
14
15 LICENSE="BSD"
16 HOMEPAGE="http://www.freebsd.org/"
17
18 # Define global package names
19 LIB="freebsd-lib-${PV}"
20 BIN="freebsd-bin-${PV}"
21 CONTRIB="freebsd-contrib-${PV}"
22 SHARE="freebsd-share-${PV}"
23 UBIN="freebsd-ubin-${PV}"
24 USBIN="freebsd-usbin-${PV}"
25 CRYPTO="freebsd-crypto-${PV}"
26 LIBEXEC="freebsd-libexec-${PV}"
27 SBIN="freebsd-sbin-${PV}"
28 GNU="freebsd-gnu-${PV}"
29 ETC="freebsd-etc-${PV}"
30 SYS="freebsd-sys-${PV}"
31 INCLUDE="freebsd-include-${PV}"
32 RESCUE="freebsd-rescue-${PV}"
33 CDDL="freebsd-cddl-${PV}"
34 SECURE="freebsd-secure-${PV}"
35
36 # Release version (5.3, 5.4, 6.0, etc)
37 RV="$(get_version_component_range 1-2 ${MY_PV})"
38
39 # SVN ebuild support.
40 #   9.1.0.9999 -->      release/9.1.0
41 #       9.1.9999   -->  releng/9.1
42 #   9.9999     -->      stable/9
43 #       9999 -->        head
44 #
45 # svn revision can be specified by patch level:
46 #       freebsd-lib-9.9999_p247000 --> set svn -r 247000
47
48 if [[ ${MY_PV} == *9999* ]]; then
49         inherit subversion
50
51         # Set SVN revision using patch level.
52         [[ ${PV} == *_p* ]] && ESVN_REVISION="${PLEVEL}"
53
54         case ${MY_PV%.9999} in
55                 *.*.*)  BRANCH="release";;
56                 *.*)    BRANCH="releng" ;;
57                 9999)   BRANCH="head"   ;;
58                 *)          BRANCH="stable"     ;;
59         esac
60
61         if [[ ${BRANCH} == head ]]  ; then
62                 SVN_SUB_URI="${BRANCH}"
63         else
64                 SVN_SUB_URI="${BRANCH}/${MY_PV%.9999}"
65         fi
66
67         ESVN_REPO_URI="svn://svn.freebsd.org/base/${SVN_SUB_URI}"
68         ESVN_PROJECT="freebsd-${BRANCH}"
69 fi
70
71 # Use the original source code.
72 if [[ ${MY_PV} != *9999* ]] && version_is_at_least 10.0 ${RV} ; then
73         DL_PV=${MY_PV/_rc/-RC}
74         DL_PV=${DL_PV/_beta/-BETA}
75         DL_PV=${DL_PV/_alpha/-ALPHA}
76         if [[ ${DL_PV} == ${MY_PV} ]]; then
77                 DL_PV="${DL_PV}-RELEASE"
78         fi
79         SRC_URI="mirror://freebsd/releases/i386/${DL_PV}/src.txz -> freebsd-src-${MY_PV}.tar.xz"
80 fi
81
82 IUSE="profile"
83
84 #unalias -a
85 alias install-info='/usr/bin/bsdinstall-info'
86
87 EXPORT_FUNCTIONS src_compile src_install src_unpack
88
89 # doperiodic <kind> <file> ...
90 doperiodic() {
91         local kind=$1
92         shift
93
94         ( # dont want to pollute calling env
95                 insinto /etc/periodic/${kind}
96                 insopts -m 0755
97                 doins "$@"
98         )
99 }
100
101 freebsd_get_bmake() {
102         local bmake
103         bmake=$(get_bmake)
104         if version_is_at_least 11.0 ${RV} ; then
105                 if [[ ${CBUILD} == *-freebsd* ]] ; then
106                         bmake="${bmake} -m /usr/share/mk/system"
107                 else
108                         bmake="${bmake} -m /usr/share/mk/freebsd/system"
109                 fi
110         else
111                 [[ ${CBUILD} == *-freebsd* ]] || bmake="${bmake} -m /usr/share/mk/freebsd"
112         fi
113
114         echo "${bmake}"
115 }
116
117 # Generates SRC_URI or DISTDIR for the upstream patch.
118 freebsd_upstream_patches() {
119         local opt=$1
120         [[ ${#UPSTREAM_PATCHES[@]} -eq 0 ]] && return 1
121         for x in "${UPSTREAM_PATCHES[@]}"
122         do
123                 local out=${PN}-${x/\//-}
124                 out=${out/:/}
125                 if [[ ${opt} == -s ]] ; then
126                         echo "${DISTDIR}/${out}"
127                 else
128                         echo "https://security.freebsd.org/patches/${x} -> ${out}"
129                 fi
130         done
131 }
132
133 freebsd_do_patches() {
134         if [[ ${#PATCHES[@]} -gt 1 ]] ; then
135                 for x in "${PATCHES[@]}"; do
136                         epatch "${x}"
137                 done
138         else
139                 for x in ${PATCHES} ; do
140                         epatch "${x}"
141                 done
142         fi
143
144         # Upstream patches need to be applied on WORKDIR.
145         if [[ ${#UPSTREAM_PATCHES[@]} -gt 0 ]] ; then
146                 cd "${WORKDIR}" || die
147                 epatch $(freebsd_upstream_patches -s)
148                 cd "${S}" || die
149         fi
150         epatch_user
151 }
152
153 freebsd_rename_libraries() {
154         ebegin "Renaming libraries"
155         # We don't use libtermcap, we use libncurses
156         find "${S}" -name Makefile -print0 | xargs -0 \
157                 sed -i -e 's:-ltermcap:-lncurses:g; s:{LIBTERMCAP}:{LIBNCURSES}:g'
158         # flex provides libfl, not libl
159         find "${S}" -name Makefile -print0 | xargs -0 \
160                 sed -i -e 's:-ll$:-lfl:g; s:-ll :-lfl :g; s:{LIBL}:{LIBFL}:g'
161         # ncurses provides libncursesw not libcursesw
162         find "${S}" -name Makefile -print0 | xargs -0 \
163                 sed -i -e 's:-lcursesw:-lncursesw:g'
164         # we use expat instead of bsdxml
165         find "${S}" -name Makefile -print0 | xargs -0 \
166                 sed -i -e 's:-lbsdxml:-lexpat:g'
167
168         eend $?
169 }
170
171 freebsd_src_unpack() {
172         if [[ ${MY_PV} == *9999* ]]; then
173                 S="${WORKDIR}" subversion_src_unpack
174
175                 # When share/mk exists in ${WORKDIR}, BSD's make will try to use it on FreeBSD 10.0 or later.
176                 # We should remove "${WORKDIR}"/share/mk/*.mk to use /usr/share/mk{,/system}.
177                 if [[ ${PN} != freebsd-mk-defs ]] ; then
178                         [[ -e "${WORKDIR}"/share/mk ]] && rm -rf "${WORKDIR}"/share/mk/*.mk
179                 fi
180         else
181                 if version_is_at_least 10.0 ${RV} ; then
182                         local tarball="freebsd-src-${MY_PV}.tar.xz"
183                         local topdir="usr/src/"
184                         local extractlist=()
185                         for i in ${EXTRACTONLY} tools/ ; do
186                                 extractlist+=( ${topdir}${i} )
187                         done
188                         ebegin "Unpacking parts of ${tarball} to ${WORKDIR}"
189                         cd "${WORKDIR}" || die
190                         tar -xJpf "${DISTDIR}/${tarball}" --strip-components=2 "${extractlist[@]}" 2> /dev/null || die "tar extract command failed"
191                         cd - || die
192                 else
193                         for f in ${A} ; do
194                                 [[ ${f} == *.tar.* ]] && unpack ${f}
195                         done
196                 fi
197         fi
198         cd "${S}"
199
200         dummy_mk ${REMOVE_SUBDIRS}
201
202         freebsd_do_patches
203         if ! version_is_at_least 11.0 ${RV} ; then
204                 freebsd_rename_libraries
205         fi
206
207         # Starting from FreeBSD 9.2, its install command supports the -l option and
208         # they now use it. Emulate it if we are on a system that does not have it.
209         if version_is_at_least 9.2 ${RV} && ! has_version '>=sys-freebsd/freebsd-ubin-9.2_beta1' ; then
210                 export INSTALL_LINK="ln -f"
211                 export INSTALL_SYMLINK="ln -fs"
212         fi
213         # An older version of install command doesn't support the -T option.
214         if version_is_at_least 11.0 ${RV} && ! has_version ">=sys-freebsd/freebsd-ubin-${RV}" ; then
215                 export INSTALL="sh ${WORKDIR}/tools/install.sh"
216         fi
217
218         # If CC=clang, we should use clang-cpp instead of cpp. #478810, #595878
219         if [[ $(tc-getCC) == *clang* ]] ; then
220                 if type -P clang-cpp > /dev/null ; then
221                         export CPP=clang-cpp
222                 else
223                         mkdir "${WORKDIR}"/workaround_clang-cpp || die "Could not create ${WORKDIR}/workaround_clang-cpp"
224                         ln -s "$(type -P clang)" "${WORKDIR}"/workaround_clang-cpp/clang-cpp || die "Could not create clang-cpp symlink."
225                         export CPP="${WORKDIR}/workaround_clang-cpp/clang-cpp"
226                 fi
227         fi
228
229         # Add the special CFLAGS required for multilib support.
230         use amd64-fbsd && export CFLAGS_x86_fbsd="${CFLAGS_x86_fbsd} -DCOMPAT_32BIT -B/usr/lib32 -L/usr/lib32"
231 }
232
233 freebsd_src_compile() {
234         use profile && filter-flags "-fomit-frame-pointer"
235         if version_is_at_least 11.0 ${RV} ; then
236                 if ! use profile ; then
237                         mymakeopts="${mymakeopts} WITHOUT_PROFILE= "
238                 fi
239                 # Disable the debugging information, use FEATURES=splitdebug instead.
240                 mymakeopts="${mymakeopts} WITHOUT_DEBUG_FILES= "
241                 # We don't support test yet.
242                 mymakeopts="${mymakeopts} WITHOUT_TESTS= "
243                 # Set the SRCTOP to detect the source directory.
244                 mymakeopts="${mymakeopts} SRCTOP=${WORKDIR} "
245                 # Set the common settings.
246                 mymakeopts="${mymakeopts} WITHOUT_MANCOMPRESS= WITHOUT_INFOCOMPRESS= "
247         else
248                 use profile || mymakeopts="${mymakeopts} NO_PROFILE= "
249                 mymakeopts="${mymakeopts} NO_MANCOMPRESS= NO_INFOCOMPRESS= "
250         fi
251
252         mymakeopts="${mymakeopts} NO_FSCHG="
253
254         # Make sure to use FreeBSD definitions while crosscompiling
255         [[ -z "${BMAKE}" ]] && BMAKE="$(freebsd_get_bmake)"
256
257         # Create objdir if MAKEOBJDIRPREFIX is defined, so that we can make out of
258         # tree builds easily.
259         if [[ -n "${MAKEOBJDIRPREFIX}" ]] ; then
260                 mkmake obj || die
261         fi
262
263         bsdmk_src_compile "$@"
264 }
265
266 # Helper function to make a multilib build with FreeBSD Makefiles.
267 # Usage:
268 # MULTIBUILD_VARIANTS=( $(get_all_abis) )
269 # multibuild_foreach_variant freebsd_multilib_multibuild_wrapper my_function
270 #
271 # Important note: To use this function you _have_ to:
272 # - inherit multilib.eclass and multibuild.eclass
273 # - set MULTIBUILD_VARIANTS
274
275 freebsd_multilib_multibuild_wrapper() {
276         # Get the ABI from multibuild.eclass
277         # This assumes MULTIBUILD_VARIANTS contains only valid ABIs.
278         local ABI=${MULTIBUILD_VARIANT}
279
280         # First, save the variables: CFLAGS, CXXFLAGS, LDFLAGS, LDADD and mymakeopts.
281         for i in CFLAGS CXXFLAGS LDFLAGS LDADD mymakeopts ; do
282                 export ${i}_SAVE="${!i}"
283         done
284
285         # Setup the variables specific to this ABI.
286         multilib_toolchain_setup "${ABI}"
287
288         local target="$(tc-arch-kernel ${CHOST})"
289         mymakeopts="${mymakeopts} TARGET=${target} MACHINE=${target} MACHINE_ARCH=${target} SHLIBDIR=/usr/$(get_libdir) LIBDIR=/usr/$(get_libdir)"
290         if [ "${ABI}" != "${DEFAULT_ABI}" ] ; then
291                 mymakeopts="${mymakeopts} COMPAT_32BIT="
292         fi
293
294         einfo "Building for ABI=${ABI} and TARGET=${target}"
295
296         export MAKEOBJDIRPREFIX="${BUILD_DIR}"
297         if [ ! -d "${MAKEOBJDIRPREFIX}" ] ; then
298                 mkdir "${MAKEOBJDIRPREFIX}" || die "Could not create ${MAKEOBJDIRPREFIX}."
299         fi
300
301         CTARGET="${CHOST}" "$@"
302
303         # Restore the variables now.
304         for i in CFLAGS CXXFLAGS LDFLAGS LDADD mymakeopts ; do
305                 ii="${i}_SAVE"
306                 export ${i}="${!ii}"
307         done
308 }
309
310 freebsd_src_install() {
311         if version_is_at_least 11.0 ${RV} ; then
312                 if ! use profile ; then
313                         mymakeopts="${mymakeopts} WITHOUT_PROFILE= "
314                 fi
315                 # Disable the debugging information, use FEATURES=splitdebug instead.
316                 mymakeopts="${mymakeopts} WITHOUT_DEBUG_FILES= "
317                 # We don't support test yet.
318                 mymakeopts="${mymakeopts} WITHOUT_TESTS= "
319                 # Set the SRCTOP to detect the source directory.
320                 mymakeopts="${mymakeopts} SRCTOP=${WORKDIR} "
321                 # Set the common settings.
322                 mymakeopts="${mymakeopts} WITHOUT_MANCOMPRESS= WITHOUT_INFOCOMPRESS= "
323         else
324                 use profile || mymakeopts="${mymakeopts} NO_PROFILE= "
325                 mymakeopts="${mymakeopts} NO_MANCOMPRESS= NO_INFOCOMPRESS= "
326         fi
327
328         mymakeopts="${mymakeopts} NO_FSCHG="
329
330         [[ -z "${BMAKE}" ]] && BMAKE="$(freebsd_get_bmake)"
331
332         bsdmk_src_install "$@"
333 }