/[gentoo-x86]/eclass/eutils.eclass
Gentoo

Contents of /eclass/eutils.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (show annotations) (download)
Thu Mar 6 20:35:26 2003 UTC (11 years, 4 months ago) by azarah
Branch: MAIN
Changes since 1.25: +48 -1 lines
add have_NPTL

1 # Copyright 1999-2003 Gentoo Technologies, Inc.
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /home/cvsroot/gentoo-x86/eclass/eutils.eclass,v 1.25 2003/03/03 21:42:05 vapier Exp $
4 #
5 # Author: Martin Schlemmer <azarah@gentoo.org>
6 #
7 # This eclass is for general purpose functions that most ebuilds
8 # have to implement themselves.
9 #
10 # NB: If you add anything, please comment it!
11
12 ECLASS=eutils
13 INHERITED="$INHERITED $ECLASS"
14
15 newdepend "!bootstrap? ( sys-devel/patch )"
16
17 DESCRIPTION="Based on the ${ECLASS} eclass"
18
19 # This function generate linker scripts in /usr/lib for dynamic
20 # libs in /lib. This is to fix linking problems when you have
21 # the .so in /lib, and the .a in /usr/lib. What happens is that
22 # in some cases when linking dynamic, the .a in /usr/lib is used
23 # instead of the .so in /lib due to gcc/libtool tweaking ld's
24 # library search path. This cause many builds to fail.
25 # See bug #4411 for more info.
26 #
27 # To use, simply call:
28 #
29 # gen_usr_ldscript libfoo.so
30 #
31 # Note that you should in general use the unversioned name of
32 # the library, as ldconfig should usually update it correctly
33 # to point to the latest version of the library present.
34 #
35 # <azarah@gentoo.org> (26 Oct 2002)
36 #
37 gen_usr_ldscript() {
38
39 # Just make sure it exists
40 dodir /usr/lib
41
42 cat > ${D}/usr/lib/$1 <<"END_LDSCRIPT"
43 /* GNU ld script
44 Because Gentoo have critical dynamic libraries
45 in /lib, and the static versions in /usr/lib, we
46 need to have a "fake" dynamic lib in /usr/lib,
47 otherwise we run into linking problems.
48 See bug #4411 on http://bugs.gentoo.org/ for
49 more info. */
50 GROUP ( /lib/libxxx )
51 END_LDSCRIPT
52
53 dosed "s:libxxx:$1:" /usr/lib/$1
54
55 return 0
56 }
57
58 # Simple function to draw a line consisting of '=' the same length as $*
59 #
60 # <azarah@gentoo.org> (11 Nov 2002)
61 #
62 draw_line() {
63 local i=0
64 local str_length=""
65
66 # Handle calls that do not have args, or wc not being installed ...
67 if [ -z "$1" -o ! -x "$(which wc 2>/dev/null)" ]
68 then
69 echo "==============================================================="
70 return 0
71 fi
72
73 # Get the length of $*
74 str_length="$(echo -n "$*" | wc -m)"
75
76 while [ "$i" -lt "${str_length}" ]
77 do
78 echo -n "="
79
80 i=$((i + 1))
81 done
82
83 echo
84
85 return 0
86 }
87
88 # Default directory where patches are located
89 EPATCH_SOURCE="${WORKDIR}/patch"
90 # Default extension for patches
91 EPATCH_SUFFIX="patch.bz2"
92 # Default options for patch
93 EPATCH_OPTS=""
94 # List of patches not to apply. Not this is only file names,
95 # and not the full path ..
96 EPATCH_EXCLUDE=""
97 # Change the printed message for a single patch.
98 EPATCH_SINGLE_MSG=""
99
100 # This function is for bulk patching, or in theory for just one
101 # or two patches.
102 #
103 # It should work with .bz2, .gz, .zip and plain text patches.
104 # Currently all patches should be the same format.
105 #
106 # You do not have to specify '-p' option to patch, as it will
107 # try with -p0 to -p5 until it succeed, or fail at -p5.
108 #
109 # Above EPATCH_* variables can be used to control various defaults,
110 # bug they should be left as is to ensure an ebuild can rely on
111 # them for.
112 #
113 # Patches are applied in current directory.
114 #
115 # Bulk Patches should preferibly have the form of:
116 #
117 # ??_${ARCH}_foo.${EPATCH_SUFFIX}
118 #
119 # For example:
120 #
121 # 01_all_misc-fix.patch.bz2
122 # 02_sparc_another-fix.patch.bz2
123 #
124 # This ensures that there are a set order, and you can have ARCH
125 # specific patches.
126 #
127 # If you however give an argument to epatch(), it will treat it as a
128 # single patch that need to be applied if its a file. If on the other
129 # hand its a directory, it will set EPATCH_SOURCE to this.
130 #
131 # <azarah@gentoo.org> (10 Nov 2002)
132 #
133 epatch() {
134 local PIPE_CMD=""
135 local STDERR_TARGET="${T}/$$.out"
136 local PATCH_TARGET="${T}/$$.patch"
137 local PATCH_SUFFIX=""
138 local SINGLE_PATCH="no"
139 local x=""
140
141 if [ "$#" -gt 1 ]
142 then
143 eerror "Invalid arguments to epatch()"
144 die "Invalid arguments to epatch()"
145 fi
146
147 if [ -n "$1" -a -f "$1" ]
148 then
149 SINGLE_PATCH="yes"
150
151 local EPATCH_SOURCE="$1"
152 local EPATCH_SUFFIX="${1##*\.}"
153
154 elif [ -n "$1" -a -d "$1" ]
155 then
156 local EPATCH_SOURCE="$1/*.${EPATCH_SUFFIX}"
157 else
158 if [ ! -d ${EPATCH_SOURCE} ]
159 then
160 if [ -n "$1" -a "${EPATCH_SOURCE}" = "${WORKDIR}/patch" ]
161 then
162 EPATCH_SOURCE="$1"
163 fi
164
165 echo
166 eerror "Cannot find \$EPATCH_SOURCE! Value for \$EPATCH_SOURCE is:"
167 eerror
168 eerror " ${EPATCH_SOURCE}"
169 echo
170 die "Cannot find \$EPATCH_SOURCE!"
171 fi
172
173 local EPATCH_SOURCE="${EPATCH_SOURCE}/*.${EPATCH_SUFFIX}"
174 fi
175
176 case ${EPATCH_SUFFIX##*\.} in
177 bz2)
178 PIPE_CMD="bzip2 -dc"
179 PATCH_SUFFIX="bz2"
180 ;;
181 gz|Z|z)
182 PIPE_CMD="gzip -dc"
183 PATCH_SUFFIX="gz"
184 ;;
185 ZIP|zip)
186 PIPE_CMD="unzip -p"
187 PATCH_SUFFIX="zip"
188 ;;
189 *)
190 PIPE_CMD="cat"
191 PATCH_SUFFIX="patch"
192 ;;
193 esac
194
195 if [ "${SINGLE_PATCH}" = "no" ]
196 then
197 einfo "Applying various patches (bugfixes/updates)..."
198 fi
199 for x in ${EPATCH_SOURCE}
200 do
201 # New ARCH dependant patch naming scheme...
202 #
203 # ???_arch_foo.patch
204 #
205 if [ -f ${x} ] && \
206 [ "${SINGLE_PATCH}" = "yes" -o "${x/_all_}" != "${x}" -o "`eval echo \$\{x/_${ARCH}_\}`" != "${x}" ]
207 then
208 local count=0
209 local popts="${EPATCH_OPTS}"
210
211 if [ -n "${EPATCH_EXCLUDE}" ]
212 then
213 if [ "`eval echo \$\{EPATCH_EXCLUDE/${x##*/}\}`" != "${EPATCH_EXCLUDE}" ]
214 then
215 continue
216 fi
217 fi
218
219 if [ "${SINGLE_PATCH}" = "yes" ]
220 then
221 if [ -n "${EPATCH_SINGLE_MSG}" ]
222 then
223 einfo "${EPATCH_SINGLE_MSG}"
224 else
225 einfo "Applying ${x##*/}..."
226 fi
227 else
228 einfo " ${x##*/}..."
229 fi
230
231 echo "***** ${x##*/} *****" > ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
232 echo >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
233
234 # Allow for prefix to differ ... im lazy, so shoot me :/
235 while [ "${count}" -lt 5 ]
236 do
237 # Generate some useful debug info ...
238 draw_line "***** ${x##*/} *****" >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
239 echo >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
240
241 if [ "${PATCH_SUFFIX}" != "patch" ]
242 then
243 echo -n "PIPE_COMMAND: " >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
244 echo "${PIPE_CMD} ${x} > ${PATCH_TARGET}" >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
245 else
246 PATCH_TARGET="${x}"
247 fi
248
249 echo -n "PATCH COMMAND: " >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
250 echo "patch ${popts} -p${count} < ${PATCH_TARGET}" >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
251
252 echo >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
253 draw_line "***** ${x##*/} *****" >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
254
255 if [ "${PATCH_SUFFIX}" != "patch" ]
256 then
257 if ! (${PIPE_CMD} ${x} > ${PATCH_TARGET}) >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/} 2>&1
258 then
259 echo
260 eerror "Could not extract patch!"
261 #die "Could not extract patch!"
262 count=5
263 break
264 fi
265 fi
266
267 if (cat ${PATCH_TARGET} | patch ${popts} --dry-run -f -p${count}) >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/} 2>&1
268 then
269 draw_line "***** ${x##*/} *****" > ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real
270 echo >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real
271 echo "ACTUALLY APPLYING ${x##*/}..." >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real
272 echo >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real
273 draw_line "***** ${x##*/} *****" >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real
274
275 cat ${PATCH_TARGET} | patch ${popts} -p${count} >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real 2>&1
276
277 if [ "$?" -ne 0 ]
278 then
279 cat ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real >> ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
280 echo
281 eerror "A dry-run of patch command succeeded, but actually"
282 eerror "applying the patch failed!"
283 #die "Real world sux compared to the dreamworld!"
284 count=5
285 fi
286
287 rm -f ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}.real
288
289 break
290 fi
291
292 count=$((count + 1))
293 done
294
295 if [ "${PATCH_SUFFIX}" != "patch" ]
296 then
297 rm -f ${PATCH_TARGET}
298 fi
299
300 if [ "${count}" -eq 5 ]
301 then
302 echo
303 eerror "Failed Patch: ${x##*/}!"
304 eerror
305 eerror "Include in your bugreport the contents of:"
306 eerror
307 eerror " ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}"
308 echo
309 die "Failed Patch: ${x##*/}!"
310 fi
311
312 rm -f ${STDERR_TARGET%/*}/${x##*/}-${STDERR_TARGET##*/}
313
314 eend 0
315 fi
316 done
317 if [ "${SINGLE_PATCH}" = "no" ]
318 then
319 einfo "Done with patching"
320 fi
321 }
322
323 # This function return true if we are using the NPTL pthreads
324 # implementation.
325 #
326 # <azarah@gentoo.org> (06 March 2003)
327 #
328
329 have_NPTL() {
330
331 cat > ${T}/test-nptl.c <<-"END"
332 #define _XOPEN_SOURCE
333 #include <unistd.h>
334 #include <stdio.h>
335
336 int main()
337 {
338 char buf[255];
339 char *str = buf;
340
341 confstr(_CS_GNU_LIBPTHREAD_VERSION, str, 255);
342 if (NULL != str) {
343 printf("%s\n", str);
344 if (NULL != strstr(str, "NPTL"))
345 return 0;
346 }
347
348 return 1;
349 }
350 END
351
352 einfon "Checking for _CS_GNU_LIBPTHREAD_VERSION support in glibc ... "
353 if gcc -o ${T}/nptl ${T}/test-nptl.c &> /dev/null
354 then
355 echo "yes"
356 einfon "Checking what PTHREADS implementation we have ... "
357 if ${T}/nptl
358 then
359 return 0
360 else
361 return 1
362 fi
363 else
364 echo "no"
365 fi
366
367 return 1
368 }
369
370 # This function check how many cpu's are present, and then set
371 # -j in MAKEOPTS accordingly.
372 #
373 # Thanks to nall <nall@gentoo.org> for this.
374 #
375 get_number_of_jobs() {
376 local jobs=0
377
378 if [ ! -r /proc/cpuinfo ]
379 then
380 return 1
381 fi
382
383 # This bit is from H?kan Wessberg <nacka-gentoo@refug.org>, bug #13565.
384 if [ "`egrep "^[[:space:]]*MAKEOPTS=" /etc/make.conf | wc -l`" -gt 0 ]
385 then
386 ADMINOPTS="`egrep "^[[:space:]]*MAKEOPTS=" /etc/make.conf | cut -d= -f2 | sed 's/\"//g'`"
387 ADMINPARAM="`echo ${ADMINOPTS} | gawk '{match($0, /-j *[0-9]*/, opt); print opt[0]}'`"
388 ADMINPARAM="${ADMINPARAM/-j}"
389 fi
390
391 export MAKEOPTS="`echo ${MAKEOPTS} | sed -e 's:-j *[0-9]*::g'`"
392
393 if [ "${ARCH}" = "x86" -o "${ARCH}" = "hppa" -o \
394 "${ARCH}" = "arm" -o "${ARCH}" = "mips" ]
395 then
396 # these archs will always have "[Pp]rocessor"
397 jobs="$((`grep -c ^[Pp]rocessor /proc/cpuinfo` * 2))"
398
399 elif [ "${ARCH}" = "sparc" -o "${ARCH}" = "sparc64" ]
400 then
401 # sparc always has "ncpus active"
402 jobs="$((`grep "^ncpus active" /proc/cpuinfo | sed -e "s/^.*: //"` * 2))"
403
404 elif [ "${ARCH}" = "alpha" ]
405 then
406 # alpha has "cpus active", but only when compiled with SMP
407 if [ "`grep -c "^cpus active" /proc/cpuinfo`" -eq 1 ]
408 then
409 jobs="$((`grep "^cpus active" /proc/cpuinfo | sed -e "s/^.*: //"` * 2))"
410 else
411 jobs=2
412 fi
413
414 elif [ "${ARCH}" = "ppc" ]
415 then
416 # ppc has "processor", but only when compiled with SMP
417 if [ "`grep -c "^processor" /proc/cpuinfo`" -eq 1 ]
418 then
419 jobs="$((`grep -c ^processor /proc/cpuinfo` * 2))"
420 else
421 jobs=2
422 fi
423 else
424 jobs="$((`grep -c ^cpu /proc/cpuinfo` * 2))"
425 die "Unknown ARCH -- ${ARCH}!"
426 fi
427
428 # Make sure the number is valid ...
429 if [ "${jobs}" -lt 1 ]
430 then
431 jobs=1
432 fi
433
434 if [ -n "${ADMINPARAM}" ]
435 then
436 if [ "${jobs}" -gt "${ADMINPARAM}" ]
437 then
438 einfo "Setting make jobs to \"-j${ADMINPARAM}\" to ensure successful merge..."
439 export MAKEOPTS="${MAKEOPTS} -j${ADMINPARAM}"
440 else
441 einfo "Setting make jobs to \"-j${jobs}\" to ensure successful merge..."
442 export MAKEOPTS="${MAKEOPTS} -j${jobs}"
443 fi
444 fi
445 }
446
447 # Simplify/standardize adding users to the system
448 # vapier@gentoo.org
449 #
450 # enewuser(username, uid, shell, homedir, groups, extra options)
451 #
452 # Default values if you do not specify any:
453 # username: REQUIRED !
454 # uid: next available (see useradd(8))
455 # shell: /bin/false
456 # homedir: /dev/null
457 # groups: none
458 # extra: comment of 'added by portage for ${PN}'
459 enewuser() {
460 # get the username
461 local euser="$1"; shift
462 if [ -z "${euser}" ] ; then
463 eerror "No username specified !"
464 die "Cannot call enewuser without a username"
465 fi
466 einfo "Adding user '${euser}' to your system ..."
467
468 # setup a file for testing usernames/groups
469 local tmpfile="`mktemp -p ${T}`"
470 touch ${tmpfile}
471 chown ${euser} ${tmpfile} >& /dev/null
472 local realuser="`ls -l ${tmpfile} | awk '{print $3}'`"
473
474 # see if user already exists
475 if [ "${euser}" == "${realuser}" ] ; then
476 einfo "${euser} already exists on your system :)"
477 return 0
478 fi
479
480 # options to pass to useradd
481 local opts=""
482
483 # handle uid
484 local euid="$1"; shift
485 if [ ! -z "${euid}" ] ; then
486 if [ ${euid} -gt 0 ] ; then
487 opts="${opts} -u ${euid}"
488 else
489 eerror "Userid given but is not greater than 0 !"
490 die "${euid} is not a valid UID"
491 fi
492 else
493 euid="next available"
494 fi
495 einfo " - Userid: ${euid}"
496
497 # handle shell
498 local eshell="$1"; shift
499 if [ ! -z "${eshell}" ] ; then
500 if [ ! -e ${eshell} ] ; then
501 eerror "A shell was specified but it does not exist !"
502 die "${eshell} does not exist"
503 fi
504 else
505 eshell=/bin/false
506 fi
507 einfo " - Shell: ${eshell}"
508 opts="${opts} -s ${eshell}"
509
510 # handle homedir
511 local ehome="$1"; shift
512 if [ -z "${ehome}" ] ; then
513 ehome=/dev/null
514 fi
515 einfo " - Home: ${ehome}"
516 opts="${opts} -d ${ehome}"
517
518 # handle groups
519 local egroups="$1"; shift
520 if [ ! -z "${egroups}" ] ; then
521 local realgroup
522 local oldifs="${IFS}"
523 export IFS=","
524 for g in ${egroups} ; do
525 chgrp ${g} ${tmpfile} >& /dev/null
526 realgroup="`ls -l ${tmpfile} | awk '{print $4}'`"
527 if [ "${g}" != "${realgroup}" ] ; then
528 eerror "You must add ${g} to the system first"
529 die "${g} is not a valid GID"
530 fi
531 done
532 export IFS="${oldifs}"
533 opts="${opts} -g ${egroups}"
534 else
535 egroups="(none)"
536 fi
537 einfo " - Groups: ${egroups}"
538
539 # handle extra and add the user
540 local eextra="$@"
541 local oldsandbox="${oldsandbox}"
542 export SANDBOX_ON="0"
543 if [ -z "${eextra}" ] ; then
544 useradd ${opts} ${euser} \
545 -c "added by portage for ${PN}" \
546 || die "enewuser failed"
547 else
548 einfo " - Extra: ${eextra}"
549 useradd ${opts} ${euser} ${eextra} \
550 || die "enewuser failed"
551 fi
552 export SANDBOX_ON="${oldsandbox}"
553
554 if [ ! -e ${ehome} ] && [ ! -e ${D}/${ehome} ] ; then
555 einfo " - Creating ${ehome} in ${D}"
556 dodir ${ehome}
557 fperms ${euser} ${ehome}
558 fi
559 }
560
561 # Simplify/standardize adding groups to the system
562 # vapier@gentoo.org
563 #
564 # enewgroup(group, gid)
565 #
566 # Default values if you do not specify any:
567 # groupname: REQUIRED !
568 # gid: next available (see groupadd(8))
569 # extra: none
570 enewgroup() {
571 # get the group
572 local egroup="$1"; shift
573 if [ -z "${egroup}" ] ; then
574 eerror "No group specified !"
575 die "Cannot call enewgroup without a group"
576 fi
577 einfo "Adding group '${egroup}' to your system ..."
578
579 # setup a file for testing groupname
580 local tmpfile="`mktemp -p ${T}`"
581 touch ${tmpfile}
582 chgrp ${egroup} ${tmpfile} >& /dev/null
583 local realgroup="`ls -l ${tmpfile} | awk '{print $4}'`"
584
585 # see if group already exists
586 if [ "${egroup}" == "${realgroup}" ] ; then
587 einfo "${egroup} already exists on your system :)"
588 return 0
589 fi
590
591 # options to pass to useradd
592 local opts=""
593
594 # handle gid
595 local egid="$1"; shift
596 if [ ! -z "${egid}" ] ; then
597 if [ ${egid} -gt 0 ] ; then
598 opts="${opts} -g ${egid}"
599 else
600 eerror "Groupid given but is not greater than 0 !"
601 die "${egid} is not a valid GID"
602 fi
603 else
604 egid="next available"
605 fi
606 einfo " - Groupid: ${egid}"
607
608 # handle extra
609 local eextra="$@"
610 opts="${opts} ${eextra}"
611
612 # add the group
613 local oldsandbox="${oldsandbox}"
614 export SANDBOX_ON="0"
615 groupadd ${opts} ${egroup} || die "enewgroup failed"
616 export SANDBOX_ON="${oldsandbox}"
617 }
618
619 # Simple script to replace 'dos2unix' binaries
620 # vapier@gentoo.org
621 #
622 # edos2unix(file, <more files>...)
623 edos2unix() {
624 for f in $@ ; do
625 cp ${f} ${T}/
626 sed 's/\r$//' ${T}/${f} > ${f}
627 done
628 }

  ViewVC Help
Powered by ViewVC 1.1.20