forked from awslabs/aws-lambda-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackager
More file actions
executable file
·129 lines (112 loc) · 3.54 KB
/
Copy pathpackager
File metadata and controls
executable file
·129 lines (112 loc) · 3.54 KB
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
#!/bin/bash
# Copyright 2018-present Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file 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.
set -euo pipefail
print_help() {
echo -e "Usage: packager [OPTIONS] <binary name>\n"
echo -e "OPTIONS\n"
echo -e "\t-d,--default-libc\t Use the target host libc libraries. This will not package the C library files.\n"
}
if [ $# -lt 1 ]; then
echo -e "Error: missing arguments\n"
print_help
exit 1
fi
POSITIONAL=()
INCLUDE_LIBC=true
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-d|--default-libc)
INCLUDE_LIBC=false
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
PKG_BIN_PATH=$1
if [ ! -f "$PKG_BIN_PATH" ]; then
echo "$PKG_BIN_PATH" - No such file.;
exit 1;
fi
if ! type zip > /dev/null 2>&1; then
echo "zip utility is not found. Please install it and re-run this script"
exit 1
fi
function package_libc_via_pacman {
if [[ $(cat /etc/os-release | sed '/ID_LIKE/!d;s/ID_LIKE=//') == "archlinux" ]]; then
if type pacman > /dev/null 2>&1; then
pacman --files --list --quiet glibc | sed -E '/\.so$|\.so\.[0-9]+$/!d'
fi
fi
}
function package_libc_via_dpkg() {
if type dpkg-query > /dev/null 2>&1; then
dpkg-query --listfiles libc6 | sed -E '/\.so$|\.so\.[0-9]+$/!d'
fi
}
function package_libc_via_rpm() {
if type rpm > /dev/null 2>&1; then
rpm --query --list glibc | sed -E '/\.so$|\.so\.[0-9]+$/!d'
fi
}
PKG_BIN_FILENAME=`basename "$PKG_BIN_PATH"`
PKG_DIR=tmp
list=`ldd "$PKG_BIN_PATH" | awk '{print $(NF-1)}'`
if [[ $INCLUDE_LIBC == true ]]; then
list="$list $(package_libc_via_dpkg) $(package_libc_via_rpm) $(package_libc_via_pacman)"
fi
mkdir -p "$PKG_DIR/bin" "$PKG_DIR/lib"
for i in $list
do
if [[ ! -f $i ]]; then # ignore linux-vdso.so.1
continue
fi
cp $i $PKG_DIR/lib
filename=`basename $i`
if [[ -z "${filename##ld-*}" ]]; then
PKG_LD=$filename # Use this file as the loader
fi
done
bootstrap_script="#!/bin/bash\n
\n
set -euo pipefail\n
\n
exec \$LAMBDA_TASK_ROOT/lib/$PKG_LD --library-path \$LAMBDA_TASK_ROOT/lib \$LAMBDA_TASK_ROOT/bin/$PKG_BIN_FILENAME \${_HANDLER}\n
"
bootstrap_script_no_libc="#!/bin/bash\n
\n
set -euo pipefail\n
\n
exec \$LAMBDA_TASK_ROOT/lib/$PKG_LD --library-path \$LD_LIBRARY_PATH:\$LAMBDA_TASK_ROOT/lib \$LAMBDA_TASK_ROOT/bin/$PKG_BIN_FILENAME \${_HANDLER}\n
"
cp "$PKG_BIN_PATH" "$PKG_DIR/bin"
if [[ $INCLUDE_LIBC == true ]]; then
echo -e $bootstrap_script > "$PKG_DIR/bootstrap"
else
echo -e $bootstrap_script_no_libc > "$PKG_DIR/bootstrap"
fi
chmod +x "$PKG_DIR/bootstrap"
# some shenanigans to create the right layout in the zip file without extraneous directories
pushd "$PKG_DIR" > /dev/null
zip -r $PKG_BIN_FILENAME.zip *
ORIGIN_DIR=$(dirs -l +1)
mv $PKG_BIN_FILENAME.zip $ORIGIN_DIR
popd > /dev/null
rm -r "$PKG_DIR"
echo Created "$ORIGIN_DIR/$PKG_BIN_FILENAME".zip