toolchain.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. # SPDX-License-Identifier: GPL-2.0+
  2. # Copyright (c) 2012 The Chromium OS Authors.
  3. #
  4. import re
  5. import glob
  6. from html.parser import HTMLParser
  7. import os
  8. import sys
  9. import tempfile
  10. import urllib.request, urllib.error, urllib.parse
  11. from buildman import bsettings
  12. from u_boot_pylib import command
  13. from u_boot_pylib import terminal
  14. from u_boot_pylib import tools
  15. (PRIORITY_FULL_PREFIX, PRIORITY_PREFIX_GCC, PRIORITY_PREFIX_GCC_PATH,
  16. PRIORITY_CALC) = list(range(4))
  17. (VAR_CROSS_COMPILE, VAR_PATH, VAR_ARCH, VAR_MAKE_ARGS) = range(4)
  18. # Simple class to collect links from a page
  19. class MyHTMLParser(HTMLParser):
  20. def __init__(self, arch):
  21. """Create a new parser
  22. After the parser runs, self.links will be set to a list of the links
  23. to .xz archives found in the page, and self.arch_link will be set to
  24. the one for the given architecture (or None if not found).
  25. Args:
  26. arch: Architecture to search for
  27. """
  28. HTMLParser.__init__(self)
  29. self.arch_link = None
  30. self.links = []
  31. self.re_arch = re.compile('[-_]%s-' % arch)
  32. def handle_starttag(self, tag, attrs):
  33. if tag == 'a':
  34. for tag, value in attrs:
  35. if tag == 'href':
  36. if value and value.endswith('.xz'):
  37. self.links.append(value)
  38. if self.re_arch.search(value):
  39. self.arch_link = value
  40. class Toolchain:
  41. """A single toolchain
  42. Public members:
  43. gcc: Full path to C compiler
  44. path: Directory path containing C compiler
  45. cross: Cross compile string, e.g. 'arm-linux-'
  46. arch: Architecture of toolchain as determined from the first
  47. component of the filename. E.g. arm-linux-gcc becomes arm
  48. priority: Toolchain priority (0=highest, 20=lowest)
  49. override_toolchain: Toolchain to use for sandbox, overriding the normal
  50. one
  51. """
  52. def __init__(self, fname, test, verbose=False, priority=PRIORITY_CALC,
  53. arch=None, override_toolchain=None):
  54. """Create a new toolchain object.
  55. Args:
  56. fname: Filename of the gcc component
  57. test: True to run the toolchain to test it
  58. verbose: True to print out the information
  59. priority: Priority to use for this toolchain, or PRIORITY_CALC to
  60. calculate it
  61. """
  62. self.gcc = fname
  63. self.path = os.path.dirname(fname)
  64. self.override_toolchain = override_toolchain
  65. # Find the CROSS_COMPILE prefix to use for U-Boot. For example,
  66. # 'arm-linux-gnueabihf-gcc' turns into 'arm-linux-gnueabihf-'.
  67. basename = os.path.basename(fname)
  68. pos = basename.rfind('-')
  69. self.cross = basename[:pos + 1] if pos != -1 else ''
  70. # The architecture is the first part of the name
  71. pos = self.cross.find('-')
  72. if arch:
  73. self.arch = arch
  74. else:
  75. self.arch = self.cross[:pos] if pos != -1 else 'sandbox'
  76. if self.arch == 'sandbox' and override_toolchain:
  77. self.gcc = override_toolchain
  78. env = self.MakeEnvironment(False)
  79. # As a basic sanity check, run the C compiler with --version
  80. cmd = [fname, '--version']
  81. if priority == PRIORITY_CALC:
  82. self.priority = self.GetPriority(fname)
  83. else:
  84. self.priority = priority
  85. if test:
  86. result = command.run_pipe([cmd], capture=True, env=env,
  87. raise_on_error=False)
  88. self.ok = result.return_code == 0
  89. if verbose:
  90. print('Tool chain test: ', end=' ')
  91. if self.ok:
  92. print("OK, arch='%s', priority %d" % (self.arch,
  93. self.priority))
  94. else:
  95. print('BAD')
  96. print('Command: ', cmd)
  97. print(result.stdout)
  98. print(result.stderr)
  99. else:
  100. self.ok = True
  101. def GetPriority(self, fname):
  102. """Return the priority of the toolchain.
  103. Toolchains are ranked according to their suitability by their
  104. filename prefix.
  105. Args:
  106. fname: Filename of toolchain
  107. Returns:
  108. Priority of toolchain, PRIORITY_CALC=highest, 20=lowest.
  109. """
  110. priority_list = ['-elf', '-unknown-linux-gnu', '-linux',
  111. '-none-linux-gnueabi', '-none-linux-gnueabihf', '-uclinux',
  112. '-none-eabi', '-gentoo-linux-gnu', '-linux-gnueabi',
  113. '-linux-gnueabihf', '-le-linux', '-uclinux']
  114. for prio in range(len(priority_list)):
  115. if priority_list[prio] in fname:
  116. return PRIORITY_CALC + prio
  117. return PRIORITY_CALC + prio
  118. def GetWrapper(self, show_warning=True):
  119. """Get toolchain wrapper from the setting file.
  120. """
  121. value = ''
  122. for name, value in bsettings.get_items('toolchain-wrapper'):
  123. if not value:
  124. print("Warning: Wrapper not found")
  125. if value:
  126. value = value + ' '
  127. return value
  128. def GetEnvArgs(self, which):
  129. """Get an environment variable/args value based on the the toolchain
  130. Args:
  131. which: VAR_... value to get
  132. Returns:
  133. Value of that environment variable or arguments
  134. """
  135. if which == VAR_CROSS_COMPILE:
  136. wrapper = self.GetWrapper()
  137. base = '' if self.arch == 'sandbox' else self.path
  138. return wrapper + os.path.join(base, self.cross)
  139. elif which == VAR_PATH:
  140. return self.path
  141. elif which == VAR_ARCH:
  142. return self.arch
  143. elif which == VAR_MAKE_ARGS:
  144. args = self.MakeArgs()
  145. if args:
  146. return ' '.join(args)
  147. return ''
  148. else:
  149. raise ValueError('Unknown arg to GetEnvArgs (%d)' % which)
  150. def MakeEnvironment(self, full_path):
  151. """Returns an environment for using the toolchain.
  152. Thie takes the current environment and adds CROSS_COMPILE so that
  153. the tool chain will operate correctly. This also disables localized
  154. output and possibly unicode encoded output of all build tools by
  155. adding LC_ALL=C.
  156. Note that os.environb is used to obtain the environment, since in some
  157. cases the environment many contain non-ASCII characters and we see
  158. errors like:
  159. UnicodeEncodeError: 'utf-8' codec can't encode characters in position
  160. 569-570: surrogates not allowed
  161. Args:
  162. full_path: Return the full path in CROSS_COMPILE and don't set
  163. PATH
  164. Returns:
  165. Dict containing the (bytes) environment to use. This is based on the
  166. current environment, with changes as needed to CROSS_COMPILE, PATH
  167. and LC_ALL.
  168. """
  169. env = dict(os.environb)
  170. wrapper = self.GetWrapper()
  171. if self.override_toolchain:
  172. # We'll use MakeArgs() to provide this
  173. pass
  174. elif full_path:
  175. env[b'CROSS_COMPILE'] = tools.to_bytes(
  176. wrapper + os.path.join(self.path, self.cross))
  177. else:
  178. env[b'CROSS_COMPILE'] = tools.to_bytes(wrapper + self.cross)
  179. env[b'PATH'] = tools.to_bytes(self.path) + b':' + env[b'PATH']
  180. env[b'LC_ALL'] = b'C'
  181. return env
  182. def MakeArgs(self):
  183. """Create the 'make' arguments for a toolchain
  184. This is only used when the toolchain is being overridden. Since the
  185. U-Boot Makefile sets CC and HOSTCC explicitly we cannot rely on the
  186. environment (and MakeEnvironment()) to override these values. This
  187. function returns the arguments to accomplish this.
  188. Returns:
  189. List of arguments to pass to 'make'
  190. """
  191. if self.override_toolchain:
  192. return ['HOSTCC=%s' % self.override_toolchain,
  193. 'CC=%s' % self.override_toolchain]
  194. return []
  195. class Toolchains:
  196. """Manage a list of toolchains for building U-Boot
  197. We select one toolchain for each architecture type
  198. Public members:
  199. toolchains: Dict of Toolchain objects, keyed by architecture name
  200. prefixes: Dict of prefixes to check, keyed by architecture. This can
  201. be a full path and toolchain prefix, for example
  202. {'x86', 'opt/i386-linux/bin/i386-linux-'}, or the name of
  203. something on the search path, for example
  204. {'arm', 'arm-linux-gnueabihf-'}. Wildcards are not supported.
  205. paths: List of paths to check for toolchains (may contain wildcards)
  206. """
  207. def __init__(self, override_toolchain=None):
  208. self.toolchains = {}
  209. self.prefixes = {}
  210. self.paths = []
  211. self.override_toolchain = override_toolchain
  212. self._make_flags = dict(bsettings.get_items('make-flags'))
  213. def GetPathList(self, show_warning=True):
  214. """Get a list of available toolchain paths
  215. Args:
  216. show_warning: True to show a warning if there are no tool chains.
  217. Returns:
  218. List of strings, each a path to a toolchain mentioned in the
  219. [toolchain] section of the settings file.
  220. """
  221. toolchains = bsettings.get_items('toolchain')
  222. if show_warning and not toolchains:
  223. print(("Warning: No tool chains. Please run 'buildman "
  224. "--fetch-arch all' to download all available toolchains, or "
  225. "add a [toolchain] section to your buildman config file "
  226. "%s. See buildman.rst for details" %
  227. bsettings.config_fname))
  228. paths = []
  229. for name, value in toolchains:
  230. if '*' in value:
  231. paths += glob.glob(value)
  232. else:
  233. paths.append(value)
  234. return paths
  235. def GetSettings(self, show_warning=True):
  236. """Get toolchain settings from the settings file.
  237. Args:
  238. show_warning: True to show a warning if there are no tool chains.
  239. """
  240. self.prefixes = bsettings.get_items('toolchain-prefix')
  241. self.paths += self.GetPathList(show_warning)
  242. def Add(self, fname, test=True, verbose=False, priority=PRIORITY_CALC,
  243. arch=None):
  244. """Add a toolchain to our list
  245. We select the given toolchain as our preferred one for its
  246. architecture if it is a higher priority than the others.
  247. Args:
  248. fname: Filename of toolchain's gcc driver
  249. test: True to run the toolchain to test it
  250. priority: Priority to use for this toolchain
  251. arch: Toolchain architecture, or None if not known
  252. """
  253. toolchain = Toolchain(fname, test, verbose, priority, arch,
  254. self.override_toolchain)
  255. add_it = toolchain.ok
  256. if toolchain.arch in self.toolchains:
  257. add_it = (toolchain.priority <
  258. self.toolchains[toolchain.arch].priority)
  259. if add_it:
  260. self.toolchains[toolchain.arch] = toolchain
  261. elif verbose:
  262. print(("Toolchain '%s' at priority %d will be ignored because "
  263. "another toolchain for arch '%s' has priority %d" %
  264. (toolchain.gcc, toolchain.priority, toolchain.arch,
  265. self.toolchains[toolchain.arch].priority)))
  266. def ScanPath(self, path, verbose):
  267. """Scan a path for a valid toolchain
  268. Args:
  269. path: Path to scan
  270. verbose: True to print out progress information
  271. Returns:
  272. Filename of C compiler if found, else None
  273. """
  274. fnames = []
  275. for subdir in ['.', 'bin', 'usr/bin']:
  276. dirname = os.path.join(path, subdir)
  277. if verbose: print(" - looking in '%s'" % dirname)
  278. for fname in glob.glob(dirname + '/*gcc'):
  279. if verbose: print(" - found '%s'" % fname)
  280. fnames.append(fname)
  281. return fnames
  282. def ScanPathEnv(self, fname):
  283. """Scan the PATH environment variable for a given filename.
  284. Args:
  285. fname: Filename to scan for
  286. Returns:
  287. List of matching pathanames, or [] if none
  288. """
  289. pathname_list = []
  290. for path in os.environ["PATH"].split(os.pathsep):
  291. path = path.strip('"')
  292. pathname = os.path.join(path, fname)
  293. if os.path.exists(pathname):
  294. pathname_list.append(pathname)
  295. return pathname_list
  296. def Scan(self, verbose):
  297. """Scan for available toolchains and select the best for each arch.
  298. We look for all the toolchains we can file, figure out the
  299. architecture for each, and whether it works. Then we select the
  300. highest priority toolchain for each arch.
  301. Args:
  302. verbose: True to print out progress information
  303. """
  304. if verbose: print('Scanning for tool chains')
  305. for name, value in self.prefixes:
  306. if verbose: print(" - scanning prefix '%s'" % value)
  307. if os.path.exists(value):
  308. self.Add(value, True, verbose, PRIORITY_FULL_PREFIX, name)
  309. continue
  310. fname = value + 'gcc'
  311. if os.path.exists(fname):
  312. self.Add(fname, True, verbose, PRIORITY_PREFIX_GCC, name)
  313. continue
  314. fname_list = self.ScanPathEnv(fname)
  315. for f in fname_list:
  316. self.Add(f, True, verbose, PRIORITY_PREFIX_GCC_PATH, name)
  317. if not fname_list:
  318. raise ValueError("No tool chain found for prefix '%s'" %
  319. value)
  320. for path in self.paths:
  321. if verbose: print(" - scanning path '%s'" % path)
  322. fnames = self.ScanPath(path, verbose)
  323. for fname in fnames:
  324. self.Add(fname, True, verbose)
  325. def List(self):
  326. """List out the selected toolchains for each architecture"""
  327. col = terminal.Color()
  328. print(col.build(col.BLUE, 'List of available toolchains (%d):' %
  329. len(self.toolchains)))
  330. if len(self.toolchains):
  331. for key, value in sorted(self.toolchains.items()):
  332. print('%-10s: %s' % (key, value.gcc))
  333. else:
  334. print('None')
  335. def Select(self, arch):
  336. """Returns the toolchain for a given architecture
  337. Args:
  338. args: Name of architecture (e.g. 'arm', 'ppc_8xx')
  339. returns:
  340. toolchain object, or None if none found
  341. """
  342. for tag, value in bsettings.get_items('toolchain-alias'):
  343. if arch == tag:
  344. for alias in value.split():
  345. if alias in self.toolchains:
  346. return self.toolchains[alias]
  347. if not arch in self.toolchains:
  348. raise ValueError("No tool chain found for arch '%s'" % arch)
  349. return self.toolchains[arch]
  350. def ResolveReferences(self, var_dict, args):
  351. """Resolve variable references in a string
  352. This converts ${blah} within the string to the value of blah.
  353. This function works recursively.
  354. Args:
  355. var_dict: Dictionary containing variables and their values
  356. args: String containing make arguments
  357. Returns:
  358. Resolved string
  359. >>> bsettings.setup(None)
  360. >>> tcs = Toolchains()
  361. >>> tcs.Add('fred', False)
  362. >>> var_dict = {'oblique' : 'OBLIQUE', 'first' : 'fi${second}rst', \
  363. 'second' : '2nd'}
  364. >>> tcs.ResolveReferences(var_dict, 'this=${oblique}_set')
  365. 'this=OBLIQUE_set'
  366. >>> tcs.ResolveReferences(var_dict, 'this=${oblique}_set${first}nd')
  367. 'this=OBLIQUE_setfi2ndrstnd'
  368. """
  369. re_var = re.compile('(\$\{[-_a-z0-9A-Z]{1,}\})')
  370. while True:
  371. m = re_var.search(args)
  372. if not m:
  373. break
  374. lookup = m.group(0)[2:-1]
  375. value = var_dict.get(lookup, '')
  376. args = args[:m.start(0)] + value + args[m.end(0):]
  377. return args
  378. def GetMakeArguments(self, brd):
  379. """Returns 'make' arguments for a given board
  380. The flags are in a section called 'make-flags'. Flags are named
  381. after the target they represent, for example snapper9260=TESTING=1
  382. will pass TESTING=1 to make when building the snapper9260 board.
  383. References to other boards can be added in the string also. For
  384. example:
  385. [make-flags]
  386. at91-boards=ENABLE_AT91_TEST=1
  387. snapper9260=${at91-boards} BUILD_TAG=442
  388. snapper9g45=${at91-boards} BUILD_TAG=443
  389. This will return 'ENABLE_AT91_TEST=1 BUILD_TAG=442' for snapper9260
  390. and 'ENABLE_AT91_TEST=1 BUILD_TAG=443' for snapper9g45.
  391. A special 'target' variable is set to the board target.
  392. Args:
  393. brd: Board object for the board to check.
  394. Returns:
  395. 'make' flags for that board, or '' if none
  396. """
  397. self._make_flags['target'] = brd.target
  398. arg_str = self.ResolveReferences(self._make_flags,
  399. self._make_flags.get(brd.target, ''))
  400. args = re.findall("(?:\".*?\"|\S)+", arg_str)
  401. i = 0
  402. while i < len(args):
  403. args[i] = args[i].replace('"', '')
  404. if not args[i]:
  405. del args[i]
  406. else:
  407. i += 1
  408. return args
  409. def LocateArchUrl(self, fetch_arch):
  410. """Find a toolchain available online
  411. Look in standard places for available toolchains. At present the
  412. only standard place is at kernel.org.
  413. Args:
  414. arch: Architecture to look for, or 'list' for all
  415. Returns:
  416. If fetch_arch is 'list', a tuple:
  417. Machine architecture (e.g. x86_64)
  418. List of toolchains
  419. else
  420. URL containing this toolchain, if avaialble, else None
  421. """
  422. arch = command.output_one_line('uname', '-m')
  423. if arch == 'aarch64':
  424. arch = 'arm64'
  425. base = 'https://www.kernel.org/pub/tools/crosstool/files/bin'
  426. versions = ['13.1.0', '12.2.0']
  427. links = []
  428. for version in versions:
  429. url = '%s/%s/%s/' % (base, arch, version)
  430. print('Checking: %s' % url)
  431. response = urllib.request.urlopen(url)
  432. html = tools.to_string(response.read())
  433. parser = MyHTMLParser(fetch_arch)
  434. parser.feed(html)
  435. if fetch_arch == 'list':
  436. links += parser.links
  437. elif parser.arch_link:
  438. return url + parser.arch_link
  439. if fetch_arch == 'list':
  440. return arch, links
  441. return None
  442. def Unpack(self, fname, dest):
  443. """Unpack a tar file
  444. Args:
  445. fname: Filename to unpack
  446. dest: Destination directory
  447. Returns:
  448. Directory name of the first entry in the archive, without the
  449. trailing /
  450. """
  451. stdout = command.output('tar', 'xvfJ', fname, '-C', dest)
  452. dirs = stdout.splitlines()[1].split('/')[:2]
  453. return '/'.join(dirs)
  454. def TestSettingsHasPath(self, path):
  455. """Check if buildman will find this toolchain
  456. Returns:
  457. True if the path is in settings, False if not
  458. """
  459. paths = self.GetPathList(False)
  460. return path in paths
  461. def ListArchs(self):
  462. """List architectures with available toolchains to download"""
  463. host_arch, archives = self.LocateArchUrl('list')
  464. re_arch = re.compile('[-a-z0-9.]*[-_]([^-]*)-.*')
  465. arch_set = set()
  466. for archive in archives:
  467. # Remove the host architecture from the start
  468. arch = re_arch.match(archive[len(host_arch):])
  469. if arch:
  470. if arch.group(1) != '2.0' and arch.group(1) != '64':
  471. arch_set.add(arch.group(1))
  472. return sorted(arch_set)
  473. def FetchAndInstall(self, arch):
  474. """Fetch and install a new toolchain
  475. arch:
  476. Architecture to fetch, or 'list' to list
  477. """
  478. # Fist get the URL for this architecture
  479. col = terminal.Color()
  480. print(col.build(col.BLUE, "Downloading toolchain for arch '%s'" % arch))
  481. url = self.LocateArchUrl(arch)
  482. if not url:
  483. print(("Cannot find toolchain for arch '%s' - use 'list' to list" %
  484. arch))
  485. return 2
  486. home = os.environ['HOME']
  487. dest = os.path.join(home, '.buildman-toolchains')
  488. if not os.path.exists(dest):
  489. os.mkdir(dest)
  490. # Download the tar file for this toolchain and unpack it
  491. tarfile, tmpdir = tools.download(url, '.buildman')
  492. if not tarfile:
  493. return 1
  494. print(col.build(col.GREEN, 'Unpacking to: %s' % dest), end=' ')
  495. sys.stdout.flush()
  496. path = self.Unpack(tarfile, dest)
  497. os.remove(tarfile)
  498. os.rmdir(tmpdir)
  499. print()
  500. # Check that the toolchain works
  501. print(col.build(col.GREEN, 'Testing'))
  502. dirpath = os.path.join(dest, path)
  503. compiler_fname_list = self.ScanPath(dirpath, True)
  504. if not compiler_fname_list:
  505. print('Could not locate C compiler - fetch failed.')
  506. return 1
  507. if len(compiler_fname_list) != 1:
  508. print(col.build(col.RED, 'Warning, ambiguous toolchains: %s' %
  509. ', '.join(compiler_fname_list)))
  510. toolchain = Toolchain(compiler_fname_list[0], True, True)
  511. # Make sure that it will be found by buildman
  512. if not self.TestSettingsHasPath(dirpath):
  513. print(("Adding 'download' to config file '%s'" %
  514. bsettings.config_fname))
  515. bsettings.set_item('toolchain', 'download', '%s/*/*' % dest)
  516. return 0