python: Fix build issues when using "make build_cached"
[omd.git] / build_cached
blobfcae1625486bd89b78274474b8ec64a7594c1c15
1 #!/bin/bash
3 MAKE=$1
4 SRC=$2
5 CACHESUBDIR=$3
7 # skip caching if cache folder is not set or does not exist or omd version length is too small, which would be error prone
8 if [ "x$BUILD_CACHE" = "x" ] || ! test -d $BUILD_CACHE || [ ${#OMD_VERSION} -lt 5 ]; then
9 $MAKE -C "$SRC" build
10 exit $?
13 BUILD_CACHE=$BUILD_CACHE/$CACHESUBDIR
14 mkdir -p $BUILD_CACHE
16 $MAKE -C "$SRC" clean
17 MD5=$(cd "$SRC" && find . -type f -exec md5sum {} \; | sort | md5sum | awk '{ print $1 }')
18 CACHE_FILE="$BUILD_CACHE/${SRC}_${MD5}.tgz"
20 # cache hit - just unpack the already built package
21 if test -e "$CACHE_FILE"; then
22 echo "md5sum matched, using cache from $CACHE_FILE"
23 tar zxf "$CACHE_FILE"
24 find "$SRC/" -type f | \
25 while read file; do
26 if grep -Ic "###BUILD_OMD_VERSION###" "$file" >/dev/null 2>&1; then
27 cp -p "$file" "$file.tmp"
28 sed -e "s%###BUILD_OMD_VERSION###%$OMD_VERSION%g" -i "$file"
29 touch -r "$file.tmp" "$file"
30 rm -f "$file.tmp"
32 done
34 # The version is hard coded in OMD Python binary, fix that.
35 if [ $SRC = python ]; then
36 BIN_RPATH=$(strings python/tmp.python27/bin/python2.7 | grep ^/omd/versions)
37 NEW_RPATH=/omd/versions/$OMD_VERSION/lib
38 if [ ${#BIN_RPATH} -ne ${#NEW_RPATH} ]; then
39 echo "python: Can't replace rpath (different length: $BIN_RPATH, $NEW_RPATH). Use chrpath or patchelf to fix this."
40 exit 1
42 sed -i "s%$BIN_RPATH%$NEW_RPATH%g" python/tmp.python27/bin/python2.7
45 exit 0
47 EXCLUDES_FILE=$(mktemp)
48 find "$SRC" -type f > $EXCLUDES_FILE
50 # no cache hit - rebuild package and create cache tarball
51 set -e
52 $MAKE -C "$SRC" build
53 set +e
55 # create cache file
56 find "$SRC/" -type f | \
57 while read file; do
58 if grep -Ic "$OMD_VERSION" "$file" >/dev/null 2>&1; then
59 cp -p "$file" "$file.tmp"
60 sed -e "s%$OMD_VERSION%###BUILD_OMD_VERSION###%g" -i "$file"
61 touch -r "$file.tmp" "$file"
62 rm -f "$file.tmp"
64 done
65 TARBALL_TMP=$(mktemp)
66 tar cfz $TARBALL_TMP "$SRC" --exclude-from=$EXCLUDES_FILE
67 rm -f "$CACHE_FILE"
68 mv $TARBALL_TMP "$CACHE_FILE"
69 echo "cache file $CACHE_FILE created"
70 rm $EXCLUDES_FILE
71 find "$SRC/" -type f | \
72 while read file; do
73 if grep -Ic "###BUILD_OMD_VERSION###" "$file" >/dev/null 2>&1; then
74 cp -p "$file" "$file.tmp"
75 sed -e "s%###BUILD_OMD_VERSION###%$OMD_VERSION%g" -i "$file"
76 touch -r "$file.tmp" "$file"
77 rm -f "$file.tmp"
79 done
81 exit 0