build_board.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import glob
  3. import sys
  4. import subprocess
  5. import time
  6. import build_utils
  7. SUCCEEDED = "\033[32msucceeded\033[0m"
  8. FAILED = "\033[31mfailed\033[0m"
  9. SKIPPED = "\033[33mskipped\033[0m"
  10. success_count = 0
  11. fail_count = 0
  12. skip_count = 0
  13. exit_status = 0
  14. total_time = time.monotonic()
  15. build_format = '| {:29} | {:30} | {:18} | {:7} | {:6} | {:6} |'
  16. build_separator = '-' * 106
  17. def filter_with_input(mylist):
  18. if len(sys.argv) > 1:
  19. input_args = list(set(mylist).intersection(sys.argv))
  20. if len(input_args) > 0:
  21. mylist[:] = input_args
  22. # If examples are not specified in arguments, build all
  23. all_examples = []
  24. for entry in os.scandir("examples/device"):
  25. if entry.is_dir():
  26. all_examples.append("device/" + entry.name)
  27. for entry in os.scandir("examples/host"):
  28. if entry.is_dir():
  29. all_examples.append("host/" + entry.name)
  30. filter_with_input(all_examples)
  31. all_examples.sort()
  32. # If boards are not specified in arguments, build all
  33. all_boards = []
  34. for entry in os.scandir("hw/bsp"):
  35. if entry.is_dir() and os.path.exists(entry.path + "/board.mk"):
  36. all_boards.append(entry.name)
  37. filter_with_input(all_boards)
  38. all_boards.sort()
  39. def build_board(example, board):
  40. global success_count, fail_count, skip_count, exit_status
  41. start_time = time.monotonic()
  42. flash_size = "-"
  43. sram_size = "-"
  44. # Check if board is skipped
  45. if build_utils.skip_example(example, board):
  46. success = SKIPPED
  47. skip_count += 1
  48. print(build_format.format(example, board, success, '-', flash_size, sram_size))
  49. else:
  50. subprocess.run("make -C examples/{} BOARD={} clean".format(example, board), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  51. build_result = subprocess.run("make -j -C examples/{} BOARD={} all".format(example, board), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  52. if build_result.returncode == 0:
  53. success = SUCCEEDED
  54. success_count += 1
  55. (flash_size, sram_size) = build_size(example, board)
  56. else:
  57. exit_status = build_result.returncode
  58. success = FAILED
  59. fail_count += 1
  60. build_duration = time.monotonic() - start_time
  61. print(build_format.format(example, board, success, "{:.2f}s".format(build_duration), flash_size, sram_size))
  62. if build_result.returncode != 0:
  63. print(build_result.stdout.decode("utf-8"))
  64. def build_size(example, board):
  65. #elf_file = 'examples/device/{}/_build/{}/{}-firmware.elf'.format(example, board, board)
  66. elf_file = 'examples/{}/_build/{}/*.elf'.format(example, board)
  67. size_output = subprocess.run('size {}'.format(elf_file), shell=True, stdout=subprocess.PIPE).stdout.decode("utf-8")
  68. size_list = size_output.split('\n')[1].split('\t')
  69. flash_size = int(size_list[0])
  70. sram_size = int(size_list[1]) + int(size_list[2])
  71. return (flash_size, sram_size)
  72. print(build_separator)
  73. print(build_format.format('Example', 'Board', '\033[39mResult\033[0m', 'Time', 'Flash', 'SRAM'))
  74. for example in all_examples:
  75. print(build_separator)
  76. for board in all_boards:
  77. build_board(example, board)
  78. total_time = time.monotonic() - total_time
  79. print(build_separator)
  80. print("Build Summary: {} {}, {} {}, {} {} and took {:.2f}s".format(success_count, SUCCEEDED, fail_count, FAILED, skip_count, SKIPPED, total_time))
  81. print(build_separator)
  82. sys.exit(exit_status)