forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-fido2-macos.sh
executable file
·277 lines (239 loc) · 6.93 KB
/
build-fido2-macos.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
#
# Builds libfido2 and dependencies, caching the resulting binaries in the local
# filesystem.
#
# Run `build-fido2-macos.sh build` to build libfido2 and its dependencies, at
# the versions specified in the script.
# Run `build-fido2-macos.sh pkg_config_path` to print the path to the
# prior-built libfido2-static.pc file.
#
# Written mainly for macOS builders.
set -eu
readonly MACOS_VERSION_MIN=11.0
# Cross-architecture building
# Set C_ARCH to $(uname -m) if unset, and validate supported architecture
if ! [[ "${C_ARCH:=$(uname -m)}" =~ ^(x86_64|arm64)$ ]]; then
echo "unknown or unsupported build architecture: $C_ARCH" >&2
exit 1
fi
# Note: versions are the same as the corresponding git tags for each repo.
readonly CBOR_VERSION=v0.11.0
readonly CBOR_COMMIT=170bee2b82cdb7b2ed25af301f62cb6efdd40ec1
readonly CRYPTO_VERSION=openssl-3.0.15
readonly CRYPTO_COMMIT=c523121f902fde2929909dc7f76b13ceb4961efe
readonly FIDO2_VERSION=1.15.0
readonly FIDO2_COMMIT=f87c19c9487c0131531314d9ccb475ea5325794e
readonly LIB_CACHE="/tmp/teleport-fido2-cache-$C_ARCH"
readonly PKGFILE_DIR="$LIB_CACHE/fido2-${FIDO2_VERSION}_cbor-${CBOR_VERSION}_crypto-${CRYPTO_VERSION}"
# Library cache paths, implicitly matched by fetch_and_build.
readonly CBOR_PATH="$LIB_CACHE/cbor-$CBOR_VERSION"
readonly CRYPTO_PATH="$LIB_CACHE/crypto-$CRYPTO_VERSION"
readonly FIDO2_PATH="$LIB_CACHE/fido2-$FIDO2_VERSION"
# List of folders/files to remove on exit.
# See cleanup and main.
CLEANUPS=()
fetch_and_build() {
local name="$1" # eg, cbor
local version="$2" # eg, v0.9.0
local commit="$3" # eg, 58b3319b8c3ec15171cb00f01a3a1e9d400899e1
local url="$4" # eg, https://github.com/...
local buildcmd="$5" # eg, cbor_build, a bash function name
echo "$name: fetch and build" >&2
mkdir -p "$LIB_CACHE"
local tmp=''
tmp="$(mktemp -d "$LIB_CACHE/build.XXXXXX")"
CLEANUPS+=("$tmp")
local fullname="$name-$version"
local install_path="$tmp/$fullname"
cd "$tmp"
git clone --depth=1 -b "$version" "$url"
cd "$(ls)" # a single folder exists at this point
local head
head="$(git rev-parse HEAD)"
if [[ "$head" != "$commit" ]]; then
echo "Found unexpected HEAD commit for $name, aborting: $head" >&2
exit 1
fi
mkdir -p "$install_path"
eval "$buildcmd '$PWD' '$install_path'"
# Fix path in pkgconfig files.
local dest="$LIB_CACHE/$fullname"
find "$install_path" \
-name '*.pc' \
-exec sed -i '' "s@$install_path@$dest@g" {} +
# Check if another builder beat us. Builds _should_ be equivalent.
if [[ ! -d "$dest" ]]; then
echo "$name: moving $fullname to $dest" >&2
mv "$install_path" "$dest"
fi
}
cbor_build() {
local src="$1"
local dest="$2"
echo 'cbor: building' >&2
cd "$src"
cmake \
-DCMAKE_OSX_ARCHITECTURES="$C_ARCH" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="$dest" \
-DCMAKE_OSX_DEPLOYMENT_TARGET="$MACOS_VERSION_MIN" \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-DWITH_EXAMPLES=OFF \
-G "Unix Makefiles" \
.
make
make install
}
cbor_fetch_and_build() {
fetch_and_build \
cbor "$CBOR_VERSION" "$CBOR_COMMIT" 'https://github.com/pjk/libcbor.git' \
cbor_build
}
crypto_build() {
local src="$1"
local dest="$2"
echo 'crypto: building' >&2
cd "$src"
./Configure \
"darwin64-$C_ARCH-cc" \
-fPIC \
-mmacosx-version-min="$MACOS_VERSION_MIN" \
--prefix="$dest" \
no-shared \
no-zlib
# Build and copy only what we need instead of 'make && make install'.
# It's a bit quicker.
make build_generated libcrypto.a libcrypto.pc
mkdir -p "$dest/"{include,lib/pkgconfig}
cp -r include/openssl "$dest/include/"
cp libcrypto.a "$dest/lib/"
cp libcrypto.pc "$dest/lib/pkgconfig"
}
crypto_fetch_and_build() {
fetch_and_build \
crypto "$CRYPTO_VERSION" "$CRYPTO_COMMIT" \
'https://github.com/openssl/openssl.git' \
crypto_build
}
fido2_build() {
local src="$1"
local dest="$2"
echo 'fido2: building' >&2
cd "$src"
export PKG_CONFIG_PATH="$LIB_CACHE/cbor-$CBOR_VERSION/lib/pkgconfig:$LIB_CACHE/crypto-$CRYPTO_VERSION/lib/pkgconfig"
cmake \
-DBUILD_EXAMPLES=OFF \
-DBUILD_MANPAGES=OFF \
-DBUILD_TOOLS=OFF \
-DCMAKE_OSX_ARCHITECTURES="$C_ARCH" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="$dest" \
-DCMAKE_OSX_DEPLOYMENT_TARGET="$MACOS_VERSION_MIN" \
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
-G "Unix Makefiles" \
.
grep 'CRYPTO_VERSION:INTERNAL=3\.0\.' CMakeCache.txt # double-check OpenSSL
make
make install
}
fido2_fetch_and_build() {
fetch_and_build \
fido2 "$FIDO2_VERSION" "$FIDO2_COMMIT" \
'https://github.com/Yubico/libfido2.git' \
fido2_build
}
fido2_compile_toy() {
local toydir=''
toydir="$(mktemp -d)"
CLEANUPS+=("$toydir")
cat >"$toydir/toy.c" <<EOF
#include <fido.h>
int main() {
fido_init(0 /* flags */);
return 0;
}
EOF
export PKG_CONFIG_PATH="$PKGFILE_DIR"
# Word splitting desired for pkg-config.
#shellcheck disable=SC2046
gcc \
-arch "$C_ARCH" \
$(pkg-config --cflags --libs libfido2-static) \
-o "$toydir/toy.bin" \
"$toydir/toy.c"
}
usage() {
echo "Usage: $0 build|pkg_config_path" >&2
}
build() {
if [[ ! -d "$CBOR_PATH" ]]; then
cbor_fetch_and_build
fi
if [[ ! -d "$CRYPTO_PATH" ]]; then
crypto_fetch_and_build
fi
if [[ ! -d "$FIDO2_PATH" ]]; then
fido2_fetch_and_build
fi
local pkgfile="$PKGFILE_DIR/libfido2-static.pc"
if [[ ! -f "$pkgfile" ]]; then
local tmp=''
tmp="$(mktemp)" # file, not dir!
CLEANUPS+=("$tmp")
# Write libfido2-static.pc to tmp.
cat >"$tmp" <<EOF
prefix=$FIDO2_PATH
exec_prefix=\${prefix}
libdir=\${prefix}/lib
includedir=\${prefix}/include
Name: libfido2
Description: A FIDO2 library
URL: https://github.com/yubico/libfido2
Version: $FIDO2_VERSION
Libs: -framework CoreFoundation -framework IOKit \${libdir}/libfido2.a $CBOR_PATH/lib/libcbor.a $CRYPTO_PATH/lib/libcrypto.a
Cflags: -I\${includedir} -I$CBOR_PATH/include -I$CRYPTO_PATH/include -mmacosx-version-min="$MACOS_VERSION_MIN"
EOF
# Move .pc file to expected path.
mkdir -p "$PKGFILE_DIR"
if [[ ! -f "$pkgfile" ]]; then
echo "fido2: creating $pkgfile" >&2
mv "$tmp" "$pkgfile"
fi
fi
}
cleanup() {
# The strange looking expansion below (`${arr[@]+"${arr[@]}"}`) avoids unbound
# errors when the array is empty. (The actual expansion is quoted.)
# See https://stackoverflow.com/a/7577209.
#shellcheck disable=SC2068
for path in ${CLEANUPS[@]+"${CLEANUPS[@]}"}; do
echo "Removing: $path" >&2
rm -fr "$path"
done
}
main() {
if [[ $# -ne 1 ]]; then
usage
exit 1
fi
trap cleanup EXIT
case "$1" in
build)
build
if ! fido2_compile_toy; then
echo 'Failed to compile fido2 test program, cleaning cache and retrying' >&2
rm -fr "$CBOR_PATH" "$CRYPTO_PATH" "$FIDO2_PATH"
build
fi
;;
pkg_config_path)
echo "$PKGFILE_DIR"
;;
*)
usage
exit 1
;;
esac
}
main "$@"