main.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0+
  3. #
  4. # Copyright (C) 2016 Google, Inc
  5. # Written by Simon Glass <sjg@chromium.org>
  6. #
  7. """Device tree to C tool
  8. This tool converts a device tree binary file (.dtb) into two C files. The
  9. indent is to allow a C program to access data from the device tree without
  10. having to link against libfdt. By putting the data from the device tree into
  11. C structures, normal C code can be used. This helps to reduce the size of the
  12. compiled program.
  13. Dtoc produces several output files - see OUTPUT_FILES in dtb_platdata.py
  14. This tool is used in U-Boot to provide device tree data to SPL without
  15. increasing the code size of SPL. This supports the CONFIG_SPL_OF_PLATDATA
  16. options. For more information about the use of this options and tool please
  17. see doc/driver-model/of-plat.rst
  18. """
  19. from argparse import ArgumentParser
  20. import os
  21. import sys
  22. import unittest
  23. # Bring in the patman libraries
  24. our_path = os.path.dirname(os.path.realpath(__file__))
  25. sys.path.append(os.path.join(our_path, '..'))
  26. # Bring in the libfdt module
  27. sys.path.insert(0, 'scripts/dtc/pylibfdt')
  28. sys.path.insert(0, os.path.join(our_path,
  29. '../../build-sandbox_spl/scripts/dtc/pylibfdt'))
  30. from dtoc import dtb_platdata
  31. from patman import test_util
  32. def run_tests(processes, args):
  33. """Run all the test we have for dtoc
  34. Args:
  35. processes: Number of processes to use to run tests (None=same as #CPUs)
  36. args: List of positional args provided to dtoc. This can hold a test
  37. name to execute (as in 'dtoc -t test_empty_file', for example)
  38. """
  39. from dtoc import test_src_scan
  40. from dtoc import test_dtoc
  41. result = unittest.TestResult()
  42. sys.argv = [sys.argv[0]]
  43. test_name = args.files and args.files[0] or None
  44. test_dtoc.setup()
  45. test_util.RunTestSuites(
  46. result, debug=True, verbosity=1, test_preserve_dirs=False,
  47. processes=processes, test_name=test_name, toolpath=[],
  48. test_class_list=[test_dtoc.TestDtoc,test_src_scan.TestSrcScan])
  49. return test_util.ReportResult('binman', test_name, result)
  50. def RunTestCoverage():
  51. """Run the tests and check that we get 100% coverage"""
  52. sys.argv = [sys.argv[0]]
  53. test_util.RunTestCoverage('tools/dtoc/dtoc', '/main.py',
  54. ['tools/patman/*.py', '*/fdt*', '*test*'], args.build_dir)
  55. if __name__ != '__main__':
  56. sys.exit(1)
  57. epilog = '''Generate C code from devicetree files. See of-plat.rst for details'''
  58. parser = ArgumentParser(epilog=epilog)
  59. parser.add_argument('-B', '--build-dir', type=str, default='b',
  60. help='Directory containing the build output')
  61. parser.add_argument('-c', '--c-output-dir', action='store',
  62. help='Select output directory for C files')
  63. parser.add_argument('-C', '--h-output-dir', action='store',
  64. help='Select output directory for H files (defaults to --c-output-di)')
  65. parser.add_argument('-d', '--dtb-file', action='store',
  66. help='Specify the .dtb input file')
  67. parser.add_argument('-i', '--instantiate', action='store_true', default=False,
  68. help='Instantiate devices to avoid needing device_bind()')
  69. parser.add_argument('--include-disabled', action='store_true',
  70. help='Include disabled nodes')
  71. parser.add_argument('-o', '--output', action='store',
  72. help='Select output filename')
  73. parser.add_argument('-p', '--phase', type=str,
  74. help='set phase of U-Boot this invocation is for (spl/tpl)')
  75. parser.add_argument('-P', '--processes', type=int,
  76. help='set number of processes to use for running tests')
  77. parser.add_argument('-t', '--test', action='store_true', dest='test',
  78. default=False, help='run tests')
  79. parser.add_argument('-T', '--test-coverage', action='store_true',
  80. default=False, help='run tests and check for 100%% coverage')
  81. parser.add_argument('files', nargs='*')
  82. args = parser.parse_args()
  83. # Run our meagre tests
  84. if args.test:
  85. ret_code = run_tests(args.processes, args)
  86. sys.exit(ret_code)
  87. elif args.test_coverage:
  88. RunTestCoverage()
  89. else:
  90. dtb_platdata.run_steps(args.files, args.dtb_file, args.include_disabled,
  91. args.output,
  92. [args.c_output_dir, args.h_output_dir],
  93. args.phase, instantiate=args.instantiate)