Skip to content

Commit 12d3041

Browse files
Adhil-IgnMuhammedZamroodh
authored andcommitted
dspic: Windows host fixes for west commands (zephyrproject-rtos#38)
dspic: Windows host fixes for west flash West flash tool was failing, modified flash tool script In windows build was failing with Path name with spaces, using modified cmake script. Signed-off-by: Adhil Xavier <[email protected]>
1 parent 5bdcd51 commit 12d3041

File tree

2 files changed

+128
-25
lines changed

2 files changed

+128
-25
lines changed

cmake/toolchain/xcdsc/generic.cmake

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,88 @@
11
zephyr_get(XCDSC_TOOLCHAIN_PATH)
2+
3+
if(WIN32)
4+
# Detect invoking shell.
5+
set(_CURRENT_SHELL "cmd")
6+
if(DEFINED ENV{PSModulePath})
7+
set(_CURRENT_SHELL "powershell")
8+
endif()
9+
message(STATUS "Detected Windows shell: ${_CURRENT_SHELL}")
10+
11+
# Buffer toolchain path from environment variable
12+
set(_XCDSC_REAL "$ENV{XCDSC_TOOLCHAIN_PATH}")
13+
if(NOT _XCDSC_REAL)
14+
message(FATAL_ERROR "XCDSC_TOOLCHAIN_PATH is not set in the environment.")
15+
endif()
16+
17+
# Fixed junction path under %TEMP%
18+
set(_XCDSC_LINK "$ENV{TEMP}\\xcdsc")
19+
# Native path for cmd.exe commands
20+
file(TO_NATIVE_PATH "${_XCDSC_LINK}" _XCDSC_LINK_WIN)
21+
file(TO_NATIVE_PATH "${_XCDSC_REAL}" _XCDSC_REAL_WIN)
22+
23+
# Ensures %TEMP%\xcdsc is a symlink/junction pointing to XCDSC_TOOLCHAIN_PATH,
24+
# creating or replacing it as needed (shell chosen via _CURRENT_SHELL).
25+
if(_CURRENT_SHELL STREQUAL "powershell")
26+
message(STATUS "Ensuring junction via PowerShell")
27+
execute_process(
28+
COMMAND powershell -NoProfile -ExecutionPolicy Bypass -Command
29+
"\$ErrorActionPreference='Stop'; \$ConfirmPreference='None';"
30+
"\$p = Join-Path \$env:TEMP 'xcdsc';"
31+
"\$t = '${_XCDSC_REAL}';"
32+
"if (Test-Path -LiteralPath \$p) {"
33+
" \$it = Get-Item -LiteralPath \$p -Force;"
34+
" if (-not (\$it.Attributes -band [IO.FileAttributes]::ReparsePoint)) {"
35+
" if (\$it.PSIsContainer) {"
36+
" Remove-Item -LiteralPath \$p -Recurse -Force -Confirm:\$false"
37+
" } else {"
38+
" Remove-Item -LiteralPath \$p -Force -Confirm:\$false"
39+
" }"
40+
" New-Item -ItemType Junction -Path \$p -Target \$t | Out-Null"
41+
" }"
42+
"} else {"
43+
" New-Item -ItemType Junction -Path \$p -Target \$t | Out-Null"
44+
"}"
45+
RESULT_VARIABLE _ps_rv
46+
OUTPUT_VARIABLE _ps_out
47+
ERROR_VARIABLE _ps_err
48+
)
49+
if(NOT _ps_rv EQUAL 0)
50+
message(FATAL_ERROR "Failed to ensure %TEMP%\\xcdsc junction (PowerShell).\n${_ps_err}")
51+
endif()
52+
else()
53+
message(STATUS "Ensuring junction via Command Prompt")
54+
# Ensures %TEMP%\xcdsc is a symlink/junction to XCDSC_TOOLCHAIN_PATH via cmd,
55+
# creating or replacing it as needed.
56+
execute_process(
57+
COMMAND cmd.exe /C
58+
"if exist \"${_XCDSC_LINK_WIN}\" ( "
59+
" fsutil reparsepoint query \"${_XCDSC_LINK_WIN}\" >nul 2>&1 && ( ver >nul ) "
60+
" || ( "
61+
" if exist \"${_XCDSC_LINK_WIN}\\*\" ( "
62+
" rmdir /S /Q \"${_XCDSC_LINK_WIN}\" "
63+
" ) else ( "
64+
" del /Q /F \"${_XCDSC_LINK_WIN}\" 2>nul "
65+
" ) & "
66+
" mklink /J \"${_XCDSC_LINK_WIN}\" \"${_XCDSC_REAL_WIN}\" "
67+
" ) "
68+
") else ( "
69+
" mklink /J \"${_XCDSC_LINK_WIN}\" \"${_XCDSC_REAL_WIN}\" "
70+
")"
71+
RESULT_VARIABLE _cmd_rv
72+
OUTPUT_VARIABLE _cmd_out
73+
ERROR_VARIABLE _cmd_err
74+
)
75+
if(NOT _cmd_rv EQUAL 0)
76+
message(FATAL_ERROR "Failed to ensure %TEMP%\\xcdsc junction (cmd).\n${_cmd_err}")
77+
endif()
78+
endif()
79+
80+
# Use the junction for the toolchain
81+
set(XCDSC_TOOLCHAIN_PATH "${_XCDSC_LINK}")
82+
message(STATUS "XCDSC toolchain (real): ${_XCDSC_REAL}")
83+
message(STATUS "XCDSC toolchain (via junction): ${XCDSC_TOOLCHAIN_PATH}")
84+
endif()
85+
286
# Set toolchain module name to xcdsc
387
set(COMPILER xcdsc)
488
set(LINKER xcdsc)
@@ -17,3 +101,4 @@ else() # Support for picolibc is indicated by the presence of 'picolibc.h' in th
17101
set(TOOLCHAIN_HAS_PICOLIBC ON CACHE BOOL "True if toolchain supports picolibc")
18102
endif()
19103
endif()
104+

scripts/west_commands/runners/ipecmd.py

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,35 @@ def __init__(self, cfg, device, flash_tool):
2424
self.mplabx_base = None
2525
self.java_bin = None
2626
self.ipecmd_jar = None
27-
if platform.system() == 'Linux' or platform.system() == 'Windows':
28-
self.mplabx_base = self.find_mplabx_base()
29-
if not self.mplabx_base:
30-
print("Error: Could not locate mplabx base directory")
31-
sys.exit(1)
3227

33-
version_path = self.find_latest_version_dir(self.mplabx_base)
34-
if not version_path:
35-
print("Error: No MPLAB X version directories found")
36-
sys.exit(1)
28+
self.mplabx_base = self.find_mplabx_base()
29+
if not self.mplabx_base:
30+
print("Error: Could not locate mplabx base directory")
31+
sys.exit(1)
3732

33+
version_path = self.find_latest_version_dir(self.mplabx_base)
34+
if not version_path:
35+
print("Error: No MPLAB X version directories found")
36+
sys.exit(1)
37+
if platform.system() == 'Linux':
3838
self.java_bin = self.find_java_bin(version_path)
3939
if not self.java_bin or not os.access(self.java_bin, os.X_OK):
4040
print("Error: Java executable not found or not executable")
4141
sys.exit(1)
4242

43-
self.ipecmd_jar = self.find_ipecmd_jar(version_path)
43+
self.ipecmd_jar = self.find_ipecmd(version_path, "ipecmd.jar")
4444
if not self.ipecmd_jar:
4545
print(f"Error: ipecmd.jar not found in {version_path}/mplab_platform/mplab_ipe/")
4646
sys.exit(1)
4747
else:
4848
print(f'ipecmd: {self.ipecmd_jar}')
49+
elif platform.system() == 'Windows':
50+
self.ipecmd_exe = self.find_ipecmd(version_path, "ipecmd.exe")
51+
if not self.ipecmd_exe:
52+
print(f"Error: ipecmd.exe not found in {version_path}/mplab_platform/mplab_ipe/")
53+
sys.exit(1)
54+
else:
55+
print(f'ipecmd: {self.ipecmd_exe}')
4956
self.app_bin = cfg.bin_file
5057
print(f'bin file: {cfg.bin_file}')
5158
self.hex_file = cfg.hex_file
@@ -78,19 +85,30 @@ def do_run(self, command, **kwargs):
7885
self.ensure_output('hex')
7986

8087
self.logger.info(f'Flashing file: {self.hex_file}')
88+
self.logger.info(f'flash tool: {self.flash_tool}, Device: {self.device}')
8189
if self.hex_file is not None:
82-
self.logger.info(f'flash tool: {self.flash_tool}, Device: {self.device}')
83-
self.logger.info(f'flash cmd: {self.ipecmd_jar}')
84-
cmd = [
85-
str(self.java_bin),
86-
'-jar',
87-
str(self.ipecmd_jar),
88-
'-TP' + self.flash_tool,
89-
'-P' + self.device,
90-
'-M',
91-
'-F' + self.hex_file,
92-
'-OL',
93-
]
90+
if platform.system() == 'Linux':
91+
self.logger.info(f'flash cmd: {self.ipecmd_jar}')
92+
cmd = [
93+
str(self.java_bin),
94+
'-jar',
95+
str(self.ipecmd_jar),
96+
'-TP' + self.flash_tool,
97+
'-P' + self.device,
98+
'-M',
99+
'-F' + self.hex_file,
100+
'-OL',
101+
]
102+
elif platform.system() == 'Windows':
103+
self.logger.info(f'flash cmd: {self.ipecmd_exe}')
104+
cmd = [
105+
str(self.ipecmd_exe),
106+
'-TP' + self.flash_tool,
107+
'-P' + self.device,
108+
'-M',
109+
'-F' + self.hex_file,
110+
'-OL',
111+
]
94112
self.require(cmd[0])
95113
self.check_call(cmd)
96114
else:
@@ -118,9 +136,9 @@ def find_java_bin(self, version_path):
118136
sys.exit(1)
119137
return java_dirs[0] if java_dirs else None
120138

121-
def find_ipecmd_jar(self, version_path):
139+
def find_ipecmd(self, version_path, tool):
122140
ipe_dir = version_path / "mplab_platform/mplab_ipe"
123141
for root, _, files in os.walk(ipe_dir):
124-
if "ipecmd.jar" in files:
125-
return Path(root) / "ipecmd.jar"
142+
if tool in files:
143+
return Path(root) / tool
126144
return None

0 commit comments

Comments
 (0)