forked from mapbox/mason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mason.sh
611 lines (512 loc) · 22.2 KB
/
mason.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
set -e
set -o pipefail
# set -x
MASON_ROOT=${MASON_ROOT:-`pwd`/mason_packages}
MASON_BUCKET=${MASON_BUCKET:-mason-binaries}
MASON_UNAME=`uname -s`
if [ ${MASON_UNAME} = 'Darwin' ]; then
MASON_PLATFORM=${MASON_PLATFORM:-osx}
MASON_XCODE_ROOT=`"xcode-select" -p`
elif [ ${MASON_UNAME} = 'Linux' ]; then
MASON_PLATFORM=${MASON_PLATFORM:-linux}
fi
# In non-interactive environments like Travis CI, we can't use -s because it'll fill up the log
# way too fast
case $- in
*i*) MASON_CURL_ARGS=-s ;; # interactive
*) MASON_CURL_ARGS= ;; # non-interative
esac
case ${MASON_UNAME} in
'Darwin') MASON_CONCURRENCY=`sysctl -n hw.ncpu` ;;
'Linux') MASON_CONCURRENCY=`nproc` ;;
*) MASON_CONCURRENCY=1 ;;
esac
function mason_step { >&2 echo -e "\033[1m\033[36m* $1\033[0m"; }
function mason_substep { >&2 echo -e "\033[1m\033[36m* $1\033[0m"; }
function mason_success { >&2 echo -e "\033[1m\033[32m* $1\033[0m"; }
function mason_error { >&2 echo -e "\033[1m\033[31m$1\033[0m"; }
case ${MASON_ROOT} in
*\ * ) mason_error "Directory '${MASON_ROOT} contains spaces."; exit ;;
esac
if [ ${MASON_PLATFORM} = 'osx' ]; then
export MASON_HOST_ARG="--host=x86_64-apple-darwin"
export MASON_DYNLIB_SUFFIX="dylib"
export MASON_PLATFORM_VERSION=`xcrun --sdk macosx --show-sdk-version`
MASON_SDK_ROOT=${MASON_XCODE_ROOT}/Platforms/MacOSX.platform/Developer
MASON_SDK_PATH="${MASON_SDK_ROOT}/SDKs/MacOSX${MASON_PLATFORM_VERSION}.sdk"
MIN_SDK_VERSION_FLAG="-mmacosx-version-min=10.8"
SYSROOT_FLAGS="-isysroot ${MASON_SDK_PATH} -arch x86_64 ${MIN_SDK_VERSION_FLAG}"
export CFLAGS="${SYSROOT_FLAGS}"
export CXXFLAGS="${CFLAGS} -fvisibility-inlines-hidden -stdlib=libc++ -std=c++11"
# NOTE: OSX needs '-stdlib=libc++ -std=c++11' in both CXXFLAGS and LDFLAGS
# to correctly target c++11 for build systems that don't know about it yet (like libgeos 3.4.2)
# But because LDFLAGS is also for C libs we can only put these flags into LDFLAGS per package
export LDFLAGS="-Wl,-search_paths_first ${SYSROOT_FLAGS}"
export CXX="${MASON_XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++"
export CC="${MASON_XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
elif [ ${MASON_PLATFORM} = 'ios' ]; then
export MASON_HOST_ARG="--host=arm-apple-darwin"
export MASON_DYNLIB_SUFFIX="dylib"
export MASON_PLATFORM_VERSION=`xcrun --sdk iphoneos --show-sdk-version`
MASON_SDK_ROOT=${MASON_XCODE_ROOT}/Platforms/iPhoneOS.platform/Developer
MASON_SDK_PATH="${MASON_SDK_ROOT}/SDKs/iPhoneOS${MASON_PLATFORM_VERSION}.sdk"
export MASON_IOS_CFLAGS="-miphoneos-version-min=${MASON_PLATFORM_VERSION} -isysroot ${MASON_SDK_PATH} -arch armv7 -arch armv7s -arch arm64"
if [[ ${MASON_PLATFORM_VERSION%%.*} -ge 9 ]]; then
export MASON_IOS_CFLAGS="${MASON_IOS_CFLAGS} -fembed-bitcode"
fi
if [ `xcrun --sdk iphonesimulator --show-sdk-version` != ${MASON_PLATFORM_VERSION} ]; then
mason_error "iPhone Simulator SDK version doesn't match iPhone SDK version"
exit 1
fi
MASON_SDK_ROOT=${MASON_XCODE_ROOT}/Platforms/iPhoneSimulator.platform/Developer
MASON_SDK_PATH="${MASON_SDK_ROOT}/SDKs/iPhoneSimulator${MASON_PLATFORM_VERSION}.sdk"
export MASON_ISIM_CFLAGS="-miphoneos-version-min=${MASON_PLATFORM_VERSION} -isysroot ${MASON_SDK_PATH} -arch i386 -arch x86_64"
elif [ ${MASON_PLATFORM} = 'linux' ]; then
export MASON_DYNLIB_SUFFIX="so"
# Assume current system is the target platform
if [ -z ${MASON_PLATFORM_VERSION} ] ; then
export MASON_PLATFORM_VERSION=`uname -m`
fi
export CFLAGS="-fPIC"
export CXXFLAGS="${CFLAGS} -std=c++11"
if [ `uname -m` != ${MASON_PLATFORM_VERSION} ] ; then
# Install the cross compiler
MASON_XC_PACKAGE_NAME=gcc
MASON_XC_PACKAGE_VERSION=${MASON_XC_GCC_VERSION:-4.9.2}-${MASON_PLATFORM_VERSION}
MASON_XC_PACKAGE=${MASON_XC_PACKAGE_NAME}-${MASON_XC_PACKAGE_VERSION}
MASON_XC_ROOT=$(MASON_PLATFORM_VERSION=`uname -m` MASON_PLATFORM= ${MASON_DIR:-~/.mason}/mason prefix ${MASON_XC_PACKAGE_NAME} ${MASON_XC_PACKAGE_VERSION})
if [ ! -d ${MASON_XC_ROOT} ] ; then
MASON_PLATFORM=$(MASON_PLATFORM_VERSION=`uname -m` ${MASON_DIR:-~/.mason}/mason install ${MASON_XC_PACKAGE_NAME} ${MASON_XC_PACKAGE_VERSION})
fi
# Load toolchain specific variables
if [ -f ${MASON_XC_ROOT}/toolchain.sh ] ; then
source ${MASON_XC_ROOT}/toolchain.sh
fi
fi
elif [ ${MASON_PLATFORM} = 'android' ]; then
export MASON_ANDROID_ABI=${MASON_ANDROID_ABI:-arm-v7}
CFLAGS="-fpic -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-integrated-as -O2 -g -DNDEBUG -fomit-frame-pointer -fstrict-aliasing -Wno-invalid-command-line-argument -Wno-unused-command-line-argument"
LDFLAGS="-no-canonical-prefixes"
export CPPFLAGS="-D__ANDROID__"
if [ ${MASON_ANDROID_ABI} = 'arm-v8' ]; then
MASON_ANDROID_TOOLCHAIN="aarch64-linux-android"
MASON_ANDROID_CROSS_COMPILER="aarch64-linux-android-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export CFLAGS="-target aarch64-none-linux-android -D_LITTLE_ENDIAN ${CFLAGS}"
export LDFLAGS="-target aarch64-none-linux-android -fuse-ld=gold ${LDFLAGS}"
export JNIDIR="arm64-v8a"
MASON_ANDROID_ARCH="arm64"
MASON_ANDROID_PLATFORM="21"
elif [ ${MASON_ANDROID_ABI} = 'arm-v7' ]; then
MASON_ANDROID_TOOLCHAIN="arm-linux-androideabi"
MASON_ANDROID_CROSS_COMPILER="arm-linux-androideabi-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export CFLAGS="-target armv7-none-linux-androideabi -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -D_LITTLE_ENDIAN ${CFLAGS}"
export LDFLAGS="-target armv7-none-linux-androideabi -march=armv7-a -Wl,--fix-cortex-a8 -fuse-ld=gold ${LDFLAGS}"
export JNIDIR="armeabi-v7a"
MASON_ANDROID_ARCH="arm"
MASON_ANDROID_PLATFORM="9"
elif [ ${MASON_ANDROID_ABI} = 'arm-v5' ]; then
MASON_ANDROID_TOOLCHAIN="arm-linux-androideabi"
MASON_ANDROID_CROSS_COMPILER="arm-linux-androideabi-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export CFLAGS="-target armv5te-none-linux-androideabi -march=armv5te -mtune=xscale -msoft-float -D_LITTLE_ENDIAN ${CFLAGS}"
export LDFLAGS="-target armv5te-none-linux-androideabi -march=armv5te -fuse-ld=gold ${LDFLAGS}"
export JNIDIR="armeabi"
MASON_ANDROID_ARCH="arm"
MASON_ANDROID_PLATFORM="9"
elif [ ${MASON_ANDROID_ABI} = 'x86' ]; then
MASON_ANDROID_TOOLCHAIN="i686-linux-android"
MASON_ANDROID_CROSS_COMPILER="x86-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export CFLAGS="-target i686-none-linux-android -march=i686 -msse3 -mfpmath=sse ${CFLAGS}"
export LDFLAGS="-target i686-none-linux-android -march=i686 -fuse-ld=gold ${LDFLAGS}"
export JNIDIR="x86"
MASON_ANDROID_ARCH="x86"
MASON_ANDROID_PLATFORM="9"
elif [ ${MASON_ANDROID_ABI} = 'x86-64' ]; then
MASON_ANDROID_TOOLCHAIN="x86_64-linux-android"
MASON_ANDROID_CROSS_COMPILER="x86_64-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export JNIDIR="x86_64"
export CFLAGS="-target x86_64-none-linux-android -march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel ${CFLAGS}"
export LDFLAGS="-target x86_64-none-linux-android -march=x86-64 -fuse-ld=gold ${LDFLAGS}"
MASON_ANDROID_ARCH="x86_64"
MASON_ANDROID_PLATFORM="21"
elif [ ${MASON_ANDROID_ABI} = 'mips' ]; then
MASON_ANDROID_TOOLCHAIN="mipsel-linux-android"
MASON_ANDROID_CROSS_COMPILER="mipsel-linux-android-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export CFLAGS="-target mipsel-none-linux-android ${CFLAGS}"
export LDFLAGS="-target mipsel-none-linux-android ${LDFLAGS}"
export JNIDIR="mips"
MASON_ANDROID_ARCH="mips"
MASON_ANDROID_PLATFORM="9"
elif [ ${MASON_ANDROID_ABI} = 'mips-64' ]; then
MASON_ANDROID_TOOLCHAIN="mips64el-linux-android"
MASON_ANDROID_CROSS_COMPILER="mips64el-linux-android-4.9"
export MASON_HOST_ARG="--host=${MASON_ANDROID_TOOLCHAIN}"
export CFLAGS="-target mips64el-none-linux-android ${CFLAGS}"
export LDFLAGS="-target mips64el-none-linux-android ${LDFLAGS}"
export JNIDIR="mips64"
MASON_ANDROID_ARCH="mips64"
MASON_ANDROID_PLATFORM="21"
fi
export CXXFLAGS="${CFLAGS}"
export MASON_DYNLIB_SUFFIX="so"
export MASON_PLATFORM_VERSION="${MASON_ANDROID_ABI}-${MASON_ANDROID_PLATFORM}"
MASON_API_LEVEL=${MASON_API_LEVEL:-android-$MASON_ANDROID_PLATFORM}
# Installs the native SDK
export MASON_NDK_PACKAGE_VERSION=${MASON_ANDROID_ARCH}-${MASON_ANDROID_PLATFORM}-r10e
MASON_SDK_ROOT=$(MASON_PLATFORM= ${MASON_DIR:-~/.mason}/mason prefix android-ndk ${MASON_NDK_PACKAGE_VERSION})
if [ ! -d ${MASON_SDK_ROOT} ] ; then
MASON_PLATFORM= ${MASON_DIR:-~/.mason}/mason install android-ndk ${MASON_NDK_PACKAGE_VERSION}
fi
MASON_SDK_PATH="${MASON_SDK_ROOT}/sysroot"
export PATH=${MASON_SDK_ROOT}/bin:${PATH}
export CXX="${MASON_ANDROID_TOOLCHAIN}-clang++"
export CC="${MASON_ANDROID_TOOLCHAIN}-clang"
export LD="${MASON_ANDROID_TOOLCHAIN}-ld"
export AR="${MASON_ANDROID_TOOLCHAIN}-ar"
export RANLIB="${MASON_ANDROID_TOOLCHAIN}-ranlib"
export STRIP="${MASON_ANDROID_TOOLCHAIN}-strip"
fi
# Variable defaults
MASON_HOST_ARG=${MASON_HOST_ARG:-}
MASON_PLATFORM_VERSION=${MASON_PLATFORM_VERSION:-0}
MASON_NAME=${MASON_NAME:-nopackage}
MASON_VERSION=${MASON_VERSION:-noversion}
MASON_HEADER_ONLY=${MASON_HEADER_ONLY:-false}
MASON_SLUG=${MASON_NAME}-${MASON_VERSION}
if [[ ${MASON_HEADER_ONLY} == true ]]; then
MASON_PLATFORM_ID=headers
else
MASON_PLATFORM_ID=${MASON_PLATFORM}-${MASON_PLATFORM_VERSION}
fi
MASON_PREFIX=${MASON_ROOT}/${MASON_PLATFORM_ID}/${MASON_NAME}/${MASON_VERSION}
MASON_BINARIES=${MASON_PLATFORM_ID}/${MASON_NAME}/${MASON_VERSION}.tar.gz
MASON_BINARIES_PATH=${MASON_ROOT}/.binaries/${MASON_BINARIES}
function mason_check_existing {
# skip installing if it already exists
if [ ${MASON_HEADER_ONLY:-false} = true ] ; then
if [ -d "${MASON_PREFIX}" ] ; then
mason_success "Already installed at ${MASON_PREFIX}"
exit 0
fi
elif [ ${MASON_SYSTEM_PACKAGE:-false} = true ]; then
if [ -f "${MASON_PREFIX}/version" ] ; then
mason_success "Using system-provided ${MASON_NAME} $(mason_system_version)"
exit 0
fi
else
if [ -f "${MASON_PREFIX}/${MASON_LIB_FILE}" ] ; then
mason_success "Already installed at ${MASON_PREFIX}"
exit 0
fi
fi
}
function mason_clear_existing {
if [ -d "${MASON_PREFIX}" ]; then
mason_step "Removing existing package... ${MASON_PREFIX}"
rm -rf "${MASON_PREFIX}"
fi
}
function mason_download {
mkdir -p "${MASON_ROOT}/.cache"
cd "${MASON_ROOT}/.cache"
if [ ! -f ${MASON_SLUG} ] ; then
mason_step "Downloading $1..."
curl --retry 3 ${MASON_CURL_ARGS} -f -L "$1" -o ${MASON_SLUG}
fi
MASON_HASH=`git hash-object ${MASON_SLUG}`
if [ "$2" != "${MASON_HASH}" ] ; then
mason_error "Hash ${MASON_HASH} of file ${MASON_ROOT}/.cache/${MASON_SLUG} doesn't match $2"
exit 1
fi
}
function mason_setup_build_dir {
rm -rf "${MASON_ROOT}/.build"
mkdir -p "${MASON_ROOT}/.build"
cd "${MASON_ROOT}/.build"
}
function mason_extract_tar_gz {
mason_setup_build_dir
tar xzf ../.cache/${MASON_SLUG} $@
}
function mason_extract_tar_bz2 {
mason_setup_build_dir
tar xjf ../.cache/${MASON_SLUG} $@
}
function mason_extract_tar_xz {
mason_setup_build_dir
tar xJf ../.cache/${MASON_SLUG} $@
}
function mason_prepare_compile {
:
}
function mason_compile {
mason_error "COMPILE FUNCTION MISSING"
exit 1
}
function mason_clean {
:
}
function link_files_in_root {
if [[ -d "${MASON_PREFIX}/$1/" ]] ; then
for i in $(find -H ${MASON_PREFIX}/$1/ -maxdepth 1 -mindepth 1 -name "*" ! -type d -print); do
common_part=$(python -c "import os;print(os.path.relpath('$i','${MASON_PREFIX}'))")
if [[ $common_part != '.' ]] && [[ ! -e "${MASON_ROOT}/.link/$common_part" ]]; then
mason_step "linking ${MASON_ROOT}/.link/$common_part"
mkdir -p $(dirname ${MASON_ROOT}/.link/$common_part)
ln -sf ${MASON_PREFIX}/$common_part ${MASON_ROOT}/.link/$common_part
else
mason_success "Already linked file ${MASON_ROOT}/.link/$common_part"
fi
done
fi
}
function link_files_recursively {
if [[ -d "${MASON_PREFIX}/$1/" ]] ; then
for i in $(find -H ${MASON_PREFIX}/$1/ -name "*" ! -type d -print); do
common_part=$(python -c "import os;print(os.path.relpath('$i','${MASON_PREFIX}'))")
if [[ $common_part != '.' ]] && [[ ! -e "${MASON_ROOT}/.link/$common_part" ]]; then
mason_step "linking ${MASON_ROOT}/.link/$common_part"
mkdir -p $(dirname ${MASON_ROOT}/.link/$common_part)
ln -sf ${MASON_PREFIX}/$common_part ${MASON_ROOT}/.link/$common_part
else
mason_success "Already linked file ${MASON_ROOT}/.link/$common_part"
fi
done
fi
}
function link_dir {
if [[ -d ${MASON_PREFIX}/$1 ]]; then
FOUND_SUBDIR=$(find ${MASON_PREFIX}/$1 -maxdepth 1 -mindepth 1 -name "*" -type d -print)
# for headers like boost that use include/boost it is most efficient to symlink just the directory
# skip linking include/google due to https://github.com/mapbox/mason/issues/81
if [[ ${FOUND_SUBDIR} ]] && [[ ! ${FOUND_SUBDIR} =~ "google" ]]; then
for dir in ${FOUND_SUBDIR}; do
local SUBDIR_BASENAME=$(basename $dir)
# skip man entries to avoid conflicts
if [[ $SUBDIR_BASENAME == "man" || $SUBDIR_BASENAME == "aclocal" || $SUBDIR_BASENAME == "doc" ]]; then
continue;
else
local TARGET_SUBDIR="${MASON_ROOT}/.link/$1/${SUBDIR_BASENAME}"
if [[ ! -d ${TARGET_SUBDIR} && ! -L ${TARGET_SUBDIR} ]]; then
mason_step "linking directory ${TARGET_SUBDIR}"
mkdir -p $(dirname ${TARGET_SUBDIR})
ln -s ${MASON_PREFIX}/$1/${SUBDIR_BASENAME} ${TARGET_SUBDIR}
else
mason_success "Already linked directory ${TARGET_SUBDIR}"
fi
fi
done
# still need to link files in the root directory for apps like postgres
link_files_in_root include
else
link_files_recursively include
fi
fi
}
function mason_link {
if [ ! -d "${MASON_PREFIX}" ] ; then
mason_error "${MASON_PREFIX} not found, please install first"
exit 0
fi
link_files_recursively lib
link_files_recursively bin
link_dir include
link_dir share
}
function mason_build {
mason_load_source
mason_step "Building for Platform '${MASON_PLATFORM}/${MASON_PLATFORM_VERSION}'..."
cd "${MASON_BUILD_PATH}"
mason_prepare_compile
if [ ${MASON_PLATFORM} = 'ios' ]; then
mason_substep "Building for Simulator..."
export CFLAGS="${MASON_ISIM_CFLAGS}"
cd "${MASON_BUILD_PATH}"
mason_compile
cd "${MASON_PREFIX}"
mv lib lib-isim
for i in lib-isim/*.a ; do lipo -info $i ; done
mason_substep "Building for iOS..."
export CFLAGS="${MASON_IOS_CFLAGS}"
cd "${MASON_BUILD_PATH}"
mason_clean
cd "${MASON_BUILD_PATH}"
mason_compile
cd "${MASON_PREFIX}"
cp -r lib lib-ios
for i in lib-ios/*.a ; do lipo -info $i ; done
# Create universal binary
mason_substep "Creating Universal Binary..."
cd "${MASON_PREFIX}/lib-ios"
for i in *.a ; do
lipo -create ../lib-ios/$i ../lib-isim/$i -output ../lib/$i
lipo -info ../lib/$i
done
cd "${MASON_PREFIX}"
rm -rf lib-isim lib-ios
elif [ ${MASON_PLATFORM} = 'android' ]; then
cd "${MASON_BUILD_PATH}"
mason_compile
else
cd "${MASON_BUILD_PATH}"
mason_compile
fi
mason_success "Installed at ${MASON_PREFIX}"
#rm -rf ${MASON_ROOT}/.build
}
function mason_try_binary {
MASON_BINARIES_DIR=`dirname "${MASON_BINARIES}"`
mkdir -p "${MASON_ROOT}/.binaries/${MASON_BINARIES_DIR}"
# try downloading from S3
if [ ! -f "${MASON_BINARIES_PATH}" ] ; then
mason_step "Downloading binary package ${MASON_BINARIES}..."
curl --retry 3 ${MASON_CURL_ARGS} -f -L \
https://${MASON_BUCKET}.s3.amazonaws.com/${MASON_BINARIES} \
-o "${MASON_BINARIES_PATH}.tmp" && \
mv "${MASON_BINARIES_PATH}.tmp" "${MASON_BINARIES_PATH}" || \
mason_step "Binary not available yet for ${MASON_BINARIES}"
else
mason_step "Updating binary package ${MASON_BINARIES}..."
curl --retry 3 ${MASON_CURL_ARGS} -f -L -z "${MASON_BINARIES_PATH}" \
https://${MASON_BUCKET}.s3.amazonaws.com/${MASON_BINARIES} \
-o "${MASON_BINARIES_PATH}.tmp"
if [ $? -eq 0 ] ; then
if [ -f "${MASON_BINARIES_PATH}.tmp" ]; then
mv "${MASON_BINARIES_PATH}.tmp" "${MASON_BINARIES_PATH}"
else
mason_step "Binary package is still up to date"
fi
else
mason_step "Binary not available yet for ${MASON_BINARIES}"
fi
fi
# unzip the file if it exists
if [ -f "${MASON_BINARIES_PATH}" ] ; then
mkdir -p "${MASON_PREFIX}"
cd "${MASON_PREFIX}"
# Try to force the ownership of the unpacked files
# to the current user using fakeroot if available
`which fakeroot` tar xzf "${MASON_BINARIES_PATH}"
if [ ! -z ${MASON_PKGCONFIG_FILE:-} ] ; then
if [ -f "${MASON_PREFIX}/${MASON_PKGCONFIG_FILE}" ] ; then
# Change the prefix
MASON_ESCAPED_PREFIX=$(echo "${MASON_PREFIX}" | sed -e 's/[\/&]/\\&/g')
sed -i.bak "s/prefix=.*/prefix=${MASON_ESCAPED_PREFIX}/" \
"${MASON_PREFIX}/${MASON_PKGCONFIG_FILE}"
fi
fi
mason_success "Installed binary package at ${MASON_PREFIX}"
exit 0
fi
}
function mason_pkgconfig {
echo pkg-config \
${MASON_PREFIX}/${MASON_PKGCONFIG_FILE}
}
function mason_cflags {
local FLAGS=$(`mason_pkgconfig` --static --cflags)
# Replace double-prefix in case we use a sysroot.
echo ${FLAGS//${MASON_SYSROOT}${MASON_PREFIX}/${MASON_PREFIX}}
}
function mason_ldflags {
local FLAGS=$(`mason_pkgconfig` --static --libs)
# Replace double-prefix in case we use a sysroot.
echo ${FLAGS//${MASON_SYSROOT}${MASON_PREFIX}/${MASON_PREFIX}}
}
function mason_static_libs {
if [ -z "${MASON_LIB_FILE}" ]; then
mason_substep "Linking ${MASON_NAME} ${MASON_VERSION} dynamically"
elif [ -f "${MASON_PREFIX}/${MASON_LIB_FILE}" ]; then
echo "${MASON_PREFIX}/${MASON_LIB_FILE}"
else
mason_error "No static library file '${MASON_PREFIX}/${MASON_LIB_FILE}'"
exit 1
fi
}
function mason_prefix {
echo ${MASON_PREFIX}
}
function mason_version {
if [ ${MASON_SYSTEM_PACKAGE:-false} = true ]; then
mason_system_version
else
echo ${MASON_VERSION}
fi
}
function mason_publish {
if [ ! ${MASON_HEADER_ONLY:-false} = true ] && [ ! -z ${MASON_LIB_FILE:-} ] && [ ! -f "${MASON_PREFIX}/${MASON_LIB_FILE}" ]; then
mason_error "Required library file ${MASON_PREFIX}/${MASON_LIB_FILE} doesn't exist."
exit 1
fi
if [ -z "${AWS_ACCESS_KEY_ID}" ]; then
mason_error "AWS_ACCESS_KEY_ID is not set."
exit 1
fi
if [ -z "${AWS_SECRET_ACCESS_KEY}" ]; then
mason_error "AWS_SECRET_ACCESS_KEY is not set."
exit 1
fi
mkdir -p `dirname ${MASON_BINARIES_PATH}`
cd "${MASON_PREFIX}"
rm -rf "${MASON_BINARIES_PATH}"
tar czf "${MASON_BINARIES_PATH}" .
(cd "${MASON_ROOT}/.binaries" && ls -lh "${MASON_BINARIES}")
mason_step "Uploading binary package..."
local CONTENT_TYPE="application/octet-stream"
local DATE="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
local MD5="$(openssl md5 -binary < "${MASON_BINARIES_PATH}" | base64)"
local SIGNATURE="$(printf "PUT\n$MD5\n$CONTENT_TYPE\n$DATE\nx-amz-acl:public-read\n/${MASON_BUCKET}/${MASON_BINARIES}" | openssl sha1 -binary -hmac "$AWS_SECRET_ACCESS_KEY" | base64)"
curl -S -T "${MASON_BINARIES_PATH}" https://${MASON_BUCKET}.s3.amazonaws.com/${MASON_BINARIES} \
-H "Date: $DATE" \
-H "Authorization: AWS $AWS_ACCESS_KEY_ID:$SIGNATURE" \
-H "Content-Type: $CONTENT_TYPE" \
-H "Content-MD5: $MD5" \
-H "x-amz-acl: public-read"
echo https://${MASON_BUCKET}.s3.amazonaws.com/${MASON_BINARIES}
curl -f -I https://${MASON_BUCKET}.s3.amazonaws.com/${MASON_BINARIES}
}
function mason_run {
if [ "$1" == "install" ]; then
if [ ${MASON_SYSTEM_PACKAGE:-false} = true ]; then
mason_check_existing
mason_clear_existing
mason_build
mason_success "Installed system-provided ${MASON_NAME} $(mason_system_version)"
else
mason_check_existing
mason_clear_existing
mason_try_binary
mason_build
fi
elif [ "$1" == "link" ]; then
mason_link
elif [ "$1" == "remove" ]; then
mason_clear_existing
elif [ "$1" == "publish" ]; then
mason_publish
elif [ "$1" == "build" ]; then
mason_clear_existing
mason_build
elif [ "$1" == "cflags" ]; then
mason_cflags
elif [ "$1" == "ldflags" ]; then
mason_ldflags
elif [ "$1" == "static_libs" ]; then
mason_static_libs
elif [ "$1" == "version" ]; then
mason_version
elif [ "$1" == "prefix" ]; then
mason_prefix
elif [ $1 ]; then
mason_error "Unknown command '$1'"
exit 1
else
mason_error "Usage: $0 <command> <lib> <version>"
exit 1
fi
}