utils.bbclass 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. oe_soinstall() {
  7. # Purpose: Install shared library file and
  8. # create the necessary links
  9. # Example: oe_soinstall libfoo.so.1.2.3 ${D}${libdir}
  10. libname=`basename $1`
  11. case "$libname" in
  12. *.so)
  13. bbfatal "oe_soinstall: Shared library must haved versioned filename (e.g. libfoo.so.1.2.3)"
  14. ;;
  15. esac
  16. install -m 755 $1 $2/$libname
  17. sonamelink=`${OBJDUMP} -p $1 | grep SONAME | awk '{print $2}'`
  18. if [ -z $sonamelink ]; then
  19. bbfatal "oe_soinstall: $libname is missing ELF tag 'SONAME'."
  20. fi
  21. solink=`echo $libname | sed -e 's/\.so\..*/.so/'`
  22. ln -sf $libname $2/$sonamelink
  23. ln -sf $libname $2/$solink
  24. }
  25. oe_libinstall() {
  26. # Purpose: Install a library, in all its forms
  27. # Example
  28. #
  29. # oe_libinstall libltdl ${STAGING_LIBDIR}/
  30. # oe_libinstall -C src/libblah libblah ${D}/${libdir}/
  31. dir=""
  32. libtool=""
  33. silent=""
  34. require_static=""
  35. require_shared=""
  36. while [ "$#" -gt 0 ]; do
  37. case "$1" in
  38. -C)
  39. shift
  40. dir="$1"
  41. ;;
  42. -s)
  43. silent=1
  44. ;;
  45. -a)
  46. require_static=1
  47. ;;
  48. -so)
  49. require_shared=1
  50. ;;
  51. -*)
  52. bbfatal "oe_libinstall: unknown option: $1"
  53. ;;
  54. *)
  55. break;
  56. ;;
  57. esac
  58. shift
  59. done
  60. libname="$1"
  61. shift
  62. destpath="$1"
  63. if [ -z "$destpath" ]; then
  64. bbfatal "oe_libinstall: no destination path specified"
  65. fi
  66. __runcmd () {
  67. if [ -z "$silent" ]; then
  68. echo >&2 "oe_libinstall: $*"
  69. fi
  70. $*
  71. }
  72. if [ -z "$dir" ]; then
  73. dir=`pwd`
  74. fi
  75. dotlai=$libname.lai
  76. # Sanity check that the libname.lai is unique
  77. number_of_files=`(cd $dir; find . -name "$dotlai") | wc -l`
  78. if [ $number_of_files -gt 1 ]; then
  79. bbfatal "oe_libinstall: $dotlai is not unique in $dir"
  80. fi
  81. dir=$dir`(cd $dir;find . -name "$dotlai") | sed "s/^\.//;s/\/$dotlai\$//;q"`
  82. olddir=`pwd`
  83. __runcmd cd $dir
  84. lafile=$libname.la
  85. # If such file doesn't exist, try to cut version suffix
  86. if [ ! -f "$lafile" ]; then
  87. libname1=`echo "$libname" | sed 's/-[0-9.]*$//'`
  88. lafile1=$libname.la
  89. if [ -f "$lafile1" ]; then
  90. libname=$libname1
  91. lafile=$lafile1
  92. fi
  93. fi
  94. if [ -f "$lafile" ]; then
  95. # libtool archive
  96. eval `cat $lafile|grep "^library_names="`
  97. libtool=1
  98. else
  99. library_names="$libname.so* $libname.dll.a $libname.*.dylib"
  100. fi
  101. __runcmd install -d $destpath/
  102. dota=$libname.a
  103. if [ -f "$dota" -o -n "$require_static" ]; then
  104. rm -f $destpath/$dota
  105. __runcmd install -m 0644 $dota $destpath/
  106. fi
  107. if [ -f "$dotlai" -a -n "$libtool" ]; then
  108. rm -f $destpath/$libname.la
  109. __runcmd install -m 0644 $dotlai $destpath/$libname.la
  110. fi
  111. for name in $library_names; do
  112. files=`eval echo $name`
  113. for f in $files; do
  114. if [ ! -e "$f" ]; then
  115. if [ -n "$libtool" ]; then
  116. bbfatal "oe_libinstall: $dir/$f not found."
  117. fi
  118. elif [ -L "$f" ]; then
  119. __runcmd cp -P "$f" $destpath/
  120. elif [ ! -L "$f" ]; then
  121. libfile="$f"
  122. rm -f $destpath/$libfile
  123. __runcmd install -m 0755 $libfile $destpath/
  124. fi
  125. done
  126. done
  127. if [ -z "$libfile" ]; then
  128. if [ -n "$require_shared" ]; then
  129. bbfatal "oe_libinstall: unable to locate shared library"
  130. fi
  131. elif [ -z "$libtool" ]; then
  132. # special case hack for non-libtool .so.#.#.# links
  133. baselibfile=`basename "$libfile"`
  134. if (echo $baselibfile | grep -qE '^lib.*\.so\.[0-9.]*$'); then
  135. sonamelink=`${OBJDUMP} -p $libfile | grep SONAME | awk '{print $2}'`
  136. solink=`echo $baselibfile | sed -e 's/\.so\..*/.so/'`
  137. if [ -n "$sonamelink" -a x"$baselibfile" != x"$sonamelink" ]; then
  138. __runcmd ln -sf $baselibfile $destpath/$sonamelink
  139. fi
  140. __runcmd ln -sf $baselibfile $destpath/$solink
  141. fi
  142. fi
  143. __runcmd cd "$olddir"
  144. }
  145. create_cmdline_wrapper () {
  146. # Create a wrapper script where commandline options are needed
  147. #
  148. # These are useful to work around relocation issues, by passing extra options
  149. # to a program
  150. #
  151. # Usage: create_cmdline_wrapper FILENAME <extra-options>
  152. cmd=$1
  153. shift
  154. echo "Generating wrapper script for $cmd"
  155. mv $cmd $cmd.real
  156. cmdname=`basename $cmd`
  157. dirname=`dirname $cmd`
  158. cmdoptions=$@
  159. if [ "${base_prefix}" != "" ]; then
  160. relpath=`python3 -c "import os; print(os.path.relpath('${D}${base_prefix}', '$dirname'))"`
  161. cmdoptions=`echo $@ | sed -e "s:${base_prefix}:\\$realdir/$relpath:g"`
  162. fi
  163. cat <<END >$cmd
  164. #!/bin/bash
  165. realpath=\`readlink -fn \$0\`
  166. realdir=\`dirname \$realpath\`
  167. exec -a \$realdir/$cmdname \$realdir/$cmdname.real $cmdoptions "\$@"
  168. END
  169. chmod +x $cmd
  170. }
  171. create_cmdline_shebang_wrapper () {
  172. # Create a wrapper script where commandline options are needed
  173. #
  174. # These are useful to work around shebang relocation issues, where shebangs are too
  175. # long or have arguments in them, thus preventing them from using the /usr/bin/env
  176. # shebang
  177. #
  178. # Usage: create_cmdline_wrapper FILENAME <extra-options>
  179. cmd=$1
  180. shift
  181. echo "Generating wrapper script for $cmd"
  182. # Strip #! and get remaining interpreter + arg
  183. argument="$(sed -ne 's/^#! *//p;q' $cmd)"
  184. # strip the shebang from the real script as we do not want it to be usable anyway
  185. tail -n +2 $cmd > $cmd.real
  186. chown --reference=$cmd $cmd.real
  187. chmod --reference=$cmd $cmd.real
  188. rm -f $cmd
  189. cmdname=$(basename $cmd)
  190. dirname=$(dirname $cmd)
  191. cmdoptions=$@
  192. if [ "${base_prefix}" != "" ]; then
  193. relpath=`python3 -c "import os; print(os.path.relpath('${D}${base_prefix}', '$dirname'))"`
  194. cmdoptions=`echo $@ | sed -e "s:${base_prefix}:\\$realdir/$relpath:g"`
  195. fi
  196. cat <<END >$cmd
  197. #!/usr/bin/env bash
  198. realpath=\`readlink -fn \$0\`
  199. realdir=\`dirname \$realpath\`
  200. exec -a \$realdir/$cmdname $argument \$realdir/$cmdname.real $cmdoptions "\$@"
  201. END
  202. chmod +x $cmd
  203. }
  204. create_wrapper () {
  205. # Create a wrapper script where extra environment variables are needed
  206. #
  207. # These are useful to work around relocation issues, by setting environment
  208. # variables which point to paths in the filesystem.
  209. #
  210. # Usage: create_wrapper FILENAME [[VAR=VALUE]..]
  211. cmd=$1
  212. shift
  213. echo "Generating wrapper script for $cmd"
  214. mv $cmd $cmd.real
  215. cmdname=`basename $cmd`
  216. dirname=`dirname $cmd`
  217. exportstring=$@
  218. if [ "${base_prefix}" != "" ]; then
  219. relpath=`python3 -c "import os; print(os.path.relpath('${D}${base_prefix}', '$dirname'))"`
  220. exportstring=`echo $@ | sed -e "s:${base_prefix}:\\$realdir/$relpath:g"`
  221. fi
  222. cat <<END >$cmd
  223. #!/bin/bash
  224. realpath=\`readlink -fn \$0\`
  225. realdir=\`dirname \$realpath\`
  226. export $exportstring
  227. exec -a "\$0" \$realdir/$cmdname.real "\$@"
  228. END
  229. chmod +x $cmd
  230. }
  231. # Copy files/directories from $1 to $2 but using hardlinks
  232. # (preserve symlinks)
  233. hardlinkdir () {
  234. from=$1
  235. to=$2
  236. (cd $from; find . -print0 | cpio --null -pdlu $to)
  237. }
  238. def check_app_exists(app, d):
  239. app = d.expand(app).split()[0].strip()
  240. path = d.getVar('PATH')
  241. return bool(bb.utils.which(path, app))
  242. def explode_deps(s):
  243. return bb.utils.explode_deps(s)
  244. def base_set_filespath(path, d):
  245. filespath = []
  246. extrapaths = (d.getVar("FILESEXTRAPATHS") or "")
  247. # Remove default flag which was used for checking
  248. extrapaths = extrapaths.replace("__default:", "")
  249. # Don't prepend empty strings to the path list
  250. if extrapaths != "":
  251. path = extrapaths.split(":") + path
  252. # The ":" ensures we have an 'empty' override
  253. overrides = (":" + (d.getVar("FILESOVERRIDES") or "")).split(":")
  254. overrides.reverse()
  255. for o in overrides:
  256. for p in path:
  257. if p != "":
  258. filespath.append(os.path.join(p, o))
  259. return ":".join(filespath)
  260. def extend_variants(d, var, extend, delim=':'):
  261. """Return a string of all bb class extend variants for the given extend"""
  262. variants = []
  263. whole = d.getVar(var) or ""
  264. for ext in whole.split():
  265. eext = ext.split(delim)
  266. if len(eext) > 1 and eext[0] == extend:
  267. variants.append(eext[1])
  268. return " ".join(variants)
  269. def multilib_pkg_extend(d, pkg):
  270. variants = (d.getVar("MULTILIB_VARIANTS") or "").split()
  271. if not variants:
  272. return pkg
  273. pkgs = pkg
  274. for v in variants:
  275. pkgs = pkgs + " " + v + "-" + pkg
  276. return pkgs
  277. def get_multilib_datastore(variant, d):
  278. return oe.utils.get_multilib_datastore(variant, d)
  279. def all_multilib_tune_values(d, var, unique = True, need_split = True, delim = ' '):
  280. """Return a string of all ${var} in all multilib tune configuration"""
  281. values = []
  282. variants = (d.getVar("MULTILIB_VARIANTS") or "").split() + ['']
  283. for item in variants:
  284. localdata = get_multilib_datastore(item, d)
  285. # We need WORKDIR to be consistent with the original datastore
  286. localdata.setVar("WORKDIR", d.getVar("WORKDIR"))
  287. value = localdata.getVar(var) or ""
  288. if value != "":
  289. if need_split:
  290. for item in value.split(delim):
  291. values.append(item)
  292. else:
  293. values.append(value)
  294. if unique:
  295. #we do this to keep order as much as possible
  296. ret = []
  297. for value in values:
  298. if not value in ret:
  299. ret.append(value)
  300. else:
  301. ret = values
  302. return " ".join(ret)
  303. def all_multilib_tune_list(vars, d):
  304. """
  305. Return a list of ${VAR} for each variable VAR in vars from each
  306. multilib tune configuration.
  307. Is safe to be called from a multilib recipe/context as it can
  308. figure out the original tune and remove the multilib overrides.
  309. """
  310. values = {}
  311. for v in vars:
  312. values[v] = []
  313. values['ml'] = ['']
  314. variants = (d.getVar("MULTILIB_VARIANTS") or "").split() + ['']
  315. for item in variants:
  316. localdata = get_multilib_datastore(item, d)
  317. values[v].append(localdata.getVar(v))
  318. values['ml'].append(item)
  319. return values
  320. all_multilib_tune_list[vardepsexclude] = "OVERRIDES"
  321. # If the user hasn't set up their name/email, set some defaults
  322. check_git_config() {
  323. if ! git config user.email > /dev/null ; then
  324. git config --local user.email "${PATCH_GIT_USER_EMAIL}"
  325. fi
  326. if ! git config user.name > /dev/null ; then
  327. git config --local user.name "${PATCH_GIT_USER_NAME}"
  328. fi
  329. }
  330. # Sets fixed git committer and author for reproducible commits
  331. reproducible_git_committer_author() {
  332. export GIT_COMMITTER_NAME="${PATCH_GIT_USER_NAME}"
  333. export GIT_COMMITTER_EMAIL="${PATCH_GIT_USER_EMAIL}"
  334. export GIT_COMMITTER_DATE="$(date -d @${SOURCE_DATE_EPOCH})"
  335. export GIT_AUTHOR_NAME="${PATCH_GIT_USER_NAME}"
  336. export GIT_AUTHOR_EMAIL="${PATCH_GIT_USER_EMAIL}"
  337. export GIT_AUTHOR_DATE="$(date -d @${SOURCE_DATE_EPOCH})"
  338. }