-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.bat
194 lines (167 loc) · 4.76 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
@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
if %_HELP%==1 (
call :help
exit /b !_EXITCODE!
)
if %_CLEAN%==1 (
call :clean
if not !_EXITCODE!==0 goto end
)
if %_COMPILE%==1 (
call :compile
if not !_EXITCODE!==0 goto end
)
if %_RUN%==1 (
call :run
if not !_EXITCODE!==0 goto end
)
goto end
@rem #########################################################################
@rem ## Subroutines
@rem output parameters: _PROJ_NAME, _PROJ_CONFIG, _TARGET_DIR
:env
set _BASENAME=%~n0
set "_ROOT_DIR=%~dp0"
@rem ANSI colors in standard Windows 10 shell
@rem see https://gist.github.com/mlocati/#file-win10colors-cmd
set _DEBUG_LABEL=[46m%_DEBUG_LABEL%[0m
set _ERROR_LABEL=[91mError[0m:
set _WARNING_LABEL=[93mWarning[0m:
set "_SOURCE_DIR=%_ROOT_DIR%src"
set "_TARGET_DIR=%_ROOT_DIR%build"
set _PROJ_NAME=add
if not exist "%LLVM_HOME%\bin\clang.exe" (
echo %_ERROR_LABEL% LLVM installation directory not found 1>&2
set _EXITCODE=1
goto :eof
)
set "_CLANG_CMD=%LLVM_HOME%\bin\clang.exe"
goto :eof
@rem input parameter: %*
@rem output parameters: _CLEAN, _COMPILE, _RUN, _DEBUG, _TOOLSET, _VERBOSE
:args
set _CLEAN=0
set _COMPILE=0
set _RUN=0
set _HELP=0
set _TOOLSET=clang
set _VERBOSE=0
set __N=0
:args_loop
set "__ARG=%~1"
if not defined __ARG (
if !__N!==0 set _HELP=1
goto args_done
)
if "%__ARG:~0,1%"=="-" (
@rem option
if "%__ARG%"=="-debug" ( set _DEBUG=1
) else if "%__ARG%"=="-help" ( set _HELP=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 _CLEAN=1
) else if "%__ARG%"=="compile" ( set _COMPILE=1
) else if "%__ARG%"=="help" ( set _HELP=1
) else if "%__ARG%"=="run" ( set _COMPILE=1& set _RUN=1
) 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 _STDOUT_REDIRECT=1^>NUL
if %_DEBUG%==1 set _STDOUT_REDIRECT=1^>CON
if %_DEBUG%==1 (
echo %_DEBUG_LABEL% Options : _TOOLSET=%_TOOLSET% _VERBOSE=%_VERBOSE% 1>&2
echo %_DEBUG_LABEL% Subcommands: _CLEAN=%_CLEAN% _COMPILE=%_COMPILE% _RUN=%_RUN% 1>&2
echo %_DEBUG_LABEL% Variables : "LLVM_HOME=%LLVM_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "MSVS_CMAKE_HOME=%MSVS_CMAKE_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "MSVS_HOME=%MSVS_HOME%" 1>&2
)
goto :eof
:help
echo Usage: %_BASENAME% { ^<option^> ^| ^<subcommand^> }
echo.
echo Options:
echo -debug show commands executed by this script
echo -verbose display progress messages
echo.
echo Subcommands:
echo clean delete generated files
echo compile generate executable
echo help display this help message
echo run run generated executable
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
:compile
setlocal
if not exist "%_TARGET_DIR%" mkdir "%_TARGET_DIR%"
if %_TOOLSET%==clang ( set __TOOLSET_NAME=Clang/CMake
) else if %_TOOLSET%==gcc ( set __TOOLSET_NAME=GCC/CMake
) else ( set __TOOLSET_NAME=MSVC/MSBuild
)
if %_VERBOSE%==1 echo Toolset: %__TOOLSET_NAME%, Project: %_PROJ_NAME% 1>&2
call :compile_%_TOOLSET%
endlocal
goto :eof
:compile_clang
set "__LL_FILE=%_TARGET_DIR%\add.ll"
set __CLANG_OPTS=-emit-llvm -c -S "%_SOURCE_DIR%\add.c" -o "%__LL_FILE%"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_CLANG_CMD%" %__CLANG_OPTS% 1>&2
) else if %_VERBOSE%==1 ( echo Generate IR code to file "!__LL_FILE:%_ROOT_DIR%=!" 1>&2
)
call "%_CLANG_CMD%" %__CLANG_OPTS% %_STDOUT_REDIRECT%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to generate IR code to file "!__LL_FILE:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:run
set "__LL_FILE=%_TARGET_DIR%\add.ll"
type "%__LL_FILE%"
goto :eof
@rem #########################################################################
@rem ## Cleanups
:end
if %_DEBUG%==1 echo %_DEBUG_LABEL% _EXITCODE=%_EXITCODE% 1>&2
exit /b %_EXITCODE%
endlocal