1@echo off
2REM RASC launcher 2024-05-23
3
4setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
5
6REM Initialisations
7set "RascVersionFileHeader=# RASC version and installation file"
8set "RascDescRootKey=SOFTWARE\Renesas\RASC\Installations"
9set "VersionUnknown=Unknown"
10set "RascVersionValueName=Version"
11set "RascExeValueName=ExePath"
12set "RascSearchPath=C:\Renesas"
13set /a NumRascs=0
14set "TargetRascVersion="
15set "TargetRascExe="
16set "TargetRascVersionDiffers="
17
18REM First parameter is (possibly non-existent) file containing RASC version to invoke
19set "RascVersionFile=%~1"
20
21REM Shift to leave remaining parameters as input parameters to RASC
22shift
23
24REM Extract specific RASC version from file
25REM echo "%RascVersionFile%"
26if exist "%RascVersionFile%" (
27
28    REM echo DEBUG: Have version file: "%RascVersionFile%"
29
30    set /a idx=0
31    for /f "usebackq tokens=*" %%a in ("%RascVersionFile%") do (
32        if !idx! EQU 0 (
33            if not "%%a" == "%RascVersionFileHeader%" (
34                REM echo DEBUG: Header doesn't match
35
36                goto _EndVersionFileParse
37            )
38        )
39        if !idx! EQU 1 (
40            set "TargetRascVersion=%%a"
41        )
42        if !idx! EQU 2 (
43            set "TargetRascExe=%%a"
44        )
45        set /a idx+=1
46    )
47)
48
49:_EndVersionFileParse
50
51REM echo DEBUG: Target version: "%TargetRascVersion%"
52REM echo DEBUG: Target exe: "%TargetRascExe%"
53
54REM Search through registry RASC descriptions for match on exe path and version
55for %%h in (HKCU HKLM) do (
56    for %%v in (32 64) do (
57        for /f "usebackq skip=1 tokens=*" %%a in (`reg query "%%h\%RascDescRootKey%" /reg:%%v 2^>nul`) do (
58            set "RascDescKey=%%a"
59            set "RascVersion="
60            set "RascExe="
61
62            REM echo DEBUG: Desc Key: !RascDescKey!
63
64            for /f "usebackq skip=2 tokens=3" %%b in (`reg query "!RascDescKey!" /v "%RascVersionValueName%" /reg:%%v 2^>nul`) do (
65                set "RascVersion=%%b"
66            )
67
68            REM echo DEBUG: Version: !RascVersion!
69
70            for /f "usebackq skip=2 tokens=2*" %%b in (`reg query "!RascDescKey!" /v "%RascExeValueName%" /reg:%%v 2^>nul`) do (
71                REM %%b is value name, so %%c is the value - supports values with spaces
72                set "RascExe=%%c"
73            )
74
75            REM echo DEBUG: Exe: !RascExe!
76
77            if not defined RascExe (
78                REM Error - unable to extract executable
79                set ErrorMessage=Unable to extract RASC executable path from the registry
80                goto _Error
81            )
82
83            REM Check if exe exists, otherwise assume it's been removed
84            if exist "!RascExe!" (
85                REM Check for specified target version and exe path match
86                if defined RascVersion (
87                    if defined TargetRascVersion (
88                        if /i "!RascExe!" == "%TargetRascExe%" (
89                            echo "!RascVersion!"
90                            echo "%TargetRascVersion%"
91                            if "!RascVersion!" == "%TargetRascVersion%" (
92
93                                REM echo DEBUG: Found match
94
95                                goto _LaunchRasc
96                            ) else (
97                                REM Indicate target RASC has a different version than
98                                REM the registry entry. In this case, target RASC has
99                                REM changed, so possibly prompt the user to select a
100                                REM RASC again
101                                set "TargetRascVersionDiffers=true"
102                            )
103                        )
104                    )
105                ) else (
106                    REM Error - unable to extract version
107                    set ErrorMessage=Unable to extract RASC version from the registry
108                    goto _Error
109                )
110
111                call :SubAddFoundRasc "!RascExe!" "!RascVersion!"
112            )
113        )
114    )
115)
116
117REM If target RASC exists and doesn't differ from the registry version (i.e.
118REM was not found in the registry), just run it
119if defined TargetRascExe (
120    if exist "%TargetRascExe%" (
121        if not defined TargetRascVersionDiffers (
122            set "RascExe=%TargetRascExe%"
123            set "RascVersion=%VersionUnknown%"
124            goto _LaunchRasc
125        )
126    )
127)
128
129if %NumRascs% EQU 0 (
130    REM No entries found in the registry, search C:\Renesas\ as fallback
131    echo/
132    echo Searching in "%RascSearchPath%" for RA Smart Configurator installations ...
133    for /f "usebackq tokens=*" %%a in (`dir "%RascSearchPath%\rasc.exe" /s /b 2^>nul`) do (
134        if not "%%a" == "" (
135            call :SubAddFoundRasc "%%a" "%VersionUnknown%"
136        )
137    )
138)
139
140if %NumRascs% EQU 0 (
141    REM Still no RASCs found - give up
142    set ErrorMessage=No "RA Smart Configurator" installations found, download one from renesas.com
143    goto _Error
144)
145
146if %NumRascs% EQU 1 (
147    set "RascExe=%RascExeList[0]%"
148    set "RascVersion=%RascVersionList[0]%"
149    goto _LaunchRasc
150)
151
152REM Prompt for user to choose from multiple RASCs
153echo/
154echo Multiple RA Smart Configurators installed:
155set /a RascIdxMax=%NumRascs% - 1
156set Choices=""
157for /l %%a in (0,1,%RascIdxMax%) do (
158    echo %%a: Version !RascVersionList[%%a]! ^("!RascExeList[%%a]!"^)
159    set "Choices=!Choices!%%a"
160)
161echo/
162set /a ChosenIdx=%NumRascs%
163if %RascIdxMax% GTR 9 (
164    set /p InputIdx=Select which one to run [0-%RascIdxMax%]?
165    REM Check if the input string is a number
166    set "NonNumber=" & for /f "delims=0123456789" %%i in ("!InputIdx!") do set "NonNumber=%%i"
167    if not defined NonNumber (
168        set /a ChosenIdx=!InputIdx!
169    )
170) else (
171    choice /c %Choices% /m "Select which one to run"
172    set /a ChosenIdx=!ERRORLEVEL! - 1
173)
174if %ChosenIdx% GEQ %NumRascs% (
175    REM Out of range
176    set ErrorMessage=Invalid selection
177    goto _Error
178)
179set "RascExe=!RascExeList[%ChosenIdx%]!"
180set "RascVersion=!RascVersionList[%ChosenIdx%]!"
181
182:_LaunchRasc
183
184REM Carefully re-write specific version file, if required
185if exist "%RascVersionFile%" (
186    if not defined TargetRascVersion (
187        if not defined TargetRascExe (
188            REM Unexpected version file contents, skip rewriting
189            goto _EndRascVersionRewrite
190        )
191    )
192)
193
194if "!RascVersion!" == "%TargetRascVersion%" (
195    if /i "!RascExe!" == "%TargetRascExe%" (
196        REM Version file already up-to-date, skip rewriting
197        goto _EndRascVersionRewrite
198    )
199)
200
201echo %RascVersionFileHeader%>"%RascVersionFile%"
202echo %RascVersion%>>"%RascVersionFile%"
203echo %RascExe%>>"%RascVersionFile%"
204
205:_EndRascVersionRewrite
206
207REM Synchronous behaviour for build pre/post steps
208set "WaitRasc="
209IF "%~3"=="--generate" SET CLI=true
210IF "%~3"=="--gensmartbundle" SET CLI=true
211IF "%CLI%"=="true" (
212    SET "WaitRasc=/b /wait"
213    SET RascExe=%RascExe:rasc.exe=rascc.exe%
214)
215
216set Parameters=
217for %%a in (%*) do (
218    if defined FirstParamSkipped set Parameters=!Parameters! %%a
219    set FirstParamSkipped=true
220)
221REM echo DEBUG: Launching "%RascExe%" %Parameters%
222start "" %WaitRasc% "%RascExe%" %Parameters% & goto :EOF
223
224
225REM Add specified RASC to pseudo-list
226REM Parameters:
227REM 1: RascExe
228REM 2: RascVersion
229:SubAddFoundRasc
230set "RascExeList[%NumRascs%]=%~1"
231set "RascVersionList[%NumRascs%]=%~2"
232set /a NumRascs+=1
233goto :EOF
234
235
236:_Error
237REM start cmd /c "echo %ErrorMessage% && pause"
238echo/
239echo %ErrorMessage% && pause
240goto :EOF
241