0032308: Configuration - make Xlib dependency optional
[occt.git] / adm / scripts / wasm_build.bat
1 @echo OFF
2
3 rem Auxiliary script for semi-automated building of OCCT for WASM platform.
4 rem wasm_custom.bat should be configured with paths to CMake, 3rd-parties and Emscripten SDK.
5 rem FreeType should be specified as mandatory dependency.
6
7 set "aCasSrc=%~dp0..\.."
8 set "aBuildRoot=%aCasSrc%\work"
9
10 set aNbJobs=%NUMBER_OF_PROCESSORS%
11
12 rem Paths to 3rd-party tools and libraries
13 set "aCmakeBin="
14 set "aFreeType="
15 set "aRapidJson="
16 set "aTcl="
17
18 rem Build stages to perform
19 set "toCMake=1"
20 set "toClean=0"
21 set "toMake=1"
22 set "toInstall=1"
23 set "toPack=0"
24 set "toDebug=0"
25 set "toBuildSample=0"
26 set "sourceMapBase="
27
28 rem OCCT Modules to build
29 set "BUILD_ModelingData=ON"
30 set "BUILD_ModelingAlgorithms=ON"
31 set "BUILD_Visualization=ON"
32 set "BUILD_ApplicationFramework=ON"
33 set "BUILD_DataExchange=ON"
34 set "BUILD_Draw=OFF"
35
36 rem Optional 3rd-party libraries to enable
37 set "USE_RAPIDJSON=OFF"
38
39 rem Archive tool
40 set "THE_7Z_PARAMS=-t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on"
41 set "THE_7Z_PATH=%ProgramW6432%\7-Zip\7z.exe"
42
43 rem Configuration file
44 if exist "%~dp0wasm_custom.bat" call "%~dp0wasm_custom.bat"
45
46 call "%EMSDK_ROOT%\emsdk_env.bat"
47 set "aToolchain=%EMSDK%/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
48 if not ["%aCmakeBin%"] == [""] ( set "PATH=%aCmakeBin%;%PATH%" )
49
50 set "anOcctVerSuffix="
51 set "anOcctVersion=0.0.0"
52 set "aGitBranch="
53 for /f tokens^=2^ delims^=^" %%i in ('findstr /b /c:"#define OCC_VERSION_DEVELOPMENT" "%aCasSrc%\src\Standard\Standard_Version.hxx"') do ( set "anOcctVerSuffix=%%i" )
54 for /f tokens^=2^ delims^=^" %%i in ('findstr /b /c:"#define OCC_VERSION_COMPLETE" "%aCasSrc%\src\Standard\Standard_Version.hxx"') do ( set "anOcctVersion=%%i" )
55 for /f %%i in ('git symbolic-ref --short HEAD') do ( set "aGitBranch=%%i" )
56
57 set "aBuildType=Release"
58 set "aBuildTypePrefix="
59 if ["%toDebug%"] == ["1"] (
60   set "aBuildType=Debug"
61   set "aBuildTypePrefix=-debug"
62 )
63
64 call :cmakeGenerate
65 if errorlevel 1 (
66   if not ["%1"] == ["-nopause"] (
67     pause
68   )
69   exit /B 1
70   goto :eof
71 )
72
73 for /F "skip=1 delims=" %%F in ('
74   wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE
75 ') do (
76   for /F "tokens=1-3" %%L in ("%%F") do (
77     set DAY00=0%%L
78     set MONTH00=0%%M
79     set YEAR=%%N
80   )
81 )
82 set DAY00=%DAY00:~-2%
83 set MONTH00=%MONTH00:~-2%
84 set "aRevision=-%YEAR%-%MONTH00%-%DAY00%"
85 rem set "aRevision=-%aGitBranch%"
86 set "anArchName=occt-%anOcctVersion%%anOcctVerSuffix%%aRevision%-wasm32%aBuildTypePrefix%"
87 set "aTarget=%aBuildRoot%\%anArchName%"
88 if ["%toPack%"] == ["1"] (
89   echo Creating archive %anArchName%.7z
90   rmdir /S /Q "%aTarget%"
91   if not exist "%aTarget%" ( mkdir "%aTarget%" )
92   if exist "%aBuildRoot%/%anArchName%.7z" del "%aBuildRoot%/%anArchName%.7z"
93   xcopy /S /Y "%aDestDir%\*" "%aTarget%\"
94
95   if ["%toBuildSample%"] == ["1"] (
96     if not exist "%aTarget%\sample" ( mkdir "%aTarget%\sample" )
97     xcopy /S /Y "%aDestDirSmpl%\*" "%aTarget%\sample\"
98   )
99
100   "%THE_7Z_PATH%" a -r %THE_7Z_PARAMS% "%aBuildRoot%/%anArchName%.7z" "%aTarget%"
101 )
102 if not ["%1"] == ["-nopause"] (
103   pause
104 )
105
106 goto :eof
107
108 :cmakeGenerate
109 set "aPlatformAndCompiler=wasm32%aBuildTypePrefix%"
110 set "aWorkDir=%aBuildRoot%\occt-%aPlatformAndCompiler%-make"
111 set "aDestDir=%aBuildRoot%\occt-%aPlatformAndCompiler%"
112 set "aLogFile=%aBuildRoot%\occt-%aPlatformAndCompiler%-build.log"
113 if ["%toCMake%"] == ["1"] (
114   if ["%toClean%"] == ["1"] (
115     rmdir /S /Q %aWorkDir%"
116     rmdir /S /Q %aDestDir%"
117   )
118 )
119 if not exist "%aWorkDir%" ( mkdir "%aWorkDir%" )
120 if     exist "%aLogFile%" ( del   "%aLogFile%" )
121
122 set "aSrcRootSmpl=%aCasSrc%\samples\webgl"
123 set "aWorkDirSmpl=%aBuildRoot%\sample-%aPlatformAndCompiler%-make"
124 set "aDestDirSmpl=%aBuildRoot%\sample-%aPlatformAndCompiler%"
125 set "aLogFileSmpl=%aBuildRoot%\sample-%aPlatformAndCompiler%-build.log"
126 if ["%toBuildSample%"] == ["1"] (
127   if ["%toCMake%"] == ["1"] (
128     if ["%toClean%"] == ["1"] (
129       rmdir /S /Q %aWorkDirSmpl%"
130       rmdir /S /Q %aDestDirSmpl%"
131     )
132   )
133   if not exist "%aWorkDirSmpl%" ( mkdir "%aWorkDirSmpl%" )
134   if     exist "%aLogFileSmpl%" ( del   "%aLogFileSmpl%" )
135 )
136
137 rem include some information about OCCT into archive
138 echo ^<pre^>> "%aWorkDir%\VERSION.html"
139 git status >> "%aWorkDir%\VERSION.html"
140 git log -n 100 >> "%aWorkDir%\VERSION.html"
141 echo ^</pre^>>> "%aWorkDir%\VERSION.html"
142
143 echo Start building OCCT for %aPlatformAndCompiler%
144 echo Start building OCCT for %aPlatformAndCompiler%>> %aLogFile%
145
146 pushd "%aWorkDir%"
147
148 set "aTimeZERO=%TIME%"
149 if ["%toCMake%"] == ["1"] (
150   echo Configuring OCCT for WASM...
151   cmake -G "MinGW Makefiles" ^
152  -D CMAKE_TOOLCHAIN_FILE:FILEPATH="%aToolchain%" ^
153  -D CMAKE_BUILD_TYPE:STRING="%aBuildType%" ^
154  -D BUILD_LIBRARY_TYPE:STRING="Static" ^
155  -D INSTALL_DIR:PATH="%aDestDir%" ^
156  -D INSTALL_DIR_INCLUDE:STRING="inc" ^
157  -D INSTALL_DIR_RESOURCE:STRING="src" ^
158  -D 3RDPARTY_FREETYPE_DIR:PATH="%aFreeType%" ^
159  -D 3RDPARTY_FREETYPE_INCLUDE_DIR_freetype2:FILEPATH="%aFreeType%/include" ^
160  -D 3RDPARTY_FREETYPE_INCLUDE_DIR_ft2build:FILEPATH="%aFreeType%/include" ^
161  -D BUILD_MODULE_FoundationClasses:BOOL="ON" ^
162  -D BUILD_MODULE_ModelingData:BOOL="%BUILD_ModelingData%" ^
163  -D BUILD_MODULE_ModelingAlgorithms:BOOL="%BUILD_ModelingAlgorithms%" ^
164  -D BUILD_MODULE_Visualization:BOOL="%BUILD_Visualization%" ^
165  -D BUILD_MODULE_ApplicationFramework:BOOL="%BUILD_ApplicationFramework%" ^
166  -D BUILD_MODULE_DataExchange:BOOL="%BUILD_DataExchange%" ^
167  -D BUILD_MODULE_Draw:BOOL="%BUILD_Draw%" ^
168  -D BUILD_DOC_Overview:BOOL="OFF" ^
169  -D USE_RAPIDJSON:BOOL="%USE_RAPIDJSON%" ^
170  -D 3RDPARTY_RAPIDJSON_DIR:PATH="%aRapidJson%" ^
171  -D 3RDPARTY_RAPIDJSON_INCLUDE_DIR:PATH="%aRapidJson%/include" ^
172  -D 3RDPARTY_TCL_DIR:PATH="%aTcl%" ^
173  -D 3RDPARTY_TCL_INCLUDE_DIR:PATH="%aTcl%/include" ^
174  -D 3RDPARTY_TCL_LIBRARY_DIR:PATH="%aTcl%/lib" ^
175  -D 3RDPARTY_TCL_LIBRARY:FILEPATH="%aTcl%/lib/libtcl.a" ^
176  "%aCasSrc%"
177
178   if errorlevel 1 (
179     popd
180     exit /B 1
181     goto :eof
182   )
183 )
184 set "aTimeGEN=%TIME%"
185 call :computeDuration %aTimeZERO% %aTimeGEN%
186 if ["%toCMake%"] == ["1"] (
187   echo Generation time: %DURATION%
188   echo Generation time: %DURATION%>> "%aLogFile%"
189 )
190
191 if ["%toClean%"] == ["1"] (
192   mingw32-make clean
193 )
194
195 if ["%toMake%"] == ["1"] (
196   echo Building...
197   mingw32-make -j %aNbJobs% 2>> "%aLogFile%"
198   if errorlevel 1 (
199     type "%aLogFile%"
200     popd
201     exit /B 1
202     goto :eof
203   )
204   type "%aLogFile%"
205 )
206 set "aTimeBUILD=%TIME%"
207 call :computeDuration %aTimeGEN% %aTimeBUILD%
208 if ["%toMake%"] == ["1"] (
209   echo Building time: %DURATION%
210   echo Building time: %DURATION%>> "%aLogFile%"
211 )
212 call :computeDuration %aTimeZERO% %aTimeBUILD%
213 if ["%toMake%"] == ["1"] (
214   echo Total building time: %DURATION%
215   echo Total building time: %DURATION%>> "%aLogFile%"
216 )
217
218 if ["%toInstall%"] == ["1"] (
219   echo Installing into %aDestDir%...
220   mingw32-make install 2>> "%aLogFile%"
221   copy /Y "%aWorkDir%\VERSION.html" "%aDestDir%\VERSION.html"
222 )
223 set "aTimeINSTALL=%TIME%"
224 call :computeDuration "%aTimeBUILD%" "%aTimeINSTALL%"
225 if ["%toInstall%"] == ["1"] (
226   echo Install time: %DURATION%
227   echo Install time: %DURATION%>> "%aLogFile%"
228 )
229
230 call :computeDuration "%aTimeZERO%" "%aTimeINSTALL%"
231 echo Total time: %DURATION%
232 echo Total time: %DURATION%>> "%aLogFile%"
233
234 popd
235 if ["%toBuildSample%"] == ["0"] (
236   goto :eof
237 )
238
239 pushd "%aWorkDirSmpl%"
240
241 if ["%toCMake%"] == ["1"] (
242   cmake -G "MinGW Makefiles" ^
243  -D CMAKE_TOOLCHAIN_FILE:FILEPATH="%aToolchain%" ^
244  -D CMAKE_BUILD_TYPE:STRING="%aBuildType%" ^
245  -D CMAKE_INSTALL_PREFIX:PATH="%aDestDirSmpl%" ^
246  -D SOURCE_MAP_BASE:STRING="%sourceMapBase%" ^
247  -D OpenCASCADE_DIR:PATH="%aDestDir%/lib/cmake/opencascade" ^
248  -D freetype_DIR:PATH="%aFreeType%/lib/cmake/freetype" ^
249  "%aSrcRootSmpl%"
250
251   if errorlevel 1 (
252     popd
253     pause
254     exit /B
255     goto :eof
256   )
257 )
258 if ["%toClean%"] == ["1"] (
259   mingw32-make clean
260 )
261
262 if ["%toMake%"] == ["1"] (
263   echo Building...
264   mingw32-make -j %aNbJobs% 2>> "%aLogFileSmpl%"
265   if errorlevel 1 (
266     type "%aLogFileSmpl%"
267     popd
268     pause
269     exit /B
270     goto :eof
271   )
272   type "%aLogFileSmpl%"
273 )
274
275 if ["%toInstall%"] == ["1"] (
276   mingw32-make install 2>> "%aLogFileSmpl%"
277   if errorlevel 1 (
278     type "%aLogFileSmpl%"
279     popd
280     pause
281     exit /B
282     goto :eof
283   )
284 )
285 popd
286
287 goto :eof
288
289 :computeDuration
290 set "aTimeFrom=%~1"
291 set "aTimeEnd=%~2"
292 rem handle time before 10AM (win10 - remove empty space at the beginning)
293 if "%aTimeFrom:~0,1%"==" " set "aTimeFrom=%aTimeFrom:~1%"
294 if  "%aTimeEnd:~0,1%"==" " set  "aTimeEnd=%aTimeEnd:~1%"
295 rem handle time before 10AM (win7 - add 0 at the beginning)
296 if "%aTimeFrom:~1,1%"==":" set "aTimeFrom=0%aTimeFrom%"
297 if  "%aTimeEnd:~1,1%"==":" set  "aTimeEnd=0%aTimeEnd%"
298 rem convert hours:minutes:seconds:ms into duration
299 set /A aTimeFrom=(1%aTimeFrom:~0,2%-100)*360000 + (1%aTimeFrom:~3,2%-100)*6000 + (1%aTimeFrom:~6,2%-100)*100 + (1%aTimeFrom:~9,2%-100)
300 set /A  aTimeEnd= (1%aTimeEnd:~0,2%-100)*360000 +  (1%aTimeEnd:~3,2%-100)*6000 +  (1%aTimeEnd:~6,2%-100)*100 +  (1%aTimeEnd:~9,2%-100)
301 set /A aDurTotalSec=%aTimeEnd%-%aTimeFrom%
302 if %aTimeEnd% LSS %aTimeFrom% set set /A aDurTotalSec=%aTimeFrom%-%aTimeEnd%
303 set /A aDurHH=%aDurTotalSec% / 360000
304 set /A aDurMM=(%aDurTotalSec% - %aDurHH%*360000) / 6000
305 set /A aDurSS=(%aDurTotalSec% - %aDurHH%*360000 - %aDurMM%*6000) / 100
306 if %aDurHH% LSS 10 set aDurHH=0%aDurHH%
307 if %aDurMM% LSS 10 set aDurMM=0%aDurMM%
308 if %aDurSS% LSS 10 set aDurSS=0%aDurSS%
309
310 set "DURATION=%aDurHH%:%aDurMM%:%aDurSS%"
311 goto :eof