doctor.py 12 KB

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