Skip to content

Commit

Permalink
test: embedded libs
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Sep 11, 2024
1 parent a1e63fc commit 03438aa
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 29 deletions.
8 changes: 4 additions & 4 deletions internal/native/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
package native

/*
#cgo darwin,arm64 LDFLAGS: -L/tmp -L/usr/local/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo darwin,amd64 LDFLAGS: -L/tmp -L/usr/local/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo darwin,arm64 LDFLAGS: -L/tmp -L/usr/local/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi, -L${SRCDIR}/internal/native/libs/macos-aarch64
#cgo darwin,amd64 LDFLAGS: -L/tmp -L/usr/local/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi, -L${SRCDIR}/internal/native/libs/macos-x86_64
#cgo windows,amd64 LDFLAGS: -lpact_ffi
#cgo linux,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo linux,arm64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo linux,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi, -L${SRCDIR}/internal/native/libs/linux-aarch64
#cgo linux,arm64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi, -L${SRCDIR}/internal/native/libs/linux-x86_64
*/
import "C"
Binary file added internal/native/libs/linux-aarch64/libpact_ffi.so
Binary file not shown.
Binary file added internal/native/libs/linux-x86_64/libpact_ffi.so
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added internal/native/libs/windows-x86_64/pact_ffi.dll
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions scripts/download-libs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash -eu
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running

. "${SCRIPT_DIR}/lib/export-binary-versions.sh"
"${SCRIPT_DIR}/lib/download-ffi.sh"
25 changes: 0 additions & 25 deletions scripts/lib

This file was deleted.

89 changes: 89 additions & 0 deletions scripts/lib/download-ffi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash -eu
LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
. "${LIB_DIR}/robust-bash.sh"
. "${LIB_DIR}/download-file.sh"

require_binary curl
require_binary gunzip

require_env_var FFI_VERSION

BASEURL=https://github.com/pact-foundation/pact-reference/releases/download
FFI_DIR="${LIB_DIR}/../../internal/native/libs"

if [[ $(find "${FFI_DIR}" -name "${FFI_VERSION}*") ]]; then
log "Skipping download of FFI libraries ${FFI_VERSION}, as they exist"
exit 0
fi

warn "Cleaning ffi directory $FFI_DIR"
rm -rf "${FFI_DIR:?}"
mkdir -p "$FFI_DIR/macos-x86_64"
mkdir -p "$FFI_DIR/linux-x86_64"
mkdir -p "$FFI_DIR/linux-musl-x86_64"
mkdir -p "$FFI_DIR/windows-x86_64"
mkdir -p "$FFI_DIR/macos-aarch64"
mkdir -p "$FFI_DIR/linux-aarch64"
mkdir -p "$FFI_DIR/linux-musl-aarch64"

function download_ffi_file {
if [ -z "${1:-}" ]; then
error "${FUNCNAME[0]} requires the filename to download"
exit 1
fi
if [ -z "${2:-}" ]; then
error "${FUNCNAME[0]} requires the output filename to download"
exit 1
fi
FFI_FILENAME="$1"
OUTPUT_FILENAME="$2"

URL="${BASEURL}/libpact_ffi-${FFI_VERSION}/${FFI_FILENAME}"
DOWNLOAD_LOCATION="$FFI_DIR/${OUTPUT_FILENAME}"

log "Downloading ffi $FFI_VERSION for $FFI_FILENAME"
download_to "$URL" "$DOWNLOAD_LOCATION"
debug_log " ... downloaded to '$DOWNLOAD_LOCATION'"
}

function download_ffi {
if [ -z "${1:-}" ]; then
error "${FUNCNAME[0]} requires the environment filename suffix"
exit 1
fi
SUFFIX="$1"
PREFIX="${2:-}"
OUTPUT_FILENAME="${3:-}"

download_ffi_file "${PREFIX}pact_ffi-$SUFFIX" "${OUTPUT_FILENAME}"
debug_log " ... unzipping '$DOWNLOAD_LOCATION'"
gunzip -f "$DOWNLOAD_LOCATION"
}

if [[ ${RUNNER_OS:-} == 'Windows' ]]; then
ONLY_DOWNLOAD_PACT_FOR_WINDOWS=true
fi

if [ -z "${ONLY_DOWNLOAD_PACT_FOR_WINDOWS:-}" ]; then
download_ffi "linux-x86_64.so.gz" "lib" "linux-x86_64/libpact_ffi.so.gz"
download_ffi "linux-aarch64.so.gz" "lib" "linux-aarch64/libpact_ffi.so.gz"
download_ffi "linux-x86_64-musl.so.gz" "lib" "linux-musl-x86_64/libpact_ffi_musl.so.gz"
download_ffi "linux-aarch64-musl.so.gz" "lib" "linux-musl-aarch64/libpact_ffi_musl.so.gz"
download_ffi "macos-x86_64.dylib.gz" "lib" "macos-x86_64/libpact_ffi.dylib.gz"
download_ffi "macos-aarch64.dylib.gz" "lib" "macos-aarch64/libpact_ffi.dylib.gz"
else
warn "Skipped download of non-windows FFI libs because ONLY_DOWNLOAD_PACT_FOR_WINDOWS is set"
fi

download_ffi "windows-x86_64.dll.gz" "" "windows-x86_64/pact_ffi.dll.gz"
download_ffi "windows-x86_64.dll.lib.gz" "" "windows-x86_64/pact_ffi.dll.lib.gz"

# download_ffi_file "pact.h" "pact.h"
# download_ffi_file "pact-cpp.h" "pact-cpp.h"

# Write readme in the ffi folder
# cat << EOF > "$FFI_DIR/README.md"
# # FFI binaries

# This folder is automatically populated during build by /script/download-ffi.sh
# EOF
39 changes: 39 additions & 0 deletions scripts/lib/download-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash -eu
LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
. "${LIB_DIR}/robust-bash.sh"

function download_to {
if [ -z "${1:-}" ]; then
error "${FUNCNAME[0]} requires the URL to download from as the first argument"
exit 1
fi
if [ -z "${2:-}" ]; then
error "${FUNCNAME[0]} requires the file to save the download in as the second argument"
exit 1
fi
debug_log "about to download"
URL="$1"
OUTPUT_FILE="$2"
debug_log "doing curl of: '$URL', saving in $OUTPUT_FILE"

if [[ "$(uname -m)" == "Darwin" ]] || [[ "$(uname -m)" == "Linux" ]]; then
HTTP_CODE="$(curl --silent --output "$OUTPUT_FILE" --write-out "%{http_code}" --location "$URL")"
else
# temp workaround for curl 8.8.x error on windows gha runners
# https://github.com/curl/curl/issues/13845
curl --silent --output "$OUTPUT_FILE" --location "$URL"
if [ $? -ne 0 ]; then
error "Unable to download file at url ${URL}"
exit 1
else
HTTP_CODE=200
fi
fi
debug_log "did curl, http code was '${HTTP_CODE}'"
if [[ "${HTTP_CODE}" -lt 200 || "${HTTP_CODE}" -gt 299 ]] ; then
error "Unable to download file at url ${URL}"
error "Downloaded content follows"
cat "$OUTPUT_FILE"
exit 1
fi
}
4 changes: 4 additions & 0 deletions scripts/lib/export-binary-versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash -eu
LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
PROJECT_DIR="${LIB_DIR}"/../../
export FFI_VERSION=v$(grep "version: \"" "$PROJECT_DIR"/installer/installer.go | awk -F'"' '{print $2}' )
52 changes: 52 additions & 0 deletions scripts/lib/robust-bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash -eu
if [ -z "${LIB_ROBUST_BASH_SH:-}" ]; then
LIB_ROBUST_BASH_SH=included

function error {
echo "${1:-}"
}

function log {
echo "🔵 ${1:-}"
}

function debug_log {
if [ ! -z "${LIB_ROBUST_BASH_DEBUG:-}" ]; then
echo "🔎 ${1:-}"
fi
}

function warn {
echo "🟡 ${1:-}"
}

# Check to see that we have a required binary on the path
# and fail the script if it is not present
function require_binary {
if [ -z "${1:-}" ]; then
error "${FUNCNAME[0]} requires an argument"
exit 1
fi

if ! [ -x "$(command -v "$1")" ]; then
error "The required executable '$1' is not on the path."
exit 1
fi
}

# Check to see that we have a required environment variable set,
# and fail the script if it is not set.
#
# Optionally, a second argument can be provided to display
# a helpful message before failing
function require_env_var {
var_name="${1:-}"
if [ -z "${!var_name:-}" ]; then
error "The required environment variable ${var_name} is empty"
if [ ! -z "${2:-}" ]; then
echo " - $2"
fi
exit 1
fi
}
fi

0 comments on commit 03438aa

Please sign in to comment.