0032739: Configuration, scripts - handle ABI list within macOS building script adm...
[occt.git] / adm / scripts / macos_build.sh
1 #!/bin/bash
2
3 # Auxiliary script for semi-automated building of OCCT for macOS platform.
4 # macos_custom.sh should be configured with paths to CMake and other 3rd-parties.
5 # FreeType should be specified as mandatory dependency.
6
7 aScriptDir=${BASH_SOURCE%/*}
8 if [ -d "$aScriptDir" ]; then cd "$aScriptDir"; fi
9 aScriptDir="$PWD"
10
11 aCasSrc=${aScriptDir}/../..
12 aNbJobs="$(getconf _NPROCESSORS_ONLN)"
13
14 export aBuildRoot=work
15
16 # paths to pre-built 3rd-parties
17 export aFreeType=
18 export aFreeImage=
19 export aRapidJson=
20 export aDraco=
21
22 # build stages to perform
23 export isStatic=0
24 export toCMake=1
25 export toClean=1
26 export toMake=1
27 export toInstall=1
28 export toPack=0
29 export toPackFat=0
30 export toDebug=0
31
32 export BUILD_ModelingData=ON
33 export BUILD_ModelingAlgorithms=ON
34 export BUILD_Visualization=ON
35 export BUILD_ApplicationFramework=ON
36 export BUILD_DataExchange=ON
37 export BUILD_Draw=ON
38
39 export USE_FREETYPE=ON
40 export USE_FREEIMAGE=ON
41 export USE_RAPIDJSON=OFF
42 export USE_DRACO=OFF
43
44 export MACOSX_DEPLOYMENT_TARGET=10.10
45 #export anAbiList="arm64 x86_64"
46 export anAbiList="x86_64"
47 aPlatform="macos"
48
49 if [[ -f "${aScriptDir}/macos_custom.sh" ]]; then
50   source "${aScriptDir}/macos_custom.sh"
51 fi
52
53 anOcctVerSuffix=`grep -e "#define OCC_VERSION_DEVELOPMENT" "$aCasSrc/src/Standard/Standard_Version.hxx" | awk '{print $3}' | xargs`
54 anOcctVersion=`grep -e "#define OCC_VERSION_COMPLETE" "$aCasSrc/src/Standard/Standard_Version.hxx" | awk '{print $3}' | xargs`
55 aGitBranch=`git symbolic-ref --short HEAD`
56
57 YEAR=$(date +"%Y")
58 MONTH=$(date +"%m")
59 DAY=$(date +"%d")
60 aRevision=-${YEAR}-${MONTH}-${DAY}
61 #aRevision=-${aGitBranch}
62
63 set -o pipefail
64
65 aBuildType="Release"
66 aBuildTypePrefix=
67 if [[ $toDebug == 1 ]]; then
68   aBuildType="Debug"
69   aBuildTypePrefix="-debug"
70 fi
71 aLibType="Shared"
72 aLibExt="dylib"
73 if [[ $isStatic == 1 ]]; then
74   aLibType="Static"
75   aLibExt="a"
76 fi
77
78 function buildArch {
79   anAbi=$1
80
81   aPlatformAndCompiler=${aPlatform}-${anAbi}${aBuildTypePrefix}-clang
82
83   aWorkDir="${aCasSrc}/${aBuildRoot}/${aPlatformAndCompiler}-make"
84   aDestDir="${aCasSrc}/${aBuildRoot}/${aPlatformAndCompiler}"
85   aLogFile="${aCasSrc}/${aBuildRoot}/build-${aPlatformAndCompiler}.log"
86
87   if [[ $toCMake == 1 ]] && [[ $toClean == 1 ]]; then
88     rm -r -f "$aWorkDir"
89     rm -r -f "$aDestDir"
90   fi
91   mkdir -p "$aWorkDir"
92   mkdir -p "$aDestDir"
93   rm -f "$aLogFile"
94
95   # include some information about OCCT into archive
96   echo \<pre\>> "${aWorkDir}/VERSION.html"
97   git status >> "${aWorkDir}/VERSION.html"
98   git log -n 100 >> "${aWorkDir}/VERSION.html"
99   echo \</pre\>>> "${aWorkDir}/VERSION.html"
100
101   pushd "$aWorkDir"
102
103   aTimeZERO=$SECONDS
104
105   function logDuration {
106     if [[ $1 == 1 ]]; then
107       aDur=$(($4 - $3))
108       echo $2 time: $aDur sec>> "$aLogFile"
109     fi
110   }
111
112   # (re)generate Make files
113   if [[ $toCMake == 1 ]]; then
114     echo Configuring OCCT for macOS...
115     cmake -G "Unix Makefiles" \
116   -D CMAKE_OSX_ARCHITECTURES:STRING="$anAbi" \
117   -D CMAKE_BUILD_TYPE:STRING="$aBuildType" \
118   -D BUILD_LIBRARY_TYPE:STRING="$aLibType" \
119   -D INSTALL_DIR:PATH="$aDestDir" \
120   -D INSTALL_DIR_INCLUDE:STRING="inc" \
121   -D INSTALL_DIR_LIB:STRING="lib" \
122   -D INSTALL_DIR_RESOURCE:STRING="src" \
123   -D INSTALL_NAME_DIR:STRING="@executable_path/../Frameworks" \
124   -D USE_FREETYPE:BOOL="$USE_FREETYPE" \
125   -D 3RDPARTY_FREETYPE_DIR:PATH="$aFreeType" \
126   -D 3RDPARTY_FREETYPE_INCLUDE_DIR_freetype2:FILEPATH="$aFreeType/include" \
127   -D 3RDPARTY_FREETYPE_INCLUDE_DIR_ft2build:FILEPATH="$aFreeType/include" \
128   -D 3RDPARTY_FREETYPE_LIBRARY_DIR:PATH="$aFreeType/lib" \
129   -D 3RDPARTY_FREETYPE_LIBRARY:FILEPATH="$aFreeType/lib/libfreetype.dylib" \
130   -D USE_RAPIDJSON:BOOL="$USE_RAPIDJSON" \
131   -D 3RDPARTY_RAPIDJSON_DIR:PATH="$aRapidJson" \
132   -D 3RDPARTY_RAPIDJSON_INCLUDE_DIR:PATH="$aRapidJson/include" \
133   -D USE_DRACO:BOOL="$USE_DRACO" \
134   -D 3RDPARTY_DRACO_DIR:PATH="$aDraco" \
135   -D 3RDPARTY_DRACO_INCLUDE_DIR:FILEPATH="$aDraco/include" \
136   -D 3RDPARTY_DRACO_LIBRARY_DIR:PATH="$aDraco/lib" \
137   -D USE_FREEIMAGE:BOOL="$USE_FREEIMAGE" \
138   -D 3RDPARTY_FREEIMAGE_DIR:PATH="$aFreeImage" \
139   -D 3RDPARTY_FREEIMAGE_INCLUDE_DIR:FILEPATH="$aFreeImage/include" \
140   -D 3RDPARTY_FREEIMAGE_LIBRARY_DIR:PATH="$aFreeImage/lib" \
141   -D 3RDPARTY_FREEIMAGE_LIBRARY:FILEPATH="$aFreeImage/lib/libfreeimage.a" \
142   -D BUILD_MODULE_FoundationClasses:BOOL="ON" \
143   -D BUILD_MODULE_ModelingData:BOOL="${BUILD_ModelingData}" \
144   -D BUILD_MODULE_ModelingAlgorithms:BOOL="${BUILD_ModelingAlgorithms}" \
145   -D BUILD_MODULE_Visualization:BOOL="${BUILD_Visualization}" \
146   -D BUILD_MODULE_ApplicationFramework:BOOL="${BUILD_ApplicationFramework}" \
147   -D BUILD_MODULE_DataExchange:BOOL="${BUILD_DataExchange}" \
148   -D BUILD_MODULE_Draw:BOOL="${BUILD_Draw}" \
149   -D BUILD_DOC_Overview:BOOL="OFF" \
150     "$aCasSrc" 2>&1 | tee -a "$aLogFile"
151     aResult=$?; if [[ $aResult != 0 ]]; then exit $aResult; fi
152   fi
153   aTimeGEN=$SECONDS
154   logDuration $toCMake "Generation" $aTimeZERO $aTimeGEN
155
156   # clean up from previous build
157   if [[ $toClean == 1 ]]; then
158     make clean
159   fi
160
161   # build the project
162   if [[ $toMake == 1 ]]; then
163     echo Building OCCT...
164     make -j $aNbJobs 2>&1 | tee -a "$aLogFile"
165     aResult=$?; if [[ $aResult != 0 ]]; then exit $aResult; fi
166   fi
167   aTimeBUILD=$SECONDS
168   logDuration $toMake "Building"       $aTimeGEN  $aTimeBUILD
169   logDuration $toMake "Total building" $aTimeZERO $aTimeBUILD
170
171   # install the project
172   if [[ $toInstall == 1 ]]; then
173     echo Installing OCCT into $aDestDir...
174     make install 2>&1 | tee -a "$aLogFile"
175     cp -f "$aWorkDir/VERSION.html" "$aDestDir/VERSION.html"
176     echo Platform: macOS ABI: ${anAbi} Build: ${aBuildType} MACOSX_DEPLOYMENT_TARGET: ${MACOSX_DEPLOYMENT_TARGET} > "$aDestDir/build_target.txt"
177   fi
178   aTimeINSTALL=$SECONDS
179   logDuration $toInstall "Install" $aTimeBUILD $aTimeINSTALL
180
181   # create an archive
182   if [[ $toPack == 1 ]]; then
183     anArchName=occt-${anOcctVersion}${anOcctVerSuffix}${aRevision}-${aPlatformAndCompiler}.tar.bz2
184     echo Creating an archive ${aCasSrc}/${aBuildRoot}/${anArchName}...
185     rm ${aDestDir}/../${anArchName} &>/dev/null
186     pushd "$aDestDir"
187     tar -jcf ${aDestDir}/../${anArchName} *
188     popd
189   fi
190   aTimePACK=$SECONDS
191   logDuration $toPack "Packing archive" $aTimeINSTALL $aTimePACK
192
193   # finished
194   DURATION=$(($aTimePACK - $aTimeZERO))
195   echo Total time: $DURATION sec
196   logDuration 1 "Total" $aTimeZERO $aTimePACK
197
198   popd
199 }
200
201 for anArchIter in $anAbiList
202 do
203   echo Platform: macOS ABI: ${anArchIter} Build: ${aBuildType}
204   buildArch $anArchIter
205 done
206
207 # create a FAT archive
208 if [[ $toPackFat == 1 ]]; then
209   aSuffixFat=${aPlatform}${aBuildTypePrefix}-clang
210   aFatDir="${aCasSrc}/${aBuildRoot}/${aSuffixFat}"
211
212   # merge per-arch builds into fat builds
213   hasPlatform=0
214   for anArchIter in $anAbiList
215   do
216     aSuffixThin=${aPlatform}-${anArchIter}${aBuildTypePrefix}-clang
217     anArchDir="${aCasSrc}/${aBuildRoot}/${aSuffixThin}"
218     if [[ $hasPlatform == 0 ]]; then
219       hasPlatform=1
220       echo Packing FAT archive
221       rm -r -f "$aFatDir"
222       mkdir -p "$aFatDir"
223       if [[ $isStatic == 1 ]]; then
224         rsync -r -l --exclude '*.a' "$anArchDir/" "$aFatDir"
225       else
226         rsync -r -l --exclude '*.dylib' "$anArchDir/" "$aFatDir"
227       fi
228       rm -f "$aFatDir/build_target.txt"
229
230       if [[ -L "$anArchDir/bin/DRAWEXE" ]]; then
231         aDrawExe=$(readlink "$anArchDir/bin/DRAWEXE")
232         rm $aFatDir/bin/$aDrawExe
233         lipo "$anArchDir/bin/$aDrawExe" -output "$aFatDir/bin/$aDrawExe" -create
234       fi
235
236       for aLibIter in $anArchDir/lib/*.$aLibExt; do
237         aLibName=`basename $aLibIter`
238         if [[ -L "$anArchDir/lib/$aLibName" ]]; then
239           cp -a "$anArchDir/lib/$aLibName" "$aFatDir/lib/"
240         else
241           lipo "$anArchDir/lib/$aLibName" -output "$aFatDir/lib/$aLibName" -create
242         fi
243       done
244     else
245       if [[ -L "$anArchDir/bin/DRAWEXE" ]]; then
246         aDrawExe=$(readlink "$anArchDir/bin/DRAWEXE")
247         lipo "$aFatDir/bin/$aDrawExe" "$anArchDir/bin/$aDrawExe" -output "$aFatDir/bin/$aDrawExe" -create
248       fi
249
250       for aLibIter in $aFatDir/lib/*.$aLibExt; do
251         aLibName=`basename $aLibIter`
252         if [[ ! -L "$anArchDir/lib/$aLibName" ]]; then
253           lipo "$aFatDir/lib/$aLibName" "$anArchDir/lib/$aLibName" -output "$aFatDir/lib/$aLibName" -create
254           #lipo -info "$aFatDir/lib/$aLibName"
255         fi
256       done
257     fi
258     cat "$anArchDir/build_target.txt" >> "$aFatDir/build_target.txt"
259   done
260
261   # create an archive
262   anArchName=occt-${anOcctVersion}${anOcctVerSuffix}${aRevision}-${aSuffixFat}.tar.bz2
263   echo Creating an archive ${aCasSrc}/${aBuildRoot}/${anArchName}...
264   rm ${aFatDir}/../${anArchName} &>/dev/null
265   pushd "$aFatDir"
266   tar -jcf ${aFatDir}/../${anArchName} *
267   popd
268 fi