tcc.h: Extend search path for include, lib and crt.
[tinycc.git] / win32 / build-tcc.bat
blob84c3888d6ada87ae9f2ef6528bb68952d773ba85
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 git.exe --version 2>nul
11 if not %ERRORLEVEL%==0 goto :git_done
12 for /f %%b in ('git.exe rev-parse --abbrev-ref HEAD') do set GITHASH=%%b
13 for /f %%h in ('git.exe rev-parse --short HEAD') do set GITHASH=%GITHASH%:%%h
14 git.exe diff --quiet
15 if %ERRORLEVEL%==1 set GITHASH=%GITHASH%-mod
16 set DEF_GITHASH=-DTCC_GITHASH="""%GITHASH%"""
17 :git_done
18 set INST=
19 set DOC=no
20 set EXES_ONLY=no
21 goto :a0
22 :a2
23 shift
24 :a3
25 shift
26 :a0
27 if not (%1)==(-c) goto :a1
28 set CC=%~2
29 if (%2)==(cl) set CC=@call :cl
30 goto :a2
31 :a1
32 if (%1)==(-t) set T=%2&& goto :a2
33 if (%1)==(-v) set VERSION=%~2&& goto :a2
34 if (%1)==(-i) set INST=%2&& goto :a2
35 if (%1)==(-d) set DOC=yes&& goto :a3
36 if (%1)==(-x) set EXES_ONLY=yes&& goto :a3
37 if (%1)==() goto :p1
38 :usage
39 echo usage: build-tcc.bat [ options ... ]
40 echo options:
41 echo   -c prog              use prog (gcc/tcc/cl) to compile tcc
42 echo   -c "prog options"    use prog with options to compile tcc
43 echo   -t 32/64             force 32/64 bit default target
44 echo   -v "version"         set tcc version
45 echo   -i tccdir            install tcc into tccdir
46 echo   -d                   create tcc-doc.html too (needs makeinfo)
47 echo   -x                   just create the executables
48 echo   -clean               delete all previously produced files and directories
49 exit /B 1
51 @rem ------------------------------------------------------
52 @rem sub-routines
54 :cleanup
55 set LOG=echo
56 %LOG% removing files:
57 for %%f in (*tcc.exe libtcc.dll lib\*.a) do call :del_file %%f
58 for %%f in (..\config.h ..\config.texi) do call :del_file %%f
59 for %%f in (include\*.h) do @if exist ..\%%f call :del_file %%f
60 for %%f in (include\tcclib.h examples\libtcc_test.c) do call :del_file %%f
61 for %%f in (lib\*.o *.o *.obj *.def *.pdb *.lib *.exp *.ilk) do call :del_file %%f
62 %LOG% removing directories:
63 for %%f in (doc libtcc) do call :del_dir %%f
64 %LOG% done.
65 exit /B 0
66 :del_file
67 if exist %1 del %1 && %LOG%   %1
68 exit /B 0
69 :del_dir
70 if exist %1 rmdir /Q/S %1 && %LOG%   %1
71 exit /B 0
73 :cl
74 @echo off
75 set CMD=cl
76 :c0
77 set ARG=%1
78 set ARG=%ARG:.dll=.lib%
79 if (%1)==(-shared) set ARG=-LD
80 if (%1)==(-o) shift && set ARG=-Fe%2
81 set CMD=%CMD% %ARG%
82 shift
83 if not (%1)==() goto :c0
84 echo on
85 %CMD% -O2 -W2 -Zi -MT -GS- -nologo %DEF_GITHASH% -link -opt:ref,icf
86 @exit /B %ERRORLEVEL%
88 @rem ------------------------------------------------------
89 @rem main program
91 :p1
92 if not %T%_==_ goto :p2
93 set T=32
94 if %PROCESSOR_ARCHITECTURE%_==AMD64_ set T=64
95 if %PROCESSOR_ARCHITEW6432%_==AMD64_ set T=64
96 :p2
97 if "%CC:~-3%"=="gcc" set CC=%CC% -O2 -s -static %DEF_GITHASH%
98 set D32=-DTCC_TARGET_PE -DTCC_TARGET_I386
99 set D64=-DTCC_TARGET_PE -DTCC_TARGET_X86_64
100 set P32=i386-win32
101 set P64=x86_64-win32
102 if %T%==64 goto :t64
103 set D=%D32%
104 set DX=%D64%
105 set PX=%P64%
106 set TX=64
107 goto :p3
108 :t64
109 set D=%D64%
110 set DX=%D32%
111 set PX=%P32%
112 set TX=32
113 goto :p3
116 @echo on
118 :config.h
119 echo>..\config.h #define TCC_VERSION "%VERSION%"
120 echo>> ..\config.h #ifdef TCC_TARGET_X86_64
121 echo>> ..\config.h #define TCC_LIBTCC1 "libtcc1-64.a"
122 echo>> ..\config.h #else
123 echo>> ..\config.h #define TCC_LIBTCC1 "libtcc1-32.a"
124 echo>> ..\config.h #endif
126 for %%f in (*tcc.exe *tcc.dll) do @del %%f
128 @if _%TCC_C%_==__ goto compiler_2parts
129 @rem if TCC_C was defined then build only tcc.exe
130 %CC% -o tcc.exe %TCC_C% %D%
131 @goto :compiler_done
133 :compiler_2parts
134 @if _%LIBTCC_C%_==__ set LIBTCC_C=..\libtcc.c
135 %CC% -o libtcc.dll -shared %LIBTCC_C% %D% -DLIBTCC_AS_DLL
136 @if errorlevel 1 goto :the_end
137 %CC% -o tcc.exe ..\tcc.c libtcc.dll %D% -DONE_SOURCE"=0"
138 %CC% -o %PX%-tcc.exe ..\tcc.c %DX%
139 :compiler_done
140 @if (%EXES_ONLY%)==(yes) goto :files_done
142 if not exist libtcc mkdir libtcc
143 if not exist doc mkdir doc
144 copy>nul ..\include\*.h include
145 copy>nul ..\tcclib.h include
146 copy>nul ..\libtcc.h libtcc
147 copy>nul ..\tests\libtcc_test.c examples
148 copy>nul tcc-win32.txt doc
150 if exist libtcc.dll .\tcc -impdef libtcc.dll -o libtcc\libtcc.def
151 @if errorlevel 1 goto :the_end
153 :libtcc1.a
154 call :makelib %T%
155 @if errorlevel 1 goto :the_end
156 @if exist %PX%-tcc.exe call :makelib %TX%
157 @if errorlevel 1 goto :the_end
158 .\tcc -m%T% -c ../lib/bcheck.c -o lib/bcheck.o -g
159 .\tcc -m%T% -c ../lib/bt-exe.c -o lib/bt-exe.o
160 .\tcc -m%T% -c ../lib/bt-log.c -o lib/bt-log.o
161 .\tcc -m%T% -c ../lib/bt-dll.c -o lib/bt-dll.o
163 :tcc-doc.html
164 @if not (%DOC%)==(yes) goto :doc-done
165 echo>..\config.texi @set VERSION %VERSION%
166 cmd /c makeinfo --html --no-split ../tcc-doc.texi -o doc/tcc-doc.html
167 :doc-done
169 :files_done
170 for %%f in (*.o *.def) do @del %%f
172 :copy-install
173 @if (%INST%)==() goto :the_end
174 if not exist %INST% mkdir %INST%
175 for %%f in (*tcc.exe *tcc.dll) do @copy>nul %%f %INST%\%%f
176 @if not exist %INST%\lib mkdir %INST%\lib
177 for %%f in (lib\*.a lib\*.o lib\*.def) do @copy>nul %%f %INST%\%%f
178 for %%f in (include examples libtcc doc) do @xcopy>nul /s/i/q/y %%f %INST%\%%f
180 :the_end
181 exit /B %ERRORLEVEL%
183 :makelib
184 .\tcc -m%1 -c ../lib/libtcc1.c
185 .\tcc -m%1 -c lib/crt1.c
186 .\tcc -m%1 -c lib/crt1w.c
187 .\tcc -m%1 -c lib/wincrt1.c
188 .\tcc -m%1 -c lib/wincrt1w.c
189 .\tcc -m%1 -c lib/dllcrt1.c
190 .\tcc -m%1 -c lib/dllmain.c
191 .\tcc -m%1 -c lib/chkstk.S
192 .\tcc -m%1 -c ../lib/alloca.S
193 .\tcc -m%1 -c ../lib/alloca-bt.S
194 .\tcc -m%1 -c ../lib/stdatomic.c
195 .\tcc -m%1 -ar lib/libtcc1-%1.a libtcc1.o crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o chkstk.o alloca.o alloca-bt.o stdatomic.o
196 exit /B %ERRORLEVEL%