0

Debugging the script below "rpmbuild.bat". NOTE: it contains bugs, not complete. This is command line:

rpmbuild.bat -bb --target "noarch-pc-windows 7" --buildroot D:\MyPath\MyApp\buildroot --define "_topdir D:\MyPath\MyApp" MyApp.spec

The idea is take the above ".bat" cmd parameters, modify them and redirect (via cygwin) to unix tool with the same name (rpmbuild). So to have smth like:

bash -c "rpmbuild -bb --target ""noarch-pc-windows 7"" --buildroot /cygdrive/d/MyPath/MyApp/buildroot --define ""_topdir /cygdrive/d/MyPath/MyApp"" MyApp.spec"

For transforming paths the proper way, there is an utility cygpath.

Below is source of the rpmbuild.bat. But it fails to compile on the line with string comparison as proposed here

SETLOCAL EnableExtensions EnableDelayedExpansion
PUSHD .

SET PARAM_COUNT = 0
FOR %%P IN (%*) DO (
    SET /A PARAM_COUNT += 1
    SET PARAMS[PARAM_COUNT] = %%P

    IF PARAM_COUNT GTR 1 IF PARAMS[PARAM_COUNT-1]=="--buildroot" (
        REM Update buildroot path with cygwin path
        FOR /F "tokens=*" %%i in ('cygpath %%P') do SET PARAMS[PARAM_COUNT]=%%i
    )

    REM string comparison for TOPDIR
    SET str1 = %%P
    IF NOT x%str1:_topdir=%==x%str1% (
        REM Update topdir path
        SET TOPDIR=%%P
        SET TOPDIR=%TOPDIR:~9,-1%
        FOR /F "tokens=*" %%i in ('cygpath "%TOPDIR%"') do SET NEW_TOPDIR=%%i
        SET PARAMS[PARAM_COUNT] = "_topdir %NEW_TOPDIR"
    )

    REM string comparison for .spec
    IF NOT x%str1:.spec=%==x%str1% (
        REM Replace path in spec-file
        SET OLD_PATH=%TOPDIR:\=\\%
        SET NEW_PATH=%NEW_TOPDIR:/=\/%

        sed -s -i -e s/%OLD_PATH%\\/%NEW_PATH%\//g %%P
    )
)

REM construct new rpmbuild command in cygwin

SET RPMBUILD_COMMAND = bash -c "rpmbuild
FOR /L %%i IN (1,1,PARAM_COUNT) DO SET RPMBUILD_COMMAND=!RPMBUILD_COMMAND! %%i
SET RPMBUILD_COMMAND=!RPMBUILD_COMMAND!"

REM Execute rpmbuild
%RPMBUILD_COMMAND

POPD
ENDLOCAL

How to fix?

Community
  • 1
  • 1
Alec
  • 1,486
  • 2
  • 14
  • 30

1 Answers1

2
  1. here you SET "str1 "=" %%P", two unwanted spaces
  2. here IF NOT x%str1:_topdir=%==x%str1% you are inside a for code block where you need delayed expansion !variables!, this error occures more often in your script.
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Do unwanted spaces create any issue? IF NOT x!str1:_topdir=!==x!str1! also does a failure. – Alec Aug 28 '13 at 19:29
  • the correct syntax is: `IF NOT "!str1:_topdir=!"=="!str1!"` and `SET "str1=%%P"` – Endoro Aug 28 '13 at 19:55
  • btw. you can have spaces in variable names in batch. try it: `set "var =17"` next `echo %var%` and last `echo %var %` – Endoro Aug 28 '13 at 20:05
  • 1
    Thanks Endoro. Sorry for late accept. Just finished debugging my script. – Alec Aug 29 '13 at 07:44