OSDN Git Service

c5fbb3c0871e8b8972b83c2a1b31e26b2b8a2b46
[alterlinux/alterlinux.git] / tools / fullbuild.sh
1 #!/usr/bin/env bash
2
3 script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
4
5 channels=(
6 ##  Current official channel
7     "xfce"
8     "i3"
9     "plasma"
10
11 ## Scheduled to discontinue distribution
12     "lxde"
13     "cinnamon"
14
15 ## They are unstable channel
16 #   "xfce-pro"
17 #   "gnome"
18 #   "serene"
19 )
20
21 architectures=("x86_64" "i686")
22 locale_list=("ja" "en")
23 share_options=()
24 default_options=("--boot-splash" "--cleanup" "--user" "alter" "--password" "alter" "--log")
25 failed=()
26 abort=false
27
28 work_dir="${script_path}/work"
29 out_dir="${script_path}/out"
30 simulation=false
31 retry=1
32
33 remove_cache=false
34 all_channel=false
35 customized_work=false
36 noconfirm=false
37
38 # Message common function
39 # msg_common [type] [-n] [string]
40 msg_common(){
41     local _msg_opts=("-a" "fullbuild" "-s" "5") _type="${1}"
42     shift 1
43     [[ "${1}" = "-n" ]] && _msg_opts+=("-o" "-n") && shift 1
44     [[ "${nocolor}"  = true ]] && _msg_opts+=("-n")
45     _msg_opts+=("${_type}" "${@}")
46     "${script_path}/tools/msg.sh" "${_msg_opts[@]}"
47 }
48
49 # Show an INFO message
50 # ${1}: message string
51 msg_info() { msg_common info "${@}"; }
52
53 # Show an Warning message
54 # ${1}: message string
55 msg_warn() { msg_common warn "${@}"; }
56
57 # Show an ERROR message then exit with status
58 # ${1}: message string
59 # ${2}: exit code number (with 0 does not exit)
60 msg_error() {
61     msg_common error "${1}"
62     [[ -n "${2:-}" ]] && exit "${2}"
63     return 0
64 }
65
66
67 trap_exit() {
68     local status="${?}"
69     echo
70     msg_error "fullbuild.sh has been killed by the user."
71     exit "${status}"
72 }
73
74
75 build() {
76     local _exit_code=0 _options=("${share_options[@]}")
77
78     _options+=("--arch" "${arch}" "--lang" "${lang}" "--out" "${out_dir}/${cha}/${lang}" "${cha}")
79
80     if [[ "${simulation}" = false ]] && [[ "${remove_cache}" = true ]]; then
81         msg_info "Removing package cache for ${arch}"
82         sudo rm -rf "${work_dir}/cache/${arch}"
83     fi
84
85     if [[ ! -e "${fullbuild_dir}/fullbuild.${cha}_${arch}_${lang}" ]]; then
86         if [[ "${simulation}" = true ]]; then
87             echo "sudo bash build.sh ${_options[*]}"
88             _exit_code="${?}"
89         else
90             msg_info "Build the ${lang} version of ${cha} on the ${arch} architecture."
91             sudo bash "${script_path}/build.sh" "${_options[@]}"
92             _exit_code="${?}"
93             if (( _exit_code == 0 )); then
94                 touch "${fullbuild_dir}/fullbuild.${cha}_${arch}_${lang}"
95             elif (( "${retry_count}" == "${retry}" )); then
96                 msg_error "Failed to build (Exit code: ${_exit_code})"
97                 if [[ "${abort}" = true ]]; then
98                     exit "${_exit_code}"
99                 else
100                     failed+=("${cha}-${arch}-${lang}")
101                 fi
102             else
103                 msg_error "build.sh finished with exit code ${_exit_code}. Will try again."
104             fi
105             
106         fi
107     else
108         msg_info "Found: ${fullbuild_dir}/fullbuild.${cha}_${arch}_${lang}"
109     fi
110 }
111
112 _help() {
113     echo "usage ${0} [options] [channel]"
114     echo
115     echo " General options:"
116     echo "    -a <options>       Set other options in build.sh"
117     echo "    -c                 Build all channel (DO NOT specify the channel !!)"
118     echo "    -d                 Use the default build.sh arguments. (${default_options[*]})"
119     echo "    -e                 Exit the script when the build fails"
120     echo "    -g                 Use gitversion"
121     echo "    -h | --help        This help message"
122     echo "    -l <locale>        Set the locale to build"
123     echo "    -m <architecture>  Set the architecture to build"
124     echo "    -o <dir>           Set the out dir"
125     echo "    -r <interer>       Set the number of retries"
126     echo "                       Defalut: ${retry}"
127     echo "    -s                 Enable simulation mode"
128     echo "    -t                 Build the tarball as well"
129     echo "    -w <dir>           Set the work dir"
130     echo
131     echo "    --remove-cache     Clear cache for all packages on every build"
132     echo "    --noconfirm        Run without confirmation"
133     echo
134     echo " !! WARNING !!"
135     echo " Do not set channel or architecture with -a."
136     echo " Be sure to enclose the build.sh argument with '' to avoid mixing it with the fullbuild.sh argument."
137     echo " Example: ${0} -a '-b -k zen'"
138     echo
139     echo "Run \"build.sh -h\" for channel details."
140     echo -n " Channel: "
141     "${script_path}/tools/channel.sh" show
142 }
143
144 share_options+=("--noconfirm")
145
146 # Parse options
147 ARGUMENT=("${@}")
148 OPTS=("a:" "d" "e" "g" "h" "r:" "s" "c" "t" "m:" "l:" "w:" "o:")
149 OPTL=("help" "remove-cache" "noconfirm")
150 if ! OPT=$(getopt -o "$(printf "%s," "${OPTS[@]}")" -l "$(printf "%s," "${OPTL[@]}")" --  "${ARGUMENT[@]}"); then
151     exit 1
152 fi
153 eval set -- "${OPT}"
154 unset OPT OPTS OPTL ARGUMENT
155
156 while true; do
157     case "${1}" in
158         -a)
159             IFS=" " read -r -a share_options <<< "${2}"
160             shift 2
161             ;;
162         -c)
163             all_channel=true
164             shift 1
165             ;;
166         -d)
167             share_options+=("${default_options[@]}")
168             shift 1
169             ;;
170         -e)
171             abort=true
172             shift 1
173             ;;
174         -m)
175             IFS=" " read -r -a architectures <<< "${2}"
176             shift 2
177             ;;
178         -o)
179             out_dir="${2}"
180             shift 2
181             ;;
182         -g)
183             if [[ ! -d "${script_path}/.git" ]]; then
184                 msg_error "There is no git directory. You need to use git clone to use this feature."
185                 exit 1
186             else
187                 share_options+=("--gitversion")
188             fi
189             shift 1
190             ;;
191         -s)
192             simulation=true
193             shift 1
194             ;;
195         -r)
196             retry="${2}"
197             shift 2
198             ;;
199         -t)
200             share_options+=("--tarball")
201             ;;
202         -l)
203             IFS=" " read -r -a locale_list <<< "${2}"
204             shift 2
205             ;;
206         -w)
207             work_dir="${2}"
208             customized_work=true
209             shift 2
210             ;;
211         -h | --help)
212             shift 1
213             _help
214             exit 0
215             ;;
216         --remove-cache)
217             remove_cache=true
218             shift 1
219             ;;
220         --noconfirm)
221             noconfirm=true
222             shift 1
223             ;;
224         --)
225             shift 1
226             break
227             ;;
228         *)
229             shift 1
230             _help
231             exit 1 
232             ;;
233     esac
234 done
235
236
237 if [[ "${all_channel}" = true  ]]; then
238     if [[ -n "${*}" ]]; then
239         msg_error "Do not specify the channel." "1"
240     else
241         readarray -t channels < <("${script_path}/tools/channel.sh" -b show)
242     fi
243 elif [[ -n "${*}" ]]; then
244     channels=("${@}")
245 fi
246
247 if [[ "${simulation}" = true ]]; then
248     retry=1
249 fi
250
251 if [[ "${customized_work}" = false ]]; then
252     work_dir="$(
253         source "${script_path}/default.conf"
254         if [[ -f "${script_path}/custom.conf" ]]; then
255             source "${script_path}/custom.conf"
256         fi
257         if [[ "${work_dir:0:1}" = "/" ]]; then
258             echo -n "${work_dir}"
259         else
260             echo -n "${script_path}/${work_dir}"
261         fi
262     )"
263 fi
264
265 fullbuild_dir="${work_dir}/fullbuild"
266 mkdir -p "${fullbuild_dir}"
267
268 if [[ "$(find "${fullbuild_dir}" -maxdepth 1 -mindepth 1 -name "fullbuild.*" 2> /dev/null)" ]] && [[ "${noconfirm}" = false ]]; then
269     msg_info "Do you want to reset lock files ? (y/N)"
270     read -r -n 1 _yes_or_no
271     echo
272     if [[ "${_yes_or_no}" = "y" ]] || [[ "${_yes_or_no}" = "Y" ]]; then
273         find "${fullbuild_dir}" -maxdepth 1 -mindepth 1 -name "fullbuild.*" -delete 2> /dev/null
274     fi
275 fi
276
277
278 share_options+=("--work" "${work_dir}")
279 msg_info "Options: ${share_options[*]}"
280 if [[ "${noconfirm}" = false ]]; then
281     msg_info "Press Enter to continue or Ctrl + C to cancel."
282     read -r
283 fi
284
285
286 trap 'trap_exit' 1 2 3 15
287
288 for arch in "${architectures[@]}"; do
289     for cha in "${channels[@]}"; do
290         for lang in "${locale_list[@]}"; do
291             for retry_count in $(seq 1 "${retry}"); do
292                 if grep -h -v ^'#' "${script_path}/channels/${cha}/architecture" | grep -x "${arch}" 1> /dev/null 2>&1; then
293                     build
294                 fi
295             done
296         done
297     done
298 done
299
300
301 if [[ "${simulation}" = false ]]; then
302     if (( "${#failed[@]}" == 0 )); then
303         msg_info "All editions have been built"
304     else
305         msg_error "Build of the following settings failed"
306         printf " - %s\n" "${failed[@]}"
307     fi
308 fi