|
| 1 | +#!/usr/bin/env bash |
| 2 | +# shellcheck disable=SC2086,SC2154 |
| 3 | +set -exuo pipefail |
| 4 | +echo "$0 $*" |
| 5 | +usage() { |
| 6 | + echo "usage: $0 [--platform p] [--app a] [--static] [--skip-run]" |
| 7 | + exit 1 |
| 8 | +} |
| 9 | +trap 'echo aborted; exit 1' INT ERR |
| 10 | +platform=${PLATFORM:-go-basic-cli} |
| 11 | +application=${APPLICATION:-hello} |
| 12 | +static=0 |
| 13 | +skip_run=0 |
| 14 | +eval set -- "$(getopt -o "" --long platform:,app:,static,skip-run,help -- "$@")" |
| 15 | +if [[ $# -gt 1 ]]; then |
| 16 | + while true; do |
| 17 | + case ${1:""} in |
| 18 | + --platform) |
| 19 | + platform=$2 |
| 20 | + shift 2 |
| 21 | + ;; |
| 22 | + --application) |
| 23 | + application=$2 |
| 24 | + shift 2 |
| 25 | + ;; |
| 26 | + --app) |
| 27 | + application=$2 |
| 28 | + shift 2 |
| 29 | + ;; |
| 30 | + --static) |
| 31 | + static=1 |
| 32 | + shift |
| 33 | + ;; |
| 34 | + --skip-run) |
| 35 | + skip_run=1 |
| 36 | + shift |
| 37 | + ;; |
| 38 | + --help) usage ;; |
| 39 | + --) |
| 40 | + shift |
| 41 | + # break |
| 42 | + ;; |
| 43 | + # *) usage ;; |
| 44 | + *) |
| 45 | + if [ -z "$1" ]; then |
| 46 | + usage |
| 47 | + fi |
| 48 | + application=$1 |
| 49 | + shift |
| 50 | + if [ "$#" -lt 1 ]; then |
| 51 | + break |
| 52 | + fi |
| 53 | + platform=$1 |
| 54 | + shift |
| 55 | + break |
| 56 | + ;; |
| 57 | + esac |
| 58 | + done |
| 59 | +fi |
| 60 | +platform=${platform:-go-basic-cli} |
| 61 | +application=${application:-hello} |
| 62 | +platform_path=${PLATFORM_PATH:-./platforms/${platform}} |
| 63 | +app_path=${APPLICATION_PATH:-./applications/${application}/${platform}} |
| 64 | +app_lib=$app_path/libapp.so |
| 65 | +if [[ ! -d $app_path ]]; then |
| 66 | + echo "missing dir: $app_path" |
| 67 | + exit 1 |
| 68 | +fi |
| 69 | +if [ ! -d "$app_path/Lib" ]; then |
| 70 | + ln -s ../../../lib "$app_path/Lib" |
| 71 | +fi |
| 72 | +app_main=$app_path/main.roc |
| 73 | +rm -f "$app_lib" 2>/dev/null || true |
| 74 | +roc build --lib "$app_main" --output "$app_lib" |
| 75 | +if [[ -d "$platform_path" ]]; then |
| 76 | + platform_roc_path="$platform_path" |
| 77 | + if [[ -d "$platform_path/platform" ]]; then |
| 78 | + platform_roc_path="$platform_path/platform" |
| 79 | + fi |
| 80 | + if [ ! -d "$platform_roc_path/Lib" ]; then |
| 81 | + |
| 82 | + ln -s ../../../lib "$platform_roc_path/Lib" |
| 83 | + fi |
| 84 | + host_main=$platform_roc_path/main.roc |
| 85 | + abs_app_dir=$(realpath "$app_path") |
| 86 | + rel_app_dir="../applications/${application}/${platform}" |
| 87 | + pushd "$platform_path" >/dev/null |
| 88 | + host_bin=$platform_path/dynhost |
| 89 | + roc_build_file="build.roc" |
| 90 | + if [[ -e "$roc_build_file" ]]; then |
| 91 | + # nix_file="flake.nix" |
| 92 | + # if [[ -f "$nix_file" ]] && command -v nix && eval "nix eval --json .#devShell.x86_64-linux >/dev/null 2>&1"; then |
| 93 | + # nix develop --command "roc \"$roc_build_file\"" |
| 94 | + # else |
| 95 | + roc "$roc_build_file" |
| 96 | + # fi |
| 97 | + if [ -d "target/release" ]; then |
| 98 | + host_bin="$platform_path/target/release/host" |
| 99 | + fi |
| 100 | + elif [[ $platform_path == *go-* ]] || [[ $platform_path == *-go ]]; then |
| 101 | + # TODO: build.roc? |
| 102 | + rm -f "$host_bin" 2>/dev/null || true |
| 103 | + export CGO_LDFLAGS="-L${abs_app_dir} -Wl,-rpath,'\$ORIGIN/${rel_app_dir}'" |
| 104 | + ldflags=() |
| 105 | + ((static)) && ldflags=(-ldflags "-extldflags=-static") |
| 106 | + go build -buildmode=pie "${ldflags[@]}" -o "$(basename "$host_bin")" |
| 107 | + unset CGO_LDFLAGS |
| 108 | + fi |
| 109 | + popd >/dev/null |
| 110 | + roc preprocess-host "$host_bin" "$host_main" "$app_lib" |
| 111 | +fi |
| 112 | +((skip_run)) || roc "$app_main" |
0 commit comments