build_family.py 5.0 KB

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