build_board.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 dir1 in os.scandir("examples"):
  25. if dir1.is_dir():
  26. for entry in os.scandir(dir1.path):
  27. if entry.is_dir():
  28. all_examples.append(dir1.name + '/' + 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 build_utils.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. print(build_separator)
  72. print(build_format.format('Example', 'Board', '\033[39mResult\033[0m', 'Time', 'Flash', 'SRAM'))
  73. for example in all_examples:
  74. print(build_separator)
  75. for board in all_boards:
  76. build_board(example, board)
  77. total_time = time.monotonic() - total_time
  78. print(build_separator)
  79. print("Build Summary: {} {}, {} {}, {} {} and took {:.2f}s".format(success_count, SUCCEEDED, fail_count, FAILED, skip_count, SKIPPED, total_time))
  80. print(build_separator)
  81. sys.exit(exit_status)