1@echo off
2rem ***************************************************************************
3rem *                                  _   _ ____  _
4rem *  Project                     ___| | | |  _ \| |
5rem *                             / __| | | | |_) | |
6rem *                            | (__| |_| |  _ <| |___
7rem *                             \___|\___/|_| \_\_____|
8rem *
9rem * Copyright (C) 2014 - 2015, Steve Holme, <steve_holme@hotmail.com>.
10rem *
11rem * This software is licensed as described in the file COPYING, which
12rem * you should have received as part of this distribution. The terms
13rem * are also available at http://curl.haxx.se/docs/copyright.html.
14rem *
15rem * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16rem * copies of the Software, and permit persons to whom the Software is
17rem * furnished to do so, under the terms of the COPYING file.
18rem *
19rem * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20rem * KIND, either express or implied.
21rem *
22rem ***************************************************************************
23
24rem NOTES
25rem
26rem Do not set %ERRORLEVEL% to anything. %ERRORLEVEL% is a special variable
27rem that only contains errorlevel when %ERRORLEVEL% is not set. Same for %CD%.
28rem http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx
29rem If you need to set the errorlevel do this instead: CALL :seterr [#]
30
31:begin
32  rem Check we are running on a Windows NT derived OS
33  if not "%OS%" == "Windows_NT" goto nodos
34
35  rem Check we are not running on a network drive
36  if "%~d0."=="\\." goto nonetdrv
37
38  rem Switch to this batch file's directory
39  cd /d "%~0\.." 1>NUL 2>&1
40
41  rem Set our variables
42  setlocal ENABLEEXTENSIONS
43  set VERSION=ALL
44  set MODE=GENERATE
45
46  rem Detect programs. HAVE_<PROGNAME>
47  rem When not found the variable is set undefined. The undefined pattern
48  rem allows for statements like "if not defined HAVE_PERL (command)"
49  groff --version <NUL 1>NUL 2>&1
50  if %ERRORLEVEL% EQU 0 (set HAVE_GROFF=Y) else (set HAVE_GROFF=)
51  nroff --version <NUL 1>NUL 2>&1
52  if %ERRORLEVEL% EQU 0 (set HAVE_NROFF=Y) else (set HAVE_NROFF=)
53  perl --version <NUL 1>NUL 2>&1
54  if %ERRORLEVEL% EQU 0 (set HAVE_PERL=Y) else (set HAVE_PERL=)
55  gzip --version <NUL 1>NUL 2>&1
56  if %ERRORLEVEL% EQU 0 (set HAVE_GZIP=Y) else (set HAVE_GZIP=)
57
58  rem Display the help
59  if /i "%~1" == "-?" goto syntax
60  if /i "%~1" == "-h" goto syntax
61  if /i "%~1" == "-help" goto syntax
62
63:parseArgs
64  if "%~1" == "" goto start
65
66  if /i "%~1" == "vc6" (
67    set VERSION=VC6
68  ) else if /i "%~1" == "vc7" (
69    set VERSION=VC7
70  ) else if /i "%~1" == "vc7.1" (
71    set VERSION=VC7.1
72  ) else if /i "%~1" == "vc8" (
73    set VERSION=VC8
74  ) else if /i "%~1" == "vc9" (
75    set VERSION=VC9
76  ) else if /i "%~1" == "vc10" (
77    set VERSION=VC10
78  ) else if /i "%~1" == "vc11" (
79    set VERSION=VC11
80  ) else if /i "%~1" == "vc12" (
81    set VERSION=VC12
82  ) else if /i "%~1" == "-clean" (
83    set MODE=CLEAN
84  ) else (
85    goto unknown
86  )
87  shift & goto parseArgs
88 
89:start
90  if "%MODE%" == "GENERATE" (
91    echo.
92    echo Generating prerequisite files
93    CALL :gen_curlbuild
94    if errorlevel 1 goto error
95    CALL :gen_hugehelp
96    if errorlevel 1 goto error
97  ) else (
98    echo.
99    echo Removing prerequisite files
100    call :clean ..\include\curl\curlbuild.h
101    call :clean ..\src\tool_hugehelp.c
102  )
103  if "%VERSION%" == "VC6" goto vc6
104  if "%VERSION%" == "VC7" goto vc7
105  if "%VERSION%" == "VC7.1" goto vc71
106  if "%VERSION%" == "VC8" goto vc8
107  if "%VERSION%" == "VC9" goto vc9
108  if "%VERSION%" == "VC10" goto vc10
109  if "%VERSION%" == "VC11" goto vc11
110  if "%VERSION%" == "VC12" goto vc12
111
112:vc6
113  echo.
114
115  if "%MODE%" == "GENERATE" (
116    echo Generating VC6 project files
117    call :generate dsp Windows\VC6\src\curlsrc.tmpl Windows\VC6\src\curlsrc.dsp
118    call :generate dsp Windows\VC6\lib\libcurl.tmpl Windows\VC6\lib\libcurl.dsp
119  ) else (
120    echo Removing VC6 project files
121    call :clean Windows\VC6\src\curlsrc.dsp
122    call :clean Windows\VC6\lib\libcurl.dsp
123  )
124
125  if not "%VERSION%" == "ALL" goto success
126
127:vc7
128  echo.
129
130  if "%MODE%" == "GENERATE" (
131    echo Generating VC7 project files
132    call :generate vcproj1 Windows\VC7\src\curlsrc.tmpl Windows\VC7\src\curlsrc.vcproj
133    call :generate vcproj1 Windows\VC7\lib\libcurl.tmpl Windows\VC7\lib\libcurl.vcproj
134  ) else (
135    echo Removing VC7 project files
136    call :clean Windows\VC7\src\curlsrc.vcproj
137    call :clean Windows\VC7\lib\libcurl.vcproj
138  )
139
140  if not "%VERSION%" == "ALL" goto success
141
142:vc71
143  echo.
144
145  if "%MODE%" == "GENERATE" (
146    echo Generating VC7.1 project files
147    call :generate vcproj1 Windows\VC7.1\src\curlsrc.tmpl Windows\VC7.1\src\curlsrc.vcproj
148    call :generate vcproj1 Windows\VC7.1\lib\libcurl.tmpl Windows\VC7.1\lib\libcurl.vcproj
149  ) else (
150    echo Removing VC7.1 project files
151    call :clean Windows\VC7.1\src\curlsrc.vcproj
152    call :clean Windows\VC7.1\lib\libcurl.vcproj
153  )
154
155  if not "%VERSION%" == "ALL" goto success
156
157:vc8
158  echo.
159
160  if "%MODE%" == "GENERATE" (
161    echo Generating VC8 project files
162    call :generate vcproj2 Windows\VC8\src\curlsrc.tmpl Windows\VC8\src\curlsrc.vcproj
163    call :generate vcproj2 Windows\VC8\lib\libcurl.tmpl Windows\VC8\lib\libcurl.vcproj
164  ) else (
165    echo Removing VC8 project files
166    call :clean Windows\VC8\src\curlsrc.vcproj
167    call :clean Windows\VC8\lib\libcurl.vcproj
168  )
169
170  if not "%VERSION%" == "ALL" goto success
171
172:vc9
173  echo.
174
175  if "%MODE%" == "GENERATE" (
176    echo Generating VC9 project files
177    call :generate vcproj2 Windows\VC9\src\curlsrc.tmpl Windows\VC9\src\curlsrc.vcproj
178    call :generate vcproj2 Windows\VC9\lib\libcurl.tmpl Windows\VC9\lib\libcurl.vcproj
179  ) else (
180    echo Removing VC9 project files
181    call :clean Windows\VC9\src\curlsrc.vcproj
182    call :clean Windows\VC9\lib\libcurl.vcproj
183  )
184
185  if not "%VERSION%" == "ALL" goto success
186
187:vc10
188  echo.
189
190  if "%MODE%" == "GENERATE" (
191    echo Generating VC10 project files
192    call :generate vcxproj Windows\VC10\src\curlsrc.tmpl Windows\VC10\src\curlsrc.vcxproj
193    call :generate vcxproj Windows\VC10\lib\libcurl.tmpl Windows\VC10\lib\libcurl.vcxproj
194  ) else (
195    echo Removing VC10 project files
196    call :clean Windows\VC10\src\curlsrc.vcxproj
197    call :clean Windows\VC10\lib\libcurl.vcxproj
198  )
199
200  if not "%VERSION%" == "ALL" goto success
201
202:vc11
203  echo.
204
205  if "%MODE%" == "GENERATE" (
206    echo Generating VC11 project files
207    call :generate vcxproj Windows\VC11\src\curlsrc.tmpl Windows\VC11\src\curlsrc.vcxproj
208    call :generate vcxproj Windows\VC11\lib\libcurl.tmpl Windows\VC11\lib\libcurl.vcxproj
209  ) else (
210    echo Removing VC11 project files
211    call :clean Windows\VC11\src\curlsrc.vcxproj
212    call :clean Windows\VC11\lib\libcurl.vcxproj
213  )
214
215  if not "%VERSION%" == "ALL" goto success
216
217:vc12
218  echo.
219
220  if "%MODE%" == "GENERATE" (
221    echo Generating VC12 project files
222    call :generate vcxproj Windows\VC12\src\curlsrc.tmpl Windows\VC12\src\curlsrc.vcxproj
223    call :generate vcxproj Windows\VC12\lib\libcurl.tmpl Windows\VC12\lib\libcurl.vcxproj
224  ) else (
225    echo Removing VC12 project files
226    call :clean Windows\VC12\src\curlsrc.vcxproj
227    call :clean Windows\VC12\lib\libcurl.vcxproj
228  )
229
230  goto success
231
232rem Main generate function.
233rem
234rem %1 - Project Type (dsp for VC6, vcproj1 for VC7 and VC7.1, vcproj2 for VC8 and VC9
235rem      or vcxproj for VC10, VC11 and VC12)
236rem %2 - Input template file
237rem %3 - Output project file
238rem
239:generate
240  if not exist %2 (
241    echo.
242    echo Error: Cannot open %CD%\%2
243    exit /B
244  )
245
246  if exist %3 (  
247    del %3
248  )
249
250  echo * %CD%\%3
251  for /f "usebackq delims=" %%i in (`"findstr /n ^^ %2"`) do (
252    set "var=%%i"
253    setlocal enabledelayedexpansion
254    set "var=!var:*:=!"
255
256    if "!var!" == "CURL_SRC_C_FILES" (
257      for /f "delims=" %%c in ('dir /b ..\src\*.c') do call :element %1 src "%%c" %3
258    ) else if "!var!" == "CURL_SRC_H_FILES" (
259      for /f "delims=" %%h in ('dir /b ..\src\*.h') do call :element %1 src "%%h" %3
260    ) else if "!var!" == "CURL_SRC_RC_FILES" (
261      for /f "delims=" %%r in ('dir /b ..\src\*.rc') do call :element %1 src "%%r" %3
262    ) else if "!var!" == "CURL_SRC_X_C_FILES" (
263      call :element %1 lib "strtoofft.c" %3
264      call :element %1 lib "rawstr.c" %3
265      call :element %1 lib "nonblock.c" %3
266      call :element %1 lib "warnless.c" %3
267    ) else if "!var!" == "CURL_SRC_X_H_FILES" (
268      call :element %1 lib "config-win32.h" %3
269      call :element %1 lib "curl_setup.h" %3
270      call :element %1 lib "strtoofft.h" %3
271      call :element %1 lib "rawstr.h" %3
272      call :element %1 lib "nonblock.h" %3
273      call :element %1 lib "warnless.h" %3
274    ) else if "!var!" == "CURL_LIB_C_FILES" (
275      for /f "delims=" %%c in ('dir /b ..\lib\*.c') do call :element %1 lib "%%c" %3
276    ) else if "!var!" == "CURL_LIB_H_FILES" (
277      for /f "delims=" %%h in ('dir /b ..\lib\*.h') do call :element %1 lib "%%h" %3
278    ) else if "!var!" == "CURL_LIB_RC_FILES" (
279      for /f "delims=" %%r in ('dir /b ..\lib\*.rc') do call :element %1 lib "%%r" %3
280    ) else if "!var!" == "CURL_LIB_VAUTH_C_FILES" (
281      for /f "delims=" %%c in ('dir /b ..\lib\vauth\*.c') do call :element %1 lib\vauth "%%c" %3
282    ) else if "!var!" == "CURL_LIB_VAUTH_H_FILES" (
283      for /f "delims=" %%h in ('dir /b ..\lib\vauth\*.h') do call :element %1 lib\vauth "%%h" %3
284    ) else if "!var!" == "CURL_LIB_VTLS_C_FILES" (
285      for /f "delims=" %%c in ('dir /b ..\lib\vtls\*.c') do call :element %1 lib\vtls "%%c" %3
286    ) else if "!var!" == "CURL_LIB_VTLS_H_FILES" (
287      for /f "delims=" %%h in ('dir /b ..\lib\vtls\*.h') do call :element %1 lib\vtls "%%h" %3
288    ) else (
289      echo.!var!>> %3
290    )
291
292    endlocal
293  )
294  exit /B
295
296rem Generates a single file xml element.
297rem
298rem %1 - Project Type (dsp for VC6, vcproj1 for VC7 and VC7.1, vcproj2 for VC8 and VC9
299rem      or vcxproj for VC10, VC11 and VC12)
300rem %2 - Directory (src, lib, lib\vauth or lib\vtls)
301rem %3 - Source filename
302rem %4 - Output project file
303rem
304:element
305  set "SPACES=    "
306  if "%2" == "lib\vauth" (
307    set "TABS=				"
308  ) else if "%2" == "lib\vtls" (
309    set "TABS=				"
310  ) else (
311    set "TABS=			"
312  )
313
314  call :extension %3 ext
315
316  if "%1" == "dsp" (
317    echo # Begin Source File>> %4
318    echo.>> %4
319    echo SOURCE=..\..\..\..\%2\%~3>> %4
320    echo # End Source File>> %4
321  ) else if "%1" == "vcproj1" (
322    echo %TABS%^<File>> %4
323    echo %TABS%	RelativePath="..\..\..\..\%2\%~3"^>>> %4
324    echo %TABS%^</File^>>> %4
325  ) else if "%1" == "vcproj2" (
326    echo %TABS%^<File>> %4
327    echo %TABS%	RelativePath="..\..\..\..\%2\%~3">> %4
328    echo %TABS%^>>> %4
329    echo %TABS%^</File^>>> %4
330  ) else if "%1" == "vcxproj" (
331    if "%ext%" == "c" (
332      echo %SPACES%^<ClCompile Include=^"..\..\..\..\%2\%~3^" /^>>> %4
333    ) else if "%ext%" == "h" (
334      echo %SPACES%^<ClInclude Include=^"..\..\..\..\%2\%~3^" /^>>> %4
335    ) else if "%ext%" == "rc" (
336      echo %SPACES%^<ResourceCompile Include=^"..\..\..\..\%2\%~3^" /^>>> %4
337    )
338  )
339
340  exit /B
341
342rem Returns the extension for a given filename.
343rem
344rem %1 - The filename
345rem %2 - The return value
346rem
347:extension
348  set fname=%~1
349  set ename=
350:loop1
351  if "%fname%"=="" (
352    set %2=
353    exit /B
354  )
355
356  if not "%fname:~-1%"=="." (
357    set ename=%fname:~-1%%ename%
358    set fname=%fname:~0,-1%
359    goto loop1
360  )
361
362  set %2=%ename%
363  exit /B
364
365rem Removes the given project file.
366rem
367rem %1 - The filename
368rem
369:clean
370  echo * %CD%\%1
371
372  if exist %1 (  
373    del %1
374  )
375
376  exit /B
377
378rem CALL this function to generate ..\src\tool_hugehelp.c
379rem Returns exit code 0 on success or 1 on failure.
380:gen_hugehelp
381  setlocal
382  set LC_ALL=C
383  set ROFFCMD=
384  if defined HAVE_PERL (
385    if defined HAVE_GROFF (
386      set ROFFCMD=groff -mtty-char -Tascii -P-c -man
387    ) else if defined HAVE_NROFF (
388      set ROFFCMD=nroff -c -Tascii -man
389    )
390  )
391  echo * %CD%\..\src\tool_hugehelp.c
392  echo #include "tool_setup.h"> ..\src\tool_hugehelp.c
393  echo #include "tool_hugehelp.h">> ..\src\tool_hugehelp.c
394  if defined ROFFCMD (
395    if defined HAVE_GZIP (
396      echo #ifndef HAVE_LIBZ>> ..\src\tool_hugehelp.c
397    )
398    %ROFFCMD% ..\docs\curl.1 2>NUL | perl ..\src\mkhelp.pl ..\docs\MANUAL >> ..\src\tool_hugehelp.c
399    if defined HAVE_GZIP (
400      echo #else>> ..\src\tool_hugehelp.c
401      %ROFFCMD% ..\docs\curl.1 2>NUL | perl ..\src\mkhelp.pl -c ..\docs\MANUAL >> ..\src\tool_hugehelp.c
402      echo #endif /^* HAVE_LIBZ ^*/>> ..\src\tool_hugehelp.c
403    )
404  ) else (
405    echo.
406    echo Warning: The curl manual could not be integrated in the source. This means when
407    echo you build curl the manual will not be available (curl --man^). Integration of
408    echo the manual is not required and a summary of the options will still be available
409    echo (curl --help^). To integrate the manual your PATH is required to have
410    echo groff/nroff, perl and optionally gzip for compression.
411    echo.
412    echo void hugehelp(void^)>> ..\src\tool_hugehelp.c
413    echo #ifdef USE_MANUAL>> ..\src\tool_hugehelp.c
414    echo { fputs("built-in manual not included\n", stdout^); }>> ..\src\tool_hugehelp.c
415    echo #else>> ..\src\tool_hugehelp.c
416    echo {}>> ..\src\tool_hugehelp.c
417    echo #endif>> ..\src\tool_hugehelp.c
418  )
419  findstr "/C:void hugehelp(void)" ..\src\tool_hugehelp.c 1>NUL 2>&1
420  if %ERRORLEVEL% NEQ 0 (
421    echo Error: Unable to generate ..\src\tool_hugehelp.c
422    exit /B 1
423  )
424  exit /B 0
425
426rem CALL this function to generate ..\include\curl\curlbuild.h
427rem Returns exit code 0 on success or 1 on failure.
428:gen_curlbuild
429  setlocal
430  echo * %CD%\..\include\curl\curlbuild.h
431  copy /y ..\include\curl\curlbuild.h.dist ..\include\curl\curlbuild.h 1>NUL
432  if %ERRORLEVEL% NEQ 0 (
433    echo Error: Unable to generate ..\include\curl\curlbuild.h
434    exit /B 1
435  )
436  exit /B 0
437
438:syntax
439  rem Display the help
440  echo.
441  echo Usage: generate [compiler] [-clean]
442  echo.
443  echo Compiler:
444  echo.
445  echo vc6       - Use Visual Studio 6
446  echo vc7       - Use Visual Studio .NET
447  echo vc7.1     - Use Visual Studio .NET 2003
448  echo vc8       - Use Visual Studio 2005
449  echo vc9       - Use Visual Studio 2008
450  echo vc10      - Use Visual Studio 2010
451  echo vc11      - Use Visual Studio 2012
452  echo vc12      - Use Visual Studio 2013
453  echo.
454  echo -clean    - Removes the project files
455  goto error
456
457:unknown
458  echo.
459  echo Error: Unknown argument '%1'
460  goto error
461
462:nodos
463  echo.
464  echo Error: Only a Windows NT based Operating System is supported
465  goto error
466
467:nonetdrv
468  echo.
469  echo Error: This batch file cannot run from a network drive
470  goto error
471
472:seterr
473  rem Set the caller's errorlevel.
474  rem %1[opt]: Errorlevel as integer.
475  rem If %1 is empty the errorlevel will be set to 0.
476  rem If %1 is not empty and not an integer the errorlevel will be set to 1.
477  setlocal
478  set EXITCODE=%~1
479  if not defined EXITCODE set EXITCODE=0
480  echo %EXITCODE%|findstr /r "[^0-9\-]" 1>NUL 2>&1
481  if %ERRORLEVEL% EQU 0 set EXITCODE=1
482  exit /b %EXITCODE%
483
484:error
485  endlocal
486  exit /B 1
487
488:success
489  endlocal
490  exit /B 0
491