|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -Eeo pipefail |
| 4 | + |
| 5 | +cd "$(dirname "${BASH_SOURCE[0]}")" |
| 6 | + |
| 7 | +rust_sources_dir="../filecoin-ffi/rust" |
| 8 | +release_sha1=$(cd $rust_sources_dir && git rev-parse HEAD) |
| 9 | + |
| 10 | +if [ -e filecoin_ffi_commit_installed ] && [ "$(cat filecoin_ffi_commit_installed)" = "$release_sha1" ]; then |
| 11 | + (>&2 echo "filecoin-ffi is already installed") |
| 12 | + exit 0 |
| 13 | +fi |
| 14 | + |
| 15 | +auth_header=() |
| 16 | +if [ -n "${GITHUB_TOKEN}" ]; then |
| 17 | + auth_header=("-H" "Authorization: token ${GITHUB_TOKEN}") |
| 18 | +fi |
| 19 | + |
| 20 | +download_release_tarball() { |
| 21 | + __resultvar=$1 |
| 22 | + __repo_name=$2 |
| 23 | + __release_name="${__repo_name}-$(uname)" |
| 24 | + __release_tag="${release_sha1:0:16}" |
| 25 | + __release_tag_url="https://api.github.com/repos/filecoin-project/${__repo_name}/releases/tags/${__release_tag}" |
| 26 | + __release_response="" |
| 27 | + |
| 28 | + echo "acquiring release @ ${__release_tag}" |
| 29 | + |
| 30 | + __release_response=$(curl \ |
| 31 | + --retry 3 \ |
| 32 | + "${auth_header[@]}" \ |
| 33 | + --location $__release_tag_url) |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + if ! [ -x "$(command -v jq)" ]; then |
| 38 | + (>&2 echo 'Error: jq is not installed.') |
| 39 | + (>&2 echo 'Install jq to resolve this problem.') |
| 40 | + return 1 |
| 41 | + fi |
| 42 | + |
| 43 | + __release_url=$(echo $__release_response | jq -r ".assets[] | select(.name | contains(\"${__release_name}\")) | .url") |
| 44 | + |
| 45 | + if [[ -z "$__release_url" ]]; then |
| 46 | + (>&2 echo "failed to download release (tag URL: ${__release_tag_url}, response: ${__release_response})") |
| 47 | + return 1 |
| 48 | + fi |
| 49 | + |
| 50 | + __tar_path="/tmp/${__release_name}_$(basename ${__release_url}).tar.gz" |
| 51 | + |
| 52 | + __asset_url="" |
| 53 | + |
| 54 | + __asset_url=$(curl \ |
| 55 | + --head \ |
| 56 | + --retry 3 \ |
| 57 | + --header "Accept:application/octet-stream" \ |
| 58 | + "${auth_header[@]}"\ |
| 59 | + --location \ |
| 60 | + --output /dev/null \ |
| 61 | + -w %{url_effective} \ |
| 62 | + "$__release_url") |
| 63 | + |
| 64 | + |
| 65 | + curl --retry 3 --output "${__tar_path}" "$__asset_url" |
| 66 | + if [[ $? -ne "0" ]]; then |
| 67 | + (>&2 echo "failed to download release asset (tag URL: ${__release_tag_url}, asset URL: ${__asset_url})") |
| 68 | + return 1 |
| 69 | + fi |
| 70 | + |
| 71 | + eval $__resultvar="'$__tar_path'" |
| 72 | +} |
| 73 | + |
| 74 | +build_from_source() { |
| 75 | + __library_name=$1 |
| 76 | + __rust_sources_path=$2 |
| 77 | + __repo_sha1_truncated="${release_sha1:0:16}" |
| 78 | + |
| 79 | + echo "building from source @ ${__repo_sha1_truncated}" |
| 80 | + |
| 81 | + if ! [ -x "$(command -v cargo)" ]; then |
| 82 | + (>&2 echo 'Error: cargo is not installed.') |
| 83 | + (>&2 echo 'Install Rust toolchain to resolve this problem.') |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + |
| 87 | + if ! [ -x "$(command -v rustup)" ]; then |
| 88 | + (>&2 echo 'Error: rustup is not installed.') |
| 89 | + (>&2 echo 'Install Rust toolchain installer to resolve this problem.') |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + |
| 93 | + pushd $__rust_sources_path |
| 94 | + |
| 95 | + cargo --version |
| 96 | + |
| 97 | + if [[ -f "./scripts/build-release.sh" ]]; then |
| 98 | + ./scripts/build-release.sh $__library_name $(cat rust-toolchain) |
| 99 | + else |
| 100 | + cargo build --release --all |
| 101 | + fi |
| 102 | + |
| 103 | + popd |
| 104 | +} |
| 105 | + |
| 106 | +mkdir -p include/filecoin-ffi |
| 107 | +mkdir -p lib/pkgconfig |
| 108 | + |
| 109 | +if [ "${FFI_BUILD_FROM_SOURCE}" != "1" ] && download_release_tarball tarball_path "filecoin-ffi"; then |
| 110 | + tmp_dir=$(mktemp -d) |
| 111 | + |
| 112 | + tar -C "$tmp_dir" -xzf "$tarball_path" |
| 113 | + |
| 114 | + find -L "${tmp_dir}" -type f -name filecoin.h -exec rsync --checksum "{}" ./include/filecoin-ffi \; |
| 115 | + find -L "${tmp_dir}" -type f -name libfilecoin.a -exec rsync --checksum "{}" ./lib \; |
| 116 | + find -L "${tmp_dir}" -type f -name filecoin.pc -exec rsync --checksum "{}" ./lib/pkgconfig \; |
| 117 | + |
| 118 | + (>&2 echo "successfully installed prebuilt libfilecoin") |
| 119 | +else |
| 120 | + (>&2 echo "building libfilecoin from local sources (dir = ${rust_sources_dir})") |
| 121 | + |
| 122 | + build_from_source "filecoin" "${rust_sources_dir}" |
| 123 | + |
| 124 | + find -L "${rust_sources_dir}/target/release" -type f -name filecoin.h -exec rsync --checksum "{}" ./include/filecoin-ffi \; |
| 125 | + find -L "${rust_sources_dir}/target/release" -type f -name libfilecoin.a -exec rsync --checksum "{}" ./lib \; |
| 126 | + find -L "${rust_sources_dir}" -type f -name filecoin.pc -exec rsync --checksum "{}" ./lib/pkgconfig \; |
| 127 | + |
| 128 | + if [[ ! -f "./include/filecoin-ffi/filecoin.h" ]]; then |
| 129 | + (>&2 echo "failed to install filecoin.h") |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + |
| 133 | + if [[ ! -f "./lib/libfilecoin.a" ]]; then |
| 134 | + (>&2 echo "failed to install libfilecoin.a") |
| 135 | + exit 1 |
| 136 | + fi |
| 137 | + |
| 138 | + if [[ ! -f "./lib/pkgconfig/filecoin.pc" ]]; then |
| 139 | + (>&2 echo "failed to install filecoin.pc") |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + |
| 143 | + (>&2 echo "successfully built and installed libfilecoin from source") |
| 144 | +fi |
| 145 | + |
| 146 | +echo "$release_sha1" > filecoin_ffi_commit_installed |
0 commit comments