matrix-gui: use INC_PR in versioned recipes
[openembedded.git] / classes / utils.bbclass
blobacea01bd3663fc2ec718cd6abea2aa8165e9542b
1 # For compatibility
2 def base_path_join(a, *p):
3     return oe.path.join(a, *p)
5 def base_path_relative(src, dest):
6     return oe.path.relative(src, dest)
8 def base_path_out(path, d):
9     return oe.path.format_display(path, d)
11 def base_read_file(filename):
12     return oe.utils.read_file(filename)
14 def base_ifelse(condition, iftrue = True, iffalse = False):
15     return oe.utils.ifelse(condition, iftrue, iffalse)
17 def base_conditional(variable, checkvalue, truevalue, falsevalue, d):
18     return oe.utils.conditional(variable, checkvalue, truevalue, falsevalue, d)
20 def base_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
21     return oe.utils.less_or_equal(variable, checkvalue, truevalue, falsevalue, d)
23 def base_version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d):
24     return oe.utils.version_less_or_equal(variable, checkvalue, truevalue, falsevalue, d)
26 def base_contains(variable, checkvalues, truevalue, falsevalue, d):
27     return oe.utils.contains(variable, checkvalues, truevalue, falsevalue, d)
29 def base_both_contain(variable1, variable2, checkvalue, d):
30     return oe.utils.both_contain(variable1, variable2, checkvalue, d)
32 def base_prune_suffix(var, suffixes, d):
33     return oe.utils.prune_suffix(var, suffixes, d)
35 def oe_filter(f, str, d):
36     return oe.utils.str_filter(f, str, d)
38 def oe_filter_out(f, str, d):
39     return oe.utils.str_filter_out(f, str, d)
41 def machine_paths(d):
42     """List any existing machine specific filespath directories"""
43     machine = d.getVar("MACHINE", True)
44     filespathpkg = d.getVar("FILESPATHPKG", True).split(":")
45     for basepath in d.getVar("FILESPATHBASE", True).split(":"):
46         for pkgpath in filespathpkg:
47             machinepath = os.path.join(basepath, pkgpath, machine)
48             if os.path.isdir(machinepath):
49                 yield machinepath
51 def is_machine_specific(d):
52     """Determine whether the current recipe is machine specific"""
53     machinepaths = set(machine_paths(d))
54     urldatadict = bb.fetch.init(d.getVar("SRC_URI", True).split(), d, True)
55     for urldata in (urldata for urldata in urldatadict.itervalues()
56                     if urldata.type == "file"):
57         if any(urldata.localpath.startswith(mp + "/") for mp in machinepaths):
58             return True
60 def oe_popen_env(d):
61     env = d.getVar("__oe_popen_env", False)
62     if env is None:
63         env = {}
64         for v in d.keys():
65             if d.getVarFlag(v, "export"):
66                 env[v] = d.getVar(v, True) or ""
67         d.setVar("__oe_popen_env", env)
68     return env
70 def oe_run(d, cmd, **kwargs):
71     import oe.process
72     kwargs["env"] = oe_popen_env(d)
73     return oe.process.run(cmd, **kwargs)
75 def oe_popen(d, cmd, **kwargs):
76     import oe.process
77     kwargs["env"] = oe_popen_env(d)
78     return oe.process.Popen(cmd, **kwargs)
80 def oe_system(d, cmd, **kwargs):
81     """ Popen based version of os.system. """
82     if not "shell" in kwargs:
83         kwargs["shell"] = True
84     return oe_popen(d, cmd, **kwargs).wait()
86 # for MD5/SHA handling
87 def base_chk_load_parser(config_paths):
88     import ConfigParser
89     parser = ConfigParser.ConfigParser()
90     if len(parser.read(config_paths)) < 1:
91         raise ValueError("no ini files could be found")
93     return parser
95 def setup_checksum_deps(d):
96     try:
97         import hashlib
98     except ImportError:
99         if d.getVar("PN", True) != "shasum-native":
100             depends = d.getVarFlag("do_fetch", "depends") or ""
101             d.setVarFlag("do_fetch", "depends", "%s %s" %
102                          (depends, "shasum-native:do_populate_sysroot"))
104 def base_chk_file_checksum(localpath, src_uri, expected_md5sum, expected_sha256sum, data):
105     strict_checking = True
106     if bb.data.getVar("OE_STRICT_CHECKSUMS", data, True) != "1":
107         strict_checking = False
108     if not os.path.exists(localpath):
109         localpath = base_path_out(localpath, data)
110         bb.note("The localpath does not exist '%s'" % localpath)
111         raise Exception("The path does not exist '%s'" % localpath)
113     md5data = bb.utils.md5_file(localpath)
114     sha256data = bb.utils.sha256_file(localpath)
115     if not sha256data:
116         try:
117             shapipe = os.popen('PATH=%s oe_sha256sum "%s"' % (bb.data.getVar('PATH', data, True), localpath))
118             sha256data = (shapipe.readline().split() or [ "" ])[0]
119             shapipe.close()
120         except OSError, e:
121             if strict_checking:
122                 raise Exception("Executing shasum failed")
123             else:
124                 bb.note("Executing shasum failed")
126     if (expected_md5sum == None or expected_md5sum == None):
127         from string import maketrans
128         trtable = maketrans("", "")
129         uname = src_uri.split("/")[-1].translate(trtable, "-+._")
131         try:
132             ufile = open("%s/%s.sum" % (bb.data.getVar("TMPDIR", data, 1), uname), "wt")
133         except:
134             return False
136         if not ufile:
137             raise Exception("Creating %s.sum failed" % uname)
139         ufile.write("SRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
140         ufile.close()
141         bb.note("This package has no checksums, please add to recipe")
142         bb.note("\nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
144         # fail for strict, continue for disabled strict checksums
145         return not strict_checking
147     if (expected_md5sum and expected_md5sum != md5data) or (expected_sha256sum and expected_sha256sum != sha256data):
148         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))
149         bb.note("Your checksums:\nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
150         return False
152     return True
154 def base_get_checksums(pn, pv, src_uri, localpath, params, data):
155     # Try checksum from recipe and then parse checksums.ini
156     # and try PN-PV-SRC_URI first and then try PN-SRC_URI
157     # we rely on the get method to create errors
158     try:
159         name = params["name"]
160     except KeyError:
161         name = ""
162     if name:
163         md5flag = "%s.md5sum" % name
164         sha256flag = "%s.sha256sum" % name
165     else:
166         md5flag = "md5sum"
167         sha256flag = "sha256sum"
168     expected_md5sum = bb.data.getVarFlag("SRC_URI", md5flag, data)
169     expected_sha256sum = bb.data.getVarFlag("SRC_URI", sha256flag, data)
171     if (expected_md5sum and expected_sha256sum):
172         return (expected_md5sum,expected_sha256sum)
173     else:
174         # missing checksum, parse checksums.ini
176         # Verify the SHA and MD5 sums we have in OE and check what do
177         # in
178         checksum_paths = bb.data.getVar('BBPATH', data, True).split(":")
180         # reverse the list to give precedence to directories that
181         # appear first in BBPATH
182         checksum_paths.reverse()
184         checksum_files = ["%s/conf/checksums.ini" % path for path in checksum_paths]
185         try:
186             parser = base_chk_load_parser(checksum_files)
187         except ValueError:
188             bb.note("No conf/checksums.ini found, not checking checksums")
189             return (None,None)
190         except:
191             bb.note("Creating the CheckSum parser failed: %s:%s" % (sys.exc_info()[0], sys.exc_info()[1]))
192             return (None,None)
193         pn_pv_src = "%s-%s-%s" % (pn,pv,src_uri)
194         pn_src    = "%s-%s" % (pn,src_uri)
195         if parser.has_section(pn_pv_src):
196             expected_md5sum    = parser.get(pn_pv_src, "md5")
197             expected_sha256sum = parser.get(pn_pv_src, "sha256")
198         elif parser.has_section(pn_src):
199             expected_md5sum    = parser.get(pn_src, "md5")
200             expected_sha256sum = parser.get(pn_src, "sha256")
201         elif parser.has_section(src_uri):
202             expected_md5sum    = parser.get(src_uri, "md5")
203             expected_sha256sum = parser.get(src_uri, "sha256")
204         else:
205             return (None,None)
207         if name:
208             bb.note("This package has no checksums in corresponding recipe '%s', please consider moving its checksums from checksums.ini file \
209                 \nSRC_URI[%s.md5sum] = \"%s\"\nSRC_URI[%s.sha256sum] = \"%s\"\n" % (bb.data.getVar("FILE", data, True), name, expected_md5sum, name, expected_sha256sum))
210         else:
211             bb.note("This package has no checksums in corresponding recipe '%s', please consider moving its checksums from checksums.ini file \
212                 \nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (bb.data.getVar("FILE", data, True), expected_md5sum, expected_sha256sum))
214         return (expected_md5sum, expected_sha256sum)
216 def base_chk_file(pn, pv, src_uri, localpath, params, data):
217     (expected_md5sum, expected_sha256sum) = base_get_checksums(pn, pv, src_uri, localpath, params, data)
218     return base_chk_file_checksum(localpath, src_uri, expected_md5sum, expected_sha256sum, data)
220 oedebug() {
221         test $# -ge 2 || {
222                 echo "Usage: oedebug level \"message\""
223                 exit 1
224         }
226         test ${OEDEBUG:-0} -ge $1 && {
227                 shift
228                 echo "DEBUG:" $*
229         }
232 oe_soinstall() {
233         # Purpose: Install shared library file and
234         #          create the necessary links
235         # Example:
236         #
237         # oe_
238         #
239         #oenote installing shared library $1 to $2
240         #
241         libname=`basename $1`
242         install -m 755 $1 $2/$libname
243         sonamelink=`${HOST_PREFIX}readelf -d $1 |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
244         solink=`echo $libname | sed -e 's/\.so\..*/.so/'`
245         ln -sf $libname $2/$sonamelink
246         ln -sf $libname $2/$solink
249 oe_libinstall() {
250         # Purpose: Install a library, in all its forms
251         # Example
252         #
253         # oe_libinstall libltdl ${STAGING_LIBDIR}/
254         # oe_libinstall -C src/libblah libblah ${D}/${libdir}/
255         dir=""
256         libtool=""
257         silent=""
258         require_static=""
259         require_shared=""
260         staging_install=""
261         while [ "$#" -gt 0 ]; do
262                 case "$1" in
263                 -C)
264                         shift
265                         dir="$1"
266                         ;;
267                 -s)
268                         silent=1
269                         ;;
270                 -a)
271                         require_static=1
272                         ;;
273                 -so)
274                         require_shared=1
275                         ;;
276                 -*)
277                         oefatal "oe_libinstall: unknown option: $1"
278                         ;;
279                 *)
280                         break;
281                         ;;
282                 esac
283                 shift
284         done
286         libname="$1"
287         shift
288         destpath="$1"
289         if [ -z "$destpath" ]; then
290                 oefatal "oe_libinstall: no destination path specified"
291         fi
292         if echo "$destpath/" | egrep '^${STAGING_LIBDIR}/' >/dev/null
293         then
294                 staging_install=1
295         fi
297         __runcmd () {
298                 if [ -z "$silent" ]; then
299                         echo >&2 "oe_libinstall: $*"
300                 fi
301                 $*
302         }
304         if [ -z "$dir" ]; then
305                 dir=`pwd`
306         fi
308         dotlai=$libname.lai
310         # Sanity check that the libname.lai is unique
311         number_of_files=`(cd $dir; find . -name "$dotlai") | wc -l`
312         if [ $number_of_files -gt 1 ]; then
313                 oefatal "oe_libinstall: $dotlai is not unique in $dir"
314         fi
317         dir=$dir`(cd $dir;find . -name "$dotlai") | sed "s/^\.//;s/\/$dotlai\$//;q"`
318         olddir=`pwd`
319         __runcmd cd $dir
321         lafile=$libname.la
323         # If such file doesn't exist, try to cut version suffix
324         if [ ! -f "$lafile" ]; then
325                 libname1=`echo "$libname" | sed 's/-[0-9.]*$//'`
326                 lafile1=$libname.la
327                 if [ -f "$lafile1" ]; then
328                         libname=$libname1
329                         lafile=$lafile1
330                 fi
331         fi
333         if [ -f "$lafile" ]; then
334                 # libtool archive
335                 eval `cat $lafile|grep "^library_names="`
336                 libtool=1
337         else
338                 library_names="$libname.so* $libname.dll.a"
339         fi
341         __runcmd install -d $destpath/
342         dota=$libname.a
343         if [ -f "$dota" -o -n "$require_static" ]; then
344                 __runcmd install -m 0644 $dota $destpath/
345         fi
346         if [ -f "$dotlai" -a -n "$libtool" ]; then
347                 if [ -n "$staging_install" -a "${LIBTOOL_HAS_SYSROOT}" = "no" ]
348                 then
349                         # stop libtool using the final directory name for libraries
350                         # in staging:
351                         __runcmd rm -f $destpath/$libname.la
352                         __runcmd sed -e 's/^installed=yes$/installed=no/' \
353                                      -e '/^dependency_libs=/s,${WORKDIR}[[:alnum:]/\._+-]*/\([[:alnum:]\._+-]*\),${STAGING_LIBDIR}/\1,g' \
354                                      -e "/^dependency_libs=/s,\([[:space:]']\)${libdir},\1${STAGING_LIBDIR},g" \
355                                      $dotlai >$destpath/$libname.la
356                 else
357                         __runcmd install -m 0644 $dotlai $destpath/$libname.la
358                 fi
359         fi
361         for name in $library_names; do
362                 files=`eval echo $name`
363                 for f in $files; do
364                         if [ ! -e "$f" ]; then
365                                 if [ -n "$libtool" ]; then
366                                         oefatal "oe_libinstall: $dir/$f not found."
367                                 fi
368                         elif [ -L "$f" ]; then
369                                 __runcmd cp -P "$f" $destpath/
370                         elif [ ! -L "$f" ]; then
371                                 libfile="$f"
372                                 __runcmd install -m 0755 $libfile $destpath/
373                         fi
374                 done
375         done
377         if [ -z "$libfile" ]; then
378                 if  [ -n "$require_shared" ]; then
379                         oefatal "oe_libinstall: unable to locate shared library"
380                 fi
381         elif [ -z "$libtool" ]; then
382                 # special case hack for non-libtool .so.#.#.# links
383                 baselibfile=`basename "$libfile"`
384                 if (echo $baselibfile | grep -qE '^lib.*\.so\.[0-9.]*$'); then
385                         sonamelink=`${HOST_PREFIX}readelf -d $libfile |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
386                         solink=`echo $baselibfile | sed -e 's/\.so\..*/.so/'`
387                         if [ -n "$sonamelink" -a x"$baselibfile" != x"$sonamelink" ]; then
388                                 __runcmd ln -sf $baselibfile $destpath/$sonamelink
389                         fi
390                         __runcmd ln -sf $baselibfile $destpath/$solink
391                 fi
392         fi
394         __runcmd cd "$olddir"
397 oe_machinstall() {
398         # Purpose: Install machine dependent files, if available
399         #          If not available, check if there is a default
400         #          If no default, just touch the destination
401         # Example:
402         #                $1  $2   $3         $4
403         # oe_machinstall -m 0644 fstab ${D}/etc/fstab
404         #
405         # TODO: Check argument number?
406         #
407         filename=`basename $3`
408         dirname=`dirname $3`
410         for o in `echo ${OVERRIDES} | tr ':' ' '`; do
411                 if [ -e $dirname/$o/$filename ]; then
412                         oenote $dirname/$o/$filename present, installing to $4
413                         install $1 $2 $dirname/$o/$filename $4
414                         return
415                 fi
416         done
417 #       oenote overrides specific file NOT present, trying default=$3...
418         if [ -e $3 ]; then
419                 oenote $3 present, installing to $4
420                 install $1 $2 $3 $4
421         else
422                 oenote $3 NOT present, touching empty $4
423                 touch $4
424         fi
427 create_wrapper () {
428    # Create a wrapper script
429    #
430    # These are useful to work around relocation issues, by setting environment
431    # variables which point to paths in the filesystem.
432    #
433    # Usage: create_wrapper FILENAME [[VAR=VALUE]..]
435    cmd=$1
436    shift
438    # run echo via env to test syntactic validity of the variable arguments
439    env $@ echo "Generating wrapper script for $cmd"
441    mv $cmd $cmd.real
442    cmdname=`basename $cmd`.real
443    cat <<END >$cmd
444 #!/bin/sh
445 exec env $@ \`dirname \$0\`/$cmdname "\$@"
447    chmod +x $cmd
450 def check_app_exists(app, d):
451         from bb import which, data
453         app = data.expand(app, d)
454         path = data.getVar('PATH', d, 1)
455         return bool(which(path, app))
457 def explode_deps(s):
458         return bb.utils.explode_deps(s)
460 def base_set_filespath(path, d):
461         bb.note("base_set_filespath usage is deprecated, %s should be fixed" % d.getVar("P", 1))
462         filespath = []
463         # The ":" ensures we have an 'empty' override
464         overrides = (bb.data.getVar("OVERRIDES", d, 1) or "") + ":"
465         for p in path:
466                 for o in overrides.split(":"):
467                         filespath.append(os.path.join(p, o))
468         return ":".join(filespath)