-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbuild.bat
More file actions
486 lines (432 loc) · 14.1 KB
/
build.bat
File metadata and controls
486 lines (432 loc) · 14.1 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
@ echo off
setlocal enabledelayedexpansion
rem Default arguments.
rem No default value means that the option is a flag that is either on or off.
rem A variable that can have arguments requires a default value (can be "")
set config=fm-suite
set sln_extension=sln
set build=
set vs=0
set coverage=
set build_type=Debug
set keep_build=
set compiler=ifx
rem Non-argument variables
set generator=
set cmake=cmake
set oneapi=
rem Argument variables
set -help=
set -config=
set -build=
set -vs=
set -coverage=
set -build_type=
set -keep_build=
set -compiler=
rem Jump to the directory where this build.bat script is
cd %~dp0
set root=%CD%
call :get_arguments %*
if !ERRORLEVEL! NEQ 0 exit /B %~1
call :get_environment_vars
if !ERRORLEVEL! NEQ 0 exit /B %~1
call :set_generator
if !ERRORLEVEL! NEQ 0 exit /B %~1
call :check_cmake_installation
if !ERRORLEVEL! NEQ 0 exit /B %~1
echo.
echo oneapi : !oneapi!
echo compiler : !compiler!
echo vs : !vs!
echo config : !config!
echo generator : !generator!
echo build_type : !build_type!
echo build : !build!
echo keep_build : !keep_build!
echo coverage : !coverage!
call :checks
if !ERRORLEVEL! NEQ 0 exit /B %~1
rem Only set the enviroment if not run from a developer command prompt
if "%VCINSTALLDIR%" == "" (
call :set_vs_environment
if !ERRORLEVEL! NEQ 0 exit /B %~1
)
call :do_cmake
if !ERRORLEVEL! NEQ 0 exit /B %~1
if !coverage! EQU 1 call :insert_coverage !config!
if !ERRORLEVEL! NEQ 0 exit /B %~1
call :build
if !ERRORLEVEL! NEQ 0 exit /B %~1
call :install
if !ERRORLEVEL! NEQ 0 exit /B %~1
echo.
echo Generated Visual Studio solution file: %root%\build_%config%\%config%.%sln_extension%
echo Finished
goto :end
rem ===
rem === PROCEDURES
rem ===
rem =================================
rem === Command line arguments ===
rem =================================
:get_arguments
echo.
echo Get command line arguments...
rem Read arguments
set "options=-config:!config! -help:!help! -vs:!vs! -compiler:!compiler! -coverage:!coverage! -build:!build! -build_type:!build_type! -keep_build:!keep_build!"
rem see: https://stackoverflow.com/questions/3973824/windows-bat-file-optional-argument-parsing answer 2.
for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B"
:loop
if not "%~1" == "" (
set "test=!options:*%~1:=! "
if "!test!" == "!options! " (
echo Error: Invalid option %~1
goto :argument_error
) else if "!test:~0,1!" == " " (
set "%~1=1"
) else (
set "%~1=%~2"
shift /1
)
shift /1
goto :loop
)
if !-help! == 1 (
goto :usage
)
set config=!-config!
set compilers="ifort ifx"
set "modified=!compilers:%-compiler%=!"
if !modified!==!compilers! (
echo ERROR: Compiler !-compiler! not recognized
goto :argument_error
)
set compiler=!-compiler!
if !-coverage! == 1 (
set coverage=1
) else (
set coverage=0
)
if !-build! == 1 (
set build=1
) else (
set build=0
)
if !-keep_build! == 1 (
set keep_build=1
) else (
set keep_build=0
)
set build_types="Debug Release RelWithDebInfo"
set "modified=!build_types:%-build_type%=!"
if !modified!==%build_types% (
echo ERROR: Build type !-build_type! not recognized
goto :argument_error
)
set "build_type=!-build_type!"
goto :eof
rem =======================
rem === ERROR IN ARG =====
rem =======================
:argument_error
echo.
echo Error in command line arguments.
goto :usage
rem =================================
rem === Get environment variables ===
rem =================================
:get_environment_vars
echo.
echo Attempting to find latest versions of Intel OneAPI and Visual Studio based on environment variables...
if NOT "%IFORT_COMPILER16%" == "" (
set oneapi=16
echo Found: Intel Fortran 2016
)
if NOT "%IFORT_COMPILER18%" == "" (
set oneapi=18
echo Found: Intel Fortran 2018
)
if NOT "%IFORT_COMPILER19%" == "" (
set oneapi=19
echo Found: Intel Fortran 2019
)
if NOT "%IFORT_COMPILER21%" == "" (
set oneapi=21
echo Found: Intel Fortran 2021
)
if NOT "%IFORT_COMPILER23%" == "" (
set oneapi=23
echo Found: Intel Fortran 2023
)
if NOT "%IFORT_COMPILER24%" == "" (
set oneapi=24
echo Found: Intel Fortran 2024
)
if NOT "%IFORT_COMPILER25%" == "" (
set oneapi=25
echo Found: Intel Fortran 2025
)
if "!oneapi!" == "" (
echo Warning: Could not find Intel OneAPI version in environment.
)
if "!oneapi!" == "" (
echo Error: oneapi not set. Please ensure that Intel OneAPI is installed and run build.bat from a prompt with the right environment set.
set ERRORLEVEL=1
goto :end
)
set "vs2017_found="
if NOT "%VS2017INSTALLDIR%" == "" (
set "vs2017_found=true"
)
if "%VisualStudioVersion%" == "15.0" (
set "vs2017_found=true"
)
if "%vs2017_found%" == "true" (
set vs=2017
echo Found: VisualStudio 15 2017
)
set "vs2019_found="
if NOT "%VS2019INSTALLDIR%" == "" (
set "vs2019_found=true"
)
if "%VisualStudioVersion%" == "16.0" (
set "vs2019_found=true"
)
if "%vs2019_found%" == "true" (
set vs=2019
echo Found: VisualStudio 16 2019
)
set "vs2022_found="
if NOT "%VS2022INSTALLDIR%" == "" (
set "vs2022_found=true"
)
if "%VisualStudioVersion%" == "17.0" (
set "vs2022_found=true"
)
if "%vs2022_found%" == "true" (
set vs=2022
echo Found: VisualStudio 17 2022
)
if "%VisualStudioVersion%" == "18.0" (
set vs=2026
set sln_extension=slnx
echo Found: VisualStudio 18 2026
)
if "!vs!" == "0" (
echo Warning: Could not find Visual Studio version in environment.
)
if NOT "!-vs!" == "0" (
echo Overriding automatically found VS version !vs! with argument !-vs!
set vs=!-vs!
) else if "!vs!" == "0" (
echo Warning: Visual Studio not found nor provided by -vs. Please ensure that Visual Studio is installed and run build.bat from a prompt with the right environment set.
echo Continuing without specifying the generator, using the CMake default.
)
goto :eof
rem ================================
rem === Check CMake installation ===
rem ================================
:check_cmake_installation
echo.
echo Checking whether CMake is installed...
set count=1
for /f "tokens=* usebackq" %%f in (`!cmake! --version`) do (
if !count! LEQ 1 (
set var!count!=%%f
set /a count=!count!+1
)
)
if "!var1:~0,13!" == "cmake version" (
echo !cmake! version: !var1:~13,20!
) else (
echo !cmake! not found, trying with default path...
set cmake="c:/Program Files/CMake/bin/cmake"
set count=1
for /f "tokens=* usebackq" %%f in (`!cmake! --version`) do (
if !count! LEQ 1 (
set var!count!=%%f
set /a count=!count!+1
)
)
if "!var1:~0,13!" == "cmake version" (
echo !cmake! version: !var1:~13,20!
) else (
echo ERROR: CMake not found.
echo Download page: https://cmake.org/download/
echo Be sure that the cmake directory is added to environment parameter PATH
goto :end
)
)
goto :eof
rem =======================
rem === Set generator ====
rem =======================
:set_generator
set generator=
if "!vs!" == "2017" (
set generator="Visual Studio 15 2017"
)
if "!vs!" == "2019" (
set generator="Visual Studio 16 2019"
)
if "!vs!" == "2022" (
set generator="Visual Studio 17 2022"
)
if "!vs!" == "2026" (
set generator="Visual Studio 18 2026"
)
set cmake_generator_arg=
if not "!generator!" == "" (
set "cmake_generator_arg=-G !generator!"
)
goto :eof
rem =======================
rem === Checks ============
rem =======================
:checks
if "!config!" == "" (
echo ERROR: config is empty.
set ERRORLEVEL=1
goto :end
)
goto :eof
rem =======================
rem === Set VS enviroment =
rem =======================
:set_vs_environment
rem # Attempt to execute vcvarsall.bat if not run from a developer command prompt
if %build% EQU 0 goto :eof
if !ERRORLEVEL! NEQ 0 goto :eof
echo.
if "!VS%vs%INSTALLDIR!" == "" (
echo Cannot set Visual Studio enviroment variables, please run build.bat from a Visual Studio Developer Command Prompt.
set ERRORLEVEL=1
goto :end
)
echo Calling vcvarsall.bat for VisualStudio %vs%...
call "!VS%vs%INSTALLDIR!\VC\Auxiliary\Build\vcvarsall.bat" amd64
rem # Execution of vcvarsall results in a jump to the C-drive. Jump back to the script directory
cd /d "%root%\"
if !ERRORLEVEL! NEQ 0 call :errorMessage
goto :eof
rem =======================
rem === CMake configure ===
rem =======================
:do_cmake
if !ERRORLEVEL! NEQ 0 goto :eof
echo.
call :create_cmake_dir build_!config!
echo Running CMake for !config!...
!cmake! -S .\src\cmake -B build_!config! !cmake_generator_arg! -T fortran=!compiler! -A x64 -D CONFIGURATION_TYPE:STRING="!config!" -D CMAKE_INSTALL_PREFIX=.\install_!config!\ 1>build_!config!\cmake_!config!.log 2>&1
if !ERRORLEVEL! NEQ 0 call :errorMessage
goto :eof
rem =======================
rem === Insert coverage ===
rem =======================
:insert_coverage
rem Insert options to implement the build objects with hooks for the code-coverage tool.
rem This code is running from within build_%~1
python %root%\src\scripts_lgpl\win64\testcoverage\addcovoptions.py %~1.%sln_extension%
rem =======================
rem === Build =============
rem =======================
:build
if %build% EQU 0 goto :eof
if !ERRORLEVEL! NEQ 0 goto :eof
echo.
echo Building !config!...
!cmake! --build build_!config! --config !build_type! 1>build_!config!\build_!config!.log 2>&1
if !ERRORLEVEL! NEQ 0 call :errorMessage
goto :eof
rem =======================
rem === Create CMake dir ==
rem =======================
:create_cmake_dir
cd /d %root%
if %keep_build% == 0 (
echo Cleaning directories %root%\build_%config% and %root%\install_%config%...
if exist "%root%\build_%config%\" rmdir /s/q "%root%\build_%config%\" > del.log 2>&1
if exist "%root%\install_%config%\" rmdir /s/q "%root%\install_%config%\" > del.log 2>&1
)
if NOT exist "%root%\build_%config%\" mkdir "%root%\build_%config%\" > del.log 2>&1
if exist "del.log" del /f/q del.log
goto :eof
rem =======================
rem === Install ===========
rem =======================
:install
if %build% EQU 0 goto :eof
if !ERRORLEVEL! NEQ 0 goto :eof
echo.
echo Installing !config!...
!cmake! --install build_%config% --config !build_type! 1>build_!config!\install_!config!.log 2>&1
if !ERRORLEVEL! NEQ 0 call :errorMessage
goto :eof
rem =======================
rem === Usage =============
rem =======================
:usage
echo.
echo Usage:
echo build.bat [OPTIONS]
echo The following actions will be executed:
echo - Create directory 'build_^<CONFIG^>', where ^<CONFIG^> can be specified by the -config option.
echo Delete it first when it already exists, unless -keep_build is specified
echo - Execute 'CMake ^<CONFIG^>' to create file '^<CONFIG^>.sln(x)' inside 'build_^<CONFIG^>'
echo.
echo [OPTIONS]: space separated list of options, sometimes followed by a value, in any order
echo.
echo -config ^<CONFIG^>:
echo all : All products that are in fm-suite and d3d4-suite combined
echo fm-suite (default) : D-Flow FM, D-WAQ, D-Waves, DIMR
echo d3d4-suite : Delft3D-FLOW, Delft3D-WAQ, Delft3D-PART, Delft3D-WAVE
echo dflowfm_interacter : D-Flow FM with Interacter
echo dflowfm : D-Flow FM without Interacter
echo dimr : DIMR
echo drr : D-RR
echo dwaq : D-WAQ
echo dwaves : D-Waves
echo flow2d3d : Delft3D-FLOW
echo swan : SWAN
echo fbc : FBC-tools
echo tools
echo tools_gpl
echo.
echo -help: Show this help page. Example: -help
echo -coverage: Instrument object files for code-coverage tool (codecov). Example: -coverage
echo -build: Run build and install steps after running cmake. Example: -build
echo -vs: desired visual studio version. Example: -vs 2019
echo -compiler: desired Intel compiler, either ifort or ifx (default). Example: -compiler ifx
echo -build_type: build optimization level. Example: -build_type Release
rem extra four spaces required for aligning Example, compensating for ^ characters:
echo -keep_build: do not delete the 'build_^<CONFIG^>' and 'install_^<CONFIG^>' folders. Example: -keep_build
echo.
echo More info : https://github.com/Deltares/Delft3D
echo.
set ERRORLEVEL=1
goto :end
rem =======================
rem === Error message =====
rem =======================
:errorMessage
echo.
echo.
echo.
echo ERROR: Please check previous error messages and the CMake output in build_%config%\cmake_%config%.log.
goto :eof
rem =======================
rem === End tag ===========
rem =======================
:end
rem # To prevent the DOS box from disappearing immediately: remove the rem on the following line
rem pause
if !ERRORLEVEL! NEQ 0 (
exit /B %~1
) else (
exit /B
)
rem =======================
rem === EOF tag ===========
rem =======================
:eof