main.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env python3
  2. import argparse
  3. import errno
  4. import glob
  5. import shutil
  6. import subprocess
  7. import sys
  8. import os
  9. lvgl_test_dir = os.path.dirname(os.path.realpath(__file__))
  10. # Key values must match variable names in CMakeLists.txt.
  11. build_only_options = {
  12. 'OPTIONS_MINIMAL_MONOCHROME': 'Minimal config monochrome',
  13. 'OPTIONS_NORMAL_8BIT': 'Normal config, 8 bit color depth',
  14. 'OPTIONS_16BIT': 'Minimal config, 16 bit color depth',
  15. 'OPTIONS_16BIT_SWAP': 'Normal config, 16 bit color depth swapped',
  16. 'OPTIONS_FULL_32BIT': 'Full config, 32 bit color depth',
  17. }
  18. test_options = {
  19. 'OPTIONS_TEST': 'Test config, 32 bit color depth',
  20. }
  21. def is_valid_option_name(option_name):
  22. return option_name in build_only_options or option_name in test_options
  23. def get_option_description(option_name):
  24. if option_name in build_only_options:
  25. return build_only_options[option_name]
  26. return test_options[option_name]
  27. def delete_dir_ignore_missing(dir_path):
  28. '''Recursively delete a directory and ignore if missing.'''
  29. try:
  30. shutil.rmtree(dir_path)
  31. except FileNotFoundError:
  32. pass
  33. def generate_test_runners():
  34. '''Generate the test runner source code.'''
  35. global lvgl_test_dir
  36. os.chdir(lvgl_test_dir)
  37. delete_dir_ignore_missing('src/test_runners')
  38. os.mkdir('src/test_runners')
  39. # TODO: Intermediate files should be in the build folders, not alongside
  40. # the other repo source.
  41. for f in glob.glob("./src/test_cases/test_*.c"):
  42. r = f[:-2] + "_Runner.c"
  43. r = r.replace("/test_cases/", "/test_runners/")
  44. subprocess.check_call(['ruby', 'unity/generate_test_runner.rb',
  45. f, r, 'config.yml'])
  46. def options_abbrev(options_name):
  47. '''Return an abbreviated version of the option name.'''
  48. prefix = 'OPTIONS_'
  49. assert options_name.startswith(prefix)
  50. return options_name[len(prefix):].lower()
  51. def get_base_buid_dir(options_name):
  52. '''Given the build options name, return the build directory name.
  53. Does not return the full path to the directory - just the base name.'''
  54. return 'build_%s' % options_abbrev(options_name)
  55. def get_build_dir(options_name):
  56. '''Given the build options name, return the build directory name.
  57. Returns absolute path to the build directory.'''
  58. global lvgl_test_dir
  59. return os.path.join(lvgl_test_dir, get_base_buid_dir(options_name))
  60. def build_tests(options_name, build_type, clean):
  61. '''Build all tests for the specified options name.'''
  62. global lvgl_test_dir
  63. print()
  64. print()
  65. label = 'Building: %s: %s' % (options_abbrev(
  66. options_name), get_option_description(options_name))
  67. print('=' * len(label))
  68. print(label)
  69. print('=' * len(label))
  70. print(flush=True)
  71. build_dir = get_build_dir(options_name)
  72. if clean:
  73. delete_dir_ignore_missing(build_dir)
  74. os.chdir(lvgl_test_dir)
  75. created_build_dir = False
  76. if not os.path.isdir(build_dir):
  77. os.mkdir(build_dir)
  78. created_build_dir = True
  79. os.chdir(build_dir)
  80. if created_build_dir:
  81. subprocess.check_call(['cmake', '-DCMAKE_BUILD_TYPE=%s' % build_type,
  82. '-D%s=1' % options_name, '..'])
  83. subprocess.check_call(['cmake', '--build', build_dir,
  84. '--parallel', str(os.cpu_count())])
  85. def run_tests(options_name):
  86. '''Run the tests for the given options name.'''
  87. print()
  88. print()
  89. label = 'Running tests for %s' % options_abbrev(options_name)
  90. print('=' * len(label))
  91. print(label)
  92. print('=' * len(label), flush=True)
  93. os.chdir(get_build_dir(options_name))
  94. subprocess.check_call(
  95. ['ctest', '--parallel', str(os.cpu_count()), '--output-on-failure'])
  96. def generate_code_coverage_report():
  97. '''Produce code coverage test reports for the test execution.'''
  98. global lvgl_test_dir
  99. print()
  100. print()
  101. label = 'Generating code coverage reports'
  102. print('=' * len(label))
  103. print(label)
  104. print('=' * len(label))
  105. print(flush=True)
  106. os.chdir(lvgl_test_dir)
  107. delete_dir_ignore_missing('report')
  108. os.mkdir('report')
  109. root_dir = os.pardir
  110. html_report_file = 'report/index.html'
  111. cmd = ['gcovr', '--root', root_dir, '--html-details', '--output',
  112. html_report_file, '--xml', 'report/coverage.xml',
  113. '-j', str(os.cpu_count()), '--print-summary',
  114. '--html-title', 'LVGL Test Coverage']
  115. for d in ('.*\\bexamples/.*', '\\bsrc/test_.*', '\\bsrc/lv_test.*', '\\bunity\\b'):
  116. cmd.extend(['--exclude', d])
  117. subprocess.check_call(cmd)
  118. print("Done: See %s" % html_report_file, flush=True)
  119. if __name__ == "__main__":
  120. epilog = '''This program builds and optionally runs the LVGL test programs.
  121. There are two types of LVGL tests: "build", and "test". The build-only
  122. tests, as their name suggests, only verify that the program successfully
  123. compiles and links (with various build options). There are also a set of
  124. tests that execute to verify correct LVGL library behavior.
  125. '''
  126. parser = argparse.ArgumentParser(
  127. description='Build and/or run LVGL tests.', epilog=epilog)
  128. parser.add_argument('--build-options', nargs=1,
  129. help='''the build option name to build or run. When
  130. omitted all build configurations are used.
  131. ''')
  132. parser.add_argument('--clean', action='store_true', default=False,
  133. help='clean existing build artifacts before operation.')
  134. parser.add_argument('--report', action='store_true',
  135. help='generate code coverage report for tests.')
  136. parser.add_argument('actions', nargs='*', choices=['build', 'test'],
  137. help='build: compile build tests, test: compile/run executable tests.')
  138. args = parser.parse_args()
  139. if args.build_options:
  140. options_to_build = args.build_options
  141. else:
  142. if 'build' in args.actions:
  143. if 'test' in args.actions:
  144. options_to_build = {**build_only_options, **test_options}
  145. else:
  146. options_to_build = build_only_options
  147. else:
  148. options_to_build = test_options
  149. for opt in options_to_build:
  150. if not is_valid_option_name(opt):
  151. print('Invalid build option "%s"' % opt, file=sys.stderr)
  152. sys.exit(errno.EINVAL)
  153. generate_test_runners()
  154. for options_name in options_to_build:
  155. is_test = options_name in test_options
  156. build_type = 'Debug'
  157. build_tests(options_name, build_type, args.clean)
  158. if is_test:
  159. try:
  160. run_tests(options_name)
  161. except subprocess.CalledProcessError as e:
  162. sys.exit(e.returncode)
  163. if args.report:
  164. generate_code_coverage_report()