-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.bat
184 lines (155 loc) · 6.18 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
:: https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133
@echo off
setlocal DisableDelayedExpansion
call :setESC
:: batch comments ref
:: https://stackoverflow.com/questions/12407800/which-comment-style-should-i-use-in-batch-files
:: https://stackoverflow.com/questions/64186507/question-about-comments-in-batch-bat-files-and-speed/68436626#68436626
echo.%~0 %* >&2
:: usage
:: build path/to/file -file -test
:: build path/to/package -test
:: positional args
set "-build_setting=%~1" & shift
set "-source=%~1" & shift
if "%-source%" == "" echo missing source package/file & goto :eof
if not exist "%-source%" echo source '%-source%' does not exist! & goto :eof
:parse_args
:: variable cli_match cur_arg
call :get_arg_flag "-file" "-file" "%~1" && shift && goto :parse_args
call :get_arg_flag "-dry" "-dry" "%~1" && shift && goto :parse_args
call :get_arg_flag "-run" "-run" "%~1" && shift && goto :parse_args
call :get_arg_flag "-test" "-test" "%~1" && shift && goto :parse_args
@REM call :get_arg_with_value "-build_setting" "-build_setting" "%~1" "%~2" "build" && shift && shift && goto :parse_args
@REM echo -file: %-file%
@REM echo -dry: %-dry%
@REM echo -run: %-run%
@REM echo -test: %-test%
:: requires DisableDelayedExpansion
call :define_macro_execute
setlocal EnableDelayedExpansion
:: ============================================================================
:: main script starts here
if "%-file%"=="1" (
for %%A in ("%-source%\.") do set "out_name=%%~nA"
set "-source=%-source% -file"
) else (
set "out_name=%-source%"
)
if "%-test%"=="1" set "out_name=%out_name%_test"
:: -vet
:: -vet-unused
:: -vet-unused-variables
:: -vet-unused-imports
:: -vet-shadowing
:: -vet-using-stmt
:: TODO: use in git push hook to prevent 'using' getting committed to the repo
:: -vet-using-stmt & -vet-using-param
:: 'using' is considered bad practice outside of immediate refactoring.
:: odin strip-semicolon src/%file_name%.odin -file
:: -vet-style
:: -vet-semicolon
:: -vet-cast
:: -vet-tabs
:: -strict-style
:: like -vet-style -vet-semicolon
:: + Errs when the attached-brace style in not adhered to (also known as 1TBS).
:: + Errs when 'case' labels are not in the same column as the associated 'switch' token.
:: -vet-style -vet-semicolon
set "style_settings=-vet -strict-style -vet-using-param -vet-cast -vet-tabs"
:: -o:none
:: -o:minimal (default)
:: -o:size
:: -o:speed
:: -o:aggressive
:: -sanitize:address
if "%-build_setting%"=="sanitize" set "compiler_settings=-o:minimal -debug -sanitize:address"
if "%-build_setting%"=="debug" set "compiler_settings=-o:minimal -debug"
if "%-build_setting%"=="release" set "compiler_settings=-o:speed -debug"
if "%-build_setting%"=="fast" set "compiler_settings=-o:aggressive -disable-assert"
:: if "%-build_setting%"=="test" set "compiler_settings=-o:aggressive -disable-assert"
if "%-build_setting%"=="asm" set "compiler_settings=-o:speed -build-mode:asm -keep-temp-files"
set "-command=build"
if "%-test%"=="1" set "-command=test" && set "compiler_settings=%compiler_settings% -define:ODIN_TEST_THREADS=1"
if "%-run%"=="1" set "-command=run"
set "out_dir=bin"
mkdir %out_dir% 2>nul
%execute% odin %-command% %-source% -out=%out_dir%/%out_name%.exe %compiler_settings% -subsystem:console %style_settings%
if %errorlevel%==0 (
start /b cmd /c call "misc\spplayer.bat" "C:\Windows\Media\tada.wav"
) else (
start /b cmd /c call "misc\spplayer.bat" "C:\Windows\Media\chord.wav"
)
:: start rundll32 user32.dll,MessageBeep MB_OK
:: start rundll32 user32.dll,MessageBeep MB_ERROR
:: BEL character Alt+007 in cmd line
:: echo
:: echo lol | clip
exit /b %errorlevel%
:: end
:: ============================================================================
:: ============================================================================
:: Sets a variable from a cli argument with value
:: 1 variable name
:: 2 parameter name
:: 3 argument name
:: 4 argument value
:: 5 default value
:: if "%~4"=="" (
:: REM unset the variable if value is not provided
:: set "%~1="
:: exit /B 0
:: )
:get_arg_with_value
if "%~3"=="%~2" (
set "%~1=%~4"
exit /b 0
)
if not defined %~1 set "%~1=%~5"
exit /b 1
:: ============================================================================
:: Sets a variable from a cli flag argument
:: 1 cli argument name
:: 2 variable name
:: 3 current argument name
:get_arg_flag
if "%~3"=="%~2" (
set "%~1=1"
exit /b 0
)
if not defined %~1 set "%~1=0"
exit /b 1
:: ============================================================================
:: https://en.wikipedia.org/wiki/ANSI_escape_code#Select_Graphic_Rendition_parameters
:: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
:: %ESC%[38;2;<r>;<g>;<b>m TEXT %ESC%[0m
:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set ESC=%%b
exit /B 0
)
:: ============================================================================
:: refs:
:: https://stackoverflow.com/questions/58732724/turn-on-echo-for-a-single-command-in-a-batch-file
:: https://ss64.com/nt/syntax-macros.html
:: https://www.dostips.com/forum/viewtopic.php?f=3&t=2518
:: requires setlocal DisableDelayedExpansion for definition
:define_macro_execute
:: NOTE: Post by Dave Benham (https://www.dostips.com/forum/viewtopic.php?p=58686#p58686)
:: ^^^(<CR>)<LF>(<CR>)<LF>
:: After parsing the definition, the actual stored value is ^<LF>
:: When included at the end of a macro definition line, the ^<LF> is in front of the (<CR>)<LF> at the end of
:: the source line, which is parsed as a single <LF> that gets inserted into the definition, and
:: the next line is still appended to the definition.
(set \n=^^^
)
set ^"execute=for %%# in (1 2) do if %%#==2 ( %\n%
setlocal EnableDelayedExpansion %\n%
for /F "tokens=1,*" %%1 in ("!time: =0! !argv!") do ( %\n%
@REM echo [%ESC%[32m%%1%ESC%[0m] %%2 %\n%
echo [%ESC%[38;2;0;167;204m%%1%ESC%[0m] %%2 ^>^&2%\n%
if "%-dry%"=="0" %%2 %\n%
endlocal ^& endlocal %\n%
) %\n%
) else setlocal DisableDelayedExpansion ^& set argv="
exit /b