-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.bat
467 lines (413 loc) · 14.7 KB
/
build.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
@echo off
setlocal enabledelayedexpansion
@rem only for interactive debugging !
set _DEBUG=0
@rem #########################################################################
@rem ## Environment setup
set _EXITCODE=0
call :env
if not %_EXITCODE%==0 goto end
call :args %*
if not %_EXITCODE%==0 goto end
@rem #########################################################################
@rem ## Main
for %%i in (%_COMMANDS%) do (
call :%%i
if not !_EXITCODE!==0 goto end
)
goto end
@rem #########################################################################
@rem ## Subroutines
@rem output parameters: _DEBUG_LABEL, _ERROR_LABEL, _WARNING_LABEL
@rem _CLASSES_DIR, _TARGET_DIR, _TARGET_DOCS_DIR
:env
set _BASENAME=%~n0
set "_ROOT_DIR=%~dp0"
call :env_colors
set _DEBUG_LABEL=%_NORMAL_BG_CYAN%[%_BASENAME%]%_RESET%
set _ERROR_LABEL=%_STRONG_FG_RED%Error%_RESET%:
set _WARNING_LABEL=%_STRONG_FG_YELLOW%Warning%_RESET%:
@rem common libraries
for /f "delims=" %%f in ("%~dp0.") do set "_LIB_DIR=%%~dpflib"
set "_SOURCE_DIR=%_ROOT_DIR%src"
set "_SOURCE_MAIN_DIR=%_SOURCE_DIR%\main\ada"
set "_TARGET_DIR=%_ROOT_DIR%target"
@rem path must match property `Documentation_Dir` in gpr file
set "_TARGET_HTML_DIR=%_TARGET_DIR%\html"
set "_TARGET_OBJ_DIR=%_TARGET_DIR%\obj"
@rem required for gnatdoc
if not exist "%_ROOT_DIR%build.gpr" (
echo %_ERROR_LABEL% GNAT Ada project file not found 1>&2
set _EXITCODE=1
goto :eof
)
if not exist "%GNAT_HOME%\bin\gnatmake.exe" (
echo %_ERROR_LABEL% GNAT installation not found 1>&2
set _EXITCODE=1
goto :eof
)
set "_GNATMAKE_CMD=%GNAT_HOME%\bin\gnatmake.exe"
set "_GNATDOC_CMD=%GNAT_HOME%\bin\gnatdoc.exe"
set _MSYS_GNATMAKE_CMD=
if exist "%MSYS_HOME%\mingw64\bin\gnatmake.exe" (
set "_MSYS_GNATMAKE_CMD=%MSYS_HOME%\mingw64\bin\gnatmake.exe"
)
set _ADACTL_CMD=
if exist "%ADACTL_HOME%\adactl.exe" (
set "_ADACTL_CMD=%ADACTL_HOME%\adactl.exe"
)
set _DIFF_CMD=
if exist "%GIT_HOME%\usr\bin\diff.exe" (
set "_DIFF_CMD=%GIT_HOME%\usr\bin\diff.exe"
)
@rem use newer PowerShell version if available
where /q pwsh.exe
if %ERRORLEVEL%==0 ( set _PWSH_CMD=pwsh.exe
) else ( set _PWSH_CMD=powershell.exe
)
goto :eof
:env_colors
@rem ANSI colors in standard Windows 10 shell
@rem see https://gist.github.com/mlocati/#file-win10colors-cmd
@rem normal foreground colors
set _NORMAL_FG_BLACK=[30m
set _NORMAL_FG_RED=[31m
set _NORMAL_FG_GREEN=[32m
set _NORMAL_FG_YELLOW=[33m
set _NORMAL_FG_BLUE=[34m
set _NORMAL_FG_MAGENTA=[35m
set _NORMAL_FG_CYAN=[36m
set _NORMAL_FG_WHITE=[37m
@rem normal background colors
set _NORMAL_BG_BLACK=[40m
set _NORMAL_BG_RED=[41m
set _NORMAL_BG_GREEN=[42m
set _NORMAL_BG_YELLOW=[43m
set _NORMAL_BG_BLUE=[44m
set _NORMAL_BG_MAGENTA=[45m
set _NORMAL_BG_CYAN=[46m
set _NORMAL_BG_WHITE=[47m
@rem strong foreground colors
set _STRONG_FG_BLACK=[90m
set _STRONG_FG_RED=[91m
set _STRONG_FG_GREEN=[92m
set _STRONG_FG_YELLOW=[93m
set _STRONG_FG_BLUE=[94m
set _STRONG_FG_MAGENTA=[95m
set _STRONG_FG_CYAN=[96m
set _STRONG_FG_WHITE=[97m
@rem strong background colors
set _STRONG_BG_BLACK=[100m
set _STRONG_BG_RED=[101m
set _STRONG_BG_GREEN=[102m
set _STRONG_BG_YELLOW=[103m
set _STRONG_BG_BLUE=[104m
@rem we defined _RESET in last position to avoid crazy console output with type command
set _BOLD=[1m
set _UNDERSCORE=[4m
set _INVERSE=[7m
set _RESET=[0m
goto :eof
@rem input parameter: %*
:args
set _COMMANDS=
set _MSYS=0
set _TIMER=0
set _VERBOSE=0
set __N=0
:args_loop
set "__ARG=%~1"
if not defined __ARG (
if !__N!==0 set _COMMANDS=help
goto args_done
)
if "%__ARG:~0,1%"=="-" (
@rem option
if "%__ARG%"=="-debug" ( set _DEBUG=1
) else if "%__ARG%"=="-help" ( set _COMMANDS=help
) else if "%__ARG%"=="-msys" ( set _MSYS=1
) else if "%__ARG%"=="-timer" ( set _TIMER=1
) else if "%__ARG%"=="-verbose" ( set _VERBOSE=1
) else (
echo %_ERROR_LABEL% Unknown option "%__ARG%" 1>&2
set _EXITCODE=1
goto args_done
)
) else (
@rem subcommand
if "%__ARG%"=="clean" ( set _COMMANDS=!_COMMANDS! clean
) else if "%__ARG%"=="compile" ( set _COMMANDS=!_COMMANDS! compile
) else if "%__ARG%"=="doc" ( set _COMMANDS=!_COMMANDS! compile doc
) else if "%__ARG%"=="help" ( set _COMMANDS=help
) else if "%__ARG%"=="lint" ( set _COMMANDS=!_COMMANDS! lint
) else if "%__ARG%"=="run" ( set _COMMANDS=!_COMMANDS! compile run
) else if "%__ARG%"=="test" ( set _COMMANDS=!_COMMANDS! compile test
) else (
echo %_ERROR_LABEL% Unknown subcommand "%__ARG%" 1>&2
set _EXITCODE=1
goto args_done
)
set /a __N+=1
)
shift
goto args_loop
:args_done
set _STDERR_REDIRECT=2^>NUL
if %_DEBUG%==1 set _STDERR_REDIRECT=
set _MAIN_NAME=samples_test
set _MAIN_ARGS=
set "_SOURCE_MAIN_FILE=%_SOURCE_MAIN_DIR%\%_MAIN_NAME%.adb"
for /f "delims=" %%i in ("%~dp0\.") do set "_PROJECT_NAME=%%~ni"
set "_EXE_FILE=%_TARGET_DIR%\%_PROJECT_NAME%.exe"
if %_MSYS%==1 if not defined _MSYS_GNATMAKE_CMD (
echo %_WARNING_LABEL% MSYS GNAT Make not found; use standard GNAT Make instead 1>&2
set _MSYS=0
)
if not "!_COMMANDS:lint=!"=="%_COMMANDS%" (
if not exist "%GNAT2019_HOME%\bin\gnat.exe" (
echo %_WARNING_LABEL% GNAT 2019 is required to execute AdaControl 1>&2
set "_COMMANDS=!_COMMANDS:lint=!"
) else (
for /f "delims=" %%f in ("%_ROOT_DIR%.") do set "__PARENT_DIR=%%~dpf"
for /f "delims=" %%f in ('dir /b /s "!__PARENT_DIR!*.aru" 2^>NUL') do set "_ARU_FILE=%%f"
if not exist "!_ARU_FILE!" (
echo %_WARNING_LABEL% ARU file not found 1>&2
set "_COMMANDS=!_COMMANDS:lint=!"
)
)
)
if not "!_COMMANDS:compile=!"=="%_COMMANDS%" if %_MSYS%==1 if not defined _MSYS_GNATMAKE_CMD (
echo %_WARNING_LABEL% MSYS GNAT Make not found; use standard GNAT Make instead 1>&2
set _MSYS=0
)
if %_DEBUG%==1 (
echo %_DEBUG_LABEL% Options : _MSYS=%_MSYS% _TIMER=%_TIMER% _VERBOSE=%_VERBOSE% 1>&2
echo %_DEBUG_LABEL% Subcommands: %_COMMANDS% 1>&2
echo %_DEBUG_LABEL% Variables : "ADACTL_HOME=%ADACTL_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "GIT_HOME=%GIT_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "GNAT_HOME=%GNAT_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "GNAT2019_HOME=%GNAT2019_HOME%" 1>&2
if defined MSYS_HOME echo %_DEBUG_LABEL% Variables : "MSYS_HOME=%MSYS_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : _MAIN_NAME=%_MAIN_NAME% _MAIN_ARGS=%_MAIN_ARGS% 1>&2
echo %_DEBUG_LABEL% Variables : _PROJECT_NAME=%_PROJECT_NAME% 1>&2
)
if %_TIMER%==1 for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "(Get-Date)"') do set _TIMER_START=%%i
goto :eof
:help
if %_VERBOSE%==1 (
set __BEG_P=%_STRONG_FG_CYAN%
set __BEG_O=%_STRONG_FG_GREEN%
set __BEG_N=%_NORMAL_FG_YELLOW%
set __END=%_RESET%
) else (
set __BEG_P=
set __BEG_O=
set __BEG_N=
set __END=
)
echo Usage: %__BEG_O%%_BASENAME% { ^<option^> ^| ^<subcommand^> }%__END%
echo.
echo %__BEG_P%Options:%__END%
echo %__BEG_O%-debug%__END% print commands executed by this script
echo %__BEG_O%-msys%__END% use MSYS GNAT Make if available
echo %__BEG_O%-timer%__END% print total execution time
echo %__BEG_O%-verbose%__END% print progress messages
echo.
echo %__BEG_P%Subcommands:%__END%
echo %__BEG_O%clean%__END% delete generated files
echo %__BEG_O%compile%__END% compile Ada source files
echo %__BEG_O%doc%__END% generate HTML documentation
echo %__BEG_O%help%__END% print this help message
echo %__BEG_O%lint%__END% analyze Ada source files with %__BEG_N%AdaControl%__END%
echo %__BEG_O%run%__END% execute main program "%__BEG_O%%_MAIN_NAME%%__END%"
echo %__BEG_O%test%__END% execute unit tests with %__BEG_N%AUnit%__END%
goto :eof
:clean
call :rmdir "%_TARGET_DIR%"
goto :eof
@rem input parameter: %1=directory path
:rmdir
set "__DIR=%~1"
if not exist "%__DIR%\" goto :eof
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% rmdir /s /q "%__DIR%" 1>&2
) else if %_VERBOSE%==1 ( echo Delete directory "!__DIR:%_ROOT_DIR%=!" 1>&2
)
rmdir /s /q "%__DIR%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to delete directory "!__DIR:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:lint
if not exist "%_TARGET_DIR%" mkdir "%_TARGET_DIR%" 1>NUL
@rem set "__GPR_FILE=%_ROOT_DIR%%_PROJECT_NAME%.gpr"
set "__LOG_FILE=%_TARGET_DIR%\adactl_log.txt"
@rem AdaControl requires the GNAT 2019 tool chain
set "__PATH=%PATH%"
set "PATH=%GNAT2019_HOME%\bin;%PATH%"
if %_DEBUG%==1 (
for /f "delims=" %%f in ('where gcc') do set "__GCC_CMD=%%f"
echo %_DEBUG_LABEL% GCC command is: !__GCC_CMD! 1>&2
)
@rem see https://www.adalog.fr/compo/adacontrol_ug.html#command-files-provided-with-AdaControl
@rem set __ADACTL_OPTS=-f "%_ARU_FILE%" -o "%__LOG_FILE%" -w -l "check dependencies (with, Greetings);"
set __ADACTL_OPTS=-f "%_ARU_FILE%" -o "%__LOG_FILE%" -w
if %_DEBUG%==1 ( set __ADACTL_OPTS=-d -v %__ADACTL_OPTS%
) else if %_VERBOSE%==1 ( set __ADACTL_OPTS=-v %__ADACTL_OPTS%
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_ADACTL_CMD%" %__ADACTL_OPTS% "%_SOURCE_MAIN_DIR%"\* 1>&2
) else if %_VERBOSE%==1 ( echo Analyze Ada source files 1>&2
)
@rem .adt and .ali files are generated in current directory
pushd "%_TARGET_DIR%"
call "%_ADACTL_CMD%" %__ADACTL_OPTS% "%_SOURCE_MAIN_DIR%"\*
if not %ERRORLEVEL%==0 (
if %_DEBUG%==1 ( type "%__LOG_FILE%"
) else if %_VERBOSE%==1 ( type "%__LOG_FILE%"
)
popd
set "PATH=%__PATH%"
set _EXITCODE=1
goto :eof
)
popd
set "PATH=%__PATH%"
goto :eof
:compile
if not exist "%_TARGET_OBJ_DIR%" mkdir "%_TARGET_OBJ_DIR%" 1>NUL
call :action_required "%_EXE_FILE%" "%_SOURCE_MAIN_DIR%\*.ada" "%_SOURCE_MAIN_DIR%\*.adb" "%_SOURCE_MAIN_DIR%\*.ads"
if %_ACTION_REQUIRED%==0 goto :eof
set __SOURCE_FILES=
set __N=0
for /f "delims=" %%f in ('dir /b /s "%_SOURCE_MAIN_DIR%\*.ad?" 2^>NUL') do (
set __SOURCE_FILES=!__SOURCE_FILES! "%%f"
set /a __N+=1
)
if %__N%==0 (
echo %_WARNING_LABEL% No Ada source file found 1>&2
goto :eof
) else if %__N%==1 ( set __N_FILES=%__N% Ada source file
) else ( set __N_FILES=%__N% Ada source files
)
if %_MSYS%==1 ( set "__GNATMAKE_CMD=%_MSYS_GNATMAKE_CMD%"
) else ( set "__GNATMAKE_CMD=%_GNATMAKE_CMD%"
)
@rem -we : Treat all warnings as errors
set __GNATMAKE_INCLS=-I"%GNAT_HOME%\include\aunit" -I"%_LIB_DIR%" -I"%_SOURCE_MAIN_DIR%"
set __GNATMAKE_OPTS=-gnat2022 -we %__GNATMAKE_INCLS% -D "%_TARGET_OBJ_DIR%" -o "%_EXE_FILE%"
if %_DEBUG%==1 set __GNATMAKE_OPTS=-d %__GNATMAKE_OPTS%
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%__GNATMAKE_CMD%" %__GNATMAKE_OPTS% "%_SOURCE_MAIN_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Compile %__N_FILES% to directory "!_TARGET_OBJ_DIR:%_ROOT_DIR%=!" 1>&2
)
call "%__GNATMAKE_CMD%" %__GNATMAKE_OPTS% "%_SOURCE_MAIN_FILE%" %_STDERR_REDIRECT%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to compile %__N_FILES% to directory "!_TARGET_OBJ_DIR:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
@rem input parameter: 1=target file 2,3,..=path (wildcards accepted)
@rem output parameter: _ACTION_REQUIRED
:action_required
set "__TARGET_FILE=%~1"
set __PATH_ARRAY=
set __PATH_ARRAY1=
:action_path
shift
set "__PATH=%~1"
if not defined __PATH goto action_next
set __PATH_ARRAY=%__PATH_ARRAY%,'%__PATH%'
set __PATH_ARRAY1=%__PATH_ARRAY1%,'!__PATH:%_ROOT_DIR%=!'
goto action_path
:action_next
set __TARGET_TIMESTAMP=00000000000000
for /f "usebackq" %%i in (`call "%_PWSH_CMD%" -c "gci -path '%__TARGET_FILE%' -ea Stop | select -last 1 -expandProperty LastWriteTime | Get-Date -uformat %%Y%%m%%d%%H%%M%%S" 2^>NUL`) do (
set __TARGET_TIMESTAMP=%%i
)
set __SOURCE_TIMESTAMP=00000000000000
for /f "usebackq" %%i in (`call "%_PWSH_CMD%" -c "gci -recurse -path %__PATH_ARRAY:~1% -ea Stop | sort LastWriteTime | select -last 1 -expandProperty LastWriteTime | Get-Date -uformat %%Y%%m%%d%%H%%M%%S" 2^>NUL`) do (
set __SOURCE_TIMESTAMP=%%i
)
call :newer %__SOURCE_TIMESTAMP% %__TARGET_TIMESTAMP%
set _ACTION_REQUIRED=%_NEWER%
if %_DEBUG%==1 (
echo %_DEBUG_LABEL% %__TARGET_TIMESTAMP% Target : '%__TARGET_FILE%' 1>&2
echo %_DEBUG_LABEL% %__SOURCE_TIMESTAMP% Sources: %__PATH_ARRAY:~1% 1>&2
echo %_DEBUG_LABEL% _ACTION_REQUIRED=%_ACTION_REQUIRED% 1>&2
) else if %_VERBOSE%==1 if %_ACTION_REQUIRED%==0 if %__SOURCE_TIMESTAMP% gtr 0 (
echo No action required ^("%__PATH_ARRAY1:~1%"^) 1>&2
)
goto :eof
@rem input parameters: %1=file timestamp 1, %2=file timestamp 2
@rem output parameter: _NEWER
:newer
set __TIMESTAMP1=%~1
set __TIMESTAMP2=%~2
set __DATE1=%__TIMESTAMP1:~0,8%
set __TIME1=%__TIMESTAMP1:~-6%
set __DATE2=%__TIMESTAMP2:~0,8%
set __TIME2=%__TIMESTAMP2:~-6%
if %__DATE1% gtr %__DATE2% ( set _NEWER=1
) else if %__DATE1% lss %__DATE2% ( set _NEWER=0
) else if %__TIME1% gtr %__TIME2% ( set _NEWER=1
) else ( set _NEWER=0
)
goto :eof
:doc
if not exist "%_TARGET_HTML_DIR%\" mkdir "%_TARGET_HTML_DIR%"
set "__JS_FILE=%_TARGET_HTML_DIR%\index.js"
call :action_required "%__JS_FILE%" "%_ROOT_DIR%build.gpr" "%_EXE_FILE%"
if %_ACTION_REQUIRED%==0 goto :eof
@rem https://docs.adacore.com/live/wave/gps/html/gnatdoc-doc/gnatdoc.html
@rem Options: -p=Process private part of packages
set __GNATDOC_OPTS=-d -p "--project=%_ROOT_DIR%build.gpr" --output=html
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_GNATDOC_CMD%" %__GNATDOC_OPTS% 1>&2
) else if %_VERBOSE%==1 ( echo Generate HTML documentation into directory "!_TARGET_HTML_DIR:%_ROOT_DIR%=!" 1>&2
)
call "%_GNATDOC_CMD%" %__GNATDOC_OPTS%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to generate HTML documentation into directory "!_TARGET_HTML_DIR:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:run
if not exist "%_EXE_FILE%" (
echo %_ERROR_LABEL% Main program "%_PROJECT_NAME%" not found ^(%_EXE_FILE%^) 1>&2
set _EXITCODE=1
goto :eof
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_EXE_FILE%" %_MAIN_ARGS% 1>&2
) else if %_VERBOSE%==1 ( echo Execute Ada program "!_EXE_FILE:%_ROOT_DIR%=!" 1>&2
)
call "%_EXE_FILE%" %_MAIN_ARGS%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to execute Ada program "!_EXE_FILE:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:compile_test
echo %_WARNING_LABEL% Not yet implemented 1>&2
goto :eof
:test
echo %_WARNING_LABEL% Subcommand 'test' is not yet implemented 1>&2
goto :eof
@rem output parameter: _DURATION
:duration
set __START=%~1
set __END=%~2
for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "$interval = New-TimeSpan -Start '%__START%' -End '%__END%'; Write-Host $interval"') do set _DURATION=%%i
goto :eof
@rem #########################################################################
@rem ## Cleanups
:end
if %_TIMER%==1 (
for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "(Get-Date)"') do set __TIMER_END=%%i
call :duration "%_TIMER_START%" "!__TIMER_END!"
echo Total execution time: !_DURATION! 1>&2
)
if %_DEBUG%==1 echo %_DEBUG_LABEL% _EXITCODE=%_EXITCODE% 1>&2
exit /b %_EXITCODE%
endlocal