build_all.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import os
  2. import glob
  3. import sys
  4. import subprocess
  5. import time
  6. SUCCEEDED = "\033[32msucceeded\033[0m"
  7. FAILED = "\033[31mfailed\033[0m"
  8. SKIPPED = "\033[33mskipped\033[0m"
  9. success_count = 0
  10. fail_count = 0
  11. skip_count = 0
  12. exit_status = 0
  13. total_time = time.monotonic()
  14. build_format = '| {:29} | {:30} | {:18} | {:7} | {:6} | {:6} |'
  15. build_separator = '-' * 106
  16. # If examples are not specified in arguments, build all
  17. all_examples = []
  18. for entry in os.scandir("examples/device"):
  19. if entry.is_dir():
  20. all_examples.append("device/" + entry.name)
  21. for entry in os.scandir("examples/host"):
  22. if entry.is_dir():
  23. all_examples.append("host/" + entry.name)
  24. if len(sys.argv) > 1:
  25. input_examples = list(set(all_examples).intersection(sys.argv))
  26. if len(input_examples) > 0:
  27. all_examples = input_examples
  28. all_examples.sort()
  29. # If boards are not specified in arguments, build all
  30. all_boards = []
  31. for entry in os.scandir("hw/bsp"):
  32. if entry.is_dir():
  33. all_boards.append(entry.name)
  34. if len(sys.argv) > 1:
  35. input_boards = list(set(all_boards).intersection(sys.argv))
  36. if len(input_boards) > 0:
  37. all_boards = input_boards
  38. all_boards.sort()
  39. def build_example(example, board):
  40. subprocess.run("make -C examples/{} BOARD={} clean".format(example, board), shell=True,
  41. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  42. return subprocess.run("make -j -C examples/{} BOARD={} all".format(example, board), shell=True,
  43. stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  44. def build_size(example, board):
  45. #elf_file = 'examples/device/{}/_build/build-{}/{}-firmware.elf'.format(example, board, board)
  46. elf_file = 'examples/{}/_build/build-{}/*.elf'.format(example, board)
  47. size_output = subprocess.run('size {}'.format(elf_file), shell=True, stdout=subprocess.PIPE).stdout.decode("utf-8")
  48. size_list = size_output.split('\n')[1].split('\t')
  49. flash_size = int(size_list[0])
  50. sram_size = int(size_list[1]) + int(size_list[2])
  51. return (flash_size, sram_size)
  52. def skip_example(example, board):
  53. ex_dir = 'examples/' + example
  54. board_mk = 'hw/bsp/{}/board.mk'.format(board)
  55. with open(board_mk) as mk:
  56. mk_contents = mk.read()
  57. # Skip all ESP32-S2 board for CI
  58. if 'CROSS_COMPILE = xtensa-esp32s2-elf-' in mk_contents:
  59. return 1
  60. # Skip all OPT_MCU_NONE these are WIP port
  61. if '-DCFG_TUSB_MCU=OPT_MCU_NONE' in mk_contents:
  62. return 1
  63. # Skip if CFG_TUSB_MCU in board.mk to match skip file
  64. for skip_file in glob.iglob(ex_dir + '/.skip.MCU_*'):
  65. mcu_cflag = '-DCFG_TUSB_MCU=OPT_' + os.path.basename(skip_file).split('.')[2]
  66. if mcu_cflag in mk_contents:
  67. return 1
  68. # Build only list, if exists only these MCU are built
  69. only_list = list(glob.iglob(ex_dir + '/.only.MCU_*'))
  70. if len(only_list) > 0:
  71. for only_file in only_list:
  72. mcu_cflag = '-DCFG_TUSB_MCU=OPT_' + os.path.basename(only_file).split('.')[2]
  73. if mcu_cflag in mk_contents:
  74. return 0
  75. return 1
  76. return 0
  77. print(build_separator)
  78. print(build_format.format('Example', 'Board', '\033[39mResult\033[0m', 'Time', 'Flash', 'SRAM'))
  79. for example in all_examples:
  80. print(build_separator)
  81. for board in all_boards:
  82. start_time = time.monotonic()
  83. flash_size = "-"
  84. sram_size = "-"
  85. # Check if board is skipped
  86. if skip_example(example, board):
  87. success = SKIPPED
  88. skip_count += 1
  89. print(build_format.format(example, board, success, '-', flash_size, sram_size))
  90. else:
  91. build_result = build_example(example, board)
  92. if build_result.returncode == 0:
  93. success = SUCCEEDED
  94. success_count += 1
  95. (flash_size, sram_size) = build_size(example, board)
  96. else:
  97. exit_status = build_result.returncode
  98. success = FAILED
  99. fail_count += 1
  100. build_duration = time.monotonic() - start_time
  101. print(build_format.format(example, board, success, "{:.2f}s".format(build_duration), flash_size, sram_size))
  102. if build_result.returncode != 0:
  103. print(build_result.stdout.decode("utf-8"))
  104. total_time = time.monotonic() - total_time
  105. print(build_separator)
  106. print("Build Summary: {} {}, {} {}, {} {} and took {:.2f}s".format(success_count, SUCCEEDED, fail_count, FAILED, skip_count, SKIPPED, total_time))
  107. print(build_separator)
  108. sys.exit(exit_status)