Use the python modules for checksum generation where we can
[openembedded.git] / classes / utils.bbclass
blob0252a439b02cba43341ec0ec2230ac8d9fe556eb
1 def subprocess_setup():
2    import signal
3    # Python installs a SIGPIPE handler by default. This is usually not what
4    # non-Python subprocesses expect.
5    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
7 def oe_popen(d, cmd, **kwargs):
8     """ Convenience function to call out processes with our exported
9     variables in the environment.
10     """
11     from subprocess import Popen
13     if kwargs.get("env") is None:
14         env = d.getVar("__oe_popen_env", False)
15         if env is None:
16             env = {}
17             for v in d.keys():
18                 if d.getVarFlag(v, "export"):
19                     env[v] = d.getVar(v, True) or ""
20             d.setVar("__oe_popen_env", env)
21         kwargs["env"] = env
23     kwargs["preexec_fn"] = subprocess_setup
25     return Popen(cmd, **kwargs)
27 def oe_system(d, cmd):
28     """ Popen based version of os.system. """
29     return oe_popen(d, cmd, shell=True).wait()
31 # like os.path.join but doesn't treat absolute RHS specially
32 def base_path_join(a, *p):
33     path = a
34     for b in p:
35         if path == '' or path.endswith('/'):
36             path +=  b
37         else:
38             path += '/' + b
39     return path
41 def base_path_relative(src, dest):
42     """ Return a relative path from src to dest.
44     >>> base_path_relative("/usr/bin", "/tmp/foo/bar")
45     ../../tmp/foo/bar
47     >>> base_path_relative("/usr/bin", "/usr/lib")
48     ../lib
50     >>> base_path_relative("/tmp", "/tmp/foo/bar")
51     foo/bar
52     """
53     from os.path import sep, pardir, normpath, commonprefix
55     destlist = normpath(dest).split(sep)
56     srclist = normpath(src).split(sep)
58     # Find common section of the path
59     common = commonprefix([destlist, srclist])
60     commonlen = len(common)
62     # Climb back to the point where they differentiate
63     relpath = [ pardir ] * (len(srclist) - commonlen)
64     if commonlen < len(destlist):
65         # Add remaining portion
66         relpath += destlist[commonlen:]
68     return sep.join(relpath)
70 def base_path_out(path, d):
71     """ Prepare a path for display to the user. """
72     rel = base_path_relative(d.getVar("TOPDIR", 1), path)
73     if len(rel) > len(path):
74         return path
75     else:
76         return rel
78 # for MD5/SHA handling
79 def base_chk_load_parser(config_paths):
80     import ConfigParser
81     parser = ConfigParser.ConfigParser()
82     if len(parser.read(config_paths)) < 1:
83         raise ValueError("no ini files could be found")
85     return parser
87 def setup_checksum_deps(d):
88     try:
89         import hashlib
90     except ImportError:
91         if d.getVar("PN", True) != "shasum-native":
92             depends = d.getVarFlag("do_fetch", "depends") or ""
93             d.setVarFlag("do_fetch", "depends", "%s %s" %
94                          (depends, "shasum-native:do_populate_staging"))
96 def base_chk_file_checksum(localpath, src_uri, expected_md5sum, expected_sha256sum, data):
97     strict_checking =  bb.data.getVar("OE_STRICT_CHECKSUMS", data, True)
98     if not os.path.exists(localpath):
99         localpath = base_path_out(localpath, data)
100         bb.note("The localpath does not exist '%s'" % localpath)
101         raise Exception("The path does not exist '%s'" % localpath)
103     md5data = bb.utils.md5_file(localpath)
104     sha256data = bb.utils.sha256_file(localpath)
105     if not sha256data:
106         try:
107             shapipe = os.popen('PATH=%s oe_sha256sum "%s"' % (bb.data.getVar('PATH', data, True), localpath))
108             sha256data = (shapipe.readline().split() or [ "" ])[0]
109             shapipe.close()
110         except OSError, e:
111             if strict_checking:
112                 raise Exception("Executing shasum failed")
113             else:
114                 bb.note("Executing shasum failed")
116     if (expected_md5sum == None or expected_md5sum == None):
117         from string import maketrans
118         trtable = maketrans("", "")
119         uname = src_uri.split("/")[-1].translate(trtable, "-+._")
121         try:
122             ufile = open("%s/%s.sum" % (bb.data.getVar("TMPDIR", data, 1), uname), "wt")
123         except:
124             return False
126         if not ufile:
127             raise Exception("Creating %s.sum failed" % uname)
129         ufile.write("SRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
130         ufile.close()
131         bb.note("This package has no checksums, please add to recipe")
132         bb.note("\nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
134         # fail for strict, continue for disabled strict checksums
135         return not strict_checking
137     if (expected_md5sum and expected_md5sum != md5data) or (expected_sha256sum and expected_sha256sum != sha256data):
138         bb.note("The checksums for '%s' did not match.\nExpected MD5: '%s' and Got: '%s'\nExpected SHA256: '%s' and Got: '%s'" % (localpath, expected_md5sum, md5data, expected_sha256sum, sha256data))
139         bb.note("Your checksums:\nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
140         return False
142     return True
144 def base_get_checksums(pn, pv, src_uri, localpath, params, data):
145     # Try checksum from recipe and then parse checksums.ini 
146     # and try PN-PV-SRC_URI first and then try PN-SRC_URI
147     # we rely on the get method to create errors
148     try:
149         name = params["name"]
150     except KeyError:
151         name = ""
152     if name:
153         md5flag = "%s.md5sum" % name
154         sha256flag = "%s.sha256sum" % name
155     else:
156         md5flag = "md5sum"
157         sha256flag = "sha256sum"
158     expected_md5sum = bb.data.getVarFlag("SRC_URI", md5flag, data)
159     expected_sha256sum = bb.data.getVarFlag("SRC_URI", sha256flag, data)
161     if (expected_md5sum and expected_sha256sum):
162         return (expected_md5sum,expected_sha256sum)
163     else:
164         # missing checksum, parse checksums.ini
166         # Verify the SHA and MD5 sums we have in OE and check what do
167         # in
168         checksum_paths = bb.data.getVar('BBPATH', data, True).split(":")
170         # reverse the list to give precedence to directories that
171         # appear first in BBPATH
172         checksum_paths.reverse()
174         checksum_files = ["%s/conf/checksums.ini" % path for path in checksum_paths]
175         try:
176             parser = base_chk_load_parser(checksum_files)
177         except ValueError:
178             bb.note("No conf/checksums.ini found, not checking checksums")
179             return (None,None)
180         except:
181             bb.note("Creating the CheckSum parser failed: %s:%s" % (sys.exc_info()[0], sys.exc_info()[1]))
182             return (None,None)
183         pn_pv_src = "%s-%s-%s" % (pn,pv,src_uri)
184         pn_src    = "%s-%s" % (pn,src_uri)
185         if parser.has_section(pn_pv_src):
186             expected_md5sum    = parser.get(pn_pv_src, "md5")
187             expected_sha256sum = parser.get(pn_pv_src, "sha256")
188         elif parser.has_section(pn_src):
189             expected_md5sum    = parser.get(pn_src, "md5")
190             expected_sha256sum = parser.get(pn_src, "sha256")
191         elif parser.has_section(src_uri):
192             expected_md5sum    = parser.get(src_uri, "md5")
193             expected_sha256sum = parser.get(src_uri, "sha256")
194         else:
195             return (None,None)
196         
197         if name:
198             bb.note("This package has no checksums in corresponding recipe '%s', please consider moving its checksums from checksums.ini file \
199                 \nSRC_URI[%s.md5sum] = \"%s\"\nSRC_URI[%s.sha256sum] = \"%s\"\n" % (bb.data.getVar("FILE", data, True), name, expected_md5sum, name, expected_sha256sum))
200         else:
201             bb.note("This package has no checksums in corresponding recipe '%s', please consider moving its checksums from checksums.ini file \
202                 \nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (bb.data.getVar("FILE", data, True), expected_md5sum, expected_sha256sum))
204         return (expected_md5sum, expected_sha256sum)
206 def base_chk_file(pn, pv, src_uri, localpath, params, data):
207     (expected_md5sum, expected_sha256sum) = base_get_checksums(pn, pv, src_uri, localpath, params, data)
208     return base_chk_file_checksum(localpath, src_uri, expected_md5sum, expected_sha256sum, data)
210 def base_read_file(filename):
211         try:
212                 f = file( filename, "r" )
213         except IOError, reason:
214                 return "" # WARNING: can't raise an error now because of the new RDEPENDS handling. This is a bit ugly. :M:
215         else:
216                 return f.read().strip()
217         return None
219 def base_ifelse(condition, iftrue = True, iffalse = False):
220     if condition:
221         return iftrue
222     else:
223         return iffalse
225 def base_conditional(variable, checkvalue, truevalue, falsevalue, d):
226         if bb.data.getVar(variable,d,1) == checkvalue:
227                 return truevalue
228         else:
229                 return falsevalue
231 def base_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
232         if float(bb.data.getVar(variable,d,1)) <= float(checkvalue):
233                 return truevalue
234         else:
235                 return falsevalue
237 def base_version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
238     result = bb.vercmp(bb.data.getVar(variable,d,True), checkvalue)
239     if result <= 0:
240         return truevalue
241     else:
242         return falsevalue
244 def base_contains(variable, checkvalues, truevalue, falsevalue, d):
245         val = bb.data.getVar(variable,d,1)
246         if not val:
247                 return falsevalue
248         matches = 0
249         if type(checkvalues).__name__ == "str":
250                 checkvalues = [checkvalues]
251         for value in checkvalues:
252                 if val.find(value) != -1:
253                         matches = matches + 1
254         if matches == len(checkvalues):
255                 return truevalue
256         return falsevalue
258 def base_both_contain(variable1, variable2, checkvalue, d):
259        if bb.data.getVar(variable1,d,1).find(checkvalue) != -1 and bb.data.getVar(variable2,d,1).find(checkvalue) != -1:
260                return checkvalue
261        else:
262                return ""
264 def base_prune_suffix(var, suffixes, d):
265     # See if var ends with any of the suffixes listed and 
266     # remove it if found
267     for suffix in suffixes:
268         if var.endswith(suffix):
269             return var.replace(suffix, "")
270     return var
272 def oe_filter(f, str, d):
273         from re import match
274         return " ".join(filter(lambda x: match(f, x, 0), str.split()))
276 def oe_filter_out(f, str, d):
277         from re import match
278         return " ".join(filter(lambda x: not match(f, x, 0), str.split()))
280 oedebug() {
281         test $# -ge 2 || {
282                 echo "Usage: oedebug level \"message\""
283                 exit 1
284         }
286         test ${OEDEBUG:-0} -ge $1 && {
287                 shift
288                 echo "DEBUG:" $*
289         }
292 oe_soinstall() {
293         # Purpose: Install shared library file and
294         #          create the necessary links
295         # Example:
296         #
297         # oe_
298         #
299         #oenote installing shared library $1 to $2
300         #
301         libname=`basename $1`
302         install -m 755 $1 $2/$libname
303         sonamelink=`${HOST_PREFIX}readelf -d $1 |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
304         solink=`echo $libname | sed -e 's/\.so\..*/.so/'`
305         ln -sf $libname $2/$sonamelink
306         ln -sf $libname $2/$solink
309 oe_libinstall() {
310         # Purpose: Install a library, in all its forms
311         # Example
312         #
313         # oe_libinstall libltdl ${STAGING_LIBDIR}/
314         # oe_libinstall -C src/libblah libblah ${D}/${libdir}/
315         dir=""
316         libtool=""
317         silent=""
318         require_static=""
319         require_shared=""
320         staging_install=""
321         while [ "$#" -gt 0 ]; do
322                 case "$1" in
323                 -C)
324                         shift
325                         dir="$1"
326                         ;;
327                 -s)
328                         silent=1
329                         ;;
330                 -a)
331                         require_static=1
332                         ;;
333                 -so)
334                         require_shared=1
335                         ;;
336                 -*)
337                         oefatal "oe_libinstall: unknown option: $1"
338                         ;;
339                 *)
340                         break;
341                         ;;
342                 esac
343                 shift
344         done
346         libname="$1"
347         shift
348         destpath="$1"
349         if [ -z "$destpath" ]; then
350                 oefatal "oe_libinstall: no destination path specified"
351         fi
352         if echo "$destpath/" | egrep '^${STAGING_LIBDIR}/' >/dev/null
353         then
354                 staging_install=1
355         fi
357         __runcmd () {
358                 if [ -z "$silent" ]; then
359                         echo >&2 "oe_libinstall: $*"
360                 fi
361                 $*
362         }
364         if [ -z "$dir" ]; then
365                 dir=`pwd`
366         fi
368         dotlai=$libname.lai
370         # Sanity check that the libname.lai is unique
371         number_of_files=`(cd $dir; find . -name "$dotlai") | wc -l`
372         if [ $number_of_files -gt 1 ]; then
373                 oefatal "oe_libinstall: $dotlai is not unique in $dir"
374         fi
377         dir=$dir`(cd $dir;find . -name "$dotlai") | sed "s/^\.//;s/\/$dotlai\$//;q"`
378         olddir=`pwd`
379         __runcmd cd $dir
381         lafile=$libname.la
383         # If such file doesn't exist, try to cut version suffix
384         if [ ! -f "$lafile" ]; then
385                 libname1=`echo "$libname" | sed 's/-[0-9.]*$//'`
386                 lafile1=$libname.la
387                 if [ -f "$lafile1" ]; then
388                         libname=$libname1
389                         lafile=$lafile1
390                 fi
391         fi
393         if [ -f "$lafile" ]; then
394                 # libtool archive
395                 eval `cat $lafile|grep "^library_names="`
396                 libtool=1
397         else
398                 library_names="$libname.so* $libname.dll.a"
399         fi
401         __runcmd install -d $destpath/
402         dota=$libname.a
403         if [ -f "$dota" -o -n "$require_static" ]; then
404                 __runcmd install -m 0644 $dota $destpath/
405         fi
406         if [ -f "$dotlai" -a -n "$libtool" ]; then
407                 if test -n "$staging_install"
408                 then
409                         # stop libtool using the final directory name for libraries
410                         # in staging:
411                         __runcmd rm -f $destpath/$libname.la
412                         __runcmd sed -e 's/^installed=yes$/installed=no/' \
413                                      -e '/^dependency_libs=/s,${WORKDIR}[[:alnum:]/\._+-]*/\([[:alnum:]\._+-]*\),${STAGING_LIBDIR}/\1,g' \
414                                      -e "/^dependency_libs=/s,\([[:space:]']\)${libdir},\1${STAGING_LIBDIR},g" \
415                                      $dotlai >$destpath/$libname.la
416                 else
417                         __runcmd install -m 0644 $dotlai $destpath/$libname.la
418                 fi
419         fi
421         for name in $library_names; do
422                 files=`eval echo $name`
423                 for f in $files; do
424                         if [ ! -e "$f" ]; then
425                                 if [ -n "$libtool" ]; then
426                                         oefatal "oe_libinstall: $dir/$f not found."
427                                 fi
428                         elif [ -L "$f" ]; then
429                                 __runcmd cp -P "$f" $destpath/
430                         elif [ ! -L "$f" ]; then
431                                 libfile="$f"
432                                 __runcmd install -m 0755 $libfile $destpath/
433                         fi
434                 done
435         done
437         if [ -z "$libfile" ]; then
438                 if  [ -n "$require_shared" ]; then
439                         oefatal "oe_libinstall: unable to locate shared library"
440                 fi
441         elif [ -z "$libtool" ]; then
442                 # special case hack for non-libtool .so.#.#.# links
443                 baselibfile=`basename "$libfile"`
444                 if (echo $baselibfile | grep -qE '^lib.*\.so\.[0-9.]*$'); then
445                         sonamelink=`${HOST_PREFIX}readelf -d $libfile |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
446                         solink=`echo $baselibfile | sed -e 's/\.so\..*/.so/'`
447                         if [ -n "$sonamelink" -a x"$baselibfile" != x"$sonamelink" ]; then
448                                 __runcmd ln -sf $baselibfile $destpath/$sonamelink
449                         fi
450                         __runcmd ln -sf $baselibfile $destpath/$solink
451                 fi
452         fi
454         __runcmd cd "$olddir"
457 oe_machinstall() {
458         # Purpose: Install machine dependent files, if available
459         #          If not available, check if there is a default
460         #          If no default, just touch the destination
461         # Example:
462         #                $1  $2   $3         $4
463         # oe_machinstall -m 0644 fstab ${D}/etc/fstab
464         #
465         # TODO: Check argument number?
466         #
467         filename=`basename $3`
468         dirname=`dirname $3`
470         for o in `echo ${OVERRIDES} | tr ':' ' '`; do
471                 if [ -e $dirname/$o/$filename ]; then
472                         oenote $dirname/$o/$filename present, installing to $4
473                         install $1 $2 $dirname/$o/$filename $4
474                         return
475                 fi
476         done
477 #       oenote overrides specific file NOT present, trying default=$3...
478         if [ -e $3 ]; then
479                 oenote $3 present, installing to $4
480                 install $1 $2 $3 $4
481         else
482                 oenote $3 NOT present, touching empty $4
483                 touch $4
484         fi
487 def check_app_exists(app, d):
488         from bb import which, data
490         app = data.expand(app, d)
491         path = data.getVar('PATH', d, 1)
492         return bool(which(path, app))
494 def explode_deps(s):
495         return bb.utils.explode_deps(s)
497 def base_set_filespath(path, d):
498         bb.note("base_set_filespath usage is deprecated, %s should be fixed" % d.getVar("P", 1))
499         filespath = []
500         # The ":" ensures we have an 'empty' override
501         overrides = (bb.data.getVar("OVERRIDES", d, 1) or "") + ":"
502         for p in path:
503                 for o in overrides.split(":"):
504                         filespath.append(os.path.join(p, o))
505         return ":".join(filespath)