external.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import os.path
  2. import re
  3. import shlex
  4. import subprocess
  5. import oe.path
  6. import bb
  7. def run(d, cmd, *args):
  8. topdir = d.getVar('TOPDIR')
  9. toolchain_path = d.getVar('EXTERNAL_TOOLCHAIN')
  10. if toolchain_path:
  11. target_prefix = d.getVar('EXTERNAL_TARGET_SYS') + '-'
  12. if not cmd.startswith(target_prefix):
  13. cmd = target_prefix + cmd
  14. toolchain_bin = d.getVar('EXTERNAL_TOOLCHAIN_BIN')
  15. path = os.path.join(toolchain_bin, cmd)
  16. args = shlex.split(path) + list(args)
  17. bb.debug(1, 'oe.external.run({})'.format(repr(args)))
  18. try:
  19. output, _ = bb.process.run(args, cwd=topdir)
  20. except bb.process.CmdError as exc:
  21. bb.debug(1, 'oe.external.run: {} failed: {}'.format(subprocess.list2cmdline(args), exc))
  22. else:
  23. return output
  24. return 'UNKNOWN'
  25. def get_file_search_metadata(d):
  26. '''Given the metadata, return the mirrors and sysroots to operate against.'''
  27. mirrors = []
  28. for entry in d.getVar('FILES_MIRRORS').replace('\\n', '\n').split('\n'):
  29. entry = entry.strip()
  30. if not entry:
  31. continue
  32. try:
  33. pathname, subst = entry.strip().split('|', 1)
  34. except ValueError:
  35. bb.warn('Invalid FILES_MIRRORS entry: {0}'.format(entry))
  36. mirrors.append(('^' + re.escape(pathname), subst))
  37. source_paths = [os.path.realpath(p)
  38. for p in d.getVar('EXTERNAL_INSTALL_SOURCE_PATHS').split()]
  39. return source_paths, mirrors
  40. def gather_pkg_files(d):
  41. '''Given the metadata, return all the files we want to copy to ${D} for
  42. this recipe.'''
  43. import itertools
  44. files = []
  45. for pkg in d.getVar('PACKAGES').split():
  46. files = itertools.chain(files, (d.getVar('EXTERNAL_FILES_{}'.format(pkg)) or d.getVar('FILES_{}'.format(pkg)) or '').split())
  47. files = itertools.chain(files, d.getVar('EXTERNAL_EXTRA_FILES').split())
  48. return files
  49. def copy_from_sysroots(pathnames, sysroots, mirrors, installdest):
  50. '''Copy the specified files from the specified sysroots, also checking the
  51. specified mirror patterns as alternate paths, to the specified destination.'''
  52. expanded_pathnames = expand_paths(pathnames, mirrors)
  53. searched_paths = search_sysroots(expanded_pathnames, sysroots)
  54. for path, files in searched_paths:
  55. if not files:
  56. bb.debug(1, 'oe.external: failed to find `{}`'.format(path))
  57. else:
  58. destdir = oe.path.join(installdest, os.path.dirname(path))
  59. bb.utils.mkdirhier(destdir)
  60. subprocess.check_call(['cp', '-PR', '--preserve=mode,timestamps', '--no-preserve=ownership'] + list(files) + [destdir + '/'])
  61. bb.note('Copied `{}` to `{}/`'.format(', '.join(files), destdir))
  62. def expand_paths(pathnames, mirrors):
  63. '''Apply search/replace to paths to get alternate search paths.
  64. Returns a generator with tuples of (pathname, expanded_paths).'''
  65. import re
  66. for pathname in pathnames:
  67. expanded_paths = [pathname]
  68. for search, replace in mirrors:
  69. try:
  70. new_pathname = re.sub(search, replace, pathname, count=1)
  71. except re.error as exc:
  72. bb.warn("Invalid pattern for `%s`" % search)
  73. continue
  74. if new_pathname != pathname:
  75. expanded_paths.append(new_pathname)
  76. yield pathname, expanded_paths
  77. def search_sysroots(path_entries, sysroots):
  78. '''Search the supplied sysroots for the supplied paths, checking supplied
  79. alternate paths. Expects entries in the format (pathname, all_paths).
  80. Returns a generator with tuples of (pathname, found_paths).'''
  81. import glob
  82. import itertools
  83. for path, pathnames in path_entries:
  84. for sysroot, pathname in ((s, p) for s in sysroots
  85. for p in itertools.chain([path], pathnames)):
  86. check_path = sysroot + os.sep + pathname
  87. found_paths = glob.glob(check_path)
  88. if found_paths:
  89. yield path, found_paths
  90. break
  91. else:
  92. yield path, None
  93. def find_sysroot_files(paths, d):
  94. sysroots, mirrors = get_file_search_metadata(d)
  95. expanded = expand_paths(paths, mirrors)
  96. search_results = list(search_sysroots(expanded, sysroots))
  97. return [v for k, v in search_results]