xmms: bump PR
[openembedded.git] / classes / utils.bbclass
blob7740ea3ac3f952a8224baf160fed326781cd6764
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)
42 def subprocess_setup():
43    import signal
44    # Python installs a SIGPIPE handler by default. This is usually not what
45    # non-Python subprocesses expect.
46    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
48 def oe_popen(d, cmd, **kwargs):
49     """ Convenience function to call out processes with our exported
50     variables in the environment.
51     """
52     from subprocess import Popen
54     if kwargs.get("env") is None:
55         env = d.getVar("__oe_popen_env", False)
56         if env is None:
57             env = {}
58             for v in d.keys():
59                 if d.getVarFlag(v, "export"):
60                     env[v] = d.getVar(v, True) or ""
61             d.setVar("__oe_popen_env", env)
62         kwargs["env"] = env
64     kwargs["close_fds"] = True
65     kwargs["preexec_fn"] = subprocess_setup
67     return Popen(cmd, **kwargs)
69 def oe_system(d, cmd, **kwargs):
70     """ Popen based version of os.system. """
71     if not "shell" in kwargs:
72         kwargs["shell"] = True
73     return oe_popen(d, cmd, **kwargs).wait()
75 # for MD5/SHA handling
76 def base_chk_load_parser(config_paths):
77     import ConfigParser
78     parser = ConfigParser.ConfigParser()
79     if len(parser.read(config_paths)) < 1:
80         raise ValueError("no ini files could be found")
82     return parser
84 def setup_checksum_deps(d):
85     try:
86         import hashlib
87     except ImportError:
88         if d.getVar("PN", True) != "shasum-native":
89             depends = d.getVarFlag("do_fetch", "depends") or ""
90             d.setVarFlag("do_fetch", "depends", "%s %s" %
91                          (depends, "shasum-native:do_populate_sysroot"))
93 def base_chk_file_checksum(localpath, src_uri, expected_md5sum, expected_sha256sum, data):
94     strict_checking = True
95     if bb.data.getVar("OE_STRICT_CHECKSUMS", data, True) != "1":
96         strict_checking = False
97     if not os.path.exists(localpath):
98         localpath = base_path_out(localpath, data)
99         bb.note("The localpath does not exist '%s'" % localpath)
100         raise Exception("The path does not exist '%s'" % localpath)
102     md5data = bb.utils.md5_file(localpath)
103     sha256data = bb.utils.sha256_file(localpath)
104     if not sha256data:
105         try:
106             shapipe = os.popen('PATH=%s oe_sha256sum "%s"' % (bb.data.getVar('PATH', data, True), localpath))
107             sha256data = (shapipe.readline().split() or [ "" ])[0]
108             shapipe.close()
109         except OSError, e:
110             if strict_checking:
111                 raise Exception("Executing shasum failed")
112             else:
113                 bb.note("Executing shasum failed")
115     if (expected_md5sum == None or expected_md5sum == None):
116         from string import maketrans
117         trtable = maketrans("", "")
118         uname = src_uri.split("/")[-1].translate(trtable, "-+._")
120         try:
121             ufile = open("%s/%s.sum" % (bb.data.getVar("TMPDIR", data, 1), uname), "wt")
122         except:
123             return False
125         if not ufile:
126             raise Exception("Creating %s.sum failed" % uname)
128         ufile.write("SRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
129         ufile.close()
130         bb.note("This package has no checksums, please add to recipe")
131         bb.note("\nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
133         # fail for strict, continue for disabled strict checksums
134         return not strict_checking
136     if (expected_md5sum and expected_md5sum != md5data) or (expected_sha256sum and expected_sha256sum != sha256data):
137         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))
138         bb.note("Your checksums:\nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (md5data, sha256data))
139         return False
141     return True
143 def base_get_checksums(pn, pv, src_uri, localpath, params, data):
144     # Try checksum from recipe and then parse checksums.ini 
145     # and try PN-PV-SRC_URI first and then try PN-SRC_URI
146     # we rely on the get method to create errors
147     try:
148         name = params["name"]
149     except KeyError:
150         name = ""
151     if name:
152         md5flag = "%s.md5sum" % name
153         sha256flag = "%s.sha256sum" % name
154     else:
155         md5flag = "md5sum"
156         sha256flag = "sha256sum"
157     expected_md5sum = bb.data.getVarFlag("SRC_URI", md5flag, data)
158     expected_sha256sum = bb.data.getVarFlag("SRC_URI", sha256flag, data)
160     if (expected_md5sum and expected_sha256sum):
161         return (expected_md5sum,expected_sha256sum)
162     else:
163         # missing checksum, parse checksums.ini
165         # Verify the SHA and MD5 sums we have in OE and check what do
166         # in
167         checksum_paths = bb.data.getVar('BBPATH', data, True).split(":")
169         # reverse the list to give precedence to directories that
170         # appear first in BBPATH
171         checksum_paths.reverse()
173         checksum_files = ["%s/conf/checksums.ini" % path for path in checksum_paths]
174         try:
175             parser = base_chk_load_parser(checksum_files)
176         except ValueError:
177             bb.note("No conf/checksums.ini found, not checking checksums")
178             return (None,None)
179         except:
180             bb.note("Creating the CheckSum parser failed: %s:%s" % (sys.exc_info()[0], sys.exc_info()[1]))
181             return (None,None)
182         pn_pv_src = "%s-%s-%s" % (pn,pv,src_uri)
183         pn_src    = "%s-%s" % (pn,src_uri)
184         if parser.has_section(pn_pv_src):
185             expected_md5sum    = parser.get(pn_pv_src, "md5")
186             expected_sha256sum = parser.get(pn_pv_src, "sha256")
187         elif parser.has_section(pn_src):
188             expected_md5sum    = parser.get(pn_src, "md5")
189             expected_sha256sum = parser.get(pn_src, "sha256")
190         elif parser.has_section(src_uri):
191             expected_md5sum    = parser.get(src_uri, "md5")
192             expected_sha256sum = parser.get(src_uri, "sha256")
193         else:
194             return (None,None)
195         
196         if name:
197             bb.note("This package has no checksums in corresponding recipe '%s', please consider moving its checksums from checksums.ini file \
198                 \nSRC_URI[%s.md5sum] = \"%s\"\nSRC_URI[%s.sha256sum] = \"%s\"\n" % (bb.data.getVar("FILE", data, True), name, expected_md5sum, name, expected_sha256sum))
199         else:
200             bb.note("This package has no checksums in corresponding recipe '%s', please consider moving its checksums from checksums.ini file \
201                 \nSRC_URI[md5sum] = \"%s\"\nSRC_URI[sha256sum] = \"%s\"\n" % (bb.data.getVar("FILE", data, True), expected_md5sum, expected_sha256sum))
203         return (expected_md5sum, expected_sha256sum)
205 def base_chk_file(pn, pv, src_uri, localpath, params, data):
206     (expected_md5sum, expected_sha256sum) = base_get_checksums(pn, pv, src_uri, localpath, params, data)
207     return base_chk_file_checksum(localpath, src_uri, expected_md5sum, expected_sha256sum, data)
209 oedebug() {
210         test $# -ge 2 || {
211                 echo "Usage: oedebug level \"message\""
212                 exit 1
213         }
215         test ${OEDEBUG:-0} -ge $1 && {
216                 shift
217                 echo "DEBUG:" $*
218         }
221 oe_soinstall() {
222         # Purpose: Install shared library file and
223         #          create the necessary links
224         # Example:
225         #
226         # oe_
227         #
228         #oenote installing shared library $1 to $2
229         #
230         libname=`basename $1`
231         install -m 755 $1 $2/$libname
232         sonamelink=`${HOST_PREFIX}readelf -d $1 |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
233         solink=`echo $libname | sed -e 's/\.so\..*/.so/'`
234         ln -sf $libname $2/$sonamelink
235         ln -sf $libname $2/$solink
238 oe_libinstall() {
239         # Purpose: Install a library, in all its forms
240         # Example
241         #
242         # oe_libinstall libltdl ${STAGING_LIBDIR}/
243         # oe_libinstall -C src/libblah libblah ${D}/${libdir}/
244         dir=""
245         libtool=""
246         silent=""
247         require_static=""
248         require_shared=""
249         staging_install=""
250         while [ "$#" -gt 0 ]; do
251                 case "$1" in
252                 -C)
253                         shift
254                         dir="$1"
255                         ;;
256                 -s)
257                         silent=1
258                         ;;
259                 -a)
260                         require_static=1
261                         ;;
262                 -so)
263                         require_shared=1
264                         ;;
265                 -*)
266                         oefatal "oe_libinstall: unknown option: $1"
267                         ;;
268                 *)
269                         break;
270                         ;;
271                 esac
272                 shift
273         done
275         libname="$1"
276         shift
277         destpath="$1"
278         if [ -z "$destpath" ]; then
279                 oefatal "oe_libinstall: no destination path specified"
280         fi
281         if echo "$destpath/" | egrep '^${STAGING_LIBDIR}/' >/dev/null
282         then
283                 staging_install=1
284         fi
286         __runcmd () {
287                 if [ -z "$silent" ]; then
288                         echo >&2 "oe_libinstall: $*"
289                 fi
290                 $*
291         }
293         if [ -z "$dir" ]; then
294                 dir=`pwd`
295         fi
297         dotlai=$libname.lai
299         # Sanity check that the libname.lai is unique
300         number_of_files=`(cd $dir; find . -name "$dotlai") | wc -l`
301         if [ $number_of_files -gt 1 ]; then
302                 oefatal "oe_libinstall: $dotlai is not unique in $dir"
303         fi
306         dir=$dir`(cd $dir;find . -name "$dotlai") | sed "s/^\.//;s/\/$dotlai\$//;q"`
307         olddir=`pwd`
308         __runcmd cd $dir
310         lafile=$libname.la
312         # If such file doesn't exist, try to cut version suffix
313         if [ ! -f "$lafile" ]; then
314                 libname1=`echo "$libname" | sed 's/-[0-9.]*$//'`
315                 lafile1=$libname.la
316                 if [ -f "$lafile1" ]; then
317                         libname=$libname1
318                         lafile=$lafile1
319                 fi
320         fi
322         if [ -f "$lafile" ]; then
323                 # libtool archive
324                 eval `cat $lafile|grep "^library_names="`
325                 libtool=1
326         else
327                 library_names="$libname.so* $libname.dll.a"
328         fi
330         __runcmd install -d $destpath/
331         dota=$libname.a
332         if [ -f "$dota" -o -n "$require_static" ]; then
333                 __runcmd install -m 0644 $dota $destpath/
334         fi
335         if [ -f "$dotlai" -a -n "$libtool" ]; then
336                 if test -n "$staging_install"
337                 then
338                         # stop libtool using the final directory name for libraries
339                         # in staging:
340                         __runcmd rm -f $destpath/$libname.la
341                         __runcmd sed -e 's/^installed=yes$/installed=no/' \
342                                      -e '/^dependency_libs=/s,${WORKDIR}[[:alnum:]/\._+-]*/\([[:alnum:]\._+-]*\),${STAGING_LIBDIR}/\1,g' \
343                                      -e "/^dependency_libs=/s,\([[:space:]']\)${libdir},\1${STAGING_LIBDIR},g" \
344                                      $dotlai >$destpath/$libname.la
345                 else
346                         __runcmd install -m 0644 $dotlai $destpath/$libname.la
347                 fi
348         fi
350         for name in $library_names; do
351                 files=`eval echo $name`
352                 for f in $files; do
353                         if [ ! -e "$f" ]; then
354                                 if [ -n "$libtool" ]; then
355                                         oefatal "oe_libinstall: $dir/$f not found."
356                                 fi
357                         elif [ -L "$f" ]; then
358                                 __runcmd cp -P "$f" $destpath/
359                         elif [ ! -L "$f" ]; then
360                                 libfile="$f"
361                                 __runcmd install -m 0755 $libfile $destpath/
362                         fi
363                 done
364         done
366         if [ -z "$libfile" ]; then
367                 if  [ -n "$require_shared" ]; then
368                         oefatal "oe_libinstall: unable to locate shared library"
369                 fi
370         elif [ -z "$libtool" ]; then
371                 # special case hack for non-libtool .so.#.#.# links
372                 baselibfile=`basename "$libfile"`
373                 if (echo $baselibfile | grep -qE '^lib.*\.so\.[0-9.]*$'); then
374                         sonamelink=`${HOST_PREFIX}readelf -d $libfile |grep 'Library soname:' |sed -e 's/.*\[\(.*\)\].*/\1/'`
375                         solink=`echo $baselibfile | sed -e 's/\.so\..*/.so/'`
376                         if [ -n "$sonamelink" -a x"$baselibfile" != x"$sonamelink" ]; then
377                                 __runcmd ln -sf $baselibfile $destpath/$sonamelink
378                         fi
379                         __runcmd ln -sf $baselibfile $destpath/$solink
380                 fi
381         fi
383         __runcmd cd "$olddir"
386 oe_machinstall() {
387         # Purpose: Install machine dependent files, if available
388         #          If not available, check if there is a default
389         #          If no default, just touch the destination
390         # Example:
391         #                $1  $2   $3         $4
392         # oe_machinstall -m 0644 fstab ${D}/etc/fstab
393         #
394         # TODO: Check argument number?
395         #
396         filename=`basename $3`
397         dirname=`dirname $3`
399         for o in `echo ${OVERRIDES} | tr ':' ' '`; do
400                 if [ -e $dirname/$o/$filename ]; then
401                         oenote $dirname/$o/$filename present, installing to $4
402                         install $1 $2 $dirname/$o/$filename $4
403                         return
404                 fi
405         done
406 #       oenote overrides specific file NOT present, trying default=$3...
407         if [ -e $3 ]; then
408                 oenote $3 present, installing to $4
409                 install $1 $2 $3 $4
410         else
411                 oenote $3 NOT present, touching empty $4
412                 touch $4
413         fi
416 def check_app_exists(app, d):
417         from bb import which, data
419         app = data.expand(app, d)
420         path = data.getVar('PATH', d, 1)
421         return bool(which(path, app))
423 def explode_deps(s):
424         return bb.utils.explode_deps(s)
426 def base_set_filespath(path, d):
427         bb.note("base_set_filespath usage is deprecated, %s should be fixed" % d.getVar("P", 1))
428         filespath = []
429         # The ":" ensures we have an 'empty' override
430         overrides = (bb.data.getVar("OVERRIDES", d, 1) or "") + ":"
431         for p in path:
432                 for o in overrides.split(":"):
433                         filespath.append(os.path.join(p, o))
434         return ":".join(filespath)