host-functions-source.sh 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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 host build scripts.
  5. # -----------------------------------------------------------------------------
  6. function host_get_current_date()
  7. {
  8. # Use the UTC date as version in the name of the distribution file.
  9. DISTRIBUTION_FILE_DATE=${DISTRIBUTION_FILE_DATE:-$(date -u +%Y%m%d-%H%M)}
  10. # Leave a track of the start date, in case of resume needed.
  11. mkdir -p "${HOST_WORK_FOLDER_PATH}"
  12. touch "${HOST_WORK_FOLDER_PATH}/${DISTRIBUTION_FILE_DATE}"
  13. echo
  14. echo "DISTRIBUTION_FILE_DATE=\"${DISTRIBUTION_FILE_DATE}\""
  15. }
  16. function host_start_timer()
  17. {
  18. HOST_BEGIN_SECOND=$(date +%s)
  19. echo
  20. echo "Script \"$0\" started at $(date)."
  21. }
  22. function host_stop_timer()
  23. {
  24. local host_end_second=$(date +%s)
  25. echo
  26. echo "Script \"$0\" completed at $(date)."
  27. local delta_seconds=$((host_end_second-HOST_BEGIN_SECOND))
  28. if [ ${delta_seconds} -lt 100 ]
  29. then
  30. echo "Duration: ${delta_seconds} seconds."
  31. else
  32. local delta_minutes=$(((delta_seconds+30)/60))
  33. echo "Duration: ${delta_minutes} minutes."
  34. fi
  35. }
  36. function host_notify_completed()
  37. {
  38. if [ "${HOST_UNAME}" == "Darwin" ]
  39. then
  40. say "Wake up, the build completed successfully"
  41. fi
  42. }
  43. # -----------------------------------------------------------------------------
  44. # Detect the machine the build runs on.
  45. function host_detect()
  46. {
  47. echo
  48. uname -a
  49. HOST_UNAME="$(uname)"
  50. HOST_MACHINE="$(uname -m)"
  51. HOST_DISTRO_NAME="?" # Linux distribution name (Ubuntu|CentOS|...)
  52. HOST_DISTRO_LC_NAME="?" # Same, in lower case.
  53. HOST_NODE_ARCH="?" # Node.js process.arch (x32|x64|arm|arm64)
  54. HOST_NODE_PLATFORM="?" # Node.js process.platform (darwin|linux|win32)
  55. if [ "${HOST_UNAME}" == "Darwin" ]
  56. then
  57. # uname -p -> i386
  58. # uname -m -> x86_64
  59. HOST_BITS="64"
  60. HOST_DISTRO_NAME=Darwin
  61. HOST_DISTRO_LC_NAME=darwin
  62. HOST_NODE_ARCH="x64" # For now.
  63. HOST_NODE_PLATFORM="darwin"
  64. elif [ "${HOST_UNAME}" == "Linux" ]
  65. then
  66. # ----- Determine distribution name and word size -----
  67. # uname -p -> x86_64|i686 (unknown in recent versions, use -m)
  68. # uname -m -> x86_64|i686
  69. if [ "${HOST_MACHINE}" == "x86_64" ]
  70. then
  71. HOST_BITS="64"
  72. HOST_NODE_ARCH="x64"
  73. elif [ "${HOST_MACHINE}" == "i686" ]
  74. then
  75. HOST_BITS="32"
  76. HOST_NODE_ARCH="x32"
  77. else
  78. echo "Unknown uname -m ${HOST_MACHINE}"
  79. exit 1
  80. fi
  81. HOST_NODE_PLATFORM="linux"
  82. local lsb_path=$(which lsb_release)
  83. if [ -z "${lsb_path}" ]
  84. then
  85. echo "Please install the lsb core package and rerun."
  86. exit 1
  87. fi
  88. HOST_DISTRO_NAME=$(lsb_release -si)
  89. HOST_DISTRO_LC_NAME=$(echo ${HOST_DISTRO_NAME} | tr "[:upper:]" "[:lower:]")
  90. else
  91. echo "Unsupported uname ${HOST_UNAME}"
  92. exit 1
  93. fi
  94. echo
  95. echo "Running on ${HOST_DISTRO_NAME} ${HOST_BITS}-bit."
  96. USER_ID=$(id -u)
  97. USER_NAME="$(id -u -n)"
  98. GROUP_ID=$(id -g)
  99. GROUP_NAME="$(id -g -n)"
  100. TARGET_ARCH="${HOST_NODE_ARCH}"
  101. TARGET_PLATFORM="${HOST_NODE_PLATFORM}"
  102. IS_NATIVE=""
  103. # Redefine it to "y" to run as root inside the container.
  104. CONTAINER_RUN_AS_ROOT=${CONTAINER_RUN_AS_ROOT:-""}
  105. HAS_WINPTHREAD=${HAS_WINPTHREAD:-""}
  106. }
  107. # -----------------------------------------------------------------------------
  108. function host_prepare_cache()
  109. {
  110. # The folder that caches all downloads is in Work, for easy access.
  111. HOST_CACHE_FOLDER_PATH=${HOST_CACHE_FOLDER_PATH:-"${HOME}/Work/cache"}
  112. CONTAINER_CACHE_FOLDER_PATH="/Host${HOST_CACHE_FOLDER_PATH}"
  113. mkdir -p "${HOST_CACHE_FOLDER_PATH}"
  114. }
  115. function host_options()
  116. {
  117. local help_message="$1"
  118. shift
  119. ACTION=""
  120. DO_BUILD_SOURCES=""
  121. DO_BUILD_WIN32=""
  122. DO_BUILD_WIN64=""
  123. DO_BUILD_LINUX32=""
  124. DO_BUILD_LINUX64=""
  125. DO_BUILD_OSX=""
  126. ENV_FILE=""
  127. argc=$#
  128. declare -a argv
  129. argv=( $@ )
  130. if [ ! -z "${DEBUG}" ]
  131. then
  132. echo ${argv[@]-}
  133. fi
  134. i=0
  135. # Must be declared by the caller.
  136. # declare -a rest
  137. # Identify some of the options. The rest are collected and passed
  138. # to the container script.
  139. while [ $i -lt $argc ]
  140. do
  141. arg="${argv[$i]}"
  142. case "${arg}" in
  143. clean|cleanlibs|cleanall|preload-images)
  144. ACTION="${arg}"
  145. ;;
  146. --win32|--windows32)
  147. DO_BUILD_WIN32="y"
  148. ;;
  149. --win64|--windows64)
  150. DO_BUILD_WIN64="y"
  151. ;;
  152. --linux32)
  153. DO_BUILD_LINUX32="y"
  154. ;;
  155. --linux64)
  156. DO_BUILD_LINUX64="y"
  157. ;;
  158. --osx)
  159. DO_BUILD_OSX="y"
  160. ;;
  161. --sources)
  162. DO_BUILD_SOURCES="y"
  163. ;;
  164. --all)
  165. DO_BUILD_WIN32="y"
  166. DO_BUILD_WIN64="y"
  167. DO_BUILD_LINUX32="y"
  168. DO_BUILD_LINUX64="y"
  169. DO_BUILD_SOURCES="y"
  170. if [ "$(uname)" == "Darwin" ]
  171. then
  172. DO_BUILD_OSX="y"
  173. fi
  174. ;;
  175. --env-file)
  176. ((++i))
  177. ENV_FILE="${argv[$i]}"
  178. if [ ! -f "${ENV_FILE}" ];
  179. then
  180. echo "The specified environment file \"${ENV_FILE}\" does not exist, exiting..."
  181. exit 1
  182. fi
  183. ;;
  184. # Deprecated.
  185. --date)
  186. ((++i))
  187. DISTRIBUTION_FILE_DATE="${argv[$i]}"
  188. ;;
  189. --help)
  190. echo "Usage:"
  191. # Some of the options are processed by the container script.
  192. echo "${help_message}"
  193. echo
  194. exit 1
  195. ;;
  196. *)
  197. # Collect all other in an array. Append to the end.
  198. # Will be later processed by the container script.
  199. set +u
  200. rest[${#rest[*]}]="$arg"
  201. set -u
  202. ;;
  203. esac
  204. ((++i))
  205. done
  206. DO_BUILD_ANY="${DO_BUILD_OSX}${DO_BUILD_LINUX64}${DO_BUILD_WIN64}${DO_BUILD_LINUX32}${DO_BUILD_WIN32}${DO_BUILD_SOURCES}"
  207. # The ${rest[@]} options will be passed to the inner script.
  208. if [ ! -z "${DEBUG}" ]
  209. then
  210. echo ${rest[@]-}
  211. fi
  212. }
  213. function host_options_windows()
  214. {
  215. local help_message="$1"
  216. shift
  217. ACTION=""
  218. DO_BUILD_WIN32=""
  219. DO_BUILD_WIN64=""
  220. # Kept, since they are used in various common functions.
  221. DO_BUILD_LINUX32=""
  222. DO_BUILD_LINUX64=""
  223. DO_BUILD_OSX=""
  224. ENV_FILE=""
  225. argc=$#
  226. declare -a argv
  227. argv=( $@ )
  228. if [ ! -z "${DEBUG}" ]
  229. then
  230. echo ${argv[@]-}
  231. fi
  232. i=0
  233. # Must be declared by the caller.
  234. # declare -a rest
  235. # Identify some of the options. The rest are collected and passed
  236. # to the container script.
  237. while [ $i -lt $argc ]
  238. do
  239. arg="${argv[$i]}"
  240. case "${arg}" in
  241. clean|cleanlibs|cleanall|preload-images)
  242. ACTION="${arg}"
  243. ;;
  244. --win32|--windows32)
  245. DO_BUILD_WIN32="y"
  246. ;;
  247. --win64|--windows64)
  248. DO_BUILD_WIN64="y"
  249. ;;
  250. --all)
  251. DO_BUILD_WIN32="y"
  252. DO_BUILD_WIN64="y"
  253. ;;
  254. --env-file)
  255. ((++i))
  256. ENV_FILE="${argv[$i]}"
  257. if [ ! -f "${ENV_FILE}" ];
  258. then
  259. echo "The specified environment file \"${ENV_FILE}\" does not exist, exiting..."
  260. exit 1
  261. fi
  262. ;;
  263. # Deprecated.
  264. --date)
  265. ((++i))
  266. DISTRIBUTION_FILE_DATE="${argv[$i]}"
  267. ;;
  268. --help)
  269. echo "Usage:"
  270. # Some of the options are processed by the container script.
  271. echo "${help_message}"
  272. echo
  273. exit 1
  274. ;;
  275. *)
  276. # Collect all other in an array. Append to the end.
  277. # Will be later processed by the container script.
  278. set +u
  279. rest[${#rest[*]}]="$arg"
  280. set -u
  281. ;;
  282. esac
  283. ((++i))
  284. done
  285. DO_BUILD_ANY="${DO_BUILD_WIN64}${DO_BUILD_WIN32}"
  286. # The ${rest[@]} options will be passed to the inner script.
  287. if [ ! -z "${DEBUG}" ]
  288. then
  289. echo ${rest[@]-}
  290. fi
  291. }
  292. function host_native_options()
  293. {
  294. local help_message="$1"
  295. shift
  296. ACTION=""
  297. DO_BUILD_WIN=""
  298. IS_DEBUG=""
  299. IS_DEVELOP=""
  300. WITH_STRIP=""
  301. IS_NATIVE="y"
  302. JOBS="1"
  303. while [ $# -gt 0 ]
  304. do
  305. case "$1" in
  306. clean|cleanlibs|cleanall)
  307. ACTION="$1"
  308. ;;
  309. --win|--windows)
  310. DO_BUILD_WIN="y"
  311. ;;
  312. --debug)
  313. IS_DEBUG="y"
  314. ;;
  315. --develop)
  316. IS_DEVELOP="y"
  317. ;;
  318. --jobs)
  319. shift
  320. JOBS=$1
  321. ;;
  322. --help)
  323. echo "Build a local/native ${DISTRO_UC_NAME} ${APP_UC_NAME}."
  324. echo "Usage:"
  325. # Some of the options are processed by the container script.
  326. echo "${help_message}"
  327. echo
  328. exit 0
  329. ;;
  330. *)
  331. echo "Unknown action/option $1"
  332. exit 1
  333. ;;
  334. esac
  335. shift
  336. done
  337. if [ "${DO_BUILD_WIN}" == "y" ]
  338. then
  339. if [ "${HOST_NODE_PLATFORM}" == "linux" ]
  340. then
  341. TARGET_PLATFORM="win32"
  342. else
  343. echo "Windows cross builds are available only on GNU/Linux."
  344. exit 1
  345. fi
  346. fi
  347. }
  348. function host_common()
  349. {
  350. if [ -f "${script_folder_path}/VERSION" ]
  351. then
  352. # When running from the distribution folder.
  353. RELEASE_VERSION=${RELEASE_VERSION:-"$(cat "${script_folder_path}"/VERSION)"}
  354. fi
  355. echo
  356. echo "Preparing release ${RELEASE_VERSION}..."
  357. echo
  358. defines_script_path="${script_folder_path}/defs-source.sh"
  359. echo "Definitions source script: \"${defines_script_path}\"."
  360. source "${defines_script_path}"
  361. # -----------------------------------------------------------------------------
  362. common_helper_functions_script_path="${script_folder_path}/helper/common-functions-source.sh"
  363. echo "Common helper functions source script: \"${common_helper_functions_script_path}\"."
  364. source "${common_helper_functions_script_path}"
  365. # May override some of the helper/common definitions.
  366. common_functions_script_path="${script_folder_path}/common-functions-source.sh"
  367. if [ -f "${common_functions_script_path}" ]
  368. then
  369. echo "Common functions source script: \"${common_functions_script_path}\"."
  370. source "${common_functions_script_path}"
  371. fi
  372. # ---------------------------------------------------------------------------
  373. # The Work folder is in HOME.
  374. if [ "${IS_NATIVE}" != "y" ]
  375. then
  376. HOST_WORK_FOLDER_PATH=${WORK_FOLDER_PATH:-"${HOME}/Work/${APP_LC_NAME}-${RELEASE_VERSION}"}
  377. else
  378. HOST_WORK_FOLDER_PATH=${WORK_FOLDER_PATH:-"${HOME}/Work/${APP_LC_NAME}-dev"}
  379. fi
  380. CONTAINER_WORK_FOLDER_PATH="/Host${HOST_WORK_FOLDER_PATH}"
  381. SOURCES_FOLDER_PATH="${SOURCES_FOLDER_PATH:-"${HOST_WORK_FOLDER_PATH}/sources"}"
  382. # The names of the two Docker images used for the build.
  383. # docker run --interactive --tty ilegeul/centos:6-xbb-v2.1
  384. docker_linux64_image=${docker_linux64_image:-"ilegeul/centos:6-xbb-v2.2"}
  385. docker_linux32_image=${docker_linux32_image:-"ilegeul/centos32:6-xbb-v2.2"}
  386. do_actions
  387. host_prepare_cache
  388. CONTAINER_BUILD_SCRIPT_REL_PATH="build.git/scripts/${CONTAINER_SCRIPT_NAME}"
  389. echo "Container build script: \"${HOST_WORK_FOLDER_PATH}/${CONTAINER_BUILD_SCRIPT_REL_PATH}\"."
  390. # ---------------------------------------------------------------------------
  391. mkdir -p "${HOST_WORK_FOLDER_PATH}"
  392. mkdir -p "${SOURCES_FOLDER_PATH}"
  393. # ---------------------------------------------------------------------------
  394. # Set the DISTRIBUTION_FILE_DATE.
  395. host_get_current_date
  396. # ---------------------------------------------------------------------------
  397. host_start_timer
  398. host_prepare_prerequisites
  399. # ---------------------------------------------------------------------------
  400. copy_build_git
  401. }
  402. function host_prepare_prerequisites()
  403. {
  404. if [ "${HOST_UNAME}" == "Darwin" ]
  405. then
  406. local xbb_folder
  407. local must_install=""
  408. if [ -d "${HOME}/opt/xbb" ]
  409. then
  410. xbb_folder="${HOME}/opt/xbb"
  411. elif [ -d "${HOME}/opt/homebrew/xbb" ]
  412. then
  413. xbb_folder="${HOME}/opt/homebrew/xbb"
  414. else
  415. must_install="y"
  416. fi
  417. if [ ! -z "${xbb_folder}" ]
  418. then
  419. echo
  420. echo "Checking XBB in '${xbb_folder}'..."
  421. if [ ! -f "${xbb_folder}/xbb-source.sh" ]
  422. then
  423. must_install="y"
  424. fi
  425. fi
  426. if [ -n "${must_install}" ]
  427. then
  428. echo
  429. echo "Please install the macOS XBB and rerun."
  430. echo "https://github.com/xpack/xpack-build-box/tree/master/macos"
  431. exit 1
  432. fi
  433. if true
  434. then
  435. local tl_folder="$HOME/opt/texlive"
  436. must_install=""
  437. # Check local TeX Live.
  438. if [ ! -d "${tl_folder}" ]
  439. then
  440. must_install="y"
  441. else
  442. PATH="${tl_folder}/bin/x86_64-darwin:${PATH}"
  443. export PATH
  444. echo
  445. echo "Checking TeX Live in '${tl_folder}'..."
  446. set +e
  447. tex --version | grep 'TeX 3'
  448. if [ $? != 0 ]
  449. then
  450. must_install="y"
  451. fi
  452. set -e
  453. fi
  454. if [ -n "${must_install}" ]
  455. then
  456. echo
  457. echo "Please install TeX Live and rerun."
  458. echo "Alternatively restart the build script using '--without-pdf'."
  459. echo "https://github.com/xpack/xpack-build-box/blob/master/macos/README.md#install-tex"
  460. exit 1
  461. fi
  462. fi # -z "${no_pdf}"
  463. fi # "${HOST_UNAME}" == "Darwin"
  464. host_prepare_cache
  465. # The host script will pass to the container script
  466. # various environment variables.
  467. HOST_DEFINES_SCRIPT_PATH="${HOST_WORK_FOLDER_PATH}/build.git/scripts/host-defs-source.sh"
  468. DEPLOY_FOLDER_NAME=${DEPLOY_FOLDER_NAME:-"deploy"}
  469. }
  470. # -----------------------------------------------------------------------------
  471. function host_prepare_docker()
  472. {
  473. echo
  474. echo "Checking Docker..."
  475. set +e
  476. docker --version
  477. if [ $? != 0 ]
  478. then
  479. echo "Please start docker daemon and rerun."
  480. echo "If not installed, see https://docs.docker.com/installation/."
  481. exit 1
  482. fi
  483. set -e
  484. }
  485. # -----------------------------------------------------------------------------
  486. function host_build_target()
  487. {
  488. message="$1"
  489. shift
  490. echo
  491. echo "================================================================================"
  492. echo "=== ${message}"
  493. echo
  494. echo $@
  495. local container_script_path=""
  496. local target_platform="${HOST_NODE_PLATFORM}"
  497. local target_arch="${HOST_NODE_ARCH}"
  498. local target_bits="${HOST_BITS}"
  499. # If the docker image is not set, it is a native build.
  500. local docker_image=""
  501. local build_binaries_path=""
  502. local env_file=""
  503. while [ $# -gt 0 ]
  504. do
  505. case "$1" in
  506. --script)
  507. container_script_path="$2"
  508. shift 2
  509. ;;
  510. --target-platform)
  511. target_platform="$2"
  512. shift 2
  513. ;;
  514. --target-arch)
  515. target_arch="$2"
  516. shift 2
  517. ;;
  518. --target-bits)
  519. target_bits="$2"
  520. shift 2
  521. ;;
  522. --docker-image)
  523. docker_image="$2"
  524. shift 2
  525. ;;
  526. --env-file)
  527. env_file="$2"
  528. shift 2
  529. ;;
  530. --)
  531. shift
  532. break
  533. ;;
  534. *)
  535. echo "Unknown option $1, exit."
  536. exit 1
  537. esac
  538. done
  539. if [ "${target_platform}" == "sources" ]
  540. then
  541. target_arch="none"
  542. target_bits=""
  543. fi
  544. # The remaining $@ options will be passed to the inner script.
  545. if [ -n "${DEBUG}" ]
  546. then
  547. echo $@
  548. fi
  549. # ---------------------------------------------------------------------------
  550. mkdir -p "$(dirname "${HOST_DEFINES_SCRIPT_PATH}")"
  551. echo "${RELEASE_VERSION}" >"$(dirname "${HOST_DEFINES_SCRIPT_PATH}")"/VERSION
  552. rm -rf "${HOST_DEFINES_SCRIPT_PATH}"
  553. touch "${HOST_DEFINES_SCRIPT_PATH}"
  554. echo "RELEASE_VERSION=\"${RELEASE_VERSION}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  555. echo "DISTRIBUTION_FILE_DATE=\"${DISTRIBUTION_FILE_DATE}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  556. echo "TARGET_PLATFORM=\"${target_platform}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  557. echo "TARGET_ARCH=\"${target_arch}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  558. echo "TARGET_BITS=\"${target_bits}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  559. echo "HOST_UNAME=\"${HOST_UNAME}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  560. echo "USER_ID=\"${USER_ID}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  561. echo "USER_NAME=\"${USER_NAME}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  562. echo "GROUP_ID=\"${GROUP_ID}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  563. echo "GROUP_NAME=\"${GROUP_NAME}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  564. echo "CONTAINER_RUN_AS_ROOT=\"${CONTAINER_RUN_AS_ROOT}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  565. echo "HOST_WORK_FOLDER_PATH=\"${HOST_WORK_FOLDER_PATH}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  566. echo "CONTAINER_WORK_FOLDER_PATH=\"${CONTAINER_WORK_FOLDER_PATH}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  567. echo "HOST_CACHE_FOLDER_PATH=\"${HOST_CACHE_FOLDER_PATH}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  568. echo "CONTAINER_CACHE_FOLDER_PATH=\"${CONTAINER_CACHE_FOLDER_PATH}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  569. echo "DEPLOY_FOLDER_NAME=\"${DEPLOY_FOLDER_NAME}\"" >>"${HOST_DEFINES_SCRIPT_PATH}"
  570. if [ -z "${docker_image}" ]
  571. then
  572. host_run_native_script \
  573. --script "${container_script_path}" \
  574. --env-file "${env_file}" \
  575. -- \
  576. $@
  577. else
  578. host_run_docker_script \
  579. --script "${container_script_path}" \
  580. --docker-image "${docker_image}" \
  581. --docker-container-name "${APP_LC_NAME}-${target_platform}-${target_arch}-build" \
  582. --env-file "${env_file}" \
  583. --host-uname "${HOST_UNAME}" \
  584. -- \
  585. $@
  586. fi
  587. if [ -n "${DEBUG}" ]
  588. then
  589. echo "host_build_target ${rest[@]-} completed."
  590. fi
  591. }
  592. # -----------------------------------------------------------------------------
  593. function host_run_native_script()
  594. {
  595. local local_script=""
  596. local env_file=""
  597. while [ $# -gt 0 ]
  598. do
  599. case "$1" in
  600. --script)
  601. local_script="$2"
  602. shift 2
  603. ;;
  604. --env-file)
  605. env_file="$2"
  606. shift 2
  607. ;;
  608. --)
  609. shift
  610. break
  611. ;;
  612. *)
  613. echo "Unknown option $1, exit."
  614. exit 1
  615. esac
  616. done
  617. # The remaining $@ options will be passed to the inner script.
  618. echo
  619. echo "Running script \"$(basename "${local_script}")\" natively..."
  620. # Run the inner script in a local sub-shell, possibly with the
  621. # custom environment.
  622. (
  623. if [ -n "${env_file}" -a -f "${env_file}" ]
  624. then
  625. source "${env_file}"
  626. fi
  627. /bin/bash ${DEBUG} "${local_script}" $@
  628. )
  629. }
  630. # -----------------------------------------------------------------------------
  631. function host_run_docker_script()
  632. {
  633. local docker_script=""
  634. local docker_image=""
  635. local docker_container_name=""
  636. local host_uname=""
  637. local env_file=""
  638. local opt_env_file=
  639. while [ $# -gt 0 ]
  640. do
  641. case "$1" in
  642. --script)
  643. docker_script="$2"
  644. shift 2
  645. ;;
  646. --docker-image)
  647. docker_image="$2"
  648. shift 2
  649. ;;
  650. --docker-container-name)
  651. docker_container_name="$2"
  652. shift 2
  653. ;;
  654. --host-uname)
  655. host_uname="$2"
  656. shift 2
  657. ;;
  658. --env-file)
  659. env_file="$2"
  660. shift 2
  661. ;;
  662. --)
  663. shift
  664. break
  665. ;;
  666. *)
  667. echo "Unknown option $1, exit."
  668. exit 1
  669. esac
  670. done
  671. set +e
  672. # Remove a possible previously crashed container.
  673. docker rm --force "${docker_container_name}" > /dev/null 2> /dev/null
  674. set -e
  675. echo
  676. echo "Running script \"$(basename "${docker_script}")\" inside docker image \"${docker_image}\"..."
  677. local env_file_option=""
  678. # Run the inner script in a fresh Docker container.
  679. if [ -n "${env_file}" -a -f "${env_file}" ]
  680. then
  681. env_file_option="--env-file=\"${env_file}\""
  682. fi
  683. if [ "${HOST_UNAME}" == "Darwin" -o "${CONTAINER_RUN_AS_ROOT}" == "y" ]
  684. then
  685. docker run \
  686. --name="${docker_container_name}" \
  687. --tty \
  688. --hostname "docker" \
  689. --workdir="/root" \
  690. --volume="${HOST_WORK_FOLDER_PATH}/:${CONTAINER_WORK_FOLDER_PATH}" \
  691. --volume="${HOST_CACHE_FOLDER_PATH}/:${CONTAINER_CACHE_FOLDER_PATH}" \
  692. ${env_file_option} \
  693. "${docker_image}" \
  694. /bin/bash ${DEBUG} "${docker_script}" $@
  695. else
  696. # This is a bit tricky, since it needs to do multiple actions in
  697. # one go: add a new user and run the script with that user credentials,
  698. # including passing the extra args (in the middle of the string).
  699. #
  700. # From the [bash manual](https://www.gnu.org/software/bash/manual/bash.html):
  701. # ($*) Expands to the positional parameters, starting from one.
  702. # When the expansion is not within double quotes, each positional
  703. # parameter expands to a separate word. In contexts where it is
  704. # performed, those words are subject to further word splitting and
  705. # pathname expansion. When the expansion occurs within double quotes,
  706. # it expands to a single word with the value of each parameter separated
  707. # by the first character of the IFS special variable. That is, "$*"
  708. # is equivalent to "$1c$2c…", where c is the first character of the
  709. # value of the IFS variable. If IFS is unset, the parameters are
  710. # separated by spaces. If IFS is null, the parameters are joined
  711. # without intervening separators.
  712. local ifs="${IFS}"
  713. IFS=" "
  714. local cmd_string="groupadd -g ${GROUP_ID} ${GROUP_NAME} && useradd -u ${USER_ID} -g ${GROUP_ID} ${USER_NAME} && su -c \"DEBUG=${DEBUG} bash ${docker_script} $*\" ${USER_NAME}"
  715. IFS="${ifs}"
  716. docker run \
  717. --name="${docker_container_name}" \
  718. --tty \
  719. --hostname "docker" \
  720. --workdir="/root" \
  721. --volume="${HOST_WORK_FOLDER_PATH}/:${CONTAINER_WORK_FOLDER_PATH}" \
  722. --volume="${HOST_CACHE_FOLDER_PATH}/:${CONTAINER_CACHE_FOLDER_PATH}" \
  723. ${env_file_option} \
  724. "${docker_image}" \
  725. /bin/bash ${DEBUG} -c "${cmd_string}"
  726. fi
  727. # Remove the container.
  728. echo "Docker container \"$(docker rm --force "${docker_container_name}")\" removed."
  729. }
  730. # -----------------------------------------------------------------------------
  731. function host_show_sha() {
  732. if [ -d "${HOST_WORK_FOLDER_PATH}/${DEPLOY_FOLDER_NAME}" ]
  733. then
  734. echo
  735. echo "SHA signatures..."
  736. set +e
  737. cat "${HOST_WORK_FOLDER_PATH}/${DEPLOY_FOLDER_NAME}"/*.sha
  738. set -e
  739. fi
  740. }
  741. # -----------------------------------------------------------------------------