fixup! riscv: Implement large addend for global address
[tinycc.git] / win32 / build-tcc.bat
blobbcf36665993c788c895a74332e743b0f485931e6
1 @rem ------------------------------------------------------
2 @rem batch file to build tcc using mingw, msvc or tcc itself
3 @rem ------------------------------------------------------
5 @echo off
6 setlocal
7 if (%1)==(-clean) goto :cleanup
8 set CC=gcc
9 set /p VERSION= < ..\VERSION
10 set TCCDIR=
11 set BINDIR=
12 set DOC=no
13 set XCC=no
14 goto :a0
15 :a2
16 shift
17 :a3
18 shift
19 :a0
20 if not (%1)==(-c) goto :a1
21 set CC=%~2
22 if (%2)==(cl) set CC=@call :cl
23 goto :a2
24 :a1
25 if (%1)==(-t) set T=%2&& goto :a2
26 if (%1)==(-v) set VERSION=%~2&& goto :a2
27 if (%1)==(-i) set TCCDIR=%2&& goto :a2
28 if (%1)==(-b) set BINDIR=%2&& goto :a2
29 if (%1)==(-d) set DOC=yes&& goto :a3
30 if (%1)==(-x) set XCC=yes&& goto :a3
31 if (%1)==() goto :p1
32 :usage
33 echo usage: build-tcc.bat [ options ... ]
34 echo options:
35 echo   -c prog              use prog (gcc/tcc/cl) to compile tcc
36 echo   -c "prog options"    use prog with options to compile tcc
37 echo   -t 32/64             force 32/64 bit default target
38 echo   -v "version"         set tcc version
39 echo   -i tccdir            install tcc into tccdir
40 echo   -b bindir            but install tcc.exe and libtcc.dll into bindir
41 echo   -d                   create tcc-doc.html too (needs makeinfo)
42 echo   -x                   build the cross compiler too
43 echo   -clean               delete all previously produced files and directories
44 exit /B 1
46 @rem ------------------------------------------------------
47 @rem sub-routines
49 :cleanup
50 set LOG=echo
51 %LOG% removing files:
52 for %%f in (*tcc.exe libtcc.dll lib\*.a) do call :del_file %%f
53 for %%f in (..\config.h ..\config.texi) do call :del_file %%f
54 for %%f in (include\*.h) do @if exist ..\%%f call :del_file %%f
55 for %%f in (include\tcclib.h examples\libtcc_test.c) do call :del_file %%f
56 for %%f in (lib\*.o *.o *.obj *.def *.pdb *.lib *.exp *.ilk) do call :del_file %%f
57 %LOG% removing directories:
58 for %%f in (doc libtcc) do call :del_dir %%f
59 %LOG% done.
60 exit /B 0
61 :del_file
62 if exist %1 del %1 && %LOG%   %1
63 exit /B 0
64 :del_dir
65 if exist %1 rmdir /Q/S %1 && %LOG%   %1
66 exit /B 0
68 :cl
69 @echo off
70 set CMD=cl
71 :c0
72 set ARG=%1
73 set ARG=%ARG:.dll=.lib%
74 if (%1)==(-shared) set ARG=-LD
75 if (%1)==(-o) shift && set ARG=-Fe%2
76 set CMD=%CMD% %ARG%
77 shift
78 if not (%1)==() goto :c0
79 echo on
80 %CMD% -O2 -W2 -Zi -MT -GS- -nologo %DEF_GITHASH% -link -opt:ref,icf
81 @exit /B %ERRORLEVEL%
83 @rem ------------------------------------------------------
84 @rem main program
86 :p1
87 if not %T%_==_ goto :p2
88 set T=32
89 if %PROCESSOR_ARCHITECTURE%_==AMD64_ set T=64
90 if %PROCESSOR_ARCHITEW6432%_==AMD64_ set T=64
91 :p2
92 if "%CC:~-3%"=="gcc" set CC=%CC% -O2 -s -static
93 if (%BINDIR%)==() set BINDIR=%TCCDIR%
95 set D32=-DTCC_TARGET_PE -DTCC_TARGET_I386
96 set D64=-DTCC_TARGET_PE -DTCC_TARGET_X86_64
97 set P32=i386-win32
98 set P64=x86_64-win32
100 if %T%==64 goto :t64
101 set D=%D32%
102 set P=%P32%
103 set DX=%D64%
104 set PX=%P64%
105 set TX=64
106 goto :p3
108 :t64
109 set D=%D64%
110 set P=%P64%
111 set DX=%D32%
112 set PX=%P32%
113 set TX=32
114 goto :p3
117 git.exe --version 2>nul
118 if not %ERRORLEVEL%==0 goto :git_done
119 for /f %%b in ('git.exe rev-parse --abbrev-ref HEAD') do set GITHASH=%%b
120 for /f %%b in ('git.exe log -1 "--pretty=format:%%cs_%GITHASH%@%%h"') do set GITHASH=%%b
121 git.exe diff --quiet
122 if %ERRORLEVEL%==1 set GITHASH=%GITHASH%*
123 :git_done
124 @echo on
126 :config.h
127 echo>..\config.h #define TCC_VERSION "%VERSION%"
128 if not (%GITHASH%)==() echo>> ..\config.h #define TCC_GITHASH "%GITHASH%"
129 @if not (%BINDIR%)==(%TCCDIR%) echo>> ..\config.h #define CONFIG_TCCDIR "%TCCDIR:\=/%"
130 if %TX%==64 echo>> ..\config.h #ifdef TCC_TARGET_X86_64
131 if %TX%==32 echo>> ..\config.h #ifdef TCC_TARGET_I386
132 echo>> ..\config.h #define CONFIG_TCC_CROSSPREFIX "%PX%-"
133 echo>> ..\config.h #endif
135 for %%f in (*tcc.exe *tcc.dll) do @del %%f
137 @if _%TCC_C%_==__ goto compiler_2parts
138 @rem if TCC_C was defined then build only tcc.exe
139 %CC% -o tcc.exe %TCC_C% %D%
140 @if errorlevel 1 goto :the_end
141 @goto :compiler_done
143 :compiler_2parts
144 @if _%LIBTCC_C%_==__ set LIBTCC_C=..\libtcc.c
145 %CC% -o libtcc.dll -shared %LIBTCC_C% %D% -DLIBTCC_AS_DLL
146 @if errorlevel 1 goto :the_end
147 %CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0"
148 @if errorlevel 1 goto :the_end
149 if not _%XCC%_==_yes_ goto :compiler_done
150 %CC% -o %PX%-tcc.exe ..\tcc.c %DX%
151 @if errorlevel 1 goto :the_end
152 :compiler_done
153 @if (%EXES_ONLY%)==(yes) goto :files_done
155 if not exist libtcc mkdir libtcc
156 if not exist doc mkdir doc
157 copy>nul ..\include\*.h include
158 copy>nul ..\tcclib.h include
159 copy>nul ..\libtcc.h libtcc
160 copy>nul ..\tests\libtcc_test.c examples
161 copy>nul tcc-win32.txt doc
163 if exist libtcc.dll .\tcc -impdef libtcc.dll -o libtcc\libtcc.def
164 @if errorlevel 1 goto :the_end
166 :lib
167 call :make_lib %T% || goto :the_end
168 @if exist %PX%-tcc.exe call :make_lib %TX% %PX%- || goto :the_end
170 :tcc-doc.html
171 @if not (%DOC%)==(yes) goto :doc-done
172 echo>..\config.texi @set VERSION %VERSION%
173 cmd /c makeinfo --html --no-split ../tcc-doc.texi -o doc/tcc-doc.html
174 :doc-done
176 :files_done
177 for %%f in (*.o *.def) do @del %%f
179 :copy-install
180 @if (%TCCDIR%)==() goto :the_end
181 if not exist %BINDIR% mkdir %BINDIR%
182 for %%f in (*tcc.exe *tcc.dll) do @copy>nul %%f %BINDIR%\%%f
183 if not exist %TCCDIR% mkdir %TCCDIR%
184 @if not exist %TCCDIR%\lib mkdir %TCCDIR%\lib
185 for %%f in (lib\*.a lib\*.o lib\*.def) do @copy>nul %%f %TCCDIR%\%%f
186 for %%f in (include examples libtcc doc) do @xcopy>nul /s/i/q/y %%f %TCCDIR%\%%f
188 :the_end
189 exit /B %ERRORLEVEL%
191 :make_lib
192 .\tcc -B. -m%1 -c ../lib/libtcc1.c
193 .\tcc -B. -m%1 -c lib/crt1.c
194 .\tcc -B. -m%1 -c lib/crt1w.c
195 .\tcc -B. -m%1 -c lib/wincrt1.c
196 .\tcc -B. -m%1 -c lib/wincrt1w.c
197 .\tcc -B. -m%1 -c lib/dllcrt1.c
198 .\tcc -B. -m%1 -c lib/dllmain.c
199 .\tcc -B. -m%1 -c lib/chkstk.S
200 .\tcc -B. -m%1 -c ../lib/alloca.S
201 .\tcc -B. -m%1 -c ../lib/alloca-bt.S
202 .\tcc -B. -m%1 -c ../lib/stdatomic.c
203 .\tcc -B. -m%1 -ar lib/%2libtcc1.a libtcc1.o crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o chkstk.o alloca.o alloca-bt.o stdatomic.o
204 .\tcc -B. -m%1 -c ../lib/bcheck.c -o lib/%2bcheck.o -bt
205 .\tcc -B. -m%1 -c ../lib/bt-exe.c -o lib/%2bt-exe.o
206 .\tcc -B. -m%1 -c ../lib/bt-log.c -o lib/%2bt-log.o
207 .\tcc -B. -m%1 -c ../lib/bt-dll.c -o lib/%2bt-dll.o
208 .\tcc -B. -m%1 -c ../lib/runmain.c -o lib/%2runmain.o
209 exit /B %ERRORLEVEL%