Advertisement
AlvinSeville7cf

compiler.bat

Mar 7th, 2021 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 5.93 KB | None | 0 0
  1. @echo off
  2. setlocal
  3.  
  4. call :init
  5.  
  6. set /a "i=0"
  7. :copy_options
  8.     echo %~1| findstr /r "^-- ^- ^/" 2> nul > nul
  9.     if %errorlevel% neq 0 (
  10.         set "option=%~f1"
  11.     ) else (
  12.         set "option=%~1"
  13.     )
  14.     if defined option (
  15.         if not "%option%" == "--" (
  16.             set "args[%i%]=%option%"
  17.             shift
  18.             set /a "i+=1"
  19.             goto copy_options
  20.         ) else (
  21.             shift
  22.             set /a "i+=1"
  23.         )
  24.     )
  25.  
  26. set compiler_options=-O3 --pointer-arithmetic --gnu-pascal
  27. :copy_compiler_options
  28.     set "option=%~1"
  29.     if defined option (
  30.         set compiler_options=%compiler_options% %option%
  31.         shift
  32.         goto copy_compiler_options
  33.     )
  34.  
  35. set /a "i=0"
  36. :main_loop
  37.     set /a "j=i + 1"
  38.     call set "option=%%args[%i%]%%"
  39.     call set "value=%%args[%j%]%%"
  40.  
  41.     set /a "is_help=FALSE"
  42.     if "%option%" == "-h" set /a "is_help=TRUE"
  43.     if "%option%" == "--help" set /a "is_help=TRUE"
  44.     if "%option%" == "/h" set /a "is_help=TRUE"
  45.     if "%option%" == "/help" set /a "is_help=TRUE"
  46.  
  47.     if %is_help% equ %TRUE% (
  48.         call :help
  49.         exit /b %ec_success%
  50.     )
  51.  
  52.     set /a "is_version=FALSE"
  53.     if "%option%" == "-v" set /a "is_version=TRUE"
  54.     if "%option%" == "--version" set /a "is_version=TRUE"
  55.     if "%option%" == "/v" set /a "is_version=TRUE"
  56.     if "%option%" == "/version" set /a "is_version=TRUE"
  57.  
  58.     if %is_version% equ %TRUE% (
  59.         call :version
  60.         exit /b %ec_success%
  61.     )
  62.  
  63.     set /a "is_path=FALSE"
  64.     if "%option%" == "-p" set /a "is_path=TRUE"
  65.     if "%option%" == "--path" set /a "is_path=TRUE"
  66.     if "%option%" == "/p" set /a "is_path=TRUE"
  67.     if "%option%" == "/path" set /a "is_path=TRUE"
  68.  
  69.     if %is_path% equ %TRUE% (
  70.         set "path_to_compiler=%value%"
  71.         set /a "use_path=FALSE"
  72.         set /a "i+=2"
  73.         goto main_loop
  74.     )
  75.  
  76.     set /a "is_suppress=FALSE"
  77.     if "%option%" == "!" set /a "is_suppress=TRUE"
  78.  
  79.     if %is_suppress% equ %TRUE% (
  80.         set /a "use_suppress=TRUE"
  81.         set /a "i+=1"
  82.         goto main_loop
  83.     )
  84.  
  85.     set /a "is_exec=FALSE"
  86.     if "%option%" == "-e" set /a "is_exec=TRUE"
  87.     if "%option%" == "--exec" set /a "is_exec=TRUE"
  88.     if "%option%" == "/e" set /a "is_exec=TRUE"
  89.     if "%option%" == "/exec" set /a "is_exec=TRUE"
  90.  
  91.     if %is_exec% equ %TRUE% (
  92.         set /a "use_exec=TRUE"
  93.         set /a "i+=1"
  94.         goto main_loop
  95.     )
  96.  
  97. set "path_to_file=%option%"
  98. set "path_to_out_file=%value%"
  99.  
  100. if not exist "%path_to_file%" (
  101.     echo %WRONG_FILE_PASSED_MSG%
  102.     exit /b %WRONG_FILE_PASSED_EC%
  103. )
  104.  
  105. if %use_path% equ %TRUE% (
  106.     gpc --version 2> nul > nul || (
  107.         if %use_suppress% equ %FALSE% (
  108.             setlocal enabledelayedexpansion
  109.             choice /m "Do you want to automatically add GNU pascal compiler to PATH"
  110.             set /a "key=!errorlevel!"
  111.             if !key! equ 1 (
  112.                 setx PATH "%default_path_to_compiler%;%PATH%" 2> nul > nul
  113.                 echo %SHELL_RESTART_REQUIRED_MSG%
  114.                 exit /b %SHELL_RESTART_REQUIRED_EC%
  115.             )
  116.             endlocal
  117.         )
  118.         echo %COMPILER_NOT_FOUND_MSG%
  119.         exit /b %COMPILER_NOT_FOUND_EC%
  120.     )
  121.     gpc "%path_to_file%" -o "%path_to_out_file%" %compiler_options%
  122.     set /a "error_status=%errorlevel%"
  123.     if %error_status% neq 0 exit /b %error_status%
  124. ) else (
  125.     if not exist "%path_to_compiler%" (
  126.         echo %COMPILER_NOT_FOUND_MSG%
  127.         exit /b %COMPILER_NOT_FOUND_EC%
  128.     )
  129.     cd "%path_to_compiler%"
  130.     gpc "%path_to_file%" -o "%path_to_out_file%" %compiler_options%
  131.     set /a "error_status=%errorlevel%"
  132.     if %error_status% neq 0 exit /b %error_status%
  133. )
  134.  
  135. if %use_exec% equ %TRUE% (
  136.     "%path_to_out_file%"
  137.     set /a "error_status=%errorlevel%"
  138.     pause > nul
  139.     exit /b %error_status%
  140. )
  141.  
  142. exit /b %SUCCESS_EC%
  143.  
  144. :init
  145.     set /a "SUCCESS_EC=0"
  146.     set /a "WRONG_FILE_PASSED_EC=1"
  147.     set /a "COMPILER_NOT_FOUND_EC=2"
  148.     set /a "SHELL_RESTART_REQUIRED_EC=3"
  149.  
  150.     set "WRONG_FILE_PASSED_MSG=None file passed or it is not found."
  151.     set "COMPILER_NOT_FOUND_MSG=GNU Pascal compiler is not found."
  152.     set "SHELL_RESTART_REQUIRED_MSG=You have to restart your shell to apply changes to PATH."
  153.  
  154.     set /a "TRUE=0"
  155.     set /a "FALSE=1"
  156.  
  157.     set /a "use_path=TRUE"
  158.     set /a "use_suppress=FALSE"
  159.     set /a "use_exec=FALSE"
  160.     set "default_path_to_compiler=C:\dev_gpc\bin\"
  161. exit /b %SUCCESS_EC%
  162.  
  163. :help
  164.     echo Simplifies access to GNU Pascal compiler.
  165.     echo.
  166.     echo Syntax:
  167.     echo    gnupas [options] pathToFile pathToOutFile [-- [compilerOptions]]
  168.     echo.
  169.     echo Options:
  170.     echo    - -h^|--help^|/h^|/help - writes help and exits
  171.     echo    - -v^|--version^|/v^|/version - writes version and exits
  172.     echo    - -p^|--path^|/p^|/path - specifies path to GNU Pascal compiler
  173.     echo    - -e^|--exec^|/e^|/exec - executes compiled program immediately
  174.     echo    - ! - suppress prompts to change PATH variable
  175.     echo.
  176.     echo Examples:
  177.     echo    gnupas --version
  178.     echo    gnupas test.pas test.exe
  179.     echo    gnupas --path path-to-compiler test.pas test.exe
  180.     echo.
  181.     echo Error codes:
  182.     echo    - 0 - Success
  183.     echo    - 1 - None file passed or it is not found
  184.     echo    - 2 - GNU Pascal compiler is not found
  185.     echo    - 3 - You have to restart your shell to apply changes to PATH
  186.     echo.
  187.     echo    If compilation failed then compiler error status is returned.
  188.     echo    If -e^|--exec^|/e^|/exec option passed then program execution error status is returned.
  189.     echo.
  190.     echo Notes:
  191.     echo    Optimization, pointer arithmetic and GNU Pascal extensions are enabled by default.
  192.     echo    It means that -O3, --pointer-arithmetic, --gnu-pascal compiler options are passed by default.
  193.     echo.
  194.     echo Author:
  195.     echo    Alvin Seville ^<AlvinSeville7cf@gmail.com^>
  196. exit /b %SUCCESS_EC%
  197.  
  198. :version
  199.     echo 1.0 ^(c^) 2021 year
  200. exit /b %SUCCESS_EC%
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement