blob: 7cab18ef093c8d9b682c08f37b2e00a652ed54db [file] [log] [blame]
Mike Frysinger4ed918e2012-05-09 15:44:12 -04001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Kenneth Waters512eb162010-06-02 10:19:12 -07002# Distributed under the terms of the GNU General Public License v2
3
4#
5# Original Author: The Chromium OS Authors <chromium-os-dev@chromium.org>
6# Purpose: Install binary packages for Chromium OS
7#
8
9# @ECLASS-VARIABLE: CROS_BINARY_STORE_DIR
10# @DESCRIPTION:
11# Storage directory for Chrome OS Binaries
12: ${CROS_BINARY_STORE_DIR:=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}/cros-binary}
13
Brian Harringcf0c7662012-12-19 23:06:44 -080014# @ECLASS-VARIABLE: CROS_BINARY_FETCH_REQUIRED
15# @DESCRIPTION:
16# Internal variable controlling whether cros-binary_fetch is actually ran.
17: ${CROS_BINARY_FETCH_REQUIRED:=true}
18
19# @ECLASS-FUNCTION: cros-binary_add_uri
20# @DESCRIPTION:
21# Add a fetch uri to SRC_URI for the given uri. See
22# CROS_BINARY_URI for what is accepted. Note you cannot
23# intermix a non-rewritten ssh w/ (http|https|gs).
24cros-binary_add_uri()
25{
26 if [[ $# -ne 1 ]]; then
27 die "cros-binary_add_uri takes exactly one argument; $# given."
28 fi
29 local uri="$1"
30 if [[ "${uri}" =~ ^ssh://([^@]+)@git.chromium.org[^/]*/(.*)$ ]]; then
31 uri="gs://chromeos-binaries/HOME/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
32 fi
33 case "${uri}" in
34 http://*|https://*|gs://*)
35 SRC_URI+=" ${uri}"
36 CROS_BINARY_FETCH_REQUIRED=false
37 CROS_BINARY_STORE_DIR="${DISTDIR}"
38 ;;
39 *)
40 die "Unknown protocol: ${uri}"
41 ;;
42 esac
43 RESTRICT+="mirror"
44}
45
46# @ECLASS-FUNCTION: cros-binary_add_gs_uri
47# @DESCRIPTION:
48# Wrapper around cros-binary_add_uri. Invoked with 3 arguments;
49# the bcs user, the overlay, and the filename (or bcs://<uri> for
50# backwards compatibility).
51cros-binary_add_gs_uri() {
52 if [[ $# -ne 3 ]]; then
53 die "cros-binary_add_bcs_uri needs 3 arguments; $# given."
54 fi
55 # Strip leading bcs://...
56 [[ "${3:0:6}" == "bcs://" ]] && set -- "${1}" "${2}" "${3#bcs://}"
57 cros-binary_add_uri "gs://chromeos-binaries/HOME/$1/$2/$3"
58}
59
60# @ECLASS-FUNCTION: cros-binary_add_overlay_uri
61# @DESCRIPTION:
62# Wrapper around cros-binary_add_bcs_uri. Invoked with 2 arguments;
63# the basic board target (x86-alex for example), and the filename; that filename
64# is automatically prefixed with "${CATEGORY}/${PN}/" .
65cros-binary_add_overlay_uri() {
66 if [[ $# -ne 2 ]]; then
67 die "cros-binary_add_bcs_uri_simple needs 2 arguments; $# given."
68 fi
69 cros-binary_add_gs_uri bcs-"$1" overlay-"$1" "${CATEGORY}/${PN}/$2"
70}
71
Kenneth Waters512eb162010-06-02 10:19:12 -070072# @ECLASS-VARIABLE: CROS_BINARY_URI
73# @DESCRIPTION:
74# URI for the binary may be one of:
75# http://
76# https://
77# ssh://
Brian Harringcf0c7662012-12-19 23:06:44 -080078# gs://
Kenneth Waters512eb162010-06-02 10:19:12 -070079# file:// (file is relative to the files directory)
Brian Harringcf0c7662012-12-19 23:06:44 -080080# Additionally, all bcs ssh:// urls are rewritten to gs:// automatically
81# the appropriate GS bucket- although cros-binary_add_uri is the preferred
82# way to do that.
83# TODO: Deprecate this variable's support for ssh and http/https.
Kenneth Waters512eb162010-06-02 10:19:12 -070084: ${CROS_BINARY_URI:=}
Brian Harringcf0c7662012-12-19 23:06:44 -080085if [[ -n "${CROS_BINARY_URI}" ]]; then
86 cros-binary_add_uri "${CROS_BINARY_URI}"
87fi
Kenneth Waters512eb162010-06-02 10:19:12 -070088
89# @ECLASS-VARIABLE: CROS_BINARY_SUM
90# @DESCRIPTION:
91# Optional SHA-1 sum of the file to be fetched
92: ${CROS_BINARY_SUM:=}
93
Puneet Kumar0cc9c652010-08-25 13:35:17 -070094# @ECLASS-VARIABLE: CROS_BINARY_INSTALL_FLAGS
95# @DESCRIPTION:
96# Optional Flags to use while installing the binary
97: ${CROS_BINARY_INSTALL_FLAGS:=}
98
Allen Martin967a8242012-03-27 04:51:34 +000099# @ECLASS-VARIABLE: CROS_BINARY_LOCAL_URI_BASE
100# @DESCRIPTION:
101# Optional URI to override CROS_BINARY_URI location. If this variable
102# is used the filename from CROS_BINARY_URI will be used, but the path
103# to the binary will be changed.
104: ${CROS_BINARY_LOCAL_URI_BASE:=}
105
Mike Frysinger4ed918e2012-05-09 15:44:12 -0400106# Check for EAPI 2+
Kenneth Waters512eb162010-06-02 10:19:12 -0700107case "${EAPI:-0}" in
Mike Frysinger4ed918e2012-05-09 15:44:12 -0400108 4|3|2) ;;
109 *) die "unsupported EAPI" ;;
Kenneth Waters512eb162010-06-02 10:19:12 -0700110esac
111
112cros-binary_check_file() {
113 local target="${CROS_BINARY_STORE_DIR}/${CROS_BINARY_URI##*/}"
Nick Sanders97c10552011-03-11 21:25:36 -0800114 elog "Checking for cached $target"
Kenneth Waters512eb162010-06-02 10:19:12 -0700115 if [[ -z "${CROS_BINARY_SUM}" ]]; then
Allen Martin967a8242012-03-27 04:51:34 +0000116 return 1
Kenneth Waters512eb162010-06-02 10:19:12 -0700117 else
118 echo "${CROS_BINARY_SUM} ${target}" |
119 sha1sum -c --quiet >/dev/null 2>&1
120 return
121 fi
122}
123
124cros-binary_fetch() {
Brian Harringcf0c7662012-12-19 23:06:44 -0800125 ${CROS_BINARY_FETCH_REQUIRED} || return 0
Allen Martin967a8242012-03-27 04:51:34 +0000126 local uri=${CROS_BINARY_URI}
127 if [[ ! -z "${CROS_BINARY_LOCAL_URI_BASE}" ]]; then
128 uri="${CROS_BINARY_LOCAL_URI_BASE}/${CROS_BINARY_URI##*/}"
129 fi
130
131 local scheme="${uri%%://*}"
Allen Martin7b2240d2012-11-13 13:17:35 -0800132 local non_scheme=${uri#*://}
Kenneth Waters512eb162010-06-02 10:19:12 -0700133 local netloc=${non_scheme%%/*}
Anton Staaff1afc3b2010-11-01 14:35:36 -0700134 local server=${netloc%%:*}
135 local port=${netloc##*:}
Kenneth Waters512eb162010-06-02 10:19:12 -0700136 local path=${non_scheme#*/}
137
Anton Staaff1afc3b2010-11-01 14:35:36 -0700138 if [[ -z "${port}" ]]; then
139 port="22"
140 fi
141
Kenneth Watersc3345172010-11-09 10:06:19 -0800142 local scp_args="-qo BatchMode=yes -oCheckHostIP=no -P ${port}"
Allen Martin967a8242012-03-27 04:51:34 +0000143 local target="${CROS_BINARY_STORE_DIR}/${uri##*/}"
Kenneth Waters512eb162010-06-02 10:19:12 -0700144
145 addwrite "${CROS_BINARY_STORE_DIR}"
146 if [[ ! -d "${CROS_BINARY_STORE_DIR}" ]]; then
147 mkdir -p "${CROS_BINARY_STORE_DIR}" ||
148 die "Failed to mkdir ${CROS_BINARY_STORE_DIR}"
149 fi
150
151 if ! cros-binary_check_file; then
152 rm -f "${target}"
153 case "${scheme}" in
Andrew Chewcf2e1b32013-02-06 16:11:33 -0800154 http|https|ftp)
Allen Martin967a8242012-03-27 04:51:34 +0000155 wget "${uri}" -O "${target}" -nv -nc ||
Kenneth Waters512eb162010-06-02 10:19:12 -0700156 rm -f "${target}"
157 ;;
158
159 ssh)
Nick Sanders97c10552011-03-11 21:25:36 -0800160 elog "Running: scp ${scp_args} ${server}:/${path} ${target}"
Anton Staaff1afc3b2010-11-01 14:35:36 -0700161 scp ${scp_args} "${server}:/${path}" "${target}" ||
Kenneth Waters512eb162010-06-02 10:19:12 -0700162 rm -f "${target}"
163 ;;
164
165 file)
166 if [[ "${non_scheme:0:1}" == "/" ]]; then
167 cp "${non_scheme}" "${target}" || rm -f "${target}"
168 else
169 cp "${FILESDIR}/${non_scheme}" "${target}" ||
170 rm -f "${target}"
171 fi
172 ;;
173
174 *)
Allen Martin967a8242012-03-27 04:51:34 +0000175 die "Protocol for '${uri}' is unsupported."
Kenneth Waters512eb162010-06-02 10:19:12 -0700176 ;;
177 esac
Allen Martin967a8242012-03-27 04:51:34 +0000178
179 # if no checksum, generate a new one
180 if [[ -z "${CROS_BINARY_SUM}" ]]; then
181 local sha1=( $(sha1sum "${target}") )
182 elog "cros-binary ${target} is not checksummed. Use:"
183 elog "CROS_BINARY_SUM=\"${sha1[0]}\""
184 CROS_BINARY_SUM="${sha1[0]}"
185 fi
Nick Sanders97c10552011-03-11 21:25:36 -0800186 else
187 elog "Not downloading, cached copy available in ${target}"
Kenneth Waters512eb162010-06-02 10:19:12 -0700188 fi
189
Allen Martin967a8242012-03-27 04:51:34 +0000190 cros-binary_check_file || die "Failed to fetch ${uri}."
Kenneth Waters512eb162010-06-02 10:19:12 -0700191}
192
193cros-binary_src_unpack() {
194 cros-binary_fetch
195}
196
197cros-binary_src_install() {
Brian Harringcf0c7662012-12-19 23:06:44 -0800198 local target="${CROS_BINARY_URI##*/}"
199 if ${CROS_BINARY_FETCH_REQUIRED}; then
200 target="${CROS_BINARY_STORE_DIR}/${target}"
201 else
202 target="${DISTDIR}/${target}"
203 fi
Kenneth Waters512eb162010-06-02 10:19:12 -0700204
205 local extension="${CROS_BINARY_URI##*.}"
206 local flags
207
208 case "${CROS_BINARY_URI##*.}" in
209 gz|tgz) flags="z";;
210 bz2|tbz2) flags="j";;
211 *) die "Unsupported binary file format ${CROS_BINARY_URI##*.}"
212 esac
213
214 cd "${D}" || die
Puneet Kumar0cc9c652010-08-25 13:35:17 -0700215 tar "${flags}xpf" "${target}" ${CROS_BINARY_INSTALL_FLAGS} || die "Failed to unpack"
Kenneth Waters512eb162010-06-02 10:19:12 -0700216}
217
218EXPORT_FUNCTIONS src_unpack src_install
219