container-functions-source.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -----------------------------------------------------------------------------
  2. # Helper script used in the second edition of the xPack build
  3. # scripts. As the name implies, it should contain only functions and
  4. # should be included with 'source' by the container build scripts.
  5. # -----------------------------------------------------------------------------
  6. function start_timer()
  7. {
  8. CONTAINER_BEGIN_SECOND=$(date +%s)
  9. echo
  10. echo "Container script \"$0\" started at $(date)."
  11. }
  12. function stop_timer()
  13. {
  14. local end_second=$(date +%s)
  15. echo
  16. echo "Container script \"$0\" completed at $(date)."
  17. local delta_seconds=$((end_second-CONTAINER_BEGIN_SECOND))
  18. if [ ${delta_seconds} -lt 100 ]
  19. then
  20. echo "Duration: ${delta_seconds} seconds."
  21. else
  22. local delta_minutes=$(((delta_seconds+30)/60))
  23. echo "Duration: ${delta_minutes} minutes."
  24. fi
  25. }
  26. # -----------------------------------------------------------------------------
  27. function detect_container()
  28. {
  29. echo
  30. uname -a
  31. CONTAINER_DISTRO_NAME=""
  32. CONTAINER_UNAME="$(uname)"
  33. CONTAINER_NODE_PLATFORM=""
  34. CONTAINER_NODE_ARCH=""
  35. if [ "${CONTAINER_UNAME}" == "Darwin" ]
  36. then
  37. CONTAINER_BITS="64"
  38. CONTAINER_MACHINE="x86_64"
  39. CONTAINER_NODE_PLATFORM="darwin"
  40. CONTAINER_NODE_ARCH="x64"
  41. CONTAINER_DISTRO_NAME=Darwin
  42. CONTAINER_DISTRO_LC_NAME=darwin
  43. elif [ "${CONTAINER_UNAME}" == "Linux" ]
  44. then
  45. # ----- Determine distribution name and word size -----
  46. set +e
  47. CONTAINER_DISTRO_NAME=$(lsb_release -si)
  48. set -e
  49. CONTAINER_NODE_PLATFORM="linux"
  50. CONTAINER_NODE_ARCH="x64"
  51. if [ -z "${CONTAINER_DISTRO_NAME}" ]
  52. then
  53. echo "Please install the lsb core package and rerun."
  54. CONTAINER_DISTRO_NAME="Linux"
  55. fi
  56. CONTAINER_MACHINE="$(uname -m)"
  57. if [ "${CONTAINER_MACHINE}" == "x86_64" ]
  58. then
  59. CONTAINER_BITS="64"
  60. CONTAINER_NODE_ARCH="x64"
  61. elif [ "${CONTAINER_MACHINE}" == "i686" ]
  62. then
  63. CONTAINER_BITS="32"
  64. CONTAINER_NODE_ARCH="x32"
  65. else
  66. echo "Unknown uname -m ${CONTAINER_MACHINE}"
  67. exit 1
  68. fi
  69. CONTAINER_DISTRO_LC_NAME=$(echo ${CONTAINER_DISTRO_NAME} | tr "[:upper:]" "[:lower:]")
  70. else
  71. echo "Unknown uname ${CONTAINER_UNAME}"
  72. exit 1
  73. fi
  74. echo
  75. echo "Container script running on ${CONTAINER_DISTRO_NAME} ${CONTAINER_BITS}-bit."
  76. echo "User $(whoami), in '${HOME}'"
  77. HAS_WINPTHREAD=${HAS_WINPTHREAD:-""}
  78. CONTAINER_ROOT_UMASK=${CONTAINER_ROOT_UMASK:-"000"}
  79. if [ -f "/.dockerenv" -a "$(whoami)" == "root" ]
  80. then
  81. umask ${CONTAINER_ROOT_UMASK}
  82. fi
  83. }
  84. # -----------------------------------------------------------------------------
  85. function fix_ownership()
  86. {
  87. if [ -f "/.dockerenv" -a "${CONTAINER_RUN_AS_ROOT}" == "y" ]
  88. then
  89. (
  90. xbb_activate
  91. # Set the owner of the folder and files created by the docker CentOS
  92. # container to match the user running the build script on the host.
  93. # When running on linux host, these folders and their content remain
  94. # owned by root if this is not done. However, on macOS
  95. # the owner used by Docker is the same as the macOS user, so an
  96. # ownership change is not realy necessary.
  97. echo
  98. echo "Changing ownership to non-root GNU/Linux user..."
  99. if [ -d "${BUILD_FOLDER_PATH}" ]
  100. then
  101. chown -R ${USER_ID}:${GROUP_ID} "${BUILD_FOLDER_PATH}"
  102. fi
  103. if [ -d "${INSTALL_FOLDER_PATH}" ]
  104. then
  105. chown -R ${USER_ID}:${GROUP_ID} "${INSTALL_FOLDER_PATH}"
  106. fi
  107. chown -R ${USER_ID}:${GROUP_ID} "${DEPLOY_FOLDER_PATH}"
  108. )
  109. fi
  110. }
  111. # -----------------------------------------------------------------------------
  112. # Hack to make pdftex ignore errors.
  113. # Used for newlib manuals, which issue some errors, but the
  114. # pdf file is generated anyway.
  115. function hack_pdfetex()
  116. {
  117. local bin=$(which pdfetex)
  118. local hacked_pdfetex="hack/pdfetex"
  119. mkdir -p "$(dirname "${hacked_pdfetex}")"
  120. rm -rf "${hacked_pdfetex}"
  121. echo '#!/usr/bin/env bash' >"${hacked_pdfetex}"
  122. echo 'set -x' >>"${hacked_pdfetex}"
  123. echo 'set +e' >>"${hacked_pdfetex}"
  124. echo "${bin}" '$@' >>"${hacked_pdfetex}"
  125. echo 'set -e' >>"${hacked_pdfetex}"
  126. echo 'true' >>"${hacked_pdfetex}"
  127. chmod +x "${hacked_pdfetex}"
  128. export PATH="$(pwd)/$(dirname "${hacked_pdfetex}"):${PATH}"
  129. }