|
26 | 26 | except ImportError:
|
27 | 27 | MISSING_REQUIREMENTS = True
|
28 | 28 |
|
29 |
| -if sys.platform == 'win32': |
30 |
| - # JLink.exe can collide with the JDK executable of the same name |
31 |
| - # Look in the usual locations before falling back to $PATH |
32 |
| - for root in [os.environ["ProgramFiles"], os.environ["ProgramFiles(x86)"], str(Path.home())]: # noqa SIM112 |
33 |
| - # SEGGER folder can contain a single "JLink" folder |
34 |
| - _direct = Path(root) / "SEGGER" / "JLink" / "JLink.exe" |
35 |
| - if _direct.exists(): |
36 |
| - DEFAULT_JLINK_EXE = str(_direct) |
37 |
| - del _direct |
38 |
| - else: |
39 |
| - # SEGGER folder can contain multiple versions such as: |
40 |
| - # JLink_V796b |
41 |
| - # JLink_V796t |
42 |
| - # JLink_V798c |
43 |
| - # Find the latest version |
44 |
| - _versions = glob.glob(str(Path(root) / "SEGGER" / "JLink_V*")) |
45 |
| - if len(_versions) == 0: |
46 |
| - continue |
47 |
| - _expected_jlink = Path(_versions[-1]) / "JLink.exe" |
48 |
| - if not _expected_jlink.exists(): |
49 |
| - continue |
50 |
| - DEFAULT_JLINK_EXE = str(_expected_jlink) |
51 |
| - # Cleanup variables |
52 |
| - del _versions |
53 |
| - del _expected_jlink |
54 |
| - break |
55 |
| - else: |
56 |
| - # Not found in the normal locations, hope that $PATH is correct |
57 |
| - DEFAULT_JLINK_EXE = "JLink.exe" |
58 |
| -else: |
59 |
| - DEFAULT_JLINK_EXE = "JLinkExe" |
| 29 | +# Populated in do_add_parser() |
| 30 | +DEFAULT_JLINK_EXE = None |
60 | 31 | DEFAULT_JLINK_GDB_PORT = 2331
|
61 | 32 | DEFAULT_JLINK_RTT_PORT = 19021
|
62 | 33 |
|
@@ -138,8 +109,43 @@ def dev_id_help(cls) -> str:
|
138 | 109 | def tool_opt_help(cls) -> str:
|
139 | 110 | return "Additional options for JLink Commander, e.g. '-autoconnect 1'"
|
140 | 111 |
|
| 112 | + @staticmethod |
| 113 | + def find_jlink(): |
| 114 | + global DEFAULT_JLINK_EXE |
| 115 | + |
| 116 | + if sys.platform == 'win32': |
| 117 | + # JLink.exe can collide with the JDK executable of the same name |
| 118 | + # Look in the usual locations before falling back to $PATH |
| 119 | + for root in [os.environ["ProgramFiles"], os.environ["ProgramFiles(x86)"], str(Path.home())]: # noqa SIM112 |
| 120 | + # SEGGER folder can contain a single "JLink" folder |
| 121 | + _direct = Path(root) / "SEGGER" / "JLink" / "JLink.exe" |
| 122 | + if _direct.exists(): |
| 123 | + DEFAULT_JLINK_EXE = str(_direct) |
| 124 | + else: |
| 125 | + # SEGGER folder can contain multiple versions such as: |
| 126 | + # JLink_V796b |
| 127 | + # JLink_V796t |
| 128 | + # JLink_V798c |
| 129 | + # Find the latest version |
| 130 | + _versions = glob.glob(str(Path(root) / "SEGGER" / "JLink_V*")) |
| 131 | + if len(_versions) == 0: |
| 132 | + continue |
| 133 | + _expected_jlink = Path(_versions[-1]) / "JLink.exe" |
| 134 | + if not _expected_jlink.exists(): |
| 135 | + continue |
| 136 | + DEFAULT_JLINK_EXE = str(_expected_jlink) |
| 137 | + break |
| 138 | + else: |
| 139 | + # Not found in the normal locations, hope that $PATH is correct |
| 140 | + DEFAULT_JLINK_EXE = "JLink.exe" |
| 141 | + else: |
| 142 | + DEFAULT_JLINK_EXE = "JLinkExe" |
| 143 | + |
141 | 144 | @classmethod
|
142 | 145 | def do_add_parser(cls, parser):
|
| 146 | + |
| 147 | + cls.find_jlink() |
| 148 | + |
143 | 149 | # Required:
|
144 | 150 | parser.add_argument('--device', required=True, help='device name')
|
145 | 151 |
|
|
0 commit comments