win32: build-tcc.bat: figure out correct bitness of cl.exe
[tinycc.git] / win32 / build-tcc.bat
blob09117b4a36d5d4198d62a6ed425c892a844ee33c
1 @rem ------------------------------------------------------
2 @rem batch file to build tcc using mingw, msvc or tcc itself
3 @rem ------------------------------------------------------
5 @echo off
6 setlocal
8 set VSCOMNTOOLS=%VS150COMNTOOLS%
9 if "%VSCOMNTOOLS%"=="" set VSCOMNTOOLS=%VS140COMNTOOLS%
10 if "%VSCOMNTOOLS%"=="" set VSCOMNTOOLS=%VS130COMNTOOLS%
11 if "%VSCOMNTOOLS%"=="" set VSCOMNTOOLS=%VS120COMNTOOLS%
12 set CC=gcc -Os -s
13 set /p VERSION= < ..\VERSION
14 set INST=
15 set DOC=no
16 goto :a0
18 :usage
19 echo usage: build-tcc.bat [ options ... ]
20 echo options:
21 echo   -c prog              use prog (gcc/tcc/cl) to compile tcc
22 echo   -c "prog options"    use prog with options to compile tcc
23 echo   -t 32/64             force 32/64 bit default target
24 echo   -v "version"         set tcc version
25 echo   -i dir               install tcc into dir
26 echo   -d                   create tcc-doc.html too (needs makeinfo)
27 exit /B 1
29 :cl
30 @echo off
31 set CMD=cl
32 :c0
33 set ARG=%1
34 set ARG=%ARG:.dll=.lib%
35 if (%1)==(-shared) set ARG=-LD
36 if (%1)==(-o) shift && set ARG=-Fe%2
37 set CMD=%CMD% %ARG%
38 shift
39 if not (%1)==() goto :c0
40 echo on
41 %CMD% -O1 -W2 -Zi -MT -GS- -nologo -link -opt:ref,icf
42 @exit /B %ERRORLEVEL%
44 :a2
45 shift
46 :a1
47 shift
48 :a0
49 if not (%1)==(-c) goto :a3
50 set CC=%~2
51 if (%2)==(cl) set CC=@call :cl
52 goto :a2
53 :a3
54 if not (%1)==(-t) goto :a4
55 set T=%2
56 goto :a2
57 :a4
58 if not (%1)==(-v) goto :a5
59 set VERSION=%~2
60 goto :a2
61 :a5
62 if not (%1)==(-i) goto :a6
63 set INST=%2
64 goto :a2
65 :a6
66 if not (%1)==(-d) goto :a7
67 set DOC=yes
68 goto :a1
69 :a7
70 if not (%1)==() goto :usage
72 if not "%CC%"=="@call :cl" goto :p1
73 if %T%_==32_ set CLVARS="%VSCOMNTOOLS%..\..\VC\bin\vcvars32.bat"
74 if %T%_==64_ set CLVARS="%VSCOMNTOOLS%..\..\VC\bin\amd64\vcvars64.bat"
75 if %T%_==_ set T=32& if %Platform%_==X64_ set T=64
76 :p1
77 if not %T%_==_ goto :p2
78 set T=32
79 if %PROCESSOR_ARCHITECTURE%_==AMD64_ set T=64
80 if %PROCESSOR_ARCHITEW6432%_==AMD64_ set T=64
81 :p2
83 set D32=-DTCC_TARGET_PE -DTCC_TARGET_I386
84 set D64=-DTCC_TARGET_PE -DTCC_TARGET_X86_64
85 if %T%==64 goto :t64
86 set D=%D32%
87 set DX=%D64%
88 set TX=64
89 set PX=x86_64-win32
90 goto :t96
91 :t64
92 set D=%D64%
93 set DX=%D32%
94 set TX=32
95 set PX=i386-win32
96 :t96
98 @echo on
100 @if %CLVARS%_==_ goto :config.h
101 call %CLVARS%
103 :config.h
104 echo>..\config.h #define TCC_VERSION "%VERSION%"
105 echo>> ..\config.h #ifdef TCC_TARGET_X86_64
106 echo>> ..\config.h #define CONFIG_TCC_LIBPATHS "{B}/lib/64;{B}/lib"
107 echo>> ..\config.h #else
108 echo>> ..\config.h #define CONFIG_TCC_LIBPATHS "{B}/lib/32;{B}/lib"
109 echo>> ..\config.h #endif
111 @del /q *tcc.exe tiny_*.exe *tcc.dll
113 :compiler
114 %CC% -o libtcc.dll -shared ..\libtcc.c %D% -DONE_SOURCE -DLIBTCC_AS_DLL
115 @if errorlevel 1 goto :the_end
116 %CC% -o tcc.exe ..\tcc.c libtcc.dll %D%
117 %CC% -o %PX%-tcc.exe ..\tcc.c %DX% -DONE_SOURCE
119 :tools
120 %CC% -o tiny_impdef.exe tools\tiny_impdef.c %D%
121 %CC% -o tiny_libmaker.exe tools\tiny_libmaker.c %D%
123 @if (%TCC_FILES%)==(no) goto :files-done
125 if not exist libtcc mkdir libtcc
126 if not exist doc mkdir doc
127 if not exist lib\32 mkdir lib\32
128 if not exist lib\64 mkdir lib\64
129 copy>nul ..\include\*.h include
130 copy>nul ..\tcclib.h include
131 copy>nul ..\libtcc.h libtcc
132 tiny_impdef libtcc.dll -o libtcc\libtcc.def
133 copy>nul ..\tests\libtcc_test.c examples
134 copy>nul tcc-win32.txt doc
136 copy>nul tiny_libmaker.exe tiny_libmaker-m%T%.exe
137 %CC% -o tiny_libmaker-m%TX%.exe tools\tiny_libmaker.c %DX%
139 :libtcc1.a
140 @set O1=libtcc1.o crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o bcheck.o
141 .\tcc -m32 %D32% -c ../lib/libtcc1.c
142 .\tcc -m32 %D32% -c lib/crt1.c
143 .\tcc -m32 %D32% -c lib/wincrt1.c
144 .\tcc -m32 %D32% -c lib/dllcrt1.c
145 .\tcc -m32 %D32% -c lib/dllmain.c
146 .\tcc -m32 %D32% -c lib/chkstk.S
147 .\tcc -m32 %D32% -w -c ../lib/bcheck.c
148 .\tcc -m32 %D32% -c ../lib/alloca86.S
149 .\tcc -m32 %D32% -c ../lib/alloca86-bt.S
150 tiny_libmaker-m32 lib/32/libtcc1.a %O1% alloca86.o alloca86-bt.o
151 @if errorlevel 1 goto :the_end
152 .\tcc -m64 %D64% -c ../lib/libtcc1.c
153 .\tcc -m64 %D64% -c lib/crt1.c
154 .\tcc -m64 %D64% -c lib/wincrt1.c
155 .\tcc -m64 %D64% -c lib/dllcrt1.c
156 .\tcc -m64 %D64% -c lib/dllmain.c
157 .\tcc -m64 %D64% -c lib/chkstk.S
158 .\tcc -m64 %D64% -w -c ../lib/bcheck.c
159 .\tcc -m64 %D64% -c ../lib/alloca86_64.S
160 .\tcc -m64 %D64% -c ../lib/alloca86_64-bt.S
161 tiny_libmaker-m64 lib/64/libtcc1.a %O1% alloca86_64.o alloca86_64-bt.o
162 @if errorlevel 1 goto :the_end
164 :tcc-doc.html
165 @if not (%DOC%)==(yes) goto :doc-done
166 echo>..\config.texi @set VERSION %VERSION%
167 cmd /c makeinfo --html --no-split ../tcc-doc.texi -o doc/tcc-doc.html
168 :doc-done
170 :files-done
171 @del /q *.o *.def *-m??.exe
173 :copy-install
174 @if (%INST%)==() goto :the_end
175 if not exist %INST% mkdir %INST%
176 @for %%f in (*tcc.exe tiny_*.exe *tcc.dll) do copy>nul %%f %INST%
177 @for %%f in (include lib examples libtcc doc) do xcopy>nul /s/i/q/y %%f %INST%\%%f
178 del %INST%\lib\*.c %INST%\lib\*.S
180 :the_end
181 exit /B %ERRORLEVEL%