Posted By

mwb1100 on 02/09/10


Tagged

run various compilers


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Tyster
thastyle


RunCL


 / Published in: DOS Batch
 

Script to run various compilers - tailored to make it easy to launch a compiler from an editor.

Now tries to choose the C++ standard according to GCC version that's found.

What a mess this script has turned into. I hate cmd scripting.

  1. @echo off
  2. goto end_comments
  3. rem --------------------------------------------------------
  4.  
  5. Find the most recent version of this: https://gist.github.com/mburr/3308168
  6.  
  7. This script helps integrate compilers into UltraEdit or other editors
  8.  
  9. The first parameter is an ID for the compiler to use (vc60, vc71, vc80, etc.)
  10.  
  11. The second parameter is the file to compile.
  12.  
  13. Some notes:
  14.  
  15. 1) An example command line (for VC 2005)that would go into UltraEdit is:
  16.  
  17. c:\util\runcl.cmd vc80 "%f"
  18.  
  19. 1) the .pdb file is deleted if it exists. VC complains when there's a .pdb file
  20. already there but from a different compiler. I don't know why it doesn't just go ahead and
  21. overwrite it, but it doesn't.
  22.  
  23. rem --------------------------------------------------------
  24. :end_comments
  25.  
  26. if /i {%1}=={vc} goto set_params
  27. if /i {%1}=={msvc} goto set_params
  28. if /i {%1}=={cl} goto set_params
  29.  
  30. if /i {%1}=={vc60} goto set_params
  31. if /i {%1}=={vc6} goto set_params
  32. if /i {%1}=={vs98} goto set_params
  33. if /i {%1}=={vc98} goto set_params
  34.  
  35. if /i {%1}=={vc71} goto set_params
  36. if /i {%1}=={vs2003} goto set_params
  37. if /i {%1}=={vc2003} goto set_params
  38.  
  39. if /i {%1}=={vc80} goto set_params
  40. if /i {%1}=={vc8} goto set_params
  41. if /i {%1}=={vs2005} goto set_params
  42. if /i {%1}=={vc2005} goto set_params
  43.  
  44. if /i {%1}=={vc90} goto set_params
  45. if /i {%1}=={vc9} goto set_params
  46. if /i {%1}=={vs2008} goto set_params
  47. if /i {%1}=={vc2008} goto set_params
  48.  
  49. if /i {%1}=={vc10} goto set_params
  50. if /i {%1}=={vs2010} goto set_params
  51. if /i {%1}=={vc2010} goto set_params
  52.  
  53. if /i {%1}=={vc11} goto set_params
  54. if /i {%1}=={vs2012} goto set_params
  55. if /i {%1}=={vc2012} goto set_params
  56.  
  57. if /i {%1}=={vc12} goto set_params
  58. if /i {%1}=={vs2013} goto set_params
  59. if /i {%1}=={vc2013} goto set_params
  60.  
  61. if /i {%1}=={gcc} goto set_params
  62. if /i {%1}=={gcc32} goto set_params
  63. if /i {%1}=={gcc64} goto set_params
  64. if /i {%1}=={mingw} goto set_params
  65. if /i {%1}=={mingw32} goto set_params
  66. if /i {%1}=={mingw64} goto set_params
  67.  
  68. if /i {%1}=={como} goto set_params
  69. if /i {%1}=={dm} goto set_params
  70. if /i {%1}=={test} goto set_params
  71.  
  72.  
  73. echo Unsupported Compiler: %1
  74. echo Usage:
  75. echo %0 ^<vc ^| vc60 ^| vc71 ^| vc80 ^| vc90 ^| vc10 ^| vc11 ^| vc12 ^| como ^| dm ^| gcc ^| test^> ^<filename^>
  76. goto :eof
  77.  
  78. :set_params
  79. (set ECHO_COMMANDLINE=1)
  80. if {%CC_OPT%} == {} (set CC_OPT=/Wall)
  81. if {%CC_OPT_VC6%} == {} (set CC_OPT_VC6=) & rem VC6 doesn't support "/Wall"
  82. if {%WARNING_LEVEL%} == {} (set WARNING_LEVEL=3)
  83. rem if {%INTERSLICE_INCLUDE%} == {} (set INTERSLICE_INCLUDE=C:\DevTrees\includes;C:\DevTrees\Boost\boost_1_34_1)
  84. if {%INTERSLICE_INCLUDE%} == {} (set INTERSLICE_INCLUDE=C:\DevTrees\includes;C:\boost\boost_1_49_0)
  85. if {%WIN32_WINNT%} == {} (set WIN32_WINNT=0x0500)
  86. (set SOURCE_BASE_FILENAME=%~n2) & rem - base filename (without extension)
  87. (set SOURCE_FILE=%~f2) & rem - full path and filename
  88. (set SOURCE_FILEEXT=%~x2) & rem - just the extension
  89. (set PDB_FILE=%~dpn2.pdb) & rem - drive+path+basename followed by .pdb
  90. (set LIBRARIES=kernel32.lib user32.lib gdi32.lib advapi32.lib shlwapi.lib oleaut32.lib ws2_32.lib)
  91. (set x64=0)
  92. (set PROG_FILES=c:\Program Files)
  93.  
  94. rem - The ProgramFiles(x86) environment variable usually contains parens
  95. rem - when the variable is expanded, the ')' paren gets parsed as the
  96. rem - paren that ends a cmd.exe 'nesting' set of parens.
  97. rem -
  98. rem - This means YOU CANNOT USE the "ProgramFiles(x86)" environment
  99. rem - variable inside a block of cmd.exe command's enclosed in parens.
  100. rem -
  101. rem - Jeez - another straw in the pile of cmd scripting goofs...
  102. rem
  103. rem **NOTE** Johannes Roessel describes another way to use
  104. rem environment variables that contain parens in `set`
  105. rem statements: http://stackoverflow.com/questions/2771285/2771431#2771431
  106. rem
  107. rem essentially, put the stuff **after** the `set` command in quotes:
  108. rem
  109. rem set "PROG_FILES=%ProgramFilesx86%"
  110. rem
  111. rem Note, my workaround of not using those env vars inside a paren-
  112. rem delimted block was also described by him.
  113.  
  114. set ProgramFilesx86=%ProgramFiles(x86)%
  115. if not {"%ProgramFiles%"} == {""} set PROG_FILES=%ProgramFiles%
  116. if not {"%ProgramFilesx86%"} == {""} set PROG_FILES=%ProgramFilesx86%
  117. if not {"%ProgramFilesx86%"} == {""} (
  118. (set x64=1)
  119. )
  120. @rem echo PROG_FILES: "%PROG_FILES%"
  121. rem @echo off
  122.  
  123. goto %1
  124.  
  125.  
  126.  
  127. :delpdb
  128. rem subroutine to delete the PDB file
  129. if exist %PDB_FILE% del %PDB_FILE%
  130. goto :eof
  131.  
  132. :get_filetype
  133. rem subroutine to set the type of file on the commandline
  134. (set FILETYPE="")
  135. if /i "%SOURCE_FILEEXT%" == ".c" (set FILETYPE="C")
  136. if /i "%SOURCE_FILEEXT%" == ".cpp" (set FILETYPE="CPP")
  137. if /i "%SOURCE_FILEEXT%" == ".cxx" (set FILETYPE="CPP")
  138. if /i "%SOURCE_FILEEXT%" == ".cc" (set FILETYPE="CPP")
  139. goto :eof
  140.  
  141.  
  142. :msvc
  143. :vc
  144. :cl
  145. rem - a generic VC target - this will try to figure out the most recent version of
  146. rem visual studio on the machine and use that...
  147. rem
  148.  
  149. if exist "%PROG_FILES%\Microsoft Visual Studio 12.0\Common7\Tools\vsvars32.bat" goto vs2013
  150. if exist "%PROG_FILES%\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat" goto vs2012
  151. if exist "%PROG_FILES%\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat" goto vs2010
  152. if exist "%PROG_FILES%\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat" goto vs2008
  153. if exist "%PROG_FILES%\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat" goto vs2005
  154. if exist "%PROG_FILES%\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat" goto vs2003
  155. if exist "%PROG_FILES%\Microsoft Visual Studio\VC98\Bin\vcvars32.bat" goto vc6
  156.  
  157. echo. Error: Could not find an appropriate MSVC compiler...
  158. goto :eof
  159.  
  160.  
  161.  
  162.  
  163. :vs98
  164. :vc98
  165. :vc60
  166. :vc6
  167. call :delpdb
  168. call :get_filetype
  169. if NOT %FILETYPE% == "C" (
  170. if NOT %FILETYPE% == "CPP" goto :not_compilable
  171. )
  172. call "%PROG_FILES%\Microsoft Visual Studio\VC98\Bin\vcvars32.bat"
  173. echo Visual C/C++ 6 (VC98) Compile...
  174. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  175. set CC_OPT=%CC_OPT_VC6% -W%WARNING_LEVEL%
  176. (set CC="%PROG_FILES%\Microsoft Visual Studio\VC98\Bin\cl" %CC_OPT% /GX /Zi -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /INCREMENTAL:NO %LIBRARIES%)
  177. call :exec %CC%
  178. goto :eof
  179.  
  180.  
  181. :vs2003
  182. :vc2003
  183. :vc71
  184. call :delpdb
  185. call :get_filetype
  186. if NOT %FILETYPE% == "C" (
  187. if NOT %FILETYPE% == "CPP" goto :not_compilable
  188. )
  189. call "%PROG_FILES%\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat"
  190. echo Visual C/C++ 2003 (VC 7.1) Compile...
  191. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  192.  
  193. rem
  194. rem /Wall generates too many warnings from system includes, that I've decided
  195. rem just to not bother with it for VS2003 (so I use the VC6 options)
  196. rem
  197. set CC_OPT=%CC_OPT_VC6% -W%WARNING_LEVEL%
  198.  
  199. (set CC="%PROG_FILES%\Microsoft Visual Studio .NET 2003\Vc7\bin\cl" %CC_OPT% /Gi- /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /incremental:no %LIBRARIES%)
  200. call :exec %CC%
  201. goto :eof
  202.  
  203. :vs2005
  204. :vc2005
  205. :vc80
  206. :vc8
  207. call :delpdb
  208. call :get_filetype
  209. if NOT %FILETYPE% == "C" (
  210. if NOT %FILETYPE% == "CPP" goto :not_compilable
  211. )
  212. call "%PROG_FILES%\Microsoft Visual Studio 8\Common7\Tools\vsvars32.bat"
  213. echo Visual C/C++ 2005 (VC 8.0) Compile...
  214. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  215. set CC_OPT=%CC_OPT% -W%WARNING_LEVEL%
  216. (set CC="%PROG_FILES%\Microsoft Visual Studio 8\VC\bin\cl.exe" %CC_OPT% /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /incremental:no %LIBRARIES%)
  217. call :exec %CC%
  218. goto :eof
  219.  
  220. :vs2008
  221. :vc2008
  222. :vc90
  223. :vc9
  224. call :delpdb
  225. call :get_filetype
  226.  
  227. if NOT %FILETYPE% == "C" (
  228. if NOT %FILETYPE% == "CPP" goto :not_compilable
  229. )
  230.  
  231. call "%PROG_FILES%\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
  232. echo Visual C/C++ 2008 (VC 9.0) Compile...
  233. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  234. set CC_OPT=%CC_OPT% -W%WARNING_LEVEL%
  235. (set CC="%PROG_FILES%\Microsoft Visual Studio 9.0\VC\bin\cl.exe" %CC_OPT% /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /incremental:no %LIBRARIES%)
  236. call :exec %CC%
  237. goto :eof
  238.  
  239.  
  240. :vs2010
  241. :vc2010
  242. :vc10
  243. call :delpdb
  244. call :get_filetype
  245. if NOT %FILETYPE% == "C" (
  246. if NOT %FILETYPE% == "CPP" goto :not_compilable
  247. )
  248. call "%PROG_FILES%\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
  249. echo Visual C/C++ 2010 (VC 10.0) Compile...
  250. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  251. set CC_OPT=%CC_OPT% -W%WARNING_LEVEL%
  252. (set CC="%PROG_FILES%\Microsoft Visual Studio 10.0\VC\bin\cl.exe" %CC_OPT% /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /incremental:no %LIBRARIES%)
  253. call :exec %CC%
  254. goto :eof
  255.  
  256. :vs2012
  257. :vc2012
  258. :vc11
  259. call :delpdb
  260. call :get_filetype
  261. if NOT %FILETYPE% == "C" (
  262. if NOT %FILETYPE% == "CPP" goto :not_compilable
  263. )
  264. call "%PROG_FILES%\Microsoft Visual Studio 11.0\Common7\Tools\vsvars32.bat"
  265. echo Visual C/C++ 2012 (VC 11.0) Compile...
  266. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  267. set CC_OPT=%CC_OPT% -W%WARNING_LEVEL%
  268. (set CC="%PROG_FILES%\Microsoft Visual Studio 11.0\VC\bin\cl.exe" %CC_OPT% /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /incremental:no %LIBRARIES%)
  269. call :exec %CC%
  270. goto :eof
  271.  
  272.  
  273. :vs2013
  274. :vc2013
  275. :vc12
  276. call :delpdb
  277. call :get_filetype
  278. if NOT %FILETYPE% == "C" (
  279. if NOT %FILETYPE% == "CPP" goto :not_compilable
  280. )
  281. call "%PROG_FILES%\Microsoft Visual Studio 12.0\Common7\Tools\vsvars32.bat"
  282. echo Visual C/C++ 2013 (VC 12.0) Compile...
  283. set INCLUDE=%INCLUDE%;%INTERSLICE_INCLUDE%
  284. set CC_OPT=%CC_OPT% -W%WARNING_LEVEL%
  285. (set CC="%PROG_FILES%\Microsoft Visual Studio 12.0\VC\bin\cl.exe" %CC_OPT% /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" /link /incremental:no %LIBRARIES%)
  286. call :exec %CC%
  287. goto :eof
  288.  
  289.  
  290. :como
  291. call :get_filetype
  292. if NOT %FILETYPE% == "C" (
  293. if NOT %FILETYPE% == "CPP" goto :not_compilable
  294. )
  295. rem - set up environment for Comeau C/C++ compiler
  296.  
  297. call "%PROG_FILES%\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
  298. set path=c:\como\xp4310beta2\bin;%path%
  299.  
  300. rem - COMO_MS_INCLUDE is a special environemnt variable that como uses internally to
  301. rem find headers for MSVC. It's not passed on the command line.
  302. set COMO_MS_INCLUDE=%PROG_FILES%\Microsoft Visual Studio 9.0\VC\include
  303.  
  304. rem - VS2008 installs the Platform SDK in the following directory instead of in the VS directory
  305. rem
  306. rem - Note that VS 2008 puts the SDK libraries in "C:\Program Files" even if the machine has a 64-bit OS.
  307.  
  308. rem - COMO_PLATFORM_SDK is a special env var that como uses internally (so it's not passed on
  309. rem any command line)
  310. set COMO_PLATFORM_SDK=c:\Program Files\Microsoft SDKs\Windows\v6.0A
  311.  
  312. rem - Comeau does not use the file extension to figure out whether to
  313. rem compile as C++ or straight C, so we need to do it
  314. rem
  315. rem - Also, there's some weirdness with the inline keyword and the
  316. rem Windows SDK (at least v6.0a, which comes with VC9)
  317. rem for C compiles, define "inline" to "__inline"
  318. rem
  319. rem That seems to help, but I'm not sure exactly what's going on...
  320. rem
  321. rem Oh, and use the "-D" option, not the "--define_macro" option -
  322. rem "--define_macro" causes a weird error about the "--c"
  323. rem command line option...
  324. rem
  325. set COMPILE_TYPE_OPT=--c++
  326. set COMO_INLINE=
  327. if %FILETYPE% == "C" (
  328. set COMPILE_TYPE_OPT=--c
  329. set COMO_INLINE=-D inline=__inline
  330. )
  331.  
  332.  
  333. set SOURCE_BASE_FILENAME=%SOURCE_BASE_FILENAME:"=%
  334.  
  335. rem - set up a compiler switch for INTERSLICE_INCLUDE if there's something there
  336. (set COMO_INCLUDE=)
  337. if NOT "%INTERSLICE_INCLUDE%" == "" (set COMO_INCLUDE=-I%INTERSLICE_INCLUDE:;= -I%)
  338.  
  339.  
  340. rem (set CC=como --plv %COMO_INLINE% -o "%SOURCE_BASE_FILENAME%.exe" %COMPILE_TYPE_OPT% "%SOURCE_FILE%")
  341. (set CC=como %COMO_INLINE% -c %COMPILE_TYPE_OPT% %COMO_INCLUDE% "%SOURCE_FILE%")
  342.  
  343. rem
  344. rem como needs to have the libraries it links to specified without spaces (even if the path is quoted)
  345. rem the following `for` command converts the long-filename-path for advapi32.lib to a path that
  346. rem only has short, 8.3 path components (no spaces). This uses the 'percent' ~s modifier available
  347. rem to the `for` command. (note that I couldn't use the actual percent sign in the comment, because the
  348. rem stinkin' cmd.exe parser actually throws some sort of syntax error even though it's in a comment -
  349. rem jeez, batch files really, really drive me crazy).
  350. rem
  351. for %%a in ("%COMO_PLATFORM_SDK%\Lib\AdvAPI32.Lib") do set advapi32lib=%%~sa
  352. (set LINK=como -o "%SOURCE_BASE_FILENAME%.exe" "%SOURCE_BASE_FILENAME%.obj" "%advapi32lib%")
  353.  
  354. @echo compile...
  355. call :exec %CC%
  356. @echo link...
  357. call :exec %LINK%
  358.  
  359. goto :eof
  360.  
  361. :dm
  362. call :get_filetype
  363. if NOT %FILETYPE% == "C" (
  364. if NOT %FILETYPE% == "CPP" goto :not_compilable
  365. )
  366. echo Digital Mars C/C++ Compile...
  367. rem - the following dumps Digital Mars compiler version info (I think scppn.exe is the actual compiler)
  368. for /F "usebackq tokens=1*" %%i in (`c:\dm\bin\scppn.exe`) do (
  369. if /i "%%i" == "Digital" echo %%i %%j
  370. )
  371.  
  372. rem - do not let any residual VC include path from the system environment get to DMC
  373. rem - the default DMC include path is set in the c:\dm\bin\sc.ini file
  374. (set INCLUDE=)
  375. rem - set up a compiler switch for INTERSLICE_INCLUDE if there's something there
  376. (set DMC_INCLUDE=)
  377. if NOT "%INTERSLICE_INCLUDE%" == "" (set DMC_INCLUDE=-I%INTERSLICE_INCLUDE%)
  378. rem - C++ compiles get an additional include directory that's set in the DMC_CPP_INCLUDE var
  379. (set DMC_CPP_INCLUDE=)
  380. if {%FILETYPE%} == {"CPP"} (set DMC_CPP_INCLUDE=-Ic:\dm\stlport\stlport)
  381. rem "C:\dm\bin\dmc" -Ae -v1 -r -g -mn %DMC_INCLUDE% %DMC_CPP_INCLUDE% -IC:/DevTrees/Boost/boost_1_34_1 -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" %LIBRARIES%
  382. (set CC="C:\dm\bin\dmc" -Ae -v1 -r -g -mn %DMC_INCLUDE% %DMC_CPP_INCLUDE% -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" %LIBRARIES%)
  383. call :exec %CC%
  384. goto :eof
  385.  
  386.  
  387. :mingw64
  388. :gcc64
  389. set GCC_TARGET=-m64
  390.  
  391. :mingw
  392. :mingw32
  393. :gcc
  394. :gcc32
  395. if "%GCC_TARGET%" == "" (set GCC_TARGET=-m32)
  396.  
  397. call :get_filetype
  398. if NOT %FILETYPE% == "C" (
  399. if NOT %FILETYPE% == "CPP" goto :not_compilable
  400. )
  401.  
  402.  
  403. rem some GCC options:
  404. rem
  405. rem -g compile in debugging information
  406. rem -Werror treat warnings as errors
  407. rem -Wextra enable extra warnings (unsigned compare with 0, comma expression with no side effects, etc)
  408. rem
  409. rem -E preprocess only (no compile/assembly/link)
  410. rem -E -dM causes GCC to dump pre-defined macros
  411. rem
  412. rem -M will cause included files to be listed (similar to /showIncludes)
  413. rem
  414. rem -S produce assembly output file filename.s (but won't link)
  415. rem -masm=intel use Intel syntax for assembly language
  416. rem
  417. rem -verbose-asm add some information to assembly output
  418. rem -instrument-functions add calls to user-supplied profiling functions
  419. rem
  420. rem -Wshadow warn if a variable for built-in function is hidden
  421. rem -Wcast-align warn if a pointer is cast to something with a stricter alignment
  422. rem
  423. rem -fmudflap add runtime buffer boundary checks
  424. rem
  425. rem -save-temps retain preprocessor and/or assembly outputs
  426. rem
  427. rem -v dump information about default options, and search paths
  428. rem -Wl,--verbose tell linker to dump info about libraries being searched
  429. rem
  430. rem -std=c89
  431. rem -std=c90
  432. rem -std=c99
  433. rem -std=c11 4.7+
  434. rem
  435. rem -std=gnu90 (default for C compiles)
  436. rem -std=gnu99
  437. rem -std=gnu11 4.7+
  438. rem
  439. rem -std=c++98
  440. rem -std=c++03 4.8+ (same function as c++98)
  441. rem -std=c++0x 4.3+ (deprecated 4.7)
  442. rem -std=c++11 4.7+
  443. rem -std=c++14 5.0+(?)
  444. rem
  445. rem -std=gnu++98 (default for C++ compiles)
  446. rem -std=gnu++03 4.8+ (same function as gnu++98)
  447. rem -std=gnu++0x 4.3+ (deprecated 4.7)
  448. rem -std=gnu++11 4.7+
  449. rem -std=gnu++14 5.0+(?)
  450. rem
  451. rem -O0 disable optimizations (default)
  452. rem -O or -O1
  453. rem -O2
  454. rem -O3
  455. rem
  456. rem -x c compile files as C
  457. rem -x c++ compile files as C++
  458. rem -x none compile files according to extension
  459. rem
  460. rem other options for `-x` include c-header, cpp-output, c++-header,
  461. rem c++-cpp-output, java, objective-c, and many more.
  462. rem
  463. rem -mwindows compile/link as Win32 GUI program
  464. rem -mconsole compile/link as Win32 console program
  465. rem -mdll generate a DLL, enabling the selection of the required runtime
  466. rem startup object and entry point.
  467. rem -mthread MinGW-specific thread support is to be used. I'm not sure how This
  468. rem relates to pthreads (if at all)
  469. rem
  470. rem --help=class[,qualifier]
  471. rem Print on the standard output a description of the command line options
  472. rem understood by the compiler that fit into a specific class. The class can be
  473. rem one of `optimizers', `warnings', `target', `params', or language:
  474. rem
  475. rem `optimizers'
  476. rem This will display all of the optimization options supported by the compiler.
  477. rem `warnings'
  478. rem This will display all of the options controlling warning messages produced by the compiler.
  479. rem `target'
  480. rem This will display target-specific options. Unlike the --target-help option however,
  481. rem target-specific options of the linker and assembler will not be displayed.
  482. rem This is because those tools do not currently support the extended --help= syntax.
  483. rem `params'
  484. rem This will display the values recognized by the --param option.
  485. rem language
  486. rem This will display the options supported for language, where language is the
  487. rem name of one of the languages supported in this version of GCC.
  488. rem `common'
  489. rem This will display the options that are common to all languages.
  490. rem
  491. rem It is possible to further refine the output of the --help= option by adding a comma
  492. rem separated list of qualifiers after the class. These can be any from the following list:
  493. rem
  494. rem `undocumented'
  495. rem Display only those options which are undocumented.
  496. rem `joined'
  497. rem Display options which take an argument that appears after an equal sign in
  498. rem the same continuous piece of text, such as: `--help=target'.
  499. rem `separate'
  500. rem Display options which take an argument that appears as a separate
  501. rem word following the original option, such as: `-o output-file'.
  502. rem
  503. rem use -Q with the above to display the actual setting instead of help for options:
  504. rem
  505. rem -Q --help=target # display options affected by target architecture setting
  506. rem -Q --help=warning # display warnings enabled/disbled
  507. rem
  508. rem These warning options are turned on by `-Wall` and/or `-Wextra`.
  509. rem
  510. rem They create too much noise for little test runs, and seem to provide little benefit,
  511. rem so I turn them off explicitly:
  512. rem
  513. rem -Wunused-parameter
  514. rem -Wunused-variable
  515. rem -Wunused-function
  516. rem
  517. rem These warnings cause common structure initializations (such as `= {0}` or not using full bracing)
  518. rem to cause warnings. So I turn them off explicitly as well:
  519. rem
  520. rem -Wmissing-field-initializers
  521. rem -Wmissing-braces
  522. rem
  523. rem set GCC_OPTS=-Wall
  524.  
  525. rem if MINGW_PATH isn't explicitly set, use the first of:
  526. rem x:\MinGW64\bin, x:\MinGW32\bin, x:\MinGW\bin that we find
  527. rem where x: is c: or d:
  528. if {%MINGW_PATH%} == {} if exist c:\MinGW-4.8.1-tdm64\bin\gcc.exe (set MINGW_PATH=c:\MinGW-4.8.1-tdm64\bin)
  529. if {%MINGW_PATH%} == {} if exist d:\MinGW-4.8.1-tdm64\bin\gcc.exe (set MINGW_PATH=d:\MinGW-4.8.1-tdm64\bin)
  530. if {%MINGW_PATH%} == {} if exist c:\MinGW64\bin\gcc.exe (set MINGW_PATH=c:\MinGW64\bin)
  531. if {%MINGW_PATH%} == {} if exist d:\MinGW64\bin\gcc.exe (set MINGW_PATH=d:\MinGW64\bin)
  532. if {%MINGW_PATH%} == {} if exist c:\MinGW32\bin\gcc.exe (set MINGW_PATH=c:\MinGW32\bin)
  533. if {%MINGW_PATH%} == {} if exist d:\MinGW32\bin\gcc.exe (set MINGW_PATH=d:\MinGW32\bin)
  534. if {%MINGW_PATH%} == {} if exist c:\MinGW\bin\gcc.exe (set MINGW_PATH=c:\MinGW\bin)
  535. if {%MINGW_PATH%} == {} if exist d:\MinGW\bin\gcc.exe (set MINGW_PATH=d:\MinGW\bin)
  536.  
  537. if {%MINGW_PATH%} == {} (
  538. echo "MinGW toolchain could not be found"
  539. goto :eof
  540. )
  541.  
  542.  
  543. if "%GCC_WARNING_CONFIG%" == "" (
  544. (set GCC_WARNING_CONFIG=-Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-missing-field-initializers -Wno-missing-braces)
  545. )
  546.  
  547. if "%GCC_OPTS%" == "" (
  548. (set GCC_OPTS=-g -Wall -Wextra %GCC_WARNING_CONFIG%)
  549. )
  550.  
  551. if "%GCC_OPTS_C%" == "" (
  552. rem - default options that apply only to C builds
  553. (set GCC_OPTS_C=-Wstrict-prototypes)
  554. )
  555.  
  556. if "%GCC_OPTS_CPP%" == "" (
  557. rem - no default C++ only options yet
  558. (set GCC_OPTS_CPP=)
  559. )
  560.  
  561. echo GCC/MinGW C/C++ Compile...
  562. rem - collect the GCC version
  563. for /f %%a in ('%MINGW_PATH%\gcc -dumpversion') do (set GCC_VERSION=%%a)
  564. @echo GCC Version %GCC_VERSION%
  565.  
  566. rem sets the "-std=" option appropriately (GCC_STD_C and GCC_STD_CPP variables)
  567. call :gcc_set_std %GCC_VERSION%
  568.  
  569. rem - do not let any residual VC include path from the system environement get to MinGW
  570. (set INCLUDE=%MINGW_PATH%\..\include)
  571. rem - set up a compiler switch for INTERSLICE_INCLUDE if there's something there
  572. (set GCC_INCLUDE=-I%INCLUDE%)
  573. if NOT "%INTERSLICE_INCLUDE%" == "" (set GCC_INCLUDE=%GCC_INCLUDE% -I%INTERSLICE_INCLUDE:;= -I%)
  574. rem - C++ compiles get an additional include directory that's set in the GCC_CPP_INCLUDE var
  575. (set GCC_CPP_INCLUDE=)
  576.  
  577.  
  578. rem C++ specific items
  579. if {%FILETYPE%} == {"CPP"} (
  580. (set GCC_OPTS=%GCC_STD_CPP% %GCC_OPTS% %GCC_OPTS_CPP%)
  581. (set GCC_CPP_INCLUDE=-I%INCLUDE%\c++\%GCC_VERSION%)
  582.  
  583. (set GCC_COMPILER="%MINGW_PATH%\g++")
  584. )
  585.  
  586. rem C specific items
  587. if {%FILETYPE%} == {"C"} (
  588. (set GCC_OPTS=%GCC_STD_C% %GCC_OPTS% %GCC_OPTS_C%)
  589.  
  590. (set GCC_COMPILER="%MINGW_PATH%\gcc")
  591. )
  592.  
  593.  
  594. rem set up the libraries for the gcc command line
  595. (set GCC_LIBRARIES=)
  596. for %%a in (%LIBRARIES%) do call :gcc_appendlib %%a
  597.  
  598. rem NOTE
  599. rem MinGW versions before 4.2.2 need to be in the path on Vista or later because
  600. rem of the _access() bug (worked around in later GCC builds with the
  601. rem -D__USE_MINGW_ACCESS hack). One of these days, I'll make the script
  602. rem smart enoough to detect this situation, but for now I'll just add the
  603. rem configured MinGW to the path (which I'd rather not do)...
  604. rem
  605. rem Versions 4.2.2 or later of MinGW (or earler versions running on XP) will find
  606. rem subprocesses just fine without being on the path because of the -B option.
  607. rem (-B is supported on GCC 2.96 or later - I've never used anything prior to 3.4.5)
  608. rem
  609. set PATH=%MINGW_PATH%;%PATH%
  610.  
  611. (set CC=%GCC_COMPILER% -B%MINGW_PATH% %GCC_TARGET% %GCC_OPTS% %GCC_CPP_INCLUDE% %GCC_INCLUDE% -D__USE_MINGW_ACCESS -D_WIN32_WINNT=%WIN32_WINNT% "%SOURCE_FILE%" %GCC_LIBRARIES% -o "%SOURCE_BASE_FILENAME%.exe")
  612. call :exec %CC%
  613. goto :eof
  614.  
  615. :gcc_appendlib
  616. (set GCC_LIBRARIES=%GCC_LIBRARIES% -l%~n1)
  617. goto :eof
  618.  
  619.  
  620. rem Function: parse_gcc_ver
  621. goto :end_parse_gcc_ver
  622. :parse_gcc_ver
  623.  
  624. rem @echo off
  625. goto :start_parse_gcc_ver
  626. rem
  627. rem Parse a GCC version number in the form 4.5.6 into a single number
  628. rem (allowing two digits for each part, just in case) such as 40506.
  629. rem
  630. rem This function expects a single argument in the form os the
  631. rem version triplet produced by "gcc -dumpversion" - for example: 4.5.6
  632. rem
  633. rem The return value of the function is provided in an environment
  634. rem variable named RET, as per convention.
  635. rem
  636. rem This will allow us to easily compare the GCC version in the script
  637. rem so that we can set sensible language defaults. For example, if
  638. rem GCC 4.7 (40700) or later is used we can default to "-std=gnu++11"
  639. rem
  640. rem As a note, the GCC docs claim that C99 isn't fully supported until
  641. rem version 4.9! I think it's probably mostly usable far earlier than
  642. rem that - in fact, I think the the default of "-std=gnu90" probably
  643. rem supports most the the useful C99 features as extensions.
  644. rem
  645.  
  646. :start_parse_gcc_ver
  647.  
  648. set RAWVER=%1
  649.  
  650. for /f "tokens=1-3 delims=." %%i in ("%RAWVER%") do (
  651. set /a MAJ=%%i + 0
  652. set /a MIN=%%j + 0
  653. set /a PAT=%%k + 0
  654. )
  655.  
  656. set /a VERNUM= MAJ * 10000
  657. set /a VERNUM+=MIN * 100
  658. set /a VERNUM+=PAT
  659.  
  660. endlocal && set "RET=%VERNUM%"
  661. goto :eof
  662.  
  663. :end_parse_gcc_ver
  664.  
  665.  
  666. rem Function to set the proper support "-std=" option
  667. rem
  668. rem expects the gcc version in the form 4.5.6
  669. rem
  670. :gcc_set_std
  671.  
  672. call :parse_gcc_ver %1
  673. set /a VERNUM=RET + 0
  674.  
  675. rem set basic defaults
  676. set GCC_STD_C=-std=gnu90
  677. set GCC_STD_CPP=-std=gnu++98
  678.  
  679. rem 4.7.0 and later support C++11
  680. if %VERNUM% GEQ 40700 (
  681. set GCC_STD_CPP=-std=gnu++11
  682. )
  683.  
  684. rem 4.9.0 and later support C99
  685. if %VERNUM% GEQ 40900 (
  686. set GCC_STD_C=-std=gnu99
  687. )
  688.  
  689. rem return the correct settings in variables GCC_STD_C and GCC_STD_CPP
  690. endlocal && (set GCC_STD_C=%GCC_STD_C%) && (set GCC_STD_CPP=%GCC_STD_CPP%)
  691. goto :eof
  692.  
  693.  
  694.  
  695. :exec
  696. if {%ECHO_ENVIRONMENT%} == {1} (
  697. )
  698. if {%ECHO_COMMANDLINE%} == {1} (
  699. echo %*
  700. )
  701. %*
  702. goto :eof
  703.  
  704. :not_compilable
  705. echo Cannot compile a "%SOURCE_FILEEXT%" type of file
  706. goto :eof
  707.  
  708. :test
  709. echo current dir is:
  710. echo %%1: %1
  711. echo %%2: %2
  712. echo %%SOURCE_FILE%%: %SOURCE_FILE%
  713. echo %%SOURCE_FILEEXT%%: %SOURCE_FILEEXT%
  714. echo %%SOURCE_BASE_FILENAME%%: %SOURCE_BASE_FILENAME%
  715. echo %%PDB_FILE%%: %PDB_FILE%
  716. echo %%~d2: %~d2
  717. echo %%~p2: %~p2
  718. echo %%~n2: %~n2
  719. echo %%~x2: %~x2
  720. echo %%~dpn2: %~dpn2
  721. echo %%3: %3
  722. goto :eof

Report this snippet  

You need to login to post a comment.