main.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 pathlib
  22. import sys
  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 u_boot_pylib import test_util
  32. DTOC_DIR = pathlib.Path(__file__).parent
  33. HAVE_TESTS = (DTOC_DIR / 'test_dtoc.py').exists()
  34. def run_tests(processes, args):
  35. """Run all the test we have for dtoc
  36. Args:
  37. processes: Number of processes to use to run tests (None=same as #CPUs)
  38. args: List of positional args provided to dtoc. This can hold a test
  39. name to execute (as in 'dtoc -t test_empty_file', for example)
  40. """
  41. from dtoc import test_src_scan
  42. from dtoc import test_dtoc
  43. sys.argv = [sys.argv[0]]
  44. test_name = args.files and args.files[0] or None
  45. test_dtoc.setup()
  46. result = test_util.run_test_suites(
  47. toolname='dtoc', debug=True, verbosity=1, test_preserve_dirs=False,
  48. processes=processes, test_name=test_name, toolpath=[],
  49. class_and_module_list=[test_dtoc.TestDtoc,test_src_scan.TestSrcScan])
  50. return (0 if result.wasSuccessful() else 1)
  51. def RunTestCoverage(build_dir):
  52. """Run the tests and check that we get 100% coverage"""
  53. sys.argv = [sys.argv[0]]
  54. test_util.run_test_coverage('tools/dtoc/dtoc', '/main.py',
  55. ['tools/patman/*.py', 'tools/u_boot_pylib/*','*/fdt*', '*test*'],
  56. build_dir)
  57. def run_dtoc():
  58. epilog = 'Generate C code from devicetree files. See of-plat.rst for details'
  59. parser = ArgumentParser(epilog=epilog)
  60. parser.add_argument('-B', '--build-dir', type=str, default='b',
  61. help='Directory containing the build output')
  62. parser.add_argument('-c', '--c-output-dir', action='store',
  63. help='Select output directory for C files')
  64. parser.add_argument(
  65. '-C', '--h-output-dir', action='store',
  66. help='Select output directory for H files (defaults to --c-output-di)')
  67. parser.add_argument('-d', '--dtb-file', action='store',
  68. help='Specify the .dtb input file')
  69. parser.add_argument(
  70. '-i', '--instantiate', action='store_true', default=False,
  71. help='Instantiate devices to avoid needing device_bind()')
  72. parser.add_argument('--include-disabled', action='store_true',
  73. help='Include disabled nodes')
  74. parser.add_argument('-o', '--output', action='store',
  75. help='Select output filename')
  76. parser.add_argument(
  77. '-p', '--phase', type=str,
  78. help='set phase of U-Boot this invocation is for (spl/tpl)')
  79. parser.add_argument('-P', '--processes', type=int,
  80. help='set number of processes to use for running tests')
  81. if HAVE_TESTS:
  82. parser.add_argument('-t', '--test', action='store_true', dest='test',
  83. default=False, help='run tests')
  84. parser.add_argument(
  85. '-T', '--test-coverage', action='store_true',
  86. default=False, help='run tests and check for 100%% coverage')
  87. parser.add_argument('files', nargs='*')
  88. args = parser.parse_args()
  89. # Run our meagre tests
  90. if HAVE_TESTS and args.test:
  91. ret_code = run_tests(args.processes, args)
  92. sys.exit(ret_code)
  93. elif HAVE_TESTS and args.test_coverage:
  94. RunTestCoverage(args.build_dir)
  95. else:
  96. dtb_platdata.run_steps(args.files, args.dtb_file, args.include_disabled,
  97. args.output,
  98. [args.c_output_dir, args.h_output_dir],
  99. args.phase, instantiate=args.instantiate)
  100. if __name__ == '__main__':
  101. run_dtoc()