Make --with-pjproject-bundled the default for Asterisk 15
[asterisk/asterisk.git] / build_tools / download_externals
1 #!/usr/bin/env bash
2
3 if [[ ( ${BASH_VERSINFO[0]} == 4 && ${BASH_VERSINFO[1]} > 1 ) || ${BASH_VERSINFO[0]} > 4 ]] ; then
4         shopt -s compat41
5 fi
6 set -e
7
8 ASTTOPDIR=${ASTTOPDIR:-.}
9
10 module_name=${1%%-*}
11 variant=${1##*-}
12
13 if [[ "${variant}" = "${module_name}" ]] ; then
14         unset variant
15 fi
16
17 if [[ -z ${module_name} ]] ; then
18         echo "You must supply a module name."
19         exit 64
20 fi
21
22 tmpdir=$(mktemp -d)
23 if [[ -z "${tmpdir}" ]] ; then
24         echo "${module_name}: Unable to create temporary directory."
25         exit 1
26 fi
27 trap "rm -rf ${tmpdir}" EXIT
28
29 # We have to pre-process the makeopts file so it will be parsable by bash
30 # Surround values with double quotes
31 # Convert make  $(or) functions to bash ${name:-value}
32 sed -r  -e "s/^([^ =]+)\s*=\s*(.*)$/\1=\"\2\"/g" \
33         -e 's/^([^ =]+)="\$\(or ([^,]*),([^)]+)\)"/_tmp="\2"\n\1="${_tmp:-\3}"/g'  ${ASTTOPDIR}/makeopts >${tmpdir}/makeopts
34 source ${tmpdir}/makeopts
35 if [[ -z "${ASTMODDIR}" ]] ; then
36         echo "${module_name}: Unable to parse ${ASTTOPDIR}/makeopts."
37         exit 1
38 fi
39
40 XMLSTARLET=${XMLSTARLET:-xmlstarlet}
41 if [[ "${XMLSTARLET}" = ":" ]] ; then
42         echo "${module_name}: The externals downloader requires xmlstarlet to be installed."
43         exit 1
44 fi
45
46 cache_dir="${EXTERNALS_CACHE_DIR}"
47 if [[ -z ${cache_dir} ]] ; then
48         cache_dir=${tmpdir}
49 fi
50
51 version=$(${ASTTOPDIR}/build_tools/make_version ${ASTTOPDIR})
52 if [[ ! ${version} =~ ^(GIT-)?(certified/)?([^.-]+)[.-].* ]] ; then
53         echo "${module_name}: Couldn't parse version ${version}"
54         exit 1
55 fi
56 major_version=${BASH_REMATCH[3]}
57
58 if [[ "${major_version}" == "master" ]] ; then
59         echo "${module_name}: External module downloading is not available in the 'master' git branch.  Please disable in menuselect and download manually."
60         exit 1
61 fi
62
63 major_version=${major_version}.0
64
65 if [[ "${HOST_CPU}" = "x86_64" ]] ; then
66         host_bits=64
67 else
68         host_bits=32
69 fi
70
71 if [[ -z "${variant}" ]] ; then
72         variants=$(${XMLSTARLET} sel -t -m "/menu/category/member[@name = '${module_name}']/member_data/downloader/variants/variant" -v "@tag" -n ${ASTTOPDIR}/menuselect-tree || :)
73         member_name=${module_name}
74         for tag in ${variants} ; do
75                 condition=$(${XMLSTARLET} sel -t -v "/menu/category/member[@name = '${module_name}']/member_data/downloader/variants/variant[@tag = '${tag}']/@condition" ${ASTTOPDIR}/menuselect-tree || :)
76                 variant=$(eval "if $condition ; then echo $tag ; fi")
77                 if [[ -n "${variant}" ]] ; then
78                         break
79                 fi
80         done
81 else
82         member_name=${module_name}${variant:+-${variant}}
83 fi
84
85 full_name=${module_name}${variant:+-${variant}}
86 variant_manifest=manifest${variant:+-${variant}}.xml
87
88 # Override the remote base for all packages
89 # useful for testing
90 remote_url=${REMOTE_BASE:+${REMOTE_BASE}/asterisk-${major_version}/x86-${host_bits}}
91
92 if [[ -z "${remote_url}" ]] ; then
93         remote_url=$(${XMLSTARLET} sel -t -v "/menu/category/member[@name = '${member_name}']/member_data/downloader/@remote_url" ${ASTTOPDIR}/menuselect-tree || :)
94         if [[ -n "${remote_url}" ]] ; then
95                 remote_url="${remote_url}/asterisk-${major_version}/x86-${host_bits}"
96         else
97                 directory_name=$(${XMLSTARLET} sel -t -v "/menu/category/member[@name = '${member_name}']/member_data/downloader/@directory_name" ${ASTTOPDIR}/menuselect-tree || :)
98                 remote_url="http://downloads.digium.com/pub/telephony/${directory_name:-${module_name}}/asterisk-${major_version}/x86-${host_bits}"
99         fi
100 fi
101
102 version_convert() {
103         local v=${1##*_}
104         if [[ ${v} =~ ([0-9]+)[.]([0-9]+)[.]([0-9]+) ]] ; then
105                 v=$(( ${BASH_REMATCH[1]}<<18 | ${BASH_REMATCH[2]}<<9 | ${BASH_REMATCH[3]} ))
106         fi
107         echo ${v}
108 }
109
110 ${DOWNLOAD_TO_STDOUT} ${remote_url}/${variant_manifest} > ${tmpdir}/${variant_manifest} || {
111         echo "${full_name}: Unable to fetch ${remote_url}/${variant_manifest}"
112         exit 1
113 }
114
115 rpv=$(${XMLSTARLET} sel -t -v "/package/@version" ${tmpdir}/${variant_manifest})
116 rpvi=$(version_convert ${rpv})
117 echo "${full_name}: Remote package version ${rpv} (${rpvi})"
118
119 module_dir=${full_name}-${rpv}-x86_${host_bits}
120 tarball=${module_dir}.tar.gz
121
122 export need_install=0
123
124 if [[ -f ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml ]] ; then
125         package_arch=$(${XMLSTARLET} sel -t -v "/package/@arch" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml)
126         ipv=$(${XMLSTARLET} sel -t -v "/package/@version" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml)
127         package_variant=$(${XMLSTARLET} sel -t -v "/package/@variant" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml || :)
128         ipvi=$(version_convert ${ipv})
129         ip_major=${ipv%_*}
130         echo "${full_name}: Installed package version ${ipv} (${ipvi})"
131         if [[ "${ip_major}" != "${major_version}" || "${package_arch}" != "x86_${host_bits}" || "${package_variant}" != "${variant}" ]] ; then
132                 echo "${full_name}: The installed package is not for this version of Asterisk.  Reinstalling."
133                 need_install=1
134         elif [[ ${rpvi} > ${ipvi} ]] ; then
135                 echo "${full_name}: A newer package is available"
136                 need_install=1
137         else
138                 sums=$(${XMLSTARLET} sel -t -m "//file" -v "@md5sum" -n ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml)
139                 for sum in ${sums} ; do
140                         install_path=$(${XMLSTARLET} sel -t -v "//file[@md5sum = '${sum}']/@install_path" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml )
141                         executable=$(${XMLSTARLET} sel -t -v "//file[@md5sum = '${sum}']/@executable" ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml || : )
142                         f=${DESTDIR}$(eval echo ${install_path})
143                         if [[ ! -f ${f} ]] ; then
144                                 echo Not found: ${f}
145                                 need_install=1
146                                 break
147                         else
148                                 if [[ "$executable" = "yes" ]] ; then
149                                         # There are easier ways of doing this (objcopy --dump-section) but not in older bunutils
150                                         length_offset=$(objdump -h $f | sed -n -r -e "s/^\s+[0-9]+\s+.ast_manifest\s+([0-9a-fA-F]+)\s+[0-9a-fA-F]+\s+[0-9a-fA-F]+\s+([0-9a-fA-F]+)\s+.*$/0x\1 0x\2/p")
151                                         tags=$($(eval 'printf "dd if=$f bs=1 count=%d skip=%d\n" $length_offset') 2>/dev/null)
152                                         if [[ -n "${tags}" && "${tags}" != "${module_name},${variant},${rpv}" ]] ; then
153                                                 echo Tag mismatch: ${f} File: "${tags}" Manifest: "${module_name},${variant},${rpv}"
154                                                 need_install=1
155                                                 break
156                                         fi
157                                 fi
158
159                                 cs=$(md5sum ${f} | cut -b1-32)
160                                 if [[ "${cs}" !=  "${sum}" ]] ; then
161                                         echo Checksum mismatch: ${f}
162                                         need_install=1
163                                         break
164                                 fi
165                         fi
166                 done
167         fi
168 else
169         need_install=1
170 fi
171
172 if [[ ${need_install} == 1 ]] ; then
173         if [[ ( -n "${ipvi}" ) && ${ipvi} > ${rpvi} ]] ; then
174                 echo "${full_name}: Installed package is newer than that available for download."
175                 exit 0
176         fi
177 else
178         echo "${full_name} is up to date."
179         exit 0;
180 fi
181
182 need_download=1
183 if [[ -f ${cache_dir}/${full_name}.manifest.xml ]] ; then
184         cpv=$(${XMLSTARLET} sel -t -v "/package/@version" ${cache_dir}/${full_name}.manifest.xml)
185         cpvi=$(version_convert ${cpv})
186         echo "${full_name}: Cached package version ${cpv} (${cpvi})"
187         if [[ ${cpvi} == ${rpvi} && ( -f ${cache_dir}/${tarball} ) ]] ; then
188                 echo "${full_name}: Cached version is available."
189                 need_download=0
190         fi
191 fi
192
193 if [[ ${need_download} = 1 ]] ; then
194         echo "${full_name}: Downloading ${remote_url}/${tarball}"
195         ${DOWNLOAD_TO_STDOUT} ${remote_url}/${tarball} > ${cache_dir}/${tarball} || {
196                 echo "${full_name}: Unable to fetch ${remote_url}/${tarball}"
197                 exit 1
198         }
199         cp ${tmpdir}/${variant_manifest}  ${cache_dir}/${full_name}.manifest.xml
200 fi
201
202 tar -xzf ${cache_dir}/${tarball} -C ${cache_dir}
203 trap "rm -rf ${cache_dir}/${module_dir} ; rm -rf ${tmpdir}" EXIT
204
205 echo "${full_name}: Installing."
206
207 if [[ $EUID == 0 ]] ; then
208         install_params="--group=0 --owner=0"
209 fi
210
211 names=$(${XMLSTARLET} sel -t -m "//file" -v "@name" -n ${cache_dir}/${module_dir}/manifest.xml)
212 for name in ${names} ; do
213         source_path=${cache_dir}/${module_dir}/${name}
214         install_path=$(${XMLSTARLET} sel -t -v "//file[@name = '${name}']/@install_path" ${cache_dir}/${module_dir}/manifest.xml)
215         install_path=${DESTDIR}$(eval echo ${install_path})
216         executable=$(${XMLSTARLET} sel -t -v "//file[@name = '${name}']/@executable" ${cache_dir}/${module_dir}/manifest.xml || :)
217         if [[ "${executable}" = "yes" ]] ; then
218                 mode=0755
219         else
220                 mode=0644
221         fi
222
223         ${INSTALL} -Dp ${install_params} --mode=${mode} ${source_path} ${install_path}
224
225 done
226 ${INSTALL} -Dp ${install_params} --mode=0644 ${cache_dir}/${module_dir}/manifest.xml ${DESTDIR}${ASTMODDIR}/${module_name}.manifest.xml
227
228 echo "${full_name}: Installed."