-
Notifications
You must be signed in to change notification settings - Fork 92
/
dynmake.bat
executable file
·358 lines (286 loc) · 12.1 KB
/
dynmake.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
@if defined TRACEON (@echo on) else (@echo off)
REM If this batch file works okay then it was written by Fish.
REM If it doesn't work then I don't know who the heck wrote it.
::*****************************************************************************
::*****************************************************************************
::*****************************************************************************
::*** ***
::*** DYNMAKE.BAT ***
::*** ***
::*****************************************************************************
::*****************************************************************************
::*****************************************************************************
::* *
::* Designed to be called either from the command line or by a *
::* Visual Studio makefile project with the "Build command line" *
::* set to: "dynmake.bat <arguments...>". See 'HELP' section *
::* further below for details regarding use. *
::* *
::*****************************************************************************
::* *
::* PROGRAMMING NOTE *
::* *
::* All error messages *MUST* be issued in the following strict format: *
::* *
::* echo %~nx0^(1^) : error C9999 : error-message-text... *
::* *
::* in order for the Visual Studio IDE to detect it as a build error since *
::* exiting with a non-zero return code doesn't do the trick. Visual Studio *
::* apparently examines the message-text looking for error/warning strings. *
::* *
::*****************************************************************************
setlocal
pushd .
set rc=0
set "TRACE=if defined TRACEON echo"
if "%~1" == "" goto :help
if "%~1" == "?" goto :help
if "%~1" == "/?" goto :help
if "%~1" == "-?" goto :help
if "%~1" == "--help" goto :help
set hercdir=%~dp0
set projdir=%~1
set modname=%~2
set build_type=%~3
set num_cpus=%~4
set extra_nmake_args=%~5
:extra_args
if "%~6" == "" goto :begin
set extra_nmake_args=%extra_nmake_args% %~6
shift /1
goto :extra_args
::---------------------------------------------------------------------
:: HELP
::---------------------------------------------------------------------
:help
set rc=1
echo.
echo %~nx0^(1^) : error C9999 : Help information is as follows:
echo.
echo.
echo.
echo %~nx0
echo.
echo.
echo Build script for building a dynamically loadable Hercules plugin
echo module. This script performs some necessary pre-processing before
echo eventually invoking the main Hercules build script to build your
echo dynamic module.
echo.
echo.
echo Format:
echo.
echo.
echo %~nx0 {projdir} {modname} {build_type} {num_cpus} [-a^|clean]
echo.
echo.
echo Where:
echo.
echo {projdir} The fully qualified path of your dynamic module
echo project directory. If the path contains spaces
echo enclose it within double quotes.
echo.
echo {modname} The root filename of your dynamic module (e.g.
echo "dyn75"). One word without any spaces in it.
echo.
echo {build_type} Desired build configuration. Valid values are
echo DEBUG or RETAIL for building a 32-bit version,
echo or DEBUG-X64/RETAIL-X64 for a 64-bit version.
echo The same value is passed back to your optional
echo prebuild.bat and or postbld.bat scripts as the
echo second argument.
echo.
echo {num_cpus} The maximum number of emulated CPUs (NUMCPU=)
echo your module supports: 1 to 64. Must be the same
echo value that was used to build Hercules with.
echo The same value is passed back to you as the
echo 3rd parameter to your optional prebuild.bat
echo and/or postbld.bat scripts.
echo.
echo [-a^|clean] Either '-a' to perform a full rebuild of your
echo module, or 'clean' to clean the output directory
echo of all previously built binaries and work files.
echo If not specified then your module is only built
echo if it actually needs rebuilding (i.e. only if
echo any of your source files changed for example).
echo It is HIGHLY RECOMMENDED that you ALWAYS use
echo '-a' for any/all "retail" type builds. These
echo value(s) you specify here are passed back to
echo your optional prebuild.bat and/or postbld.bat
echo scripts as the last set of arguments (4 - n).
echo.
goto :exit
::---------------------------------------------------------------------
:begin
echo %~nx0^(1^) : Building dynamic module "%modname%" from "%projdir%"...
::---------------------------------------------------------------------
:: Validate parameters...
call :parseargs
if %rc% NEQ 0 goto :exit
::---------------------------------------------------------------------
:: Switch immediately to our primary directory...
cd /d "%hercdir%"
set xcopy_opts=/v /c /r /k /y
::---------------------------------------------------------------------
:: Create our o/p module building sub-directory...
call :isdir %DYNDIR%
if not defined # mkdir %DYNDIR%
::---------------------------------------------------------------------
:: Add the dynamic module's sub-directory to the list of directories
:: that the compiler uses to resolve #include statements with (which
:: 'nmake' also uses to resolve its own !INCLUDE statements with)...
set INCLUDE=%INCLUDE%;%hercdir%;%hercdir%%DYNDIR%
::---------------------------------------------------------------------
:: Perform pre-build processing...
call :do_callback "prebuild.bat" "pre-build"
if %rc% NEQ 0 goto :exit
::---------------------------------------------------------------------
:: Copy the dynamic module's source files from their directory
:: over into our module building sub-directory...
cd /d %DYNDIR%
if exist "%projdir%*.c" xcopy "%projdir%*.c" . %xcopy_opts% > NUL
if exist "%projdir%*.h" xcopy "%projdir%*.h" . %xcopy_opts% > NUL
if exist "%projdir%*.rc" xcopy "%projdir%*.rc" . %xcopy_opts% > NUL
if exist "%projdir%*.rc2" xcopy "%projdir%*.rc2" . %xcopy_opts% > NUL
if exist "%projdir%%DYNMOD%.msvc" xcopy "%projdir%%DYNMOD%.msvc" . %xcopy_opts% > NUL
cd ..
::---------------------------------------------------------------------
:: Now switch back to the main Hercules source-code directory
:: to do the actual build, by calling its main "makefile.bat"
cd /d "%hercdir%"
call "%hercdir%makefile.bat" "%build_type%" "%hercdir%makefile.msvc" "%num_cpus%" %extra_nmake_args%
set rc=%errorlevel%
if %rc% NEQ 0 goto :exit
::---------------------------------------------------------------------
:: Perform post-build processing...
call :do_callback "postbld.bat" "post-build"
if %rc% NEQ 0 goto :exit
::---------------------------------------------------------------------
:: Done!
echo %~nx0^(1^) : Build complete.
goto :exit
::---------------------------------------------------------------------
:: Restore original directory and environment before exiting...
:exit
popd
endlocal & set rc=%rc%
exit /b %rc%
::---------------------------------------------------------------------
:: CALLED SUBROUTINES
::---------------------------------------------------------------------
:parseargs
:: Validate hercules directory...
call :isdir "%hercdir%"
if not defined # (
echo %~nx0^(1^) : error C9999 : hercdir "%hercdir%" not found?!
set rc=1
goto :EOF
)
:: Validate module name...
call :hasblank "%modname%"
if defined # goto :bad_modname
call :goodfn "%modname%"
if not defined # (
:bad_modname
echo %~nx0^(1^) : error C9999 : Invalid module name "%modname%"
set rc=1
goto :EOF
)
:: Specified dynamic module name becomes our work sub-directory.
set DYNMOD=%modname%
set DYNDIR=%modname%\
:: Validate i/p project directory...
call :isdir "%projdir%"
if not defined # (
echo %~nx0^(1^) : error C9999 : projdir "%projdir%" not found.
set rc=1
goto :EOF
)
:: Verify their makefile include exists...
call :isfile "%projdir%%DYNMOD%.msvc"
if not defined # (
:bad_projdir
echo %~nx0^(1^) : error C9999 : makefile include "%projdir%%DYNMOD%.msvc" not found.
set rc=1
goto :EOF
)
:: Make sure the project directory they specified
:: was a fully qualified directory path...
set @=%cd%
cd /d "%projdir%"
call :fullpath "%projdir%%DYNMOD%.msvc"
cd /d "%@%"
if not defined # goto :bad_projdir
if /i not "%#%" == "%projdir%%DYNMOD%.msvc" if /i not "%#%" == "%projdir%\%DYNMOD%.msvc" (
echo %~nx0^(1^) : error C9999 : projdir "%projdir%" not fully qualified.
set rc=1
goto :EOF
)
:: Everything looks okay...
goto :EOF
::---------------------------------------------------------------------
:do_callback
set batfile=%~1
set msgtxt=%~2
call :isfile "%projdir%%batfile%"
if not defined # goto :EOF
echo %~nx0^(1^) : Invoking %msgtxt% processing...
cd /d "%projdir%"
call "%projdir%%batfile%" "%hercdir%" "%modname%" "%build_type%" "%num_cpus%" %extra_nmake_args%
set rc=%errorlevel%
cd /d "%hercdir%"
if %rc% NEQ 0 echo %~nx0^(1^) : error C9999 : %msgtxt% processing failed.
if %rc% EQU 0 echo %~nx0^(1^) : continuing...
goto :EOF
::---------------------------------------------------------------------
:hasblank
REM %1 = string to check
REM # = return code: 1 if contains blank, undefined if not
setlocal
set @=%~1
set #=
for /f "tokens=1*" %%a in ("@%@%@") do if not "%%b" == "" set #=1
endlocal & set #=%#%
goto :EOF
::---------------------------------------------------------------------
:goodfn
REM %1 = string to check
REM # = return code: 1 if good filename, undefined if not
setlocal
set @=%~1
set #=1
for /f "delims=\/:*?<>|" %%i in ("@%@%@") do if not "%%i" == "@%@%@" set #=
endlocal & set #=%#%
goto :EOF
::---------------------------------------------------------------------
:isfile
REM %1 = file to check
REM # = return code: defined if file exists, else undefined
set "#=%~a1"
if not defined # goto :EOF
if "%#:~0,1%" == "-" goto :EOF
set "#="
goto :EOF
::---------------------------------------------------------------------
:isdir
REM %1 = directory to check
REM # = return code: defined if directory exists, else undefined
set #=%~a1
if not defined # goto :EOF
if "%#:~0,1%" == "d" goto :EOF
set "#="
goto :EOF
::---------------------------------------------------------------------
:fullpath
REM %1 = filename to check (with or without directory)
REM # = returned full path or undefined if not found
:: Search the Windows PATH for the file in question and return
:: its fully qualified path if found. Otherwise return an empty
:: string. Note: the below does not work for directories, only
:: files...
setlocal
set path=.;%path%
set #=%~dpnx$PATH:1
endlocal & set #=%#%
goto :EOF
::---------------------------------------------------------------------