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