-
Notifications
You must be signed in to change notification settings - Fork 42
/
make-macos-pkg.sh
executable file
·152 lines (121 loc) · 4.26 KB
/
make-macos-pkg.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
#! /bin/sh
# Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# Builds a self-installer package for macOS.
#
# This assumes Cargo, OSXFUSE, and pkg-config are installed.
#
# All arguments given to this script are delegated to "configure".
# Directory name of the running script.
DirName="$(dirname "${0}")"
# Base name of the running script.
ProgName="${0##*/}"
# Prints the given error message to stderr and exits.
#
# \param ... The message to print.
err() {
echo "${ProgName}: E: $*" 1>&2
exit 1
}
# Prints the given informational message to stderr.
#
# \param ... The message to print.
info() {
echo "${ProgName}: I: $*" 1>&2
}
# Creates the package scripts.
#
# \param dir Directory in which to store the scripts.
write_scripts() {
local dir="${1}"; shift
mkdir -p "${dir}"
cat >"${dir}/postinstall" <<EOF
#! /bin/sh
# Try to configure OSXFUSE immediately after installation so that things
# work as intended without having to first go through a reboot.
/usr/local/libexec/sandboxfs/setup-osxfuse.sh || true
EOF
chmod +x "${dir}/postinstall"
}
# Modifies the fresh sandboxfs installation for our packaging needs.
#
# \param root Path to the new file system root used to build the package.
configure_root() {
local root="${1}"; shift
mkdir -p "${root}/etc/paths.d"
cat >"${root}/etc/paths.d/sandboxfs" <<EOF
/usr/local/bin
EOF
mkdir -p "${root}/Library/LaunchDaemons"
cp admin/org.bazelbuild.sandboxfs.setup-osxfuse.plist \
"${root}/Library/LaunchDaemons"
mkdir -p "${root}/usr/local/libexec/sandboxfs"
cp admin/setup-osxfuse.sh "${root}/usr/local/libexec/sandboxfs"
cat >"${root}/usr/local/libexec/sandboxfs/uninstall.sh" <<EOF
#! /bin/sh
cd /
for f in \$(tail -r /usr/local/share/sandboxfs/manifest); do
if [ ! -d "\${f}" ]; then
rm "\${f}"
else
# Some directories are shared with other packages (like
# /usr/local/bin) so just ignore errors during their removal.
rmdir "\${f}" 2>/dev/null || true
fi
done
EOF
chmod +x "${root}/usr/local/libexec/sandboxfs/uninstall.sh"
mkdir -p "${root}/usr/local/share/sandboxfs"
touch "${root}/usr/local/share/sandboxfs/manifest"
( cd "${root}" && \
find . | sort >>"${root}/usr/local/share/sandboxfs/manifest" )
}
# Program's entry point.
main() {
[ "$(uname -s)" = Darwin ] || err "This script is for macOS only"
[ -x "${DirName}/../configure" ] || err "configure not found; make" \
"sure to run this from a cloned repository"
local tempdir
tempdir="$(mktemp -d "${TMPDIR:-/tmp}/${ProgName}.XXXXXX" 2>/dev/null)" \
|| err "Failed to create temporary directory"
trap "rm -rf '${tempdir}'" EXIT
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig # For OSXFUSE.
info "Cloning fresh copy of the source tree"
git clone "${DirName}/.." "${tempdir}/src"
info "Building and installing into temporary root"
(
set -e
cd "${tempdir}/src"
./configure --goroot=none --prefix=/usr/local "${@}"
make release
make install DESTDIR="${tempdir}/root"
) || err "Build failed"
info "Preparing temporary root for packaging"
configure_root "${tempdir}/root"
local version="$(grep ^version "${tempdir}/src/Cargo.toml" \
| cut -d '"' -f 2)"
local revision="$(date +%Y%m%d)"
local pkgversion="${version}-${revision}"
local pkgfile="sandboxfs-${pkgversion}-macos.pkg"
info "Building package ${pkgfile}"
write_scripts "${tempdir}/scripts"
( cd "${tempdir}/root" && find . ) | sed 's,^,MANIFEST: ,'
pkgbuild \
--identifier com.github.bazelbuild.sandboxfs \
--root "${tempdir}/root" \
--scripts "${tempdir}/scripts" \
--version "${pkgversion}" \
"${pkgfile}"
}
main "${@}"