doctor.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. """QMK Doctor
  2. Check out the user's QMK environment and make sure it's ready to compile.
  3. """
  4. import platform
  5. import re
  6. import shutil
  7. import subprocess
  8. from pathlib import Path
  9. from enum import Enum
  10. from milc import cli
  11. from milc.questions import yesno
  12. from qmk import submodules
  13. from qmk.constants import QMK_FIRMWARE
  14. from qmk.commands import run
  15. class CheckStatus(Enum):
  16. OK = 1
  17. WARNING = 2
  18. ERROR = 3
  19. ESSENTIAL_BINARIES = {
  20. 'dfu-programmer': {},
  21. 'avrdude': {},
  22. 'dfu-util': {},
  23. 'avr-gcc': {
  24. 'version_arg': '-dumpversion'
  25. },
  26. 'arm-none-eabi-gcc': {
  27. 'version_arg': '-dumpversion'
  28. },
  29. 'bin/qmk': {},
  30. }
  31. def _udev_rule(vid, pid=None, *args):
  32. """ Helper function that return udev rules
  33. """
  34. rule = ""
  35. if pid:
  36. rule = 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", ATTRS{idProduct}=="%s", TAG+="uaccess"' % (
  37. vid,
  38. pid,
  39. )
  40. else:
  41. rule = 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", TAG+="uaccess"' % vid
  42. if args:
  43. rule = ', '.join([rule, *args])
  44. return rule
  45. def _deprecated_udev_rule(vid, pid=None):
  46. """ Helper function that return udev rules
  47. Note: these are no longer the recommended rules, this is just used to check for them
  48. """
  49. if pid:
  50. return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", ATTRS{idProduct}=="%s", MODE:="0666"' % (vid, pid)
  51. else:
  52. return 'SUBSYSTEMS=="usb", ATTRS{idVendor}=="%s", MODE:="0666"' % vid
  53. def parse_gcc_version(version):
  54. m = re.match(r"(\d+)(?:\.(\d+))?(?:\.(\d+))?", version)
  55. return {
  56. 'major': int(m.group(1)),
  57. 'minor': int(m.group(2)) if m.group(2) else 0,
  58. 'patch': int(m.group(3)) if m.group(3) else 0,
  59. }
  60. def check_arm_gcc_version():
  61. """Returns True if the arm-none-eabi-gcc version is not known to cause problems.
  62. """
  63. if 'output' in ESSENTIAL_BINARIES['arm-none-eabi-gcc']:
  64. version_number = ESSENTIAL_BINARIES['arm-none-eabi-gcc']['output'].strip()
  65. cli.log.info('Found arm-none-eabi-gcc version %s', version_number)
  66. return CheckStatus.OK # Right now all known arm versions are ok
  67. def check_avr_gcc_version():
  68. """Returns True if the avr-gcc version is not known to cause problems.
  69. """
  70. rc = CheckStatus.ERROR
  71. if 'output' in ESSENTIAL_BINARIES['avr-gcc']:
  72. version_number = ESSENTIAL_BINARIES['avr-gcc']['output'].strip()
  73. cli.log.info('Found avr-gcc version %s', version_number)
  74. rc = CheckStatus.OK
  75. parsed_version = parse_gcc_version(version_number)
  76. if parsed_version['major'] > 8:
  77. cli.log.warning('{fg_yellow}We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended.')
  78. rc = CheckStatus.WARNING
  79. return rc
  80. def check_avrdude_version():
  81. if 'output' in ESSENTIAL_BINARIES['avrdude']:
  82. last_line = ESSENTIAL_BINARIES['avrdude']['output'].split('\n')[-2]
  83. version_number = last_line.split()[2][:-1]
  84. cli.log.info('Found avrdude version %s', version_number)
  85. return CheckStatus.OK
  86. def check_dfu_util_version():
  87. if 'output' in ESSENTIAL_BINARIES['dfu-util']:
  88. first_line = ESSENTIAL_BINARIES['dfu-util']['output'].split('\n')[0]
  89. version_number = first_line.split()[1]
  90. cli.log.info('Found dfu-util version %s', version_number)
  91. return CheckStatus.OK
  92. def check_dfu_programmer_version():
  93. if 'output' in ESSENTIAL_BINARIES['dfu-programmer']:
  94. first_line = ESSENTIAL_BINARIES['dfu-programmer']['output'].split('\n')[0]
  95. version_number = first_line.split()[1]
  96. cli.log.info('Found dfu-programmer version %s', version_number)
  97. return CheckStatus.OK
  98. def check_binaries():
  99. """Iterates through ESSENTIAL_BINARIES and tests them.
  100. """
  101. ok = True
  102. for binary in sorted(ESSENTIAL_BINARIES):
  103. if not is_executable(binary):
  104. ok = False
  105. return ok
  106. def check_submodules():
  107. """Iterates through all submodules to make sure they're cloned and up to date.
  108. """
  109. for submodule in submodules.status().values():
  110. if submodule['status'] is None:
  111. cli.log.error('Submodule %s has not yet been cloned!', submodule['name'])
  112. return CheckStatus.ERROR
  113. elif not submodule['status']:
  114. cli.log.warning('Submodule %s is not up to date!', submodule['name'])
  115. return CheckStatus.WARNING
  116. return CheckStatus.OK
  117. def check_udev_rules():
  118. """Make sure the udev rules look good.
  119. """
  120. rc = CheckStatus.OK
  121. udev_dir = Path("/etc/udev/rules.d/")
  122. desired_rules = {
  123. 'atmel-dfu': {
  124. _udev_rule("03eb", "2fef"), # ATmega16U2
  125. _udev_rule("03eb", "2ff0"), # ATmega32U2
  126. _udev_rule("03eb", "2ff3"), # ATmega16U4
  127. _udev_rule("03eb", "2ff4"), # ATmega32U4
  128. _udev_rule("03eb", "2ff9"), # AT90USB64
  129. _udev_rule("03eb", "2ffb") # AT90USB128
  130. },
  131. 'kiibohd': {_udev_rule("1c11", "b007")},
  132. 'stm32': {
  133. _udev_rule("1eaf", "0003"), # STM32duino
  134. _udev_rule("0483", "df11") # STM32 DFU
  135. },
  136. 'bootloadhid': {_udev_rule("16c0", "05df")},
  137. 'usbasploader': {_udev_rule("16c0", "05dc")},
  138. 'massdrop': {_udev_rule("03eb", "6124", 'ENV{ID_MM_DEVICE_IGNORE}="1"')},
  139. 'caterina': {
  140. # Spark Fun Electronics
  141. _udev_rule("1b4f", "9203", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Pro Micro 3V3/8MHz
  142. _udev_rule("1b4f", "9205", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Pro Micro 5V/16MHz
  143. _udev_rule("1b4f", "9207", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # LilyPad 3V3/8MHz (and some Pro Micro clones)
  144. # Pololu EleCTRONICS
  145. _udev_rule("1ffb", "0101", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # A-Star 32U4
  146. # Arduino SA
  147. _udev_rule("2341", "0036", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Leonardo
  148. _udev_rule("2341", "0037", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Micro
  149. # Adafruit INDUSTRIES llC
  150. _udev_rule("239a", "000c", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Feather 32U4
  151. _udev_rule("239a", "000d", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # ItsyBitsy 32U4 3V3/8MHz
  152. _udev_rule("239a", "000e", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # ItsyBitsy 32U4 5V/16MHz
  153. # dog hunter ag
  154. _udev_rule("2a03", "0036", 'ENV{ID_MM_DEVICE_IGNORE}="1"'), # Leonardo
  155. _udev_rule("2a03", "0037", 'ENV{ID_MM_DEVICE_IGNORE}="1"') # Micro
  156. }
  157. }
  158. # These rules are no longer recommended, only use them to check for their presence.
  159. deprecated_rules = {
  160. 'atmel-dfu': {_deprecated_udev_rule("03eb", "2ff4"), _deprecated_udev_rule("03eb", "2ffb"), _deprecated_udev_rule("03eb", "2ff0")},
  161. 'kiibohd': {_deprecated_udev_rule("1c11")},
  162. 'stm32': {_deprecated_udev_rule("1eaf", "0003"), _deprecated_udev_rule("0483", "df11")},
  163. 'bootloadhid': {_deprecated_udev_rule("16c0", "05df")},
  164. 'caterina': {'ATTRS{idVendor}=="2a03", ENV{ID_MM_DEVICE_IGNORE}="1"', 'ATTRS{idVendor}=="2341", ENV{ID_MM_DEVICE_IGNORE}="1"'},
  165. 'tmk': {_deprecated_udev_rule("feed")}
  166. }
  167. if udev_dir.exists():
  168. udev_rules = [rule_file for rule_file in udev_dir.glob('*.rules')]
  169. current_rules = set()
  170. # Collect all rules from the config files
  171. for rule_file in udev_rules:
  172. for line in rule_file.read_text().split('\n'):
  173. line = line.strip()
  174. if not line.startswith("#") and len(line):
  175. current_rules.add(line)
  176. # Check if the desired rules are among the currently present rules
  177. for bootloader, rules in desired_rules.items():
  178. if not rules.issubset(current_rules):
  179. deprecated_rule = deprecated_rules.get(bootloader)
  180. if deprecated_rule and deprecated_rule.issubset(current_rules):
  181. cli.log.warning("{fg_yellow}Found old, deprecated udev rules for '%s' boards. The new rules on https://docs.qmk.fm/#/faq_build?id=linux-udev-rules offer better security with the same functionality.", bootloader)
  182. else:
  183. # For caterina, check if ModemManager is running
  184. if bootloader == "caterina":
  185. if check_modem_manager():
  186. rc = CheckStatus.WARNING
  187. cli.log.warning("{fg_yellow}Detected ModemManager without the necessary udev rules. Please either disable it or set the appropriate udev rules if you are using a Pro Micro.")
  188. rc = CheckStatus.WARNING
  189. cli.log.warning("{fg_yellow}Missing or outdated udev rules for '%s' boards. Run 'sudo cp %s/util/udev/50-qmk.rules /etc/udev/rules.d/'.", bootloader, QMK_FIRMWARE)
  190. else:
  191. cli.log.warning("{fg_yellow}'%s' does not exist. Skipping udev rule checking...", udev_dir)
  192. return rc
  193. def check_systemd():
  194. """Check if it's a systemd system
  195. """
  196. return bool(shutil.which("systemctl"))
  197. def check_modem_manager():
  198. """Returns True if ModemManager is running.
  199. """
  200. if check_systemd():
  201. mm_check = run(["systemctl", "--quiet", "is-active", "ModemManager.service"], timeout=10)
  202. if mm_check.returncode == 0:
  203. return True
  204. else:
  205. """(TODO): Add check for non-systemd systems
  206. """
  207. return False
  208. def is_executable(command):
  209. """Returns True if command exists and can be executed.
  210. """
  211. # Make sure the command is in the path.
  212. res = shutil.which(command)
  213. if res is None:
  214. cli.log.error("{fg_red}Can't find %s in your path.", command)
  215. return False
  216. # Make sure the command can be executed
  217. version_arg = ESSENTIAL_BINARIES[command].get('version_arg', '--version')
  218. check = run([command, version_arg], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=5, universal_newlines=True)
  219. ESSENTIAL_BINARIES[command]['output'] = check.stdout
  220. if check.returncode in [0, 1]: # Older versions of dfu-programmer exit 1
  221. cli.log.debug('Found {fg_cyan}%s', command)
  222. return True
  223. cli.log.error("{fg_red}Can't run `%s %s`", command, version_arg)
  224. return False
  225. def os_tests():
  226. """Determine our OS and run platform specific tests
  227. """
  228. platform_id = platform.platform().lower()
  229. if 'darwin' in platform_id or 'macos' in platform_id:
  230. return os_test_macos()
  231. elif 'linux' in platform_id:
  232. return os_test_linux()
  233. elif 'windows' in platform_id:
  234. return os_test_windows()
  235. else:
  236. cli.log.warning('Unsupported OS detected: %s', platform_id)
  237. return CheckStatus.WARNING
  238. def os_test_linux():
  239. """Run the Linux specific tests.
  240. """
  241. cli.log.info("Detected {fg_cyan}Linux.")
  242. return check_udev_rules()
  243. def os_test_macos():
  244. """Run the Mac specific tests.
  245. """
  246. cli.log.info("Detected {fg_cyan}macOS.")
  247. return CheckStatus.OK
  248. def os_test_windows():
  249. """Run the Windows specific tests.
  250. """
  251. cli.log.info("Detected {fg_cyan}Windows.")
  252. return CheckStatus.OK
  253. @cli.argument('-y', '--yes', action='store_true', arg_only=True, help='Answer yes to all questions.')
  254. @cli.argument('-n', '--no', action='store_true', arg_only=True, help='Answer no to all questions.')
  255. @cli.subcommand('Basic QMK environment checks')
  256. def doctor(cli):
  257. """Basic QMK environment checks.
  258. This is currently very simple, it just checks that all the expected binaries are on your system.
  259. TODO(unclaimed):
  260. * [ ] Compile a trivial program with each compiler
  261. """
  262. cli.log.info('QMK Doctor is checking your environment.')
  263. status = os_tests()
  264. cli.log.info('QMK home: {fg_cyan}%s', QMK_FIRMWARE)
  265. # Make sure the basic CLI tools we need are available and can be executed.
  266. bin_ok = check_binaries()
  267. if not bin_ok:
  268. if yesno('Would you like to install dependencies?', default=True):
  269. run(['util/qmk_install.sh'])
  270. bin_ok = check_binaries()
  271. if bin_ok:
  272. cli.log.info('All dependencies are installed.')
  273. else:
  274. status = CheckStatus.ERROR
  275. # Make sure the tools are at the correct version
  276. ver_ok = []
  277. for check in (check_arm_gcc_version, check_avr_gcc_version, check_avrdude_version, check_dfu_util_version, check_dfu_programmer_version):
  278. ver_ok.append(check())
  279. if CheckStatus.ERROR in ver_ok:
  280. status = CheckStatus.ERROR
  281. elif CheckStatus.WARNING in ver_ok and status == CheckStatus.OK:
  282. status = CheckStatus.WARNING
  283. # Check out the QMK submodules
  284. sub_ok = check_submodules()
  285. if sub_ok == CheckStatus.OK:
  286. cli.log.info('Submodules are up to date.')
  287. else:
  288. if yesno('Would you like to clone the submodules?', default=True):
  289. submodules.update()
  290. sub_ok = check_submodules()
  291. if CheckStatus.ERROR in sub_ok:
  292. status = CheckStatus.ERROR
  293. elif CheckStatus.WARNING in sub_ok and status == CheckStatus.OK:
  294. status = CheckStatus.WARNING
  295. # Report a summary of our findings to the user
  296. if status == CheckStatus.OK:
  297. cli.log.info('{fg_green}QMK is ready to go')
  298. return 0
  299. elif status == CheckStatus.WARNING:
  300. cli.log.info('{fg_yellow}QMK is ready to go, but minor problems were found')
  301. return 1
  302. else:
  303. cli.log.info('{fg_red}Major problems detected, please fix these problems before proceeding.')
  304. cli.log.info('{fg_blue}Check out the FAQ (https://docs.qmk.fm/#/faq_build) or join the QMK Discord (https://discord.gg/Uq7gcHh) for help.')
  305. return 2