Skip to content

Commit 5f0c800

Browse files
JordanYateskartben
authored andcommitted
runners: jlink: win32: search for valid JLink.exe
Since "JLink.exe" is also an executable distributed with JDK's, do an explicit search on the standard SEGGER install directories for a JTAG "JLink.exe" before falling back to whatever is first on PATH. Fixes #51825. Signed-off-by: Jordan Yates <[email protected]>
1 parent a5643a4 commit 5f0c800

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

scripts/west_commands/runners/jlink.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'''Runner for debugging with J-Link.'''
66

77
import argparse
8+
import glob
89
import ipaddress
910
import logging
1011
import os
@@ -25,7 +26,37 @@
2526
except ImportError:
2627
MISSING_REQUIREMENTS = True
2728

28-
DEFAULT_JLINK_EXE = 'JLink.exe' if sys.platform == 'win32' else 'JLinkExe'
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"
2960
DEFAULT_JLINK_GDB_PORT = 2331
3061
DEFAULT_JLINK_RTT_PORT = 19021
3162

0 commit comments

Comments
 (0)