docker_build_sdformat.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #!/bin/bash
  2. #
  3. # Copyright (c) Contributors to the Open 3D Engine Project.
  4. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. #
  6. # SPDX-License-Identifier: Apache-2.0 OR MIT
  7. #
  8. LIB_NAME=sdformat
  9. # Validate the build directory
  10. BUILD_FOLDER=${DOCKER_BUILD_PATH}
  11. if [ "${BUILD_FOLDER}" == "" ]
  12. then
  13. echo "Missing required build target folder environment"
  14. exit 1
  15. elif [ "${BUILD_FOLDER}" == "temp" ]
  16. then
  17. echo "Build target folder environment cannot be 'temp'"
  18. exit 1
  19. fi
  20. # Set the install path from the DOCKER_INSTALL_PATH argument
  21. INSTALL_FOLDER=${DOCKER_INSTALL_PATH}
  22. BUILD_TYPE=${CMAKE_BUILD_TYPE}
  23. # Stores array of each installed dependency after building locally
  24. DEP_INSTALL_PATHS=()
  25. # Get the base directory where the source file for dependencies will be fetched to
  26. GIT_DEPS_BASE=/data/workspace/deps
  27. GIT_DEPS_BUILD_ROOT=${GIT_DEPS_BASE}/build
  28. # Build the dependent tinyxml2 library
  29. DEP_NAME=tinyxml2
  30. GZ_TINYXML2_SRC_FOLDER=${GIT_DEPS_BASE}/$DEP_NAME
  31. GZ_TINYXML2_BUILD_FOLDER=${GIT_DEPS_BUILD_ROOT}/$DEP_NAME
  32. # Install the tinyxml2 library files to the local filesystem
  33. GZ_TINYXML2_INSTALL_FOLDER=${LOCAL_FILESYSTEM}/deps/install
  34. if [ -d ${GIT_DEPS_BUILD_ROOT} ]; then
  35. rm -rf ${GIT_DEPS_BUILD_ROOT}
  36. fi
  37. # Append the tinyxml2 install folder
  38. DEP_INSTALL_PATHS+=( $GZ_TINYXML2_INSTALL_FOLDER )
  39. pushd $GZ_TINYXML2_SRC_FOLDER
  40. echo "Configuring $DEP_NAME"
  41. CMD="cmake -B ${GZ_TINYXML2_BUILD_FOLDER} -S. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${GZ_TINYXML2_INSTALL_FOLDER} -DCMAKE_POSITION_INDEPENDENT_CODE=ON"
  42. echo $CMD
  43. eval $CMD
  44. if [ $? -ne 0 ]
  45. then
  46. echo "Error configuring $DEP_NAME"
  47. exit 1
  48. fi
  49. echo "Building and installing $DEP_NAME"
  50. CMD="cmake --build $GZ_TINYXML2_BUILD_FOLDER --target install --config ${BUILD_TYPE}"
  51. echo $CMD
  52. eval $CMD
  53. if [ $? -ne 0 ]
  54. then
  55. echo "Error building $DEP_NAME"
  56. exit 1
  57. fi
  58. popd
  59. # Build the dependent gz-cmake library
  60. DEP_NAME=gz-cmake
  61. GZ_CMAKE_SRC_FOLDER=${GIT_DEPS_BASE}/$DEP_NAME
  62. GZ_CMAKE_BUILD_FOLDER=${GIT_DEPS_BUILD_ROOT}/$DEP_NAME
  63. # Install gz-cmake to the mounted local filesystem
  64. GZ_CMAKE_INSTALL_FOLDER=${LOCAL_FILESYSTEM}/deps/install
  65. # Append the gz-cmake install folder
  66. DEP_INSTALL_PATHS+=( $GZ_CMAKE_INSTALL_FOLDER )
  67. pushd $GZ_CMAKE_SRC_FOLDER
  68. echo "Configuring $DEP_NAME"
  69. CMD="cmake -B ${GZ_CMAKE_BUILD_FOLDER} -S. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${GZ_CMAKE_INSTALL_FOLDER} -DBUILD_TESTING=OFF"
  70. echo $CMD
  71. eval $CMD
  72. if [ $? -ne 0 ]
  73. then
  74. echo "Error configuring $DEP_NAME"
  75. exit 1
  76. fi
  77. echo "Building and installing $DEP_NAME"
  78. CMD="cmake --build $GZ_CMAKE_BUILD_FOLDER --target install --config ${BUILD_TYPE}"
  79. echo $CMD
  80. eval $CMD
  81. if [ $? -ne 0 ]
  82. then
  83. echo "Error building $DEP_NAME"
  84. exit 1
  85. fi
  86. popd
  87. # Build the dependent gz-utils library
  88. # NOTE: This must be done after gz-cmake it depends on that library
  89. DEP_NAME=gz-utils
  90. GZ_UTILS_SRC_FOLDER=${GIT_DEPS_BASE}/$DEP_NAME
  91. GZ_UTILS_BUILD_FOLDER=${GIT_DEPS_BUILD_ROOT}/$DEP_NAME
  92. # install gz-utils to the local filesystem
  93. GZ_UTILS_INSTALL_FOLDER=${LOCAL_FILESYSTEM}/deps/install
  94. # Append the gz-utils install folder
  95. DEP_INSTALL_PATHS+=( $GZ_UTILS_INSTALL_FOLDER )
  96. pushd $GZ_UTILS_SRC_FOLDER
  97. echo "Configuring $DEP_NAME"
  98. CMD=(cmake -B ${GZ_UTILS_BUILD_FOLDER} -S. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${GZ_UTILS_INSTALL_FOLDER} -DBUILD_TESTING=OFF)
  99. CMD+=(-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE -DCMAKE_INSTALL_RPATH=\$ORIGIN)
  100. echo "${CMD[@]}"
  101. "${CMD[@]}"
  102. if [ $? -ne 0 ]
  103. then
  104. echo "Error configuring $DEP_NAME"
  105. exit 1
  106. fi
  107. echo "Building and installing $DEP_NAME"
  108. CMD="cmake --build $GZ_UTILS_BUILD_FOLDER --target install --config ${BUILD_TYPE}"
  109. echo $CMD
  110. eval $CMD
  111. if [ $? -ne 0 ]
  112. then
  113. echo "Error building $DEP_NAME"
  114. exit 1
  115. fi
  116. popd
  117. # Build the dependent gz-math library
  118. # NOTE: This must be done after gz-cmake and gz-utils as it depends on those libraries
  119. DEP_NAME=gz-math
  120. GZ_MATH_SRC_FOLDER=${GIT_DEPS_BASE}/$DEP_NAME
  121. GZ_MATH_BUILD_FOLDER=${GIT_DEPS_BUILD_ROOT}/$DEP_NAME
  122. GZ_MATH_INSTALL_FOLDER=${LOCAL_FILESYSTEM}/deps/install
  123. # Append the gz-math install folder
  124. DEP_INSTALL_PATHS+=( $GZ_MATH_INSTALL_FOLDER )
  125. pushd $GZ_MATH_SRC_FOLDER
  126. echo "Configuring $DEP_NAME"
  127. CMD=(cmake -B ${GZ_MATH_BUILD_FOLDER} -S. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${GZ_MATH_INSTALL_FOLDER} -DSKIP_SWIG=ON -DSKIP_PYBIND11=ON -DBUILD_TESTING=OFF)
  128. CMD+=(-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE -DCMAKE_INSTALL_RPATH=\$ORIGIN)
  129. echo "${CMD[@]}"
  130. "${CMD[@]}"
  131. if [ $? -ne 0 ]
  132. then
  133. echo "Error configuring $DEP_NAME"
  134. exit 1
  135. fi
  136. echo "Building and installing $DEP_NAME"
  137. CMD="cmake --build $GZ_MATH_BUILD_FOLDER --target install --config ${BUILD_TYPE}"
  138. echo $CMD
  139. eval $CMD
  140. if [ $? -ne 0 ]
  141. then
  142. echo "Error building $DEP_NAME"
  143. exit 1
  144. fi
  145. popd
  146. # Now build the SDF library
  147. pushd ${LOCAL_FILESYSTEM}/src
  148. # Convert the dependent install path array in bash to a cmake list
  149. CMAKE_PREFIX_PATH=${DEP_INSTALL_PATHS[0]}
  150. # splace the first element of the array and then join with the semicolon CMake delimiter
  151. SPLICED_INSTALL_PATHS=(${DEP_INSTALL_PATHS[@]:1})
  152. CMAKE_PREFIX_PATH+=$(printf ";%s" "${SPLICED_INSTALL_PATHS[@]}")
  153. # Supply the CMAKE_PREFIX_PATH to allow the dependent libraries of gz-cmake to be located
  154. # Remove the build folder if it exist
  155. if [ -d ${BUILD_FOLDER} ]; then
  156. rm -rf ${BUILD_FOLDER}
  157. fi
  158. # When building sdformat, set it to use its internal copy of urdfdom for parsing urdf files
  159. # instead of relying on an externally-installed package.
  160. # This keeps the dependencies self-contained.
  161. echo "Configuring ${LIB_NAME}"
  162. CMD=(cmake -B ${BUILD_FOLDER} -S. -DUSE_INTERNAL_URDF=ON -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${INSTALL_FOLDER} -DCMAKE_PREFIX_PATH=\"${CMAKE_PREFIX_PATH}\")
  163. # Update the RPATH to $ORIGIN to allow sdformat library to find the dependent libgz-utils/gz-math.so files on Linux
  164. CMD+=(-DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE -DCMAKE_INSTALL_RPATH=\$ORIGIN)
  165. echo "${CMD[@]}"
  166. "${CMD[@]}"
  167. if [ $? -ne 0 ]
  168. then
  169. echo "Error configuring ${LIB_NAME}"
  170. exit 1
  171. fi
  172. if [ -d ${INSTALL_FOLDER} ]; then
  173. echo "Removing artifacts from existing ${INSTALL_FOLDER}"
  174. rm -rf ${INSTALL_FOLDER}
  175. fi
  176. echo "Building and installing ${LIB_NAME} to ${INSTALL_FOLDER}"
  177. CMD="cmake --build ${BUILD_FOLDER} --target install --config ${BUILD_TYPE}"
  178. echo $CMD
  179. eval $CMD
  180. if [ $? -ne 0 ]
  181. then
  182. echo "Error building ${LIB_NAME}"
  183. exit 1
  184. fi
  185. popd
  186. # Finally detach the debug symbols to a separate dbg file for sdformat
  187. echo "Detaching debug symbols for ${LIB_NAME}"
  188. SDFORMAT_VERSION=13.5.0
  189. LIB_FILENAME="lib${LIB_NAME}13.so.${SDFORMAT_VERSION}"
  190. LIB_DIRECTORY="${INSTALL_FOLDER}/lib"
  191. LIB_PATH="${LIB_DIRECTORY}/${LIB_FILENAME}"
  192. CMD="objcopy --only-keep-debug ${LIB_PATH} ${LIB_PATH}.dbg"
  193. echo $CMD
  194. eval $CMD
  195. if [ $? -ne 0 ]
  196. then
  197. echo "Error detaching debug symbols for ${LIB_NAME}"
  198. exit 1
  199. fi
  200. CMD="strip --strip-debug ${LIB_PATH}"
  201. echo $CMD
  202. eval $CMD
  203. if [ $? -ne 0 ]
  204. then
  205. echo "Error stripping debugging symbols for ${LIB_NAME}"
  206. exit 1
  207. fi
  208. # Change directory to the folder containing the sdformat library
  209. pushd ${LIB_DIRECTORY}
  210. CMD="objcopy --add-gnu-debuglink=${LIB_FILENAME}.dbg ${LIB_PATH}"
  211. echo $CMD
  212. eval $CMD
  213. if [ $? -ne 0 ]
  214. then
  215. echo "Error adding debug symbol link for ${LIB_NAME}"
  216. exit 1
  217. fi
  218. popd
  219. # Detach the debug symbols to a separate dbg file for gz-math
  220. DEP_NAME=gz-math7
  221. echo "Detaching debug symbols for ${DEP_NAME}"
  222. GZ_MATH_VERSION=7.2.0
  223. LIB_FILENAME="lib${DEP_NAME}.so.${GZ_MATH_VERSION}"
  224. LIB_DIRECTORY="${GZ_MATH_INSTALL_FOLDER}/lib"
  225. LIB_PATH="${LIB_DIRECTORY}/${LIB_FILENAME}"
  226. CMD="objcopy --only-keep-debug ${LIB_PATH} ${LIB_PATH}.dbg"
  227. echo $CMD
  228. eval $CMD
  229. if [ $? -ne 0 ]
  230. then
  231. echo "Error detaching debug symbols for ${DEP_NAME}"
  232. exit 1
  233. fi
  234. CMD="strip --strip-debug ${LIB_PATH}"
  235. echo $CMD
  236. eval $CMD
  237. if [ $? -ne 0 ]
  238. then
  239. echo "Error stripping debugging symbols for ${DEP_NAME}"
  240. exit 1
  241. fi
  242. # Change directory to the folder containing the gz-math library
  243. pushd ${LIB_DIRECTORY}
  244. CMD="objcopy --add-gnu-debuglink=${LIB_FILENAME}.dbg ${LIB_PATH}"
  245. echo $CMD
  246. eval $CMD
  247. if [ $? -ne 0 ]
  248. then
  249. echo "Error adding debug symbol link for ${DEP_NAME}"
  250. exit 1
  251. fi
  252. popd
  253. # Detach the debug symbols to a separate dbg file for gz-utils
  254. DEP_NAME=gz-utils2
  255. echo "Detaching debug symbols for ${DEP_NAME}"
  256. GZ_UTILS_VERSION=2.0.0
  257. LIB_FILENAME="lib${DEP_NAME}.so.${GZ_UTILS_VERSION}"
  258. LIB_DIRECTORY="${GZ_UTILS_INSTALL_FOLDER}/lib"
  259. LIB_PATH="${LIB_DIRECTORY}/${LIB_FILENAME}"
  260. CMD="objcopy --only-keep-debug ${LIB_PATH} ${LIB_PATH}.dbg"
  261. echo $CMD
  262. eval $CMD
  263. if [ $? -ne 0 ]
  264. then
  265. echo "Error detaching debug symbols for ${DEP_NAME}"
  266. exit 1
  267. fi
  268. CMD="strip --strip-debug ${LIB_PATH}"
  269. echo $CMD
  270. eval $CMD
  271. if [ $? -ne 0 ]
  272. then
  273. echo "Error stripping debugging symbols for ${DEP_NAME}"
  274. exit 1
  275. fi
  276. # Change directory to the folder containing the gz-utils library
  277. pushd ${LIB_DIRECTORY}
  278. CMD="objcopy --add-gnu-debuglink=${LIB_FILENAME}.dbg ${LIB_PATH}"
  279. echo $CMD
  280. eval $CMD
  281. if [ $? -ne 0 ]
  282. then
  283. echo "Error adding debug symbol link for ${DEP_NAME}"
  284. exit 1
  285. fi
  286. popd
  287. exit 0