2020-01-24 20:31:16 +01:00
""" QMK Doctor
2019-07-15 21:14:27 +02:00
2020-01-24 20:31:16 +01:00
Check out the user ' s QMK environment and make sure it ' s ready to compile .
2019-07-15 21:14:27 +02:00
"""
2019-08-22 08:40:24 +02:00
import platform
2020-06-18 20:37:47 +02:00
import re
2019-08-22 08:40:24 +02:00
import shutil
import subprocess
2020-01-24 20:31:16 +01:00
from pathlib import Path
2019-07-15 21:14:27 +02:00
from milc import cli
2020-01-24 20:31:16 +01:00
from qmk import submodules
2020-09-24 07:16:00 +02:00
from qmk . constants import QMK_FIRMWARE
2020-01-24 20:31:16 +01:00
from qmk . questions import yesno
2020-03-29 14:29:44 +02:00
from qmk . commands import run
2020-01-24 20:31:16 +01:00
2020-03-08 17:21:45 +01:00
ESSENTIAL_BINARIES = {
' dfu-programmer ' : { } ,
2020-04-04 20:18:31 +02:00
' avrdude ' : { } ,
2020-03-08 17:21:45 +01:00
' dfu-util ' : { } ,
2020-04-03 01:46:26 +02:00
' avr-gcc ' : {
' version_arg ' : ' -dumpversion '
} ,
' arm-none-eabi-gcc ' : {
' version_arg ' : ' -dumpversion '
} ,
2020-03-08 17:21:45 +01:00
' bin/qmk ' : { } ,
}
2019-07-15 21:14:27 +02:00
2020-01-11 21:15:28 +01:00
2020-05-12 21:59:09 +02:00
def _udev_rule ( vid , pid = None , * args ) :
2019-11-07 19:53:03 +01:00
""" Helper function that return udev rules
"""
2020-05-12 21:59:09 +02:00
rule = " "
if pid :
rule = ' SUBSYSTEMS== " usb " , ATTRS {idVendor} == " %s " , ATTRS {idProduct} == " %s " , TAG+= " uaccess " , RUN {builtin} += " uaccess " ' % ( vid , pid )
else :
rule = ' SUBSYSTEMS== " usb " , ATTRS {idVendor} == " %s " , TAG+= " uaccess " , RUN {builtin} += " uaccess " ' % vid
if args :
rule = ' , ' . join ( [ rule , * args ] )
return rule
def _deprecated_udev_rule ( vid , pid = None ) :
""" Helper function that return udev rules
Note : these are no longer the recommended rules , this is just used to check for them
"""
2019-11-07 19:53:03 +01:00
if pid :
return ' SUBSYSTEMS== " usb " , ATTRS {idVendor} == " %s " , ATTRS {idProduct} == " %s " , MODE:= " 0666 " ' % ( vid , pid )
else :
return ' SUBSYSTEMS== " usb " , ATTRS {idVendor} == " %s " , MODE:= " 0666 " ' % vid
2019-07-15 21:14:27 +02:00
2020-01-11 21:15:28 +01:00
2020-06-18 20:37:47 +02:00
def parse_gcc_version ( version ) :
m = re . match ( r " ( \ d+)(?: \ .( \ d+))?(?: \ .( \ d+))? " , version )
return {
' major ' : int ( m . group ( 1 ) ) ,
' minor ' : int ( m . group ( 2 ) ) if m . group ( 2 ) else 0 ,
2020-10-07 02:10:19 +02:00
' patch ' : int ( m . group ( 3 ) ) if m . group ( 3 ) else 0 ,
2020-06-18 20:37:47 +02:00
}
2020-03-08 17:21:45 +01:00
def check_arm_gcc_version ( ) :
""" Returns True if the arm-none-eabi-gcc version is not known to cause problems.
"""
if ' output ' in ESSENTIAL_BINARIES [ ' arm-none-eabi-gcc ' ] :
2020-03-28 04:09:21 +01:00
version_number = ESSENTIAL_BINARIES [ ' arm-none-eabi-gcc ' ] [ ' output ' ] . strip ( )
2020-03-08 17:21:45 +01:00
cli . log . info ( ' Found arm-none-eabi-gcc version %s ' , version_number )
return True # Right now all known arm versions are ok
def check_avr_gcc_version ( ) :
""" Returns True if the avr-gcc version is not known to cause problems.
"""
if ' output ' in ESSENTIAL_BINARIES [ ' avr-gcc ' ] :
2020-03-28 04:09:21 +01:00
version_number = ESSENTIAL_BINARIES [ ' avr-gcc ' ] [ ' output ' ] . strip ( )
2020-03-08 17:21:45 +01:00
2020-06-18 20:37:47 +02:00
parsed_version = parse_gcc_version ( version_number )
if parsed_version [ ' major ' ] > 8 :
2020-03-08 17:21:45 +01:00
cli . log . error ( ' We do not recommend avr-gcc newer than 8. Downgrading to 8.x is recommended. ' )
return False
cli . log . info ( ' Found avr-gcc version %s ' , version_number )
return True
return False
2020-04-05 18:11:55 +02:00
def check_avrdude_version ( ) :
if ' output ' in ESSENTIAL_BINARIES [ ' avrdude ' ] :
last_line = ESSENTIAL_BINARIES [ ' avrdude ' ] [ ' output ' ] . split ( ' \n ' ) [ - 2 ]
version_number = last_line . split ( ) [ 2 ] [ : - 1 ]
cli . log . info ( ' Found avrdude version %s ' , version_number )
return True
def check_dfu_util_version ( ) :
if ' output ' in ESSENTIAL_BINARIES [ ' dfu-util ' ] :
first_line = ESSENTIAL_BINARIES [ ' dfu-util ' ] [ ' output ' ] . split ( ' \n ' ) [ 0 ]
version_number = first_line . split ( ) [ 1 ]
cli . log . info ( ' Found dfu-util version %s ' , version_number )
return True
def check_dfu_programmer_version ( ) :
if ' output ' in ESSENTIAL_BINARIES [ ' dfu-programmer ' ] :
first_line = ESSENTIAL_BINARIES [ ' dfu-programmer ' ] [ ' output ' ] . split ( ' \n ' ) [ 0 ]
version_number = first_line . split ( ) [ 1 ]
cli . log . info ( ' Found dfu-programmer version %s ' , version_number )
return True
2020-01-24 20:31:16 +01:00
def check_binaries ( ) :
""" Iterates through ESSENTIAL_BINARIES and tests them.
"""
ok = True
2020-03-08 17:21:45 +01:00
for binary in sorted ( ESSENTIAL_BINARIES ) :
2020-01-24 20:31:16 +01:00
if not is_executable ( binary ) :
ok = False
return ok
def check_submodules ( ) :
""" Iterates through all submodules to make sure they ' re cloned and up to date.
"""
ok = True
for submodule in submodules . status ( ) . values ( ) :
if submodule [ ' status ' ] is None :
2020-05-21 21:56:43 +02:00
cli . log . error ( ' Submodule %s has not yet been cloned! ' , submodule [ ' name ' ] )
ok = False
2020-01-24 20:31:16 +01:00
elif not submodule [ ' status ' ] :
2020-05-21 21:56:43 +02:00
cli . log . error ( ' Submodule %s is not up to date! ' , submodule [ ' name ' ] )
ok = False
2020-01-24 20:31:16 +01:00
return ok
def check_udev_rules ( ) :
""" Make sure the udev rules look good.
"""
ok = True
udev_dir = Path ( " /etc/udev/rules.d/ " )
desired_rules = {
2020-07-12 17:33:43 +02:00
' atmel-dfu ' : {
_udev_rule ( " 03EB " , " 2FEF " ) , # ATmega16U2
_udev_rule ( " 03EB " , " 2FF0 " ) , # ATmega32U2
_udev_rule ( " 03EB " , " 2FF3 " ) , # ATmega16U4
_udev_rule ( " 03EB " , " 2FF4 " ) , # ATmega32U4
_udev_rule ( " 03EB " , " 2FF9 " ) , # AT90USB64
_udev_rule ( " 03EB " , " 2FFB " ) # AT90USB128
} ,
' kiibohd ' : {
_udev_rule ( " 1C11 " , " B007 " )
} ,
' stm32 ' : {
_udev_rule ( " 1EAF " , " 0003 " ) , # STM32duino
_udev_rule ( " 0483 " , " DF11 " ) # STM32 DFU
} ,
' bootloadhid ' : {
_udev_rule ( " 16C0 " , " 05DF " )
} ,
' usbasploader ' : {
_udev_rule ( " 16C0 " , " 05DC " )
} ,
' massdrop ' : {
_udev_rule ( " 03EB " , " 6124 " )
} ,
2020-05-12 21:59:09 +02:00
' caterina ' : {
2020-09-18 21:40:25 +02:00
# Spark Fun Electronics
_udev_rule ( " 1B4F " , " 9203 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # Pro Micro 3V3/8MHz
_udev_rule ( " 1B4F " , " 9205 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # Pro Micro 5V/16MHz
_udev_rule ( " 1B4F " , " 9207 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # LilyPad 3V3/8MHz (and some Pro Micro clones)
# Pololu Electronics
_udev_rule ( " 1FFB " , " 0101 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # A-Star 32U4
# Arduino SA
_udev_rule ( " 2341 " , " 0036 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # Leonardo
_udev_rule ( " 2341 " , " 0037 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # Micro
# Adafruit Industries LLC
_udev_rule ( " 239A " , " 000C " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # Feather 32U4
_udev_rule ( " 239A " , " 000D " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # ItsyBitsy 32U4 3V3/8MHz
_udev_rule ( " 239A " , " 000E " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # ItsyBitsy 32U4 5V/16MHz
# dog hunter AG
_udev_rule ( " 2A03 " , " 0036 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) , # Leonardo
_udev_rule ( " 2A03 " , " 0037 " , ' ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' ) # Micro
2020-05-12 21:59:09 +02:00
}
}
# These rules are no longer recommended, only use them to check for their presence.
deprecated_rules = {
2020-07-12 17:33:43 +02:00
' atmel-dfu ' : { _deprecated_udev_rule ( " 03eb " , " 2ff4 " ) , _deprecated_udev_rule ( " 03eb " , " 2ffb " ) , _deprecated_udev_rule ( " 03eb " , " 2ff0 " ) } ,
' kiibohd ' : { _deprecated_udev_rule ( " 1c11 " ) } ,
2020-05-12 21:59:09 +02:00
' stm32 ' : { _deprecated_udev_rule ( " 1eaf " , " 0003 " ) , _deprecated_udev_rule ( " 0483 " , " df11 " ) } ,
' bootloadhid ' : { _deprecated_udev_rule ( " 16c0 " , " 05df " ) } ,
2020-07-12 17:26:46 +02:00
' caterina ' : { ' ATTRS {idVendor} == " 2a03 " , ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' , ' ATTRS {idVendor} == " 2341 " , ENV {ID_MM_DEVICE_IGNORE} = " 1 " ' } ,
2020-07-12 17:24:28 +02:00
' tmk ' : { _deprecated_udev_rule ( " feed " ) }
2020-01-24 20:31:16 +01:00
}
if udev_dir . exists ( ) :
2020-04-18 22:00:56 +02:00
udev_rules = [ rule_file for rule_file in udev_dir . glob ( ' *.rules ' ) ]
2020-01-24 20:31:16 +01:00
current_rules = set ( )
# Collect all rules from the config files
for rule_file in udev_rules :
2020-04-18 22:00:56 +02:00
for line in rule_file . read_text ( ) . split ( ' \n ' ) :
line = line . strip ( )
if not line . startswith ( " # " ) and len ( line ) :
current_rules . add ( line )
2020-01-24 20:31:16 +01:00
# Check if the desired rules are among the currently present rules
for bootloader , rules in desired_rules . items ( ) :
2020-05-12 21:59:09 +02:00
# For caterina, check if ModemManager is running
if bootloader == " caterina " :
if check_modem_manager ( ) :
ok = False
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. " )
2020-01-24 20:31:16 +01:00
if not rules . issubset ( current_rules ) :
2020-05-12 21:59:09 +02:00
deprecated_rule = deprecated_rules . get ( bootloader )
if deprecated_rule and deprecated_rule . issubset ( current_rules ) :
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 )
2020-01-24 20:31:16 +01:00
else :
2020-07-12 17:37:31 +02:00
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 )
2020-01-24 20:31:16 +01:00
return ok
def check_modem_manager ( ) :
""" Returns True if ModemManager is running.
"""
if shutil . which ( " systemctl " ) :
2020-03-29 14:29:44 +02:00
mm_check = run ( [ " systemctl " , " --quiet " , " is-active " , " ModemManager.service " ] , timeout = 10 )
2020-01-24 20:31:16 +01:00
if mm_check . returncode == 0 :
return True
else :
cli . log . warn ( " Can ' t find systemctl to check for ModemManager. " )
def is_executable ( command ) :
""" Returns True if command exists and can be executed.
"""
# Make sure the command is in the path.
res = shutil . which ( command )
if res is None :
cli . log . error ( " {fg_red} Can ' t find %s in your path. " , command )
return False
# Make sure the command can be executed
2020-03-31 02:41:58 +02:00
version_arg = ESSENTIAL_BINARIES [ command ] . get ( ' version_arg ' , ' --version ' )
2020-04-05 18:11:55 +02:00
check = run ( [ command , version_arg ] , stdout = subprocess . PIPE , stderr = subprocess . STDOUT , timeout = 5 , universal_newlines = True )
2020-03-28 04:09:21 +01:00
2020-03-08 17:21:45 +01:00
ESSENTIAL_BINARIES [ command ] [ ' output ' ] = check . stdout
2020-01-24 20:31:16 +01:00
if check . returncode in [ 0 , 1 ] : # Older versions of dfu-programmer exit 1
cli . log . debug ( ' Found {fg_cyan} %s ' , command )
return True
2020-04-14 16:58:00 +02:00
cli . log . error ( " {fg_red} Can ' t run ` %s %s ` " , command , version_arg )
2020-01-24 20:31:16 +01:00
return False
def os_test_linux ( ) :
""" Run the Linux specific tests.
"""
cli . log . info ( " Detected {fg_cyan} Linux. " )
ok = True
if not check_udev_rules ( ) :
ok = False
return ok
def os_test_macos ( ) :
""" Run the Mac specific tests.
"""
cli . log . info ( " Detected {fg_cyan} macOS. " )
return True
def os_test_windows ( ) :
""" Run the Windows specific tests.
"""
cli . log . info ( " Detected {fg_cyan} Windows. " )
return True
@cli.argument ( ' -y ' , ' --yes ' , action = ' store_true ' , arg_only = True , help = ' Answer yes to all questions. ' )
@cli.argument ( ' -n ' , ' --no ' , action = ' store_true ' , arg_only = True , help = ' Answer no to all questions. ' )
2019-09-22 22:25:33 +02:00
@cli.subcommand ( ' Basic QMK environment checks ' )
def doctor ( cli ) :
2019-07-15 21:14:27 +02:00
""" Basic QMK environment checks.
This is currently very simple , it just checks that all the expected binaries are on your system .
TODO ( unclaimed ) :
* [ ] Compile a trivial program with each compiler
"""
2019-08-22 18:38:10 +02:00
cli . log . info ( ' QMK Doctor is checking your environment. ' )
2019-07-15 21:14:27 +02:00
ok = True
2019-08-22 18:38:10 +02:00
# Determine our OS and run platform specific tests
2020-03-29 14:29:44 +02:00
platform_id = platform . platform ( ) . lower ( )
2019-08-22 18:38:10 +02:00
2020-03-29 14:29:44 +02:00
if ' darwin ' in platform_id or ' macos ' in platform_id :
2020-01-24 20:31:16 +01:00
if not os_test_macos ( ) :
ok = False
2020-03-29 14:29:44 +02:00
elif ' linux ' in platform_id :
2020-01-24 20:31:16 +01:00
if not os_test_linux ( ) :
ok = False
2020-03-29 14:29:44 +02:00
elif ' windows ' in platform_id :
2020-01-24 20:31:16 +01:00
if not os_test_windows ( ) :
ok = False
else :
2020-03-29 14:29:44 +02:00
cli . log . error ( ' Unsupported OS detected: %s ' , platform_id )
2020-01-24 20:31:16 +01:00
ok = False
2019-08-22 18:38:10 +02:00
2020-09-24 07:16:00 +02:00
cli . log . info ( ' QMK home: {fg_cyan} %s ' , QMK_FIRMWARE )
2020-01-24 20:31:16 +01:00
# Make sure the basic CLI tools we need are available and can be executed.
bin_ok = check_binaries ( )
if not bin_ok :
if yesno ( ' Would you like to install dependencies? ' , default = True ) :
2020-03-29 14:29:44 +02:00
run ( [ ' util/qmk_install.sh ' ] )
2020-01-24 20:31:16 +01:00
bin_ok = check_binaries ( )
if bin_ok :
cli . log . info ( ' All dependencies are installed. ' )
2019-07-15 21:14:27 +02:00
else :
2020-01-24 20:31:16 +01:00
ok = False
2020-03-08 17:21:45 +01:00
# Make sure the tools are at the correct version
2020-04-05 18:11:55 +02:00
for check in ( check_arm_gcc_version , check_avr_gcc_version , check_avrdude_version , check_dfu_util_version , check_dfu_programmer_version ) :
if not check ( ) :
ok = False
2020-03-08 17:21:45 +01:00
2020-01-24 20:31:16 +01:00
# Check out the QMK submodules
sub_ok = check_submodules ( )
if sub_ok :
cli . log . info ( ' Submodules are up to date. ' )
else :
if yesno ( ' Would you like to clone the submodules? ' , default = True ) :
submodules . update ( )
sub_ok = check_submodules ( )
if not sub_ok :
ok = False
2019-07-15 21:14:27 +02:00
2019-08-22 18:38:10 +02:00
# Report a summary of our findings to the user
2019-07-15 21:14:27 +02:00
if ok :
cli . log . info ( ' {fg_green} QMK is ready to go ' )
2019-08-22 08:40:24 +02:00
else :
cli . log . info ( ' {fg_yellow} Problems detected, please fix these problems before proceeding. ' )
2020-01-24 20:31:16 +01:00
# FIXME(skullydazed/unclaimed): Link to a document about troubleshooting, or discord or something
2020-10-07 01:46:10 +02:00
return ok