build_board.py 4.3 KB

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