-
Notifications
You must be signed in to change notification settings - Fork 339
/
compile
executable file
·144 lines (120 loc) · 5.69 KB
/
compile
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
#!/usr/bin/env bash
# bin/compile <build-dir> <cache-dir>
# fail fast
set -eo pipefail
# debug
# set -x
# parse and derive params
BUILD_DIR=$1
CACHE_DIR=$2
LP_DIR=$(cd "$(dirname "$0")"; cd ..; pwd)
function error() {
echo " ! $*" >&2
exit 1
}
function topic() {
echo "-----> $*"
}
function indent() {
c='s/^/ /'
case $(uname) in
Darwin) sed -l "$c";;
*) sed -u "$c";;
esac
}
if ! grep --invert-match -e "^\s*#" -e "^\s*$" -e "^:repo:" -q "${BUILD_DIR}/Aptfile"; then
echo "
! You have no packages listed in your Aptfile. If you don't need custom Apt packages,
! delete your Aptfile and remove the buildpack with:
!
! $ heroku buildpacks:remove heroku-community/apt
"
exit 0
fi
# Store which STACK we are running on in the cache to bust the cache if it changes
if [[ -f "$CACHE_DIR/.apt/STACK" ]]; then
CACHED_STACK=$(cat "$CACHE_DIR/.apt/STACK")
else
CACHED_STACK=$STACK
fi
# Ensure we store the STACK in the cache for next time.
mkdir -p "$CACHE_DIR/.apt"
echo "$STACK" > "$CACHE_DIR/.apt/STACK"
APT_CACHE_DIR="$CACHE_DIR/apt/cache"
APT_STATE_DIR="$CACHE_DIR/apt/state"
APT_SOURCELIST_DIR="$CACHE_DIR/apt/sources" # place custom sources.list here
APT_SOURCEPARTS_DIR="$APT_SOURCELIST_DIR/sources.list.d"
APT_SOURCES="$APT_SOURCELIST_DIR/sources.list"
APT_VERSION=$(apt-get -v | awk 'NR == 1{ print $2 }')
case "$APT_VERSION" in
0* | 1.0*) APT_FORCE_YES=("--force-yes");;
*) APT_FORCE_YES=("--allow-downgrades" "--allow-remove-essential" "--allow-change-held-packages");;
esac
if [[ -f "$APT_CACHE_DIR/Aptfile" ]] && cmp -s "$BUILD_DIR/Aptfile" "$APT_CACHE_DIR/Aptfile" && [[ "$CACHED_STACK" == "$STACK" ]] ; then
# Old Aptfile is the same as new and STACK has not changed
topic "Reusing cache"
else
# Aptfile changed or does not exist or STACK changed
topic "Detected Aptfile or Stack changes, flushing cache"
rm -rf "$APT_CACHE_DIR"
mkdir -p "$APT_CACHE_DIR/archives/partial"
mkdir -p "$APT_STATE_DIR/lists/partial"
mkdir -p "$APT_SOURCELIST_DIR" # make dir for sources
cp -f "$BUILD_DIR/Aptfile" "$APT_CACHE_DIR/Aptfile"
cat "/etc/apt/sources.list" > "$APT_SOURCES" # no cp here
cp -R "/etc/apt/sources.list.d" "$APT_SOURCEPARTS_DIR"
# add custom repositories from Aptfile to sources.list
# like>> :repo:deb http://cz.archive.ubuntu.com/ubuntu artful main universe
if grep -q -e "^:repo:" "$BUILD_DIR/Aptfile"; then
topic "Adding custom repositories"
grep -s -e "^:repo:" "$BUILD_DIR/Aptfile" | sed 's/^:repo:\(.*\)\s*$/\1/g' >> "$APT_SOURCES"
fi
fi
APT_OPTIONS=("-o" "debug::nolocking=true" "-o" "dir::cache=$APT_CACHE_DIR" "-o" "dir::state=$APT_STATE_DIR")
# Override the use of /etc/apt/sources.list (sourcelist) and /etc/apt/sources.list.d/* (sourceparts).
APT_OPTIONS+=("-o" "dir::etc::sourcelist=$APT_SOURCES" "-o" "dir::etc::sourceparts=$APT_SOURCEPARTS_DIR")
topic "Updating apt caches"
apt-get "${APT_OPTIONS[@]}" update 2>&1 | indent
while IFS= read -r PACKAGE; do
if [[ $PACKAGE == *deb ]]; then
PACKAGE_NAME=$(basename "$PACKAGE" .deb)
PACKAGE_FILE=$APT_CACHE_DIR/archives/$PACKAGE_NAME.deb
topic "Fetching $PACKAGE"
curl --silent --show-error --fail -L -z "$PACKAGE_FILE" -o "$PACKAGE_FILE" "$PACKAGE" 2>&1 | indent
else
topic "Fetching .debs for $PACKAGE"
# while this is not documented behavior, the Aptfile format technically
# did allow for multiple packages separated by spaces to be specified
# on a single line due to how the download command was implemented so we
# should respect that behavior since users are doing this
IFS=$' \t' read -ra PACKAGE_NAMES <<< "$PACKAGE"
apt-get "${APT_OPTIONS[@]}" -y "${APT_FORCE_YES[@]}" -d install --reinstall "${PACKAGE_NAMES[@]}" | indent
fi
done < <(grep --invert-match -e "^\s*#" -e "^\s*$" -e "^:repo:" "${BUILD_DIR}/Aptfile")
mkdir -p "$BUILD_DIR/.apt"
for DEB in "$APT_CACHE_DIR/archives/"*.deb; do
topic "Installing $(basename "$DEB")"
dpkg -x "$DEB" "$BUILD_DIR/.apt/"
done
topic "Writing profile script"
mkdir -p "$BUILD_DIR/.profile.d"
cat <<EOF >"$BUILD_DIR/.profile.d/000_apt.sh"
export PATH="\$HOME/.apt/usr/bin:\$PATH"
export LD_LIBRARY_PATH="\$HOME/.apt/usr/lib/x86_64-linux-gnu:\$HOME/.apt/usr/lib/i386-linux-gnu:\$HOME/.apt/usr/lib:\$LD_LIBRARY_PATH"
export LIBRARY_PATH="\$HOME/.apt/usr/lib/x86_64-linux-gnu:\$HOME/.apt/usr/lib/i386-linux-gnu:\$HOME/.apt/usr/lib:\$LIBRARY_PATH"
export INCLUDE_PATH="\$HOME/.apt/usr/include:\$HOME/.apt/usr/include/x86_64-linux-gnu:\$INCLUDE_PATH"
export CPATH="\$INCLUDE_PATH"
export CPPPATH="\$INCLUDE_PATH"
export PKG_CONFIG_PATH="\$HOME/.apt/usr/lib/x86_64-linux-gnu/pkgconfig:\$HOME/.apt/usr/lib/i386-linux-gnu/pkgconfig:\$HOME/.apt/usr/lib/pkgconfig:\$PKG_CONFIG_PATH"
EOF
export PATH="$BUILD_DIR/.apt/usr/bin:$PATH"
export LD_LIBRARY_PATH="$BUILD_DIR/.apt/usr/lib/x86_64-linux-gnu:$BUILD_DIR/.apt/usr/lib/i386-linux-gnu:$BUILD_DIR/.apt/usr/lib:$LD_LIBRARY_PATH"
export LIBRARY_PATH="$BUILD_DIR/.apt/usr/lib/x86_64-linux-gnu:$BUILD_DIR/.apt/usr/lib/i386-linux-gnu:$BUILD_DIR/.apt/usr/lib:$LIBRARY_PATH"
export INCLUDE_PATH="$BUILD_DIR/.apt/usr/include:$BUILD_DIR/.apt/usr/include/x86_64-linux-gnu:$INCLUDE_PATH"
export CPATH="$INCLUDE_PATH"
export CPPPATH="$INCLUDE_PATH"
export PKG_CONFIG_PATH="$BUILD_DIR/.apt/usr/lib/x86_64-linux-gnu/pkgconfig:$BUILD_DIR/.apt/usr/lib/i386-linux-gnu/pkgconfig:$BUILD_DIR/.apt/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
#give environment to later buildpacks
export | grep -E -e ' (PATH|LD_LIBRARY_PATH|LIBRARY_PATH|INCLUDE_PATH|CPATH|CPPPATH|PKG_CONFIG_PATH)=' > "$LP_DIR/export"
topic "Rewrite package-config files"
find "$BUILD_DIR/.apt" -type f -ipath '*/pkgconfig/*.pc' -print0 | xargs -0 --no-run-if-empty -n 1 sed -i -e 's!^prefix=\(.*\)$!prefix='"$BUILD_DIR"'/.apt\1!g'