openmoko-session2: add RDEPENDS openmoko-panel-memory and oe-stylize
[vuplus_openembedded] / classes / base.bbclass
1 BB_DEFAULT_TASK ?= "build"
2
3 # like os.path.join but doesn't treat absolute RHS specially
4 def base_path_join(a, *p):
5     path = a
6     for b in p:
7         if path == '' or path.endswith('/'):
8             path +=  b
9         else:
10             path += '/' + b
11     return path
12
13 # for MD5/SHA handling
14 def base_chk_load_parser(config_path):
15     import ConfigParser, os, bb
16     parser = ConfigParser.ConfigParser()
17     if not len(parser.read(config_path)) == 1:
18         bb.note("Can not open the '%s' ini file" % config_path)
19         raise Exception("Can not open the '%s'" % config_path)
20
21     return parser
22
23 def base_chk_file(parser, pn, pv, src_uri, localpath, data):
24     import os, bb
25     no_checksum = False
26     # Try PN-PV-SRC_URI first and then try PN-SRC_URI
27     # we rely on the get method to create errors
28     pn_pv_src = "%s-%s-%s" % (pn,pv,src_uri)
29     pn_src    = "%s-%s" % (pn,src_uri)
30     if parser.has_section(pn_pv_src):
31         md5    = parser.get(pn_pv_src, "md5")
32         sha256 = parser.get(pn_pv_src, "sha256")
33     elif parser.has_section(pn_src):
34         md5    = parser.get(pn_src, "md5")
35         sha256 = parser.get(pn_src, "sha256")
36     elif parser.has_section(src_uri):
37         md5    = parser.get(src_uri, "md5")
38         sha256 = parser.get(src_uri, "sha256")
39     else:
40         no_checksum = True
41
42     # md5 and sha256 should be valid now
43     if not os.path.exists(localpath):
44         bb.note("The localpath does not exist '%s'" % localpath)
45         raise Exception("The path does not exist '%s'" % localpath)
46
47
48     # call md5(sum) and shasum
49     try:
50         md5pipe = os.popen('md5sum ' + localpath)
51         md5data = (md5pipe.readline().split() or [ "" ])[0]
52         md5pipe.close()
53     except OSError:
54         raise Exception("Executing md5sum failed")
55
56     try:
57         shapipe = os.popen('PATH=%s oe_sha256sum %s' % (bb.data.getVar('PATH', data, True), localpath))
58         shadata = (shapipe.readline().split() or [ "" ])[0]
59         shapipe.close()
60     except OSError:
61         raise Exception("Executing shasum failed")
62
63     if no_checksum == True:     # we do not have conf/checksums.ini entry
64         try:
65             file = open("%s/checksums.ini" % bb.data.getVar("TMPDIR", data, 1), "a")
66         except:
67             return False
68
69         if not file:
70             raise Exception("Creating checksums.ini failed")
71         
72         file.write("[%s]\nmd5=%s\nsha256=%s\n\n" % (src_uri, md5data, shadata))
73         file.close()
74         return False
75
76     if not md5 == md5data:
77         bb.note("The MD5Sums did not match. Wanted: '%s' and Got: '%s'" % (md5,md5data))
78         raise Exception("MD5 Sums do not match. Wanted: '%s' Got: '%s'" % (md5, md5data))
79
80     if not sha256 == shadata:
81         bb.note("The SHA256 Sums do not match. Wanted: '%s' Got: '%s'" % (sha256,shadata))
82         raise Exception("SHA256 Sums do not match. Wanted: '%s' Got: '%s'" % (sha256, shadata))
83
84     return True
85
86
87 def base_dep_prepend(d):
88         import bb
89         #
90         # Ideally this will check a flag so we will operate properly in
91         # the case where host == build == target, for now we don't work in
92         # that case though.
93         #
94         deps = "shasum-native "
95         if bb.data.getVar('PN', d, True) == "shasum-native":
96                 deps = ""
97
98         # INHIBIT_DEFAULT_DEPS doesn't apply to the patch command.  Whether or  not
99         # we need that built is the responsibility of the patch function / class, not
100         # the application.
101         if not bb.data.getVar('INHIBIT_DEFAULT_DEPS', d):
102                 if (bb.data.getVar('HOST_SYS', d, 1) !=
103                     bb.data.getVar('BUILD_SYS', d, 1)):
104                         deps += " virtual/${TARGET_PREFIX}gcc virtual/libc "
105         return deps
106
107 def base_read_file(filename):
108         import bb
109         try:
110                 f = file( filename, "r" )
111         except IOError, reason:
112                 return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
113         else:
114                 return f.read().strip()
115         return None
116
117 def base_conditional(variable, checkvalue, truevalue, falsevalue, d):
118         import bb
119         if bb.data.getVar(variable,d,1) == checkvalue:
120                 return truevalue
121         else:
122                 return falsevalue
123
124 def base_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
125         import bb
126         if float(bb.data.getVar(variable,d,1)) <= float(checkvalue):
127                 return truevalue
128         else:
129                 return falsevalue
130
131 def base_version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
132     import bb
133     result = bb.vercmp(bb.data.getVar(variable,d,True), checkvalue)
134     if result <= 0:
135         return truevalue
136     else:
137         return falsevalue
138
139 def base_contains(variable, checkvalues, truevalue, falsevalue, d):
140         import bb
141         matches = 0
142         if type(checkvalues).__name__ == "str":
143                 checkvalues = [checkvalues]
144         for value in checkvalues:
145                 if bb.data.getVar(variable,d,1).find(value) != -1:      
146                         matches = matches + 1
147         if matches == len(checkvalues):
148                 return truevalue                
149         return falsevalue
150
151 def base_both_contain(variable1, variable2, checkvalue, d):
152        import bb
153        if bb.data.getVar(variable1,d,1).find(checkvalue) != -1 and bb.data.getVar(variable2,d,1).find(checkvalue) != -1:
154                return checkvalue
155        else:
156                return ""
157
158 DEPENDS_prepend="${@base_dep_prepend(d)} "
159
160 def base_set_filespath(path, d):
161         import os, bb
162         filespath = []
163         for p in path:
164                 overrides = bb.data.getVar("OVERRIDES", d, 1) or ""
165                 overrides = overrides + ":"
166                 for o in overrides.split(":"):
167                         filespath.append(os.path.join(p, o))
168         return ":".join(filespath)
169
170 FILESPATH = "${@base_set_filespath([ "${FILE_DIRNAME}/${PF}", "${FILE_DIRNAME}/${P}", "${FILE_DIRNAME}/${PN}", "${FILE_DIRNAME}/files", "${FILE_DIRNAME}" ], d)}"
171
172 def oe_filter(f, str, d):
173         from re import match
174         return " ".join(filter(lambda x: match(f, x, 0), str.split()))
175
176 def oe_filter_out(f, str, d):
177         from re import match
178         return " ".join(filter(lambda x: not match(f, x, 0), str.split()))
179
180 die() {
181         oefatal "$*"
182 }
183
184 oenote() {
185         echo "NOTE:" "$*"
186 }
187
188 oewarn() {
189         echo "WARNING:" "$*"
190 }
191
192 oefatal() {
193         echo "FATAL:" "$*"
194         exit 1
195 }
196
197 oedebug() {
198         test $# -ge 2 || {
199                 echo "Usage: oedebug level \"message\""
200                 exit 1
201         }
202
203         test ${OEDEBUG:-0} -ge $1 && {
204                 shift
205                 echo "DEBUG:" $*
206         }
207 }
208
209 oe_runmake() {
210         if [ x"$MAKE" = x ]; then MAKE=make; fi
211         oenote ${MAKE} ${EXTRA_OEMAKE} "$@"
212         ${MAKE} ${EXTRA_OEMAKE} "$@" || die "oe_runmake failed"
213 }
214
215 oe_soinstall() {
216         # Purpose: Install shared library file and
217         #          create the necessary links
218         # Example:
219         #
220         # oe_
221         #
222         #oenote installing shared library $1 to $2
223         #
224         libname=`basename $1`
225         install -m 755 $1 $2/$libname
226         sonamelink=`${HOST_PREFIX}readelf -d $1 |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
227         solink=`echo $libname | sed -e 's/\.so\..*/.so/'`
228         ln -sf $libname $2/$sonamelink
229         ln -sf $libname $2/$solink
230 }
231
232 oe_libinstall() {
233         # Purpose: Install a library, in all its forms
234         # Example
235         #
236         # oe_libinstall libltdl ${STAGING_LIBDIR}/
237         # oe_libinstall -C src/libblah libblah ${D}/${libdir}/
238         dir=""
239         libtool=""
240         silent=""
241         require_static=""
242         require_shared=""
243         staging_install=""
244         while [ "$#" -gt 0 ]; do
245                 case "$1" in
246                 -C)
247                         shift
248                         dir="$1"
249                         ;;
250                 -s)
251                         silent=1
252                         ;;
253                 -a)
254                         require_static=1
255                         ;;
256                 -so)
257                         require_shared=1
258                         ;;
259                 -*)
260                         oefatal "oe_libinstall: unknown option: $1"
261                         ;;
262                 *)
263                         break;
264                         ;;
265                 esac
266                 shift
267         done
268
269         libname="$1"
270         shift
271         destpath="$1"
272         if [ -z "$destpath" ]; then
273                 oefatal "oe_libinstall: no destination path specified"
274         fi
275         if echo "$destpath/" | egrep '^${STAGING_LIBDIR}/' >/dev/null
276         then
277                 staging_install=1
278         fi
279
280         __runcmd () {
281                 if [ -z "$silent" ]; then
282                         echo >&2 "oe_libinstall: $*"
283                 fi
284                 $*
285         }
286
287         if [ -z "$dir" ]; then
288                 dir=`pwd`
289         fi
290         dotlai=$libname.lai
291         dir=$dir`(cd $dir;find . -name "$dotlai") | sed "s/^\.//;s/\/$dotlai\$//;q"`
292         olddir=`pwd`
293         __runcmd cd $dir
294
295         lafile=$libname.la
296
297         # If such file doesn't exist, try to cut version suffix
298         if [ ! -f "$lafile" ]; then
299                 libname1=`echo "$libname" | sed 's/-[0-9.]*$//'`
300                 lafile1=$libname.la
301                 if [ -f "$lafile1" ]; then
302                         libname=$libname1
303                         lafile=$lafile1
304                 fi
305         fi
306
307         if [ -f "$lafile" ]; then
308                 # libtool archive
309                 eval `cat $lafile|grep "^library_names="`
310                 libtool=1
311         else
312                 library_names="$libname.so* $libname.dll.a"
313         fi
314
315         __runcmd install -d $destpath/
316         dota=$libname.a
317         if [ -f "$dota" -o -n "$require_static" ]; then
318                 __runcmd install -m 0644 $dota $destpath/
319         fi
320         if [ -f "$dotlai" -a -n "$libtool" ]; then
321                 if test -n "$staging_install"
322                 then
323                         # stop libtool using the final directory name for libraries
324                         # in staging:
325                         __runcmd rm -f $destpath/$libname.la
326                         __runcmd sed -e 's/^installed=yes$/installed=no/' \
327                                      -e '/^dependency_libs=/s,${WORKDIR}[[:alnum:]/\._+-]*/\([[:alnum:]\._+-]*\),${STAGING_LIBDIR}/\1,g' \
328                                      $dotlai >$destpath/$libname.la
329                 else
330                         __runcmd install -m 0644 $dotlai $destpath/$libname.la
331                 fi
332         fi
333
334         for name in $library_names; do
335                 files=`eval echo $name`
336                 for f in $files; do
337                         if [ ! -e "$f" ]; then
338                                 if [ -n "$libtool" ]; then
339                                         oefatal "oe_libinstall: $dir/$f not found."
340                                 fi
341                         elif [ -L "$f" ]; then
342                                 __runcmd cp -P "$f" $destpath/
343                         elif [ ! -L "$f" ]; then
344                                 libfile="$f"
345                                 __runcmd install -m 0755 $libfile $destpath/
346                         fi
347                 done
348         done
349
350         if [ -z "$libfile" ]; then
351                 if  [ -n "$require_shared" ]; then
352                         oefatal "oe_libinstall: unable to locate shared library"
353                 fi
354         elif [ -z "$libtool" ]; then
355                 # special case hack for non-libtool .so.#.#.# links
356                 baselibfile=`basename "$libfile"`
357                 if (echo $baselibfile | grep -qE '^lib.*\.so\.[0-9.]*$'); then
358                         sonamelink=`${HOST_PREFIX}readelf -d $libfile |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
359                         solink=`echo $baselibfile | sed -e 's/\.so\..*/.so/'`
360                         if [ -n "$sonamelink" -a x"$baselibfile" != x"$sonamelink" ]; then
361                                 __runcmd ln -sf $baselibfile $destpath/$sonamelink
362                         fi
363                         __runcmd ln -sf $baselibfile $destpath/$solink
364                 fi
365         fi
366
367         __runcmd cd "$olddir"
368 }
369
370 oe_machinstall() {
371         # Purpose: Install machine dependent files, if available
372         #          If not available, check if there is a default
373         #          If no default, just touch the destination
374         # Example:
375         #                $1  $2   $3         $4
376         # oe_machinstall -m 0644 fstab ${D}/etc/fstab
377         #
378         # TODO: Check argument number?
379         #
380         filename=`basename $3`
381         dirname=`dirname $3`
382
383         for o in `echo ${OVERRIDES} | tr ':' ' '`; do
384                 if [ -e $dirname/$o/$filename ]; then
385                         oenote $dirname/$o/$filename present, installing to $4
386                         install $1 $2 $dirname/$o/$filename $4
387                         return
388                 fi
389         done
390 #       oenote overrides specific file NOT present, trying default=$3...
391         if [ -e $3 ]; then
392                 oenote $3 present, installing to $4
393                 install $1 $2 $3 $4
394         else
395                 oenote $3 NOT present, touching empty $4
396                 touch $4
397         fi
398 }
399
400 addtask listtasks
401 do_listtasks[nostamp] = "1"
402 python do_listtasks() {
403         import sys
404         # emit variables and shell functions
405         #bb.data.emit_env(sys.__stdout__, d)
406         # emit the metadata which isnt valid shell
407         for e in d.keys():
408                 if bb.data.getVarFlag(e, 'task', d):
409                         sys.__stdout__.write("%s\n" % e)
410 }
411
412 addtask clean
413 do_clean[dirs] = "${TOPDIR}"
414 do_clean[nostamp] = "1"
415 python base_do_clean() {
416         """clear the build and temp directories"""
417         dir = bb.data.expand("${WORKDIR}", d)
418         if dir == '//': raise bb.build.FuncFailed("wrong DATADIR")
419         bb.note("removing " + dir)
420         os.system('rm -rf ' + dir)
421
422         dir = "%s.*" % bb.data.expand(bb.data.getVar('STAMP', d), d)
423         bb.note("removing " + dir)
424         os.system('rm -f '+ dir)
425 }
426
427 #Uncomment this for bitbake 1.8.12
428 #addtask rebuild after do_${BB_DEFAULT_TASK}
429 addtask rebuild
430 do_rebuild[dirs] = "${TOPDIR}"
431 do_rebuild[nostamp] = "1"
432 python base_do_rebuild() {
433         """rebuild a package"""
434         from bb import __version__
435         try:
436                 from distutils.version import LooseVersion
437         except ImportError:
438                 def LooseVersion(v): print "WARNING: sanity.bbclass can't compare versions without python-distutils"; return 1
439         if (LooseVersion(__version__) < LooseVersion('1.8.11')):
440                 bb.build.exec_func('do_clean', d)
441                 bb.build.exec_task('do_' + bb.data.getVar('BB_DEFAULT_TASK', d, 1), d)
442 }
443
444 addtask mrproper
445 do_mrproper[dirs] = "${TOPDIR}"
446 do_mrproper[nostamp] = "1"
447 python base_do_mrproper() {
448         """clear downloaded sources, build and temp directories"""
449         dir = bb.data.expand("${DL_DIR}", d)
450         if dir == '/': bb.build.FuncFailed("wrong DATADIR")
451         bb.debug(2, "removing " + dir)
452         os.system('rm -rf ' + dir)
453         bb.build.exec_func('do_clean', d)
454 }
455
456 addtask fetch
457 do_fetch[dirs] = "${DL_DIR}"
458 do_fetch[depends] = "shasum-native:do_populate_staging"
459 python base_do_fetch() {
460         import sys
461
462         localdata = bb.data.createCopy(d)
463         bb.data.update_data(localdata)
464
465         src_uri = bb.data.getVar('SRC_URI', localdata, 1)
466         if not src_uri:
467                 return 1
468
469         try:
470                 bb.fetch.init(src_uri.split(),d)
471         except bb.fetch.NoMethodError:
472                 (type, value, traceback) = sys.exc_info()
473                 raise bb.build.FuncFailed("No method: %s" % value)
474
475         try:
476                 bb.fetch.go(localdata)
477         except bb.fetch.MissingParameterError:
478                 (type, value, traceback) = sys.exc_info()
479                 raise bb.build.FuncFailed("Missing parameters: %s" % value)
480         except bb.fetch.FetchError:
481                 (type, value, traceback) = sys.exc_info()
482                 raise bb.build.FuncFailed("Fetch failed: %s" % value)
483         except bb.fetch.MD5SumError:
484                 (type, value, traceback) = sys.exc_info()
485                 raise bb.build.FuncFailed("MD5  failed: %s" % value)
486         except:
487                 (type, value, traceback) = sys.exc_info()
488                 raise bb.build.FuncFailed("Unknown fetch Error: %s" % value)
489
490
491         # Verify the SHA and MD5 sums we have in OE and check what do
492         # in
493         check_sum = bb.which(bb.data.getVar('BBPATH', d, True), "conf/checksums.ini")
494         if not check_sum:
495                 bb.note("No conf/checksums.ini found, not checking checksums")
496                 return
497
498         try:
499                 parser = base_chk_load_parser(check_sum)
500         except:
501                 bb.note("Creating the CheckSum parser failed")
502                 return
503
504         pv = bb.data.getVar('PV', d, True)
505         pn = bb.data.getVar('PN', d, True)
506
507         # Check each URI
508         for url in src_uri.split():
509                 localpath = bb.data.expand(bb.fetch.localpath(url, localdata), localdata)
510                 (type,host,path,_,_,_) = bb.decodeurl(url)
511                 uri = "%s://%s%s" % (type,host,path)
512                 try:
513                         if type == "http" or type == "https" or type == "ftp" or type == "ftps":
514                                 if not base_chk_file(parser, pn, pv,uri, localpath, d):
515                                         bb.note("%s-%s: %s has no entry in conf/checksums.ini, not checking URI" % (pn,pv,uri))
516                 except Exception:
517                         raise bb.build.FuncFailed("Checksum of '%s' failed" % uri)
518 }
519
520 addtask fetchall after do_fetch
521 do_fetchall[recrdeptask] = "do_fetch"
522 base_do_fetchall() {
523         :
524 }
525
526 addtask buildall after do_build
527 do_buildall[recrdeptask] = "do_build"
528 base_do_buildall() {
529         :
530 }
531
532
533 def oe_unpack_file(file, data, url = None):
534         import bb, os
535         if not url:
536                 url = "file://%s" % file
537         dots = file.split(".")
538         if dots[-1] in ['gz', 'bz2', 'Z']:
539                 efile = os.path.join(bb.data.getVar('WORKDIR', data, 1),os.path.basename('.'.join(dots[0:-1])))
540         else:
541                 efile = file
542         cmd = None
543         if file.endswith('.tar'):
544                 cmd = 'tar x --no-same-owner -f %s' % file
545         elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'):
546                 cmd = 'tar xz --no-same-owner -f %s' % file
547         elif file.endswith('.tbz') or file.endswith('.tbz2') or file.endswith('.tar.bz2'):
548                 cmd = 'bzip2 -dc %s | tar x --no-same-owner -f -' % file
549         elif file.endswith('.gz') or file.endswith('.Z') or file.endswith('.z'):
550                 cmd = 'gzip -dc %s > %s' % (file, efile)
551         elif file.endswith('.bz2'):
552                 cmd = 'bzip2 -dc %s > %s' % (file, efile)
553         elif file.endswith('.zip'):
554                 cmd = 'unzip -q'
555                 (type, host, path, user, pswd, parm) = bb.decodeurl(url)
556                 if 'dos' in parm:
557                         cmd = '%s -a' % cmd
558                 cmd = '%s %s' % (cmd, file)
559         elif os.path.isdir(file):
560                 filesdir = os.path.realpath(bb.data.getVar("FILESDIR", data, 1))
561                 destdir = "."
562                 if file[0:len(filesdir)] == filesdir:
563                         destdir = file[len(filesdir):file.rfind('/')]
564                         destdir = destdir.strip('/')
565                         if len(destdir) < 1:
566                                 destdir = "."
567                         elif not os.access("%s/%s" % (os.getcwd(), destdir), os.F_OK):
568                                 os.makedirs("%s/%s" % (os.getcwd(), destdir))
569                 cmd = 'cp -pPR %s %s/%s/' % (file, os.getcwd(), destdir)
570         else:
571                 (type, host, path, user, pswd, parm) = bb.decodeurl(url)
572                 if not 'patch' in parm:
573                         # The "destdir" handling was specifically done for FILESPATH
574                         # items.  So, only do so for file:// entries.
575                         if type == "file":
576                                 destdir = bb.decodeurl(url)[1] or "."
577                         else:
578                                 destdir = "."
579                         bb.mkdirhier("%s/%s" % (os.getcwd(), destdir))
580                         cmd = 'cp %s %s/%s/' % (file, os.getcwd(), destdir)
581
582         if not cmd:
583                 return True
584
585         dest = os.path.join(os.getcwd(), os.path.basename(file))
586         if os.path.exists(dest):
587                 if os.path.samefile(file, dest):
588                         return True
589
590         cmd = "PATH=\"%s\" %s" % (bb.data.getVar('PATH', data, 1), cmd)
591         bb.note("Unpacking %s to %s/" % (file, os.getcwd()))
592         ret = os.system(cmd)
593         return ret == 0
594
595 addtask unpack after do_fetch
596 do_unpack[dirs] = "${WORKDIR}"
597 python base_do_unpack() {
598         import re, os
599
600         localdata = bb.data.createCopy(d)
601         bb.data.update_data(localdata)
602
603         src_uri = bb.data.getVar('SRC_URI', localdata)
604         if not src_uri:
605                 return
606         src_uri = bb.data.expand(src_uri, localdata)
607         for url in src_uri.split():
608                 try:
609                         local = bb.data.expand(bb.fetch.localpath(url, localdata), localdata)
610                 except bb.MalformedUrl, e:
611                         raise FuncFailed('Unable to generate local path for malformed uri: %s' % e)
612                 local = os.path.realpath(local)
613                 ret = oe_unpack_file(local, localdata, url)
614                 if not ret:
615                         raise bb.build.FuncFailed()
616 }
617
618
619 addhandler base_eventhandler
620 python base_eventhandler() {
621         from bb import note, error, data
622         from bb.event import Handled, NotHandled, getName
623         import os
624
625         messages = {}
626         messages["Completed"] = "completed"
627         messages["Succeeded"] = "completed"
628         messages["Started"] = "started"
629         messages["Failed"] = "failed"
630
631         name = getName(e)
632         msg = ""
633         if name.startswith("Pkg"):
634                 msg += "package %s: " % data.getVar("P", e.data, 1)
635                 msg += messages.get(name[3:]) or name[3:]
636         elif name.startswith("Task"):
637                 msg += "package %s: task %s: " % (data.getVar("PF", e.data, 1), e.task)
638                 msg += messages.get(name[4:]) or name[4:]
639         elif name.startswith("Build"):
640                 msg += "build %s: " % e.name
641                 msg += messages.get(name[5:]) or name[5:]
642         elif name == "UnsatisfiedDep":
643                 msg += "package %s: dependency %s %s" % (e.pkg, e.dep, name[:-3].lower())
644         if msg:
645                 note(msg)
646
647         if name.startswith("BuildStarted"):
648                 bb.data.setVar( 'BB_VERSION', bb.__version__, e.data )
649                 path_to_bbfiles = bb.data.getVar( 'BBFILES', e.data, 1 )
650                 path_to_packages = path_to_bbfiles[:path_to_bbfiles.rindex( "packages" )]
651                 monotone_revision = "<unknown>"
652                 try:
653                         monotone_revision = file( "%s/_MTN/revision" % path_to_packages ).read().strip()
654                         if monotone_revision.startswith( "format_version" ):
655                                 monotone_revision_words = monotone_revision.split()
656                                 monotone_revision = monotone_revision_words[ monotone_revision_words.index( "old_revision" )+1][1:-1]
657                 except IOError:
658                         pass
659                 bb.data.setVar( 'OE_REVISION', monotone_revision, e.data )
660                 statusvars = ['BB_VERSION', 'OE_REVISION', 'TARGET_ARCH', 'TARGET_OS', 'MACHINE', 'DISTRO', 'DISTRO_VERSION','TARGET_FPU']
661                 statuslines = ["%-14s = \"%s\"" % (i, bb.data.getVar(i, e.data, 1) or '') for i in statusvars]
662                 statusmsg = "\nOE Build Configuration:\n%s\n" % '\n'.join(statuslines)
663                 print statusmsg
664
665                 needed_vars = [ "TARGET_ARCH", "TARGET_OS" ]
666                 pesteruser = []
667                 for v in needed_vars:
668                         val = bb.data.getVar(v, e.data, 1)
669                         if not val or val == 'INVALID':
670                                 pesteruser.append(v)
671                 if pesteruser:
672                         bb.fatal('The following variable(s) were not set: %s\nPlease set them directly, or choose a MACHINE or DISTRO that sets them.' % ', '.join(pesteruser))
673
674         #
675         # Handle removing stamps for 'rebuild' task
676         #
677         if name.startswith("StampUpdate"):
678                 for (fn, task) in e.targets:
679                         #print "%s %s" % (task, fn)         
680                         if task == "do_rebuild":
681                                 dir = "%s.*" % e.stampPrefix[fn]
682                                 bb.note("Removing stamps: " + dir)
683                                 os.system('rm -f '+ dir)
684
685         if not data in e.__dict__:
686                 return NotHandled
687
688         log = data.getVar("EVENTLOG", e.data, 1)
689         if log:
690                 logfile = file(log, "a")
691                 logfile.write("%s\n" % msg)
692                 logfile.close()
693
694         return NotHandled
695 }
696
697 addtask configure after do_unpack do_patch
698 do_configure[dirs] = "${S} ${B}"
699 do_configure[deptask] = "do_populate_staging"
700 base_do_configure() {
701         :
702 }
703
704 addtask compile after do_configure
705 do_compile[dirs] = "${S} ${B}"
706 base_do_compile() {
707         if [ -e Makefile -o -e makefile ]; then
708                 oe_runmake || die "make failed"
709         else
710                 oenote "nothing to compile"
711         fi
712 }
713
714 base_do_stage () {
715         :
716 }
717
718 do_populate_staging[dirs] = "${STAGING_DIR_TARGET}/${layout_bindir} ${STAGING_DIR_TARGET}/${layout_libdir} \
719                              ${STAGING_DIR_TARGET}/${layout_includedir} \
720                              ${STAGING_BINDIR_NATIVE} ${STAGING_LIBDIR_NATIVE} \
721                              ${STAGING_INCDIR_NATIVE} \
722                              ${STAGING_DATADIR} \
723                              ${S} ${B}"
724
725 # Could be compile but populate_staging and do_install shouldn't run at the same time
726 addtask populate_staging after do_install
727
728 python do_populate_staging () {
729     bb.build.exec_func('do_stage', d)
730 }
731
732 addtask install after do_compile
733 do_install[dirs] = "${D} ${S} ${B}"
734 # Remove and re-create ${D} so that is it guaranteed to be empty
735 do_install[cleandirs] = "${D}"
736
737 base_do_install() {
738         :
739 }
740
741 base_do_package() {
742         :
743 }
744
745 addtask build after do_populate_staging
746 do_build = ""
747 do_build[func] = "1"
748
749 # Functions that update metadata based on files outputted
750 # during the build process.
751
752 def explode_deps(s):
753         r = []
754         l = s.split()
755         flag = False
756         for i in l:
757                 if i[0] == '(':
758                         flag = True
759                         j = []
760                 if flag:
761                         j.append(i)
762                         if i.endswith(')'):
763                                 flag = False
764                                 r[-1] += ' ' + ' '.join(j)
765                 else:
766                         r.append(i)
767         return r
768
769 def packaged(pkg, d):
770         import os, bb
771         return os.access(get_subpkgedata_fn(pkg, d) + '.packaged', os.R_OK)
772
773 def read_pkgdatafile(fn):
774         pkgdata = {}
775
776         def decode(str):
777                 import codecs
778                 c = codecs.getdecoder("string_escape")
779                 return c(str)[0]
780
781         import os
782         if os.access(fn, os.R_OK):
783                 import re
784                 f = file(fn, 'r')
785                 lines = f.readlines()
786                 f.close()
787                 r = re.compile("([^:]+):\s*(.*)")
788                 for l in lines:
789                         m = r.match(l)
790                         if m:
791                                 pkgdata[m.group(1)] = decode(m.group(2))
792
793         return pkgdata
794
795 def get_subpkgedata_fn(pkg, d):
796         import bb, os
797         archs = bb.data.expand("${PACKAGE_ARCHS}", d).split(" ")
798         archs.reverse()
799         pkgdata = bb.data.expand('${STAGING_DIR}/pkgdata/', d)
800         targetdir = bb.data.expand('${TARGET_VENDOR}-${TARGET_OS}/runtime/', d)
801         for arch in archs:
802                 fn = pkgdata + arch + targetdir + pkg
803                 if os.path.exists(fn):
804                         return fn
805         return bb.data.expand('${PKGDATA_DIR}/runtime/%s' % pkg, d)
806
807 def has_subpkgdata(pkg, d):
808         import bb, os
809         return os.access(get_subpkgedata_fn(pkg, d), os.R_OK)
810
811 def read_subpkgdata(pkg, d):
812         import bb, os
813         return read_pkgdatafile(get_subpkgedata_fn(pkg, d))
814
815 def has_pkgdata(pn, d):
816         import bb, os
817         fn = bb.data.expand('${PKGDATA_DIR}/%s' % pn, d)
818         return os.access(fn, os.R_OK)
819
820 def read_pkgdata(pn, d):
821         import bb, os
822         fn = bb.data.expand('${PKGDATA_DIR}/%s' % pn, d)
823         return read_pkgdatafile(fn)
824
825 python read_subpackage_metadata () {
826         import bb
827         data = read_pkgdata(bb.data.getVar('PN', d, 1), d)
828
829         for key in data.keys():
830                 bb.data.setVar(key, data[key], d)
831
832         for pkg in bb.data.getVar('PACKAGES', d, 1).split():
833                 sdata = read_subpkgdata(pkg, d)
834                 for key in sdata.keys():
835                         bb.data.setVar(key, sdata[key], d)
836 }
837
838 # Make sure MACHINE isn't exported
839 # (breaks binutils at least)
840 MACHINE[unexport] = "1"
841
842 # Make sure TARGET_ARCH isn't exported
843 # (breaks Makefiles using implicit rules, e.g. quilt, as GNU make has this 
844 # in them, undocumented)
845 TARGET_ARCH[unexport] = "1"
846
847 # Make sure DISTRO isn't exported
848 # (breaks sysvinit at least)
849 DISTRO[unexport] = "1"
850
851
852 def base_after_parse(d):
853     import bb, os, exceptions
854
855     source_mirror_fetch = bb.data.getVar('SOURCE_MIRROR_FETCH', d, 0)
856     if not source_mirror_fetch:
857         need_host = bb.data.getVar('COMPATIBLE_HOST', d, 1)
858         if need_host:
859             import re
860             this_host = bb.data.getVar('HOST_SYS', d, 1)
861             if not re.match(need_host, this_host):
862                 raise bb.parse.SkipPackage("incompatible with host %s" % this_host)
863
864         need_machine = bb.data.getVar('COMPATIBLE_MACHINE', d, 1)
865         if need_machine:
866             import re
867             this_machine = bb.data.getVar('MACHINE', d, 1)
868             if this_machine and not re.match(need_machine, this_machine):
869                 raise bb.parse.SkipPackage("incompatible with machine %s" % this_machine)
870
871     pn = bb.data.getVar('PN', d, 1)
872
873     # OBSOLETE in bitbake 1.7.4
874     srcdate = bb.data.getVar('SRCDATE_%s' % pn, d, 1)
875     if srcdate != None:
876         bb.data.setVar('SRCDATE', srcdate, d)
877
878     use_nls = bb.data.getVar('USE_NLS_%s' % pn, d, 1)
879     if use_nls != None:
880         bb.data.setVar('USE_NLS', use_nls, d)
881
882     # Git packages should DEPEND on git-native
883     srcuri = bb.data.getVar('SRC_URI', d, 1)
884     if "git://" in srcuri:
885         depends = bb.data.getVarFlag('do_fetch', 'depends', d) or ""
886         depends = depends + " git-native:do_populate_staging"
887         bb.data.setVarFlag('do_fetch', 'depends', depends, d)
888
889     mach_arch = bb.data.getVar('MACHINE_ARCH', d, 1)
890     old_arch = bb.data.getVar('PACKAGE_ARCH', d, 1)
891     if (old_arch == mach_arch):
892         # Nothing to do
893         return
894
895     #
896     # We always try to scan SRC_URI for urls with machine overrides
897     # unless the package sets SRC_URI_OVERRIDES_PACKAGE_ARCH=0
898     #
899     override = bb.data.getVar('SRC_URI_OVERRIDES_PACKAGE_ARCH', d, 1)
900     if override == '0':
901         return
902
903     paths = []
904     for p in [ "${PF}", "${P}", "${PN}", "files", "" ]:
905         path = bb.data.expand(os.path.join("${FILE_DIRNAME}", p, "${MACHINE}"), d)
906         if os.path.isdir(path):
907             paths.append(path)
908     if len(paths) == 0:
909         return
910
911     for s in srcuri.split():
912         if not s.startswith("file://"):
913             continue
914         local = bb.data.expand(bb.fetch.localpath(s, d), d)
915         for mp in paths:
916             if local.startswith(mp):
917                 #bb.note("overriding PACKAGE_ARCH from %s to %s" % (old_arch, mach_arch))
918                 bb.data.setVar('PACKAGE_ARCH', "${MACHINE_ARCH}", d)
919                 return
920
921 python () {
922     import bb
923     from bb import __version__
924     base_after_parse(d)
925
926     # Remove this for bitbake 1.8.12
927     try:
928         from distutils.version import LooseVersion
929     except ImportError:
930         def LooseVersion(v): print "WARNING: sanity.bbclass can't compare versions without python-distutils"; return 1
931     if (LooseVersion(__version__) >= LooseVersion('1.8.11')):
932         deps = bb.data.getVarFlag('do_rebuild', 'deps', d) or []
933         deps.append('do_' + bb.data.getVar('BB_DEFAULT_TASK', d, 1))
934         bb.data.setVarFlag('do_rebuild', 'deps', deps, d)
935 }
936
937 def check_app_exists(app, d):
938         from bb import which, data
939
940         app = data.expand(app, d)
941         path = data.getVar('PATH', d, 1)
942         return len(which(path, app)) != 0
943
944 def check_gcc3(data):
945
946         gcc3_versions = 'gcc-3.4 gcc34 gcc-3.4.4 gcc-3.4.6 gcc-3.4.7 gcc-3.3 gcc33 gcc-3.3.6 gcc-3.2 gcc32'
947
948         for gcc3 in gcc3_versions.split():
949                 if check_app_exists(gcc3, data):
950                         return gcc3
951         
952         return False
953
954 # Patch handling
955 inherit patch
956
957 # Configuration data from site files
958 # Move to autotools.bbclass?
959 inherit siteinfo
960
961 EXPORT_FUNCTIONS do_clean do_mrproper do_fetch do_unpack do_configure do_compile do_install do_package do_populate_pkgs do_stage do_rebuild do_fetchall
962
963 MIRRORS[func] = "0"
964 MIRRORS () {
965 ${DEBIAN_MIRROR}/main   http://snapshot.debian.net/archive/pool
966 ${DEBIAN_MIRROR}        ftp://ftp.de.debian.org/debian/pool
967 ${DEBIAN_MIRROR}        ftp://ftp.au.debian.org/debian/pool
968 ${DEBIAN_MIRROR}        ftp://ftp.cl.debian.org/debian/pool
969 ${DEBIAN_MIRROR}        ftp://ftp.hr.debian.org/debian/pool
970 ${DEBIAN_MIRROR}        ftp://ftp.fi.debian.org/debian/pool
971 ${DEBIAN_MIRROR}        ftp://ftp.hk.debian.org/debian/pool
972 ${DEBIAN_MIRROR}        ftp://ftp.hu.debian.org/debian/pool
973 ${DEBIAN_MIRROR}        ftp://ftp.ie.debian.org/debian/pool
974 ${DEBIAN_MIRROR}        ftp://ftp.it.debian.org/debian/pool
975 ${DEBIAN_MIRROR}        ftp://ftp.jp.debian.org/debian/pool
976 ${DEBIAN_MIRROR}        ftp://ftp.no.debian.org/debian/pool
977 ${DEBIAN_MIRROR}        ftp://ftp.pl.debian.org/debian/pool
978 ${DEBIAN_MIRROR}        ftp://ftp.ro.debian.org/debian/pool
979 ${DEBIAN_MIRROR}        ftp://ftp.si.debian.org/debian/pool
980 ${DEBIAN_MIRROR}        ftp://ftp.es.debian.org/debian/pool
981 ${DEBIAN_MIRROR}        ftp://ftp.se.debian.org/debian/pool
982 ${DEBIAN_MIRROR}        ftp://ftp.tr.debian.org/debian/pool
983 ${GNU_MIRROR}   ftp://mirrors.kernel.org/gnu
984 ${GNU_MIRROR}   ftp://ftp.matrix.com.br/pub/gnu
985 ${GNU_MIRROR}   ftp://ftp.cs.ubc.ca/mirror2/gnu
986 ${GNU_MIRROR}   ftp://sunsite.ust.hk/pub/gnu
987 ${GNU_MIRROR}   ftp://ftp.ayamura.org/pub/gnu
988 ${KERNELORG_MIRROR}     http://www.kernel.org/pub
989 ${KERNELORG_MIRROR}     ftp://ftp.us.kernel.org/pub
990 ${KERNELORG_MIRROR}     ftp://ftp.uk.kernel.org/pub
991 ${KERNELORG_MIRROR}     ftp://ftp.hk.kernel.org/pub
992 ${KERNELORG_MIRROR}     ftp://ftp.au.kernel.org/pub
993 ${KERNELORG_MIRROR}     ftp://ftp.jp.kernel.org/pub
994 ftp://ftp.gnupg.org/gcrypt/     ftp://ftp.franken.de/pub/crypt/mirror/ftp.gnupg.org/gcrypt/
995 ftp://ftp.gnupg.org/gcrypt/     ftp://ftp.surfnet.nl/pub/security/gnupg/
996 ftp://ftp.gnupg.org/gcrypt/     http://gulus.USherbrooke.ca/pub/appl/GnuPG/
997 ftp://dante.ctan.org/tex-archive ftp://ftp.fu-berlin.de/tex/CTAN
998 ftp://dante.ctan.org/tex-archive http://sunsite.sut.ac.jp/pub/archives/ctan/
999 ftp://dante.ctan.org/tex-archive http://ctan.unsw.edu.au/
1000 ftp://ftp.gnutls.org/pub/gnutls ftp://ftp.gnutls.org/pub/gnutls/
1001 ftp://ftp.gnutls.org/pub/gnutls ftp://ftp.gnupg.org/gcrypt/gnutls/
1002 ftp://ftp.gnutls.org/pub/gnutls http://www.mirrors.wiretapped.net/security/network-security/gnutls/
1003 ftp://ftp.gnutls.org/pub/gnutls ftp://ftp.mirrors.wiretapped.net/pub/security/network-security/gnutls/
1004 ftp://ftp.gnutls.org/pub/gnutls http://josefsson.org/gnutls/releases/
1005 http://ftp.info-zip.org/pub/infozip/src/ http://mirror.switch.ch/ftp/mirror/infozip/src/
1006 http://ftp.info-zip.org/pub/infozip/src/ ftp://sunsite.icm.edu.pl/pub/unix/archiving/info-zip/src/
1007 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.cerias.purdue.edu/pub/tools/unix/sysutils/lsof/
1008 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.tau.ac.il/pub/unix/admin/
1009 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.cert.dfn.de/pub/tools/admin/lsof/
1010 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.fu-berlin.de/pub/unix/tools/lsof/
1011 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.kaizo.org/pub/lsof/
1012 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.tu-darmstadt.de/pub/sysadmin/lsof/
1013 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://ftp.tux.org/pub/sites/vic.cc.purdue.edu/tools/unix/lsof/
1014 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://gd.tuwien.ac.at/utils/admin-tools/lsof/
1015 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://sunsite.ualberta.ca/pub/Mirror/lsof/
1016 ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/  ftp://the.wiretapped.net/pub/security/host-security/lsof/
1017 http://www.apache.org/dist  http://archive.apache.org/dist
1018
1019 }
1020