preflight-checks.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #
  2. # preflight-checks.py
  3. # Check for common issues prior to compiling
  4. #
  5. import pioutil
  6. if pioutil.is_pio_build():
  7. import re, sys
  8. from pathlib import Path
  9. env = pioutil.env
  10. def get_envs_for_board(board):
  11. ppath = Path("Marlin/src/pins/pins.h")
  12. with ppath.open() as file:
  13. if sys.platform == 'win32':
  14. envregex = r"(?:env|win):"
  15. elif sys.platform == 'darwin':
  16. envregex = r"(?:env|mac|uni):"
  17. elif sys.platform == 'linux':
  18. envregex = r"(?:env|lin|uni):"
  19. else:
  20. envregex = r"(?:env):"
  21. r = re.compile(r"if\s+MB\((.+)\)")
  22. if board.startswith("BOARD_"):
  23. board = board[6:]
  24. for line in file:
  25. mbs = r.findall(line)
  26. if mbs and board in re.split(r",\s*", mbs[0]):
  27. line = file.readline()
  28. found_envs = re.match(r"\s*#include .+" + envregex, line)
  29. if found_envs:
  30. envlist = re.findall(envregex + r"(\w+)", line)
  31. return [ "env:"+s for s in envlist ]
  32. return []
  33. def check_envs(build_env, board_envs, config):
  34. if build_env in board_envs:
  35. return True
  36. ext = config.get(build_env, 'extends', default=None)
  37. if ext:
  38. if isinstance(ext, str):
  39. return check_envs(ext, board_envs, config)
  40. elif isinstance(ext, list):
  41. for ext_env in ext:
  42. if check_envs(ext_env, board_envs, config):
  43. return True
  44. return False
  45. def sanity_check_target():
  46. # Sanity checks:
  47. if 'PIOENV' not in env:
  48. raise SystemExit("Error: PIOENV is not defined. This script is intended to be used with PlatformIO")
  49. # Require PlatformIO 6.1.1 or later
  50. vers = pioutil.get_pio_version()
  51. if vers < [6, 1, 1]:
  52. raise SystemExit("Error: Marlin requires PlatformIO >= 6.1.1. Use 'pio upgrade' to get a newer version.")
  53. if 'MARLIN_FEATURES' not in env:
  54. raise SystemExit("Error: this script should be used after common Marlin scripts.")
  55. # Useful values
  56. project_dir = Path(env['PROJECT_DIR'])
  57. config_files = ("Configuration.h", "Configuration_adv.h")
  58. #
  59. # Update old macros BOTH and EITHER in configuration files
  60. #
  61. conf_modified = False
  62. for f in config_files:
  63. conf_path = project_dir / "Marlin" / f
  64. if conf_path.is_file():
  65. with open(conf_path, 'r', encoding="utf8") as file:
  66. text = file.read()
  67. modified_text = text.replace("BOTH(", "ALL(").replace("EITHER(", "ANY(")
  68. if text != modified_text:
  69. conf_modified = True
  70. with open(conf_path, 'w') as file:
  71. file.write(modified_text)
  72. if conf_modified:
  73. raise SystemExit('WARNING: Configuration files needed an update to remove incompatible items. Try the build again to use the updated files.')
  74. if len(env['MARLIN_FEATURES']) == 0:
  75. raise SystemExit("Error: Failed to parse Marlin features. See previous error messages.")
  76. build_env = env['PIOENV']
  77. motherboard = env['MARLIN_FEATURES']['MOTHERBOARD']
  78. board_envs = get_envs_for_board(motherboard)
  79. config = env.GetProjectConfig()
  80. result = check_envs("env:"+build_env, board_envs, config)
  81. # Make sure board is compatible with the build environment. Skip for _test,
  82. # since the board is manipulated as each unit test is executed.
  83. if not result and build_env != "linux_native_test":
  84. err = "Error: Build environment '%s' is incompatible with %s. Use one of these environments: %s" % \
  85. ( build_env, motherboard, ", ".join([ e[4:] for e in board_envs if e.startswith("env:") ]) )
  86. raise SystemExit(err)
  87. #
  88. # Check for Config files in two common incorrect places
  89. #
  90. for p in (project_dir, project_dir / "config"):
  91. for f in config_files:
  92. if (p / f).is_file():
  93. err = "ERROR: Config files found in directory %s. Please move them into the Marlin subfolder." % p
  94. raise SystemExit(err)
  95. #
  96. # Find the name.cpp.o or name.o and remove it
  97. #
  98. def rm_ofile(subdir, name):
  99. build_dir = Path(env['PROJECT_BUILD_DIR'], build_env)
  100. for outdir in (build_dir, build_dir / "debug"):
  101. for ext in (".cpp.o", ".o"):
  102. fpath = outdir / "src/src" / subdir / (name + ext)
  103. if fpath.exists():
  104. fpath.unlink()
  105. #
  106. # Give warnings on every build
  107. #
  108. rm_ofile("inc", "Warnings")
  109. #
  110. # Rebuild 'settings.cpp' for EEPROM_INIT_NOW
  111. #
  112. if 'EEPROM_INIT_NOW' in env['MARLIN_FEATURES']:
  113. rm_ofile("module", "settings")
  114. #
  115. # Check for old files indicating an entangled Marlin (mixing old and new code)
  116. #
  117. mixedin = []
  118. p = project_dir / "Marlin/src/lcd/dogm"
  119. for f in [ "ultralcd_DOGM.cpp", "ultralcd_DOGM.h" ]:
  120. if (p / f).is_file():
  121. mixedin += [ f ]
  122. p = project_dir / "Marlin/src/feature/bedlevel/abl"
  123. for f in [ "abl.cpp", "abl.h" ]:
  124. if (p / f).is_file():
  125. mixedin += [ f ]
  126. if mixedin:
  127. err = "ERROR: Old files fell into your Marlin folder. Remove %s and try again" % ", ".join(mixedin)
  128. raise SystemExit(err)
  129. #
  130. # Check FILAMENT_RUNOUT_SCRIPT has a %c parammeter when required
  131. #
  132. if 'FILAMENT_RUNOUT_SENSOR' in env['MARLIN_FEATURES'] and 'NUM_RUNOUT_SENSORS' in env['MARLIN_FEATURES']:
  133. if env['MARLIN_FEATURES']['NUM_RUNOUT_SENSORS'].isdigit() and int(env['MARLIN_FEATURES']['NUM_RUNOUT_SENSORS']) > 1:
  134. if 'FILAMENT_RUNOUT_SCRIPT' in env['MARLIN_FEATURES']:
  135. frs = env['MARLIN_FEATURES']['FILAMENT_RUNOUT_SCRIPT']
  136. if "M600" in frs and "%c" not in frs:
  137. err = "ERROR: FILAMENT_RUNOUT_SCRIPT needs a %c parameter (e.g., \"M600 T%c\") when NUM_RUNOUT_SENSORS is > 1"
  138. raise SystemExit(err)
  139. sanity_check_target()