|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +LIBPG_REPO=___LIBPG_REPO___ |
| 4 | +LIBPG_COMMIT=___LIBPG_COMMIT___ |
| 5 | +LIBPG_BRANCH=___LIBPG_BRANCH___ |
| 6 | + |
| 7 | +# Check if variables are set and exit if not |
| 8 | +if [ -z "$LIBPG_COMMIT" ]; then |
| 9 | + echo "ERROR: LIBPG_COMMIT variable is not set." |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +if [ -z "$LIBPG_BRANCH" ]; then |
| 14 | + echo "ERROR: LIBPG_BRANCH variable is not set." |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +if [ -z "$LIBPG_REPO" ]; then |
| 19 | + echo "ERROR: LIBPG_REPO variable is not set." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Remember current directory and create a new, unique, temporary directory |
| 24 | +rDIR=$(pwd) |
| 25 | +tmpDir=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmpdir.XXXX') |
| 26 | + |
| 27 | +# Define the make target |
| 28 | +makeTarget=build |
| 29 | + |
| 30 | +# Change to the newly created temp directory |
| 31 | +cd "$tmpDir" |
| 32 | + |
| 33 | +# Clone the selected branch of the libpg_query Git repo |
| 34 | +git clone -b $LIBPG_BRANCH --single-branch $LIBPG_REPO |
| 35 | +cd libpg_query |
| 36 | + |
| 37 | +# Checkout the desired commit |
| 38 | +git checkout $LIBPG_COMMIT |
| 39 | + |
| 40 | +# needed if being invoked from within gyp |
| 41 | +unset MAKEFLAGS |
| 42 | +unset MFLAGS |
| 43 | + |
| 44 | +# Adaptively build for macOS or Linux |
| 45 | +if [ "$(uname)" == "Darwin" ]; then |
| 46 | + make CFLAGS='-mmacosx-version-min=10.7' PG_CFLAGS='-mmacosx-version-min=10.7' $makeTarget |
| 47 | +elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then |
| 48 | + make CFLAGS='' PG_CFLAGS='' $makeTarget |
| 49 | +fi |
| 50 | + |
| 51 | +# Terminate if build fails |
| 52 | +if [ $? -ne 0 ]; then |
| 53 | + echo "ERROR: 'make' command failed"; |
| 54 | + exit 1; |
| 55 | +fi |
| 56 | + |
| 57 | +# Search for libpg_query.a, error if not found |
| 58 | +file=$(ls | grep 'libpg_query.a') |
| 59 | +if [ ! $file ]; then |
| 60 | + echo "ERROR: libpg_query.a not found"; |
| 61 | + exit 1; |
| 62 | +fi |
| 63 | + |
| 64 | +# Error if pg_query.h is missing |
| 65 | +file=$(ls | grep 'pg_query.h') |
| 66 | +if [ ! $file ]; then |
| 67 | + echo "ERROR: pg_query.h not found"; |
| 68 | + exit 1; |
| 69 | +fi |
| 70 | + |
| 71 | +# Copy queryparser.cc, binding.gyp to current directory |
| 72 | +if [ "$(uname)" == "Darwin" ]; then |
| 73 | + cp $(pwd)/libpg_query.a $rDIR/libpg_query/osx/ |
| 74 | +elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then |
| 75 | + cp $(pwd)/libpg_query.a $rDIR/libpg_query/linux/ |
| 76 | +fi |
| 77 | + |
| 78 | +# Copy header |
| 79 | +cp $(pwd)/pg_query.h $rDIR/libpg_query/include/ |
| 80 | +cp $(pwd)/protobuf/*.proto $rDIR/libpg_query/protobuf/ |
| 81 | + |
| 82 | +# Cleanup: revert to original directory and remove the temp |
| 83 | +cd "$rDIR" |
| 84 | +rm -rf "$tmpDir" |
0 commit comments