Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / remoting / host / installer / mac / do_signing.sh
1 #!/bin/sh
2
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 # This script signs the Chromoting binaries, builds the Chrome Remote Desktop
8 # installer and then packages it into a .dmg.  It requires that Packages be
9 # installed (for 'packagesbuild').
10 # Packages: http://s.sudre.free.fr/Software/Packages/about.html
11 #
12 # usage: do_signing.sh output_dir input_dir [codesign_keychain codesign_id
13 #            [productsign_id]]
14 #
15 # The final disk image (dmg) is placed in |output_dir|.
16
17 set -e -u
18
19 ME="$(basename "${0}")"
20 readonly ME
21
22 declare -a g_cleanup_dirs
23
24 setup() {
25   local input_dir="${1}"
26
27   # The file that contains the properties for this signing build.
28   # The file should contain only key=value pairs, one per line.
29   PROPS_FILENAME="${input_dir}/do_signing.props"
30
31   # Individually load the properties for this build. Don't 'source' the file
32   # to guard against code accidentally being added to the props file.
33   HOST_UNINSTALLER_NAME=$(read_property "HOST_UNINSTALLER_NAME")
34   HOST_PKG=$(read_property "HOST_PKG")
35   DMG_VOLUME_NAME=$(read_property "DMG_VOLUME_NAME")
36   DMG_FILE_NAME=$(read_property "DMG_FILE_NAME")
37
38   # Binaries to sign.
39   ME2ME_HOST='PrivilegedHelperTools/org.chromium.chromoting.me2me_host.app'
40   ME2ME_NM_HOST="${ME2ME_HOST}/Contents/MacOS/native_messaging_host"
41   IT2ME_NM_HOST="${ME2ME_HOST}/Contents/MacOS/remote_assistance_host"
42   UNINSTALLER="Applications/${HOST_UNINSTALLER_NAME}.app"
43   PREFPANE='PreferencePanes/org.chromium.chromoting.prefPane'
44
45   # The Chromoting Host installer is a meta-package that consists of 3
46   # components:
47   #  * Chromoting Host Service package
48   #  * Chromoting Host Uninstaller package
49   #  * Keystone package (GoogleSoftwareUpdate - for Official builds only)
50   PKGPROJ_HOST='ChromotingHost.pkgproj'
51   PKGPROJ_HOST_SERVICE='ChromotingHostService.pkgproj'
52   PKGPROJ_HOST_UNINSTALLER='ChromotingHostUninstaller.pkgproj'
53
54   # Final (user-visible) pkg name.
55   PKG_FINAL="${HOST_PKG}.pkg"
56
57   DMG_FILE_NAME="${DMG_FILE_NAME}.dmg"
58
59   # Temp directory for Packages output.
60   PKG_DIR=build
61   g_cleanup_dirs+=("${PKG_DIR}")
62
63   # Temp directories for building the dmg.
64   DMG_TEMP_DIR="$(mktemp -d -t "${ME}"-dmg)"
65   g_cleanup_dirs+=("${DMG_TEMP_DIR}")
66
67   DMG_EMPTY_DIR="$(mktemp -d -t "${ME}"-empty)"
68   g_cleanup_dirs+=("${DMG_EMPTY_DIR}")
69 }
70
71 err() {
72   echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: ${@}" >&2
73 }
74
75 err_exit() {
76   err "${@}"
77   exit 1
78 }
79
80 # shell_safe_path ensures that |path| is safe to pass to tools as a
81 # command-line argument. If the first character in |path| is "-", "./" is
82 # prepended to it. The possibly-modified |path| is output.
83 shell_safe_path() {
84   local path="${1}"
85   if [[ "${path:0:1}" = "-" ]]; then
86     echo "./${path}"
87   else
88     echo "${path}"
89   fi
90 }
91
92 # Read a single property from the properties file.
93 read_property() {
94   local property="${1}"
95   local filename="${PROPS_FILENAME}"
96   echo `grep "${property}" "${filename}" | tail -n 1 | cut -d "=" -f2-`
97 }
98
99 verify_clean_dir() {
100   local dir="${1}"
101   if [[ ! -d "${dir}" ]]; then
102     mkdir "${dir}"
103   fi
104
105   if [[ -e "${output_dir}/${DMG_FILE_NAME}" ]]; then
106     err "Output directory is dirty from previous build."
107     exit 1
108   fi
109 }
110
111 sign() {
112   local name="${1}"
113   local keychain="${2}"
114   local id="${3}"
115
116   if [[ ! -e "${name}" ]]; then
117     err_exit "Input file doesn't exist: ${name}"
118   fi
119
120   echo Signing "${name}"
121   codesign -vv -s "${id}" --keychain "${keychain}" "${name}"
122   codesign -v "${name}"
123 }
124
125 sign_binaries() {
126   local input_dir="${1}"
127   local keychain="${2}"
128   local id="${3}"
129
130   sign "${input_dir}/${ME2ME_NM_HOST}" "${keychain}" "${id}"
131   sign "${input_dir}/${IT2ME_NM_HOST}" "${keychain}" "${id}"
132   sign "${input_dir}/${ME2ME_HOST}" "${keychain}" "${id}"
133   sign "${input_dir}/${UNINSTALLER}" "${keychain}" "${id}"
134   sign "${input_dir}/${PREFPANE}" "${keychain}" "${id}"
135 }
136
137 sign_installer() {
138   local input_dir="${1}"
139   local keychain="${2}"
140   local id="${3}"
141
142   local package="${input_dir}/${PKG_DIR}/${PKG_FINAL}"
143   productsign --sign "${id}" --keychain "${keychain}" \
144       "${package}" "${package}.signed"
145   mv -f "${package}.signed" "${package}"
146 }
147
148 build_package() {
149   local pkg="${1}"
150   echo "Building .pkg from ${pkg}"
151   packagesbuild -v "${pkg}"
152 }
153
154 build_packages() {
155   local input_dir="${1}"
156   build_package "${input_dir}/${PKGPROJ_HOST_SERVICE}"
157   build_package "${input_dir}/${PKGPROJ_HOST_UNINSTALLER}"
158   build_package "${input_dir}/${PKGPROJ_HOST}"
159 }
160
161 build_dmg() {
162   local input_dir="${1}"
163   local output_dir="${2}"
164
165   # Create the .dmg.
166   echo "Building .dmg..."
167   "${input_dir}/pkg-dmg" \
168       --format UDBZ \
169       --tempdir "${DMG_TEMP_DIR}" \
170       --source "${DMG_EMPTY_DIR}" \
171       --target "${output_dir}/${DMG_FILE_NAME}" \
172       --volname "${DMG_VOLUME_NAME}" \
173       --copy "${input_dir}/${PKG_DIR}/${PKG_FINAL}" \
174       --copy "${input_dir}/Scripts/keystone_install.sh:/.keystone_install"
175
176   if [[ ! -f "${output_dir}/${DMG_FILE_NAME}" ]]; then
177     err_exit "Unable to create disk image: ${DMG_FILE_NAME}"
178   fi
179 }
180
181 cleanup() {
182   if [[ "${#g_cleanup_dirs[@]}" > 0 ]]; then
183     rm -rf "${g_cleanup_dirs[@]}"
184   fi
185 }
186
187 usage() {
188   echo "Usage: ${ME} output_dir input_dir [keychain codesign_id"\
189       "[productsign_id]]" >&2
190   echo "  Sign the binaries using the specified <codesign_id>, build" >&2
191   echo "  the installer and then sign the installer using the given" >&2
192   echo "  <productsign_id>." >&2
193   echo "  If the <keychain> and signing ids are not specified then the" >&2
194   echo "  installer is built without signing any binaries." >&2
195 }
196
197 main() {
198   local output_dir="$(shell_safe_path "${1}")"
199   local input_dir="$(shell_safe_path "${2}")"
200   local do_sign_binaries=0
201   local keychain=""
202   if [[ ${#} -ge 3 ]]; then
203     keychain="$(shell_safe_path "${3}")"
204     do_sign_binaries=1
205     echo "Signing binaries using ${keychain}"
206   else
207     echo "Not signing binaries (no keychain or identify specified)"
208   fi
209   local codesign_id=""
210   if [[ ${#} -ge 4 ]]; then
211     codesign_id="${4}"
212   fi
213   local productsign_id=""
214   if [[ ${#} -ge 5 ]]; then
215     productsign_id="${5}"
216   fi
217
218   if [[ "${do_sign_binaries}" == 1 && -z "${codesign_id}" ]]; then
219     err_exit "Can't sign binaries - please specify a codesign_id"
220   fi
221
222   setup "${input_dir}"
223   verify_clean_dir "${output_dir}"
224
225   if [[ "${do_sign_binaries}" == 1 ]]; then
226     sign_binaries "${input_dir}" "${keychain}" "${codesign_id}"
227   fi
228   build_packages "${input_dir}"
229   if [[ "${do_sign_binaries}" == 1 && -n "${productsign_id}" ]]; then
230     echo "Signing installer..."
231     sign_installer "${input_dir}" "${keychain}" "${productsign_id}"
232   fi
233   build_dmg "${input_dir}" "${output_dir}"
234
235   cleanup
236 }
237
238 if [[ ${#} < 2 ]]; then
239   usage
240   exit 1
241 fi
242
243 main "${@}"
244 exit ${?}