0032315: Configuration, CMake - fix building Draw Harness plugins as static libraries
[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  -D USE_TK:BOOL="OFF" ^
177  "%aCasSrc%"
178
179   if errorlevel 1 (
180     popd
181     exit /B 1
182     goto :eof
183   )
184 )
185 set "aTimeGEN=%TIME%"
186 call :computeDuration %aTimeZERO% %aTimeGEN%
187 if ["%toCMake%"] == ["1"] (
188   echo Generation time: %DURATION%
189   echo Generation time: %DURATION%>> "%aLogFile%"
190 )
191
192 if ["%toClean%"] == ["1"] (
193   mingw32-make clean
194 )
195
196 if ["%toMake%"] == ["1"] (
197   echo Building...
198   mingw32-make -j %aNbJobs% 2>> "%aLogFile%"
199   if errorlevel 1 (
200     type "%aLogFile%"
201     popd
202     exit /B 1
203     goto :eof
204   )
205   type "%aLogFile%"
206 )
207 set "aTimeBUILD=%TIME%"
208 call :computeDuration %aTimeGEN% %aTimeBUILD%
209 if ["%toMake%"] == ["1"] (
210   echo Building time: %DURATION%
211   echo Building time: %DURATION%>> "%aLogFile%"
212 )
213 call :computeDuration %aTimeZERO% %aTimeBUILD%
214 if ["%toMake%"] == ["1"] (
215   echo Total building time: %DURATION%
216   echo Total building time: %DURATION%>> "%aLogFile%"
217 )
218
219 if ["%toInstall%"] == ["1"] (
220   echo Installing into %aDestDir%...
221   mingw32-make install 2>> "%aLogFile%"
222   copy /Y "%aWorkDir%\VERSION.html" "%aDestDir%\VERSION.html"
223 )
224 set "aTimeINSTALL=%TIME%"
225 call :computeDuration "%aTimeBUILD%" "%aTimeINSTALL%"
226 if ["%toInstall%"] == ["1"] (
227   echo Install time: %DURATION%
228   echo Install time: %DURATION%>> "%aLogFile%"
229 )
230
231 call :computeDuration "%aTimeZERO%" "%aTimeINSTALL%"
232 echo Total time: %DURATION%
233 echo Total time: %DURATION%>> "%aLogFile%"
234
235 popd
236 if ["%toBuildSample%"] == ["0"] (
237   goto :eof
238 )
239
240 pushd "%aWorkDirSmpl%"
241
242 if ["%toCMake%"] == ["1"] (
243   cmake -G "MinGW Makefiles" ^
244  -D CMAKE_TOOLCHAIN_FILE:FILEPATH="%aToolchain%" ^
245  -D CMAKE_BUILD_TYPE:STRING="%aBuildType%" ^
246  -D CMAKE_INSTALL_PREFIX:PATH="%aDestDirSmpl%" ^
247  -D SOURCE_MAP_BASE:STRING="%sourceMapBase%" ^
248  -D OpenCASCADE_DIR:PATH="%aDestDir%/lib/cmake/opencascade" ^
249  -D freetype_DIR:PATH="%aFreeType%/lib/cmake/freetype" ^
250  "%aSrcRootSmpl%"
251
252   if errorlevel 1 (
253     popd
254     pause
255     exit /B
256     goto :eof
257   )
258 )
259 if ["%toClean%"] == ["1"] (
260   mingw32-make clean
261 )
262
263 if ["%toMake%"] == ["1"] (
264   echo Building...
265   mingw32-make -j %aNbJobs% 2>> "%aLogFileSmpl%"
266   if errorlevel 1 (
267     type "%aLogFileSmpl%"
268     popd
269     pause
270     exit /B
271     goto :eof
272   )
273   type "%aLogFileSmpl%"
274 )
275
276 if ["%toInstall%"] == ["1"] (
277   mingw32-make install 2>> "%aLogFileSmpl%"
278   if errorlevel 1 (
279     type "%aLogFileSmpl%"
280     popd
281     pause
282     exit /B
283     goto :eof
284   )
285 )
286 popd
287
288 goto :eof
289
290 :computeDuration
291 set "aTimeFrom=%~1"
292 set "aTimeEnd=%~2"
293 rem handle time before 10AM (win10 - remove empty space at the beginning)
294 if "%aTimeFrom:~0,1%"==" " set "aTimeFrom=%aTimeFrom:~1%"
295 if  "%aTimeEnd:~0,1%"==" " set  "aTimeEnd=%aTimeEnd:~1%"
296 rem handle time before 10AM (win7 - add 0 at the beginning)
297 if "%aTimeFrom:~1,1%"==":" set "aTimeFrom=0%aTimeFrom%"
298 if  "%aTimeEnd:~1,1%"==":" set  "aTimeEnd=0%aTimeEnd%"
299 rem convert hours:minutes:seconds:ms into duration
300 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)
301 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)
302 set /A aDurTotalSec=%aTimeEnd%-%aTimeFrom%
303 if %aTimeEnd% LSS %aTimeFrom% set set /A aDurTotalSec=%aTimeFrom%-%aTimeEnd%
304 set /A aDurHH=%aDurTotalSec% / 360000
305 set /A aDurMM=(%aDurTotalSec% - %aDurHH%*360000) / 6000
306 set /A aDurSS=(%aDurTotalSec% - %aDurHH%*360000 - %aDurMM%*6000) / 100
307 if %aDurHH% LSS 10 set aDurHH=0%aDurHH%
308 if %aDurMM% LSS 10 set aDurMM=0%aDurMM%
309 if %aDurSS% LSS 10 set aDurSS=0%aDurSS%
310
311 set "DURATION=%aDurHH%:%aDurMM%:%aDurSS%"
312 goto :eof