Skip to content

Commit a54fb3c

Browse files
committed
fix: paths broken in 1.1.4
1 parent 657b156 commit a54fb3c

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

bin/compile

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,26 @@ trap 'finish_error $LINENO' ERR
3434

3535
logstamp() { printf "[%s]" "$(TZ='America/Chicago' date +'%Y-%m-%d %H:%M:%S')" ; }
3636
topic() { echo "-----> $(logstamp) $*" ; }
37-
info() { echo " $*" ; }
38-
indent() { sed -u 's/^/ /' ; } # it always runs on linux. on darwin is 'sed -l'
37+
info() { echo " $*" ; }
38+
HEROKU_FREETDS_BUILDPACK_DEBUG="${HEROKU_FREETDS_BUILDPACK_DEBUG:-false}"
39+
debug() {
40+
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
41+
echo "[DEBUG] $*"
42+
fi
43+
}
44+
debug_topic() {
45+
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
46+
echo "[DEBUG] -----> $*"
47+
fi
48+
}
49+
error() { echo " ! $*" ; >&2 exit 1 ; }
50+
indent() {
51+
local command; command='s/^/ /'
52+
case $(uname) in
53+
Darwin) sed -l "$command";;
54+
*) sed -u "$command";;
55+
esac
56+
}
3957

4058
BUILD_DIR="$1"
4159
CACHE_DIR="$2"
@@ -46,11 +64,21 @@ ENV_DIR="${3}"
4664
# echo "$SOURCE_VERSION"
4765
# See https://devcenter.heroku.com/articles/buildpack-api#bin-compile-summary
4866

67+
# Store which STACK we are running on in the cache to bust the cache if it changes
68+
if [ -f "${CACHE_DIR}/.freetds/STACK" ]; then
69+
CACHED_STACK=$(cat "$CACHE_DIR/.freetds/STACK")
70+
else
71+
CACHED_STACK=$STACK
72+
fi
73+
# Ensure we store the STACK in the cache for next time.
74+
mkdir -p "$CACHE_DIR/.freetds"
75+
echo "$STACK" > "$CACHE_DIR/.freetds/STACK"
76+
4977
BIN_DIR="$(cd "$(dirname "$0")"; pwd)" # absolute path
5078
LP_DIR="$(cd "$(dirname "$0")"; cd ..; pwd)"
5179
ROOT_DIR="$(dirname "$BIN_DIR")"
5280

53-
info "BIN_DIR=${BIN_DIR}, ROOT_DIR=${ROOT_DIR}, BUILD_DIR=${BUILD_DIR}, CACHE_DIR=${CACHE_DIR}, pwd=$(pwd)"
81+
info "BIN_DIR=${BIN_DIR}, ROOT_DIR=${ROOT_DIR}, BUILD_DIR=${BUILD_DIR}, CACHE_DIR=${CACHE_DIR}, STACK=${STACK} CACHED_STACK=${CACHED_STACK} pwd=$(pwd)"
5482

5583
load_env_vars() {
5684
local env_var; env_var="${1:-}"
@@ -63,18 +91,26 @@ FREETDS_ARCHIVE_NAME="${FREETDS_ARCHIVE_NAME:-freetds-${FREETDS_VERSION}}"
6391
TDS_VERSION="${TDS_VERSION:-7.3}" # or TDSVER
6492
USE_GNUTLS="${USE_GNUTLS:---with-gnutls}"
6593

66-
CACHED_TAR="${CACHE_DIR}/freetds-${FREETDS_VERSION}-heroku.tar.bz2"
67-
# Default rebuild to true since I'm having issues linking the library to tiny_tds gem with a cached build.
68-
if [ "${FREETDS_REBUILD:-}" = "true" ]; then
94+
FREETDS_CACHE_DIR="$CACHE_DIR"
95+
mkdir -p "${FREETDS_CACHE_DIR}"
96+
CACHED_TAR="${FREETDS_CACHE_DIR}/freetds-${FREETDS_VERSION}-heroku.tar.bz2"
97+
if [ "$CACHED_STACK" != "$STACK" ] ; then
98+
topic "Stack has changed. Clearing cache"
99+
rm -f "$CACHED_TAR"
100+
elif [ "${FREETDS_REBUILD:-}" = "true" ]; then
101+
# Default rebuild to true since I'm having issues linking the library to tiny_tds gem with a cached build.
102+
topic "FREETDS_REBUILD is true. Clearing cache"
69103
rm -f "$CACHED_TAR"
70104
fi
71105
# This installation target is in the BUILD_DIR, which means that
72106
# it'll end up in the slug at runtime.
73-
BUILD_TARGET_DIR="${BUILD_DIR}/freetds"
107+
# Assert $BUILD_TARGET_DIR = $HOME/freetds | $BUILD_DIR/freetds # /app/freetds
108+
BUILD_TARGET_DIR="${BUILD_DIR}/freetds" # /app/freetds
74109
mkdir -p "${BUILD_TARGET_DIR}"
75110

76111
# This is the path that will be used at dyno runtime, and in which we build.
77-
APP_TARGET_DIR="/app/freetds"
112+
# Assert $APP_TARGET_DIR = $HOME/freetds # /app/freetds
113+
APP_TARGET_DIR="${HOME:-app}/freetds"
78114
mkdir -p "${APP_TARGET_DIR}"
79115

80116
configure_app_env_vars() {
@@ -83,46 +119,55 @@ configure_app_env_vars() {
83119

84120
# These exports must point to /app, because the profile is
85121
# executed in a running dyno, not the buildpack environment
122+
# Assert $APP_TARGET_DIR = $HOME/freetds # /app/freetds
86123
cat <<EOF > "${BUILD_DIR}/.profile.d/freetds.sh"
87-
export PATH="${APP_TARGET_DIR}/bin:\$PATH"
124+
export PATH="\${HOME}/freetds/bin:\$PATH"
88125
# tiny_tds extconf.rb uses FREETDS_DIR
89126
# https://github.com/rails-sqlserver/tiny_tds/blob/5046755ca91594003f8b3ca541d136f3ed859973/ext/tiny_tds/extconf.rb#L36-L38
90-
export FREETDS_DIR="${APP_TARGET_DIR}"
91-
export LD_LIBRARY_PATH="${APP_TARGET_DIR}/lib:${LD_LIBRARY_PATH:-/usr/local/lib}"
92-
export LD_RUN_PATH="${APP_TARGET_DIR}/lib:${LD_RUN_PATH:-/usr/local/lib}"
93-
export LIBRARY_PATH="${APP_TARGET_DIR}/lib:${LIBRARY_PATH:-/usr/local/lib}"
94-
export SYBASE="${APP_TARGET_DIR}"
127+
export FREETDS_DIR="\${HOME}/freetds"
128+
export LD_LIBRARY_PATH="\${HOME}/freetds/lib:\${LD_LIBRARY_PATH:-/usr/local/lib}"
129+
export LD_RUN_PATH="\${HOME}/freetds/lib:\${LD_RUN_PATH:-/usr/local/lib}"
130+
export LIBRARY_PATH="\${HOME}/freetds/lib:\${LIBRARY_PATH:-/usr/local/lib}"
131+
export SYBASE="\${HOME}/freetds"
95132
EOF
96133
chmod +x "${BUILD_DIR}/.profile.d/freetds.sh"
97134
echo "configured APP env vars:" | indent
98135
indent < "${BUILD_DIR}/.profile.d/freetds.sh"
99-
# shellcheck disable=SC1090
136+
# shellcheck disable=SC1091
100137
. "${BUILD_DIR}/.profile.d/freetds.sh"
101138
}
102139

103140
configure_buildpack_env_vars() {
104141
# These exports point to the build directory, not to /app, so that
105142
# they work for later buildpacks.
143+
# Assert $BUILD_TARGET_DIR = $HOME/freetds | $BUILD_DIR/freetds # /app/freetds
106144
export PATH="${BUILD_TARGET_DIR}/bin:$PATH"
107145
export FREETDS_DIR="${BUILD_TARGET_DIR}"
108146
export LD_LIBRARY_PATH="${BUILD_TARGET_DIR}/lib:${LD_LIBRARY_PATH:-/usr/local/lib}"
109147
export LD_RUN_PATH="${BUILD_TARGET_DIR}/lib:${LD_RUN_PATH:-/usr/local/lib}"
110148
export LIBRARY_PATH="${BUILD_TARGET_DIR}/lib:${LIBRARY_PATH:-/usr/local/lib}"
149+
# Assert $APP_TARGET_DIR = $HOME/freetds # /app/freetds
111150
export SYBASE="${APP_TARGET_DIR}"
112151
# give environment to later buildpacks
113-
export | grep -E -e ' (PATH|LD_LIBRARY_PATH|LIBRARY_PATH|FREETDS_DIR|SYBASE)=' > "${LP_DIR}/export"
152+
export | grep -E -e ' (PATH|LD_LIBRARY_PATH|LD_RUN_PATH|LIBRARY_PATH|FREETDS_DIR|SYBASE)=' > "${LP_DIR}/export"
114153

115154
echo "configured BUILDPACK env vars:" | indent
116155
indent < "${LP_DIR}/export"
156+
157+
topic "Rewrite package-config files"
158+
# shellcheck disable=SC2038
159+
find "$BUILD_DIR/freetds" -type f -ipath '*/pkgconfig/*.pc' | xargs --no-run-if-empty -n 1 sed -i -e 's!^prefix=\(.*\)$!prefix='"$BUILD_DIR"'/freetds\1!g'
117160
}
118161

119162
download_and_extract_freetds_archive() {
120163
FREETDS_FILE="$FREETDS_ARCHIVE_NAME.tar.gz"
121164
FREETDS_URL="https://www.freetds.org/files/stable/${FREETDS_FILE}"
122-
# TODO(BF): Print log when HEROKUR_FREETDS_BUILDPACK_DEBUG is set
123165
# TODO(BF): Consider calculating and verifying SHA256: openssl dgst -sha256 < "$FREETDS_FILE"
124166
curl -s "$FREETDS_URL" | tar xzvf - -C "${BUILD_DIR}" > "${BUILD_DIR}/build_log-unpack.log" # Can't write to the archive dir until it exists
125167
mv "${BUILD_DIR}/build_log-unpack.log" "${BUILD_DIR}/${FREETDS_ARCHIVE_NAME}/"
168+
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
169+
debug "$(cat "${BUILD_DIR}/build_log-unpack.log")"
170+
fi
126171
}
127172

128173
build_and_install_freetds() {
@@ -152,13 +197,20 @@ build_and_install_freetds() {
152197
--disable-debug \
153198
"--with-tdsver=${TDS_VERSION}"
154199
EOF
155-
# TODO(BF): Print log when HEROKUR_FREETDS_BUILDPACK_DEBUG is set
156200
/bin/bash .build_options > build_log-configure.stdout.log 2> build_log-configure.stderr.log
201+
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
202+
debug "$(cat .build_options)"
203+
debug "$(cat build_log-configure.stdout.log)"
204+
debug "$(cat build_log-configure.stderr.log)"
205+
fi
157206
# Flush make output to log since a file will be faster than printing to the screen.
158-
# TODO(BF): Print log when HEROKUR_FREETDS_BUILDPACK_DEBUG is set
159207
{
160208
make && make install && make clean
161209
} > build_log-make_install.stdout.log 2> build_log-make_install.stderr.log
210+
if [ "$HEROKU_FREETDS_BUILDPACK_DEBUG" = "true" ]; then
211+
debug "$(cat build_log-make_install.stdout.log)"
212+
debug "$(cat build_log-make_install.stderr.log)"
213+
fi
162214
cp .build_options build_log-* "${APP_TARGET_DIR}/"
163215

164216
cd "$BUILD_DIR"

0 commit comments

Comments
 (0)