main.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. # Copyright (c) 2016 Google, Inc
  4. # Written by Simon Glass <sjg@chromium.org>
  5. #
  6. # Creates binary images from input files controlled by a description
  7. #
  8. """See README for more information"""
  9. import os
  10. import site
  11. import sys
  12. import traceback
  13. # Get the absolute path to this file at run-time
  14. our_path = os.path.dirname(os.path.realpath(__file__))
  15. our1_path = os.path.dirname(our_path)
  16. our2_path = os.path.dirname(our1_path)
  17. # Extract $(srctree) from Kbuild environment, or use relative paths below
  18. srctree = os.environ.get('srctree', our2_path)
  19. #
  20. # Do not pollute source tree with cache files:
  21. # https://stackoverflow.com/a/60024195/2511795
  22. # https://bugs.python.org/issue33499
  23. #
  24. sys.pycache_prefix = os.path.relpath(our_path, srctree)
  25. # Bring in the patman and dtoc libraries (but don't override the first path
  26. # in PYTHONPATH)
  27. sys.path.insert(2, our1_path)
  28. from binman import bintool
  29. from u_boot_pylib import test_util
  30. # Bring in the libfdt module
  31. sys.path.insert(2, 'scripts/dtc/pylibfdt')
  32. sys.path.insert(2, os.path.join(srctree, 'scripts/dtc/pylibfdt'))
  33. sys.path.insert(2, os.path.join(srctree, 'build-sandbox/scripts/dtc/pylibfdt'))
  34. sys.path.insert(2, os.path.join(srctree, 'build-sandbox_spl/scripts/dtc/pylibfdt'))
  35. from binman import cmdline
  36. from binman import control
  37. from u_boot_pylib import test_util
  38. def RunTests(debug, verbosity, processes, test_preserve_dirs, args, toolpath):
  39. """Run the functional tests and any embedded doctests
  40. Args:
  41. debug: True to enable debugging, which shows a full stack trace on error
  42. verbosity: Verbosity level to use
  43. test_preserve_dirs: True to preserve the input directory used by tests
  44. so that it can be examined afterwards (only useful for debugging
  45. tests). If a single test is selected (in args[0]) it also preserves
  46. the output directory for this test. Both directories are displayed
  47. on the command line.
  48. processes: Number of processes to use to run tests (None=same as #CPUs)
  49. args: List of positional args provided to binman. This can hold a test
  50. name to execute (as in 'binman test testSections', for example)
  51. toolpath: List of paths to use for tools
  52. """
  53. from binman import bintool_test
  54. from binman import cbfs_util_test
  55. from binman import elf_test
  56. from binman import entry_test
  57. from binman import fdt_test
  58. from binman import fip_util_test
  59. from binman import ftest
  60. from binman import image_test
  61. import doctest
  62. test_name = args and args[0] or None
  63. # Run the entry tests first ,since these need to be the first to import the
  64. # 'entry' module.
  65. result = test_util.run_test_suites(
  66. 'binman', debug, verbosity, test_preserve_dirs, processes, test_name,
  67. toolpath,
  68. [bintool_test.TestBintool, entry_test.TestEntry, ftest.TestFunctional,
  69. fdt_test.TestFdt, elf_test.TestElf, image_test.TestImage,
  70. cbfs_util_test.TestCbfs, fip_util_test.TestFip])
  71. return (0 if result.wasSuccessful() else 1)
  72. def RunTestCoverage(toolpath, build_dir):
  73. """Run the tests and check that we get 100% coverage"""
  74. glob_list = control.GetEntryModules(False)
  75. all_set = set([os.path.splitext(os.path.basename(item))[0]
  76. for item in glob_list if '_testing' not in item])
  77. extra_args = ''
  78. if toolpath:
  79. for path in toolpath:
  80. extra_args += ' --toolpath %s' % path
  81. test_util.run_test_coverage('tools/binman/binman', None,
  82. ['*test*', '*main.py', 'tools/patman/*', 'tools/dtoc/*',
  83. 'tools/u_boot_pylib/*'],
  84. build_dir, all_set, extra_args or None)
  85. def RunBinman(args):
  86. """Main entry point to binman once arguments are parsed
  87. Args:
  88. args: Command line arguments Namespace object
  89. """
  90. ret_code = 0
  91. if not args.debug:
  92. sys.tracebacklimit = 0
  93. # Provide a default toolpath in the hope of finding a mkimage built from
  94. # current source
  95. if not args.toolpath:
  96. args.toolpath = ['./tools', 'build-sandbox/tools']
  97. if args.cmd == 'test':
  98. if args.test_coverage:
  99. RunTestCoverage(args.toolpath, args.build_dir)
  100. else:
  101. ret_code = RunTests(args.debug, args.verbosity, args.processes,
  102. args.test_preserve_dirs, args.tests,
  103. args.toolpath)
  104. elif args.cmd == 'bintool-docs':
  105. control.write_bintool_docs(bintool.Bintool.get_tool_list())
  106. elif args.cmd == 'entry-docs':
  107. control.WriteEntryDocs(control.GetEntryModules())
  108. else:
  109. try:
  110. ret_code = control.Binman(args)
  111. except Exception as e:
  112. print('binman: %s' % e, file=sys.stderr)
  113. if args.debug:
  114. print()
  115. traceback.print_exc()
  116. ret_code = 1
  117. return ret_code
  118. def start_binman():
  119. args = cmdline.ParseArgs(sys.argv[1:])
  120. ret_code = RunBinman(args)
  121. sys.exit(ret_code)
  122. if __name__ == "__main__":
  123. start_binman()