compile_bsp_with_drivers.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # Copyright (c) 2006-2023, RT-Thread Development Team
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Change Logs:
  7. # Date Author Notes
  8. # 2023-06-27 dejavudwh the first version
  9. #
  10. import subprocess
  11. import logging
  12. import os
  13. CONFIG_BSP_USING_X = ["CONFIG_BSP_USING_UART", "CONFIG_BSP_USING_I2C", "CONFIG_BSP_USING_SPI", "CONFIG_BSP_USING_ADC", "CONFIG_BSP_USING_DAC"]
  14. def init_logger():
  15. log_format = "[%(filename)s %(lineno)d %(levelname)s] %(message)s "
  16. date_format = '%Y-%m-%d %H:%M:%S %a '
  17. logging.basicConfig(level=logging.INFO,
  18. format=log_format,
  19. datefmt=date_format,
  20. )
  21. def diff():
  22. result = subprocess.run(['git', 'diff', '--name-only', 'HEAD', 'origin/master', '--diff-filter=ACMR', '--no-renames', '--full-index'], stdout = subprocess.PIPE)
  23. file_list = result.stdout.decode().strip().split('\n')
  24. logging.info(file_list)
  25. bsp_paths = set()
  26. for file in file_list:
  27. if "bsp/" in file:
  28. logging.info("Modifed file: {}".format(file))
  29. bsp_paths.add(file)
  30. dirs = set()
  31. for dir in bsp_paths:
  32. dir = os.path.dirname(dir)
  33. while "bsp/" in dir:
  34. files = os.listdir(dir)
  35. if ".config" in files and "rt-thread.elf" not in files and not dir.endswith("bsp"):
  36. logging.info("Found bsp path: {}".format(dir))
  37. dirs.add(dir)
  38. break
  39. new_dir = os.path.dirname(dir)
  40. dir = new_dir
  41. return dirs
  42. def check_config_in_line(line):
  43. for config in CONFIG_BSP_USING_X:
  44. if config in line and '#' in line:
  45. logging.info("Found in {}".format(line))
  46. return config
  47. return ""
  48. def check_config_in_file(file_path):
  49. configs = set()
  50. found = False
  51. try:
  52. with open(file_path, 'r') as file:
  53. for line in file:
  54. line.strip()
  55. if found:
  56. res = check_config_in_line(line)
  57. if res:
  58. configs.add(res)
  59. elif "On-chip Peripheral Drivers" in line:
  60. logging.info("Found On-chip Peripheral Drivers")
  61. found = True
  62. except FileNotFoundError:
  63. logging.error("The .config file does not exist for this BSP, please recheck the file directory!")
  64. return configs
  65. def modify_config(file_path, configs):
  66. with open(file_path + "/rtconfig.h", 'a') as file:
  67. for item in configs:
  68. define1 = item.replace("CONFIG_BSP", "BSP")
  69. define2 = item.replace("CONFIG_BSP", "RT")
  70. file.write("#define " + define1 + "\n")
  71. file.write("#define " + define2 + "\n")
  72. def recompile_bsp(dir):
  73. logging.info("recomplie bsp: {}".format(dir))
  74. os.system("scons -C " + dir)
  75. if __name__ == '__main__':
  76. init_logger()
  77. recompile_bsp_dirs = diff()
  78. for dir in recompile_bsp_dirs:
  79. dot_config_path = dir + "/" + ".config"
  80. configs = check_config_in_file(dot_config_path)
  81. logging.info("add config:")
  82. logging.info(configs)
  83. logging.info("Add configurations and recompile!")
  84. modify_config(dir, configs)
  85. recompile_bsp(dir)