Skip to content

Commit 4c12a8c

Browse files
carlescufikartben
authored andcommitted
west: runners: jlink: Avoid running logic when another runner is used
The logic to detect the default JLink location was placed at the top of the script, executing whenever the script was imported. The west extension command framework loads all runners when initializing, and so this logic was being executed even when using another runner. Move the logic to a function that is only executed when the JLink runner is selected, to avoid executing it at all times. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
1 parent 93191aa commit 4c12a8c

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

scripts/west_commands/runners/jlink.py

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,8 @@
2626
except ImportError:
2727
MISSING_REQUIREMENTS = True
2828

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
6031
DEFAULT_JLINK_GDB_PORT = 2331
6132
DEFAULT_JLINK_RTT_PORT = 19021
6233

@@ -138,8 +109,43 @@ def dev_id_help(cls) -> str:
138109
def tool_opt_help(cls) -> str:
139110
return "Additional options for JLink Commander, e.g. '-autoconnect 1'"
140111

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+
141144
@classmethod
142145
def do_add_parser(cls, parser):
146+
147+
cls.find_jlink()
148+
143149
# Required:
144150
parser.add_argument('--device', required=True, help='device name')
145151

0 commit comments

Comments
 (0)