container-functions-source.sh 4.8 KB

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