Distribute shell/batch scripts on macOS/Window

Problem: I've quickly learned that cross-compiling is a major headache
and that C programming lacks both rainbows and butterflies. It seems
that my executable only works on Linux, but probably not macOS or
Windows.

Solution: Use shell scripts on macOS, which work when you double-click
them, and batch scripts on Windows, which I *think* work when you
double-click them. I haven't tested this on a macOS device yet, but I
tested the previous shell script on macOS and it seemed to work fine.
Unless I've done something silly, this should work on macOS, but the
Windows batch script is just me writing code from memory and probably
doesn't work correcly. I'll probably beg @SoapDog to help me fix it.
This commit is contained in:
Christian Bundy 2020-02-25 20:41:57 -08:00
parent 4fac7a70b4
commit 6812347b02
1 changed files with 45 additions and 27 deletions

View File

@ -2,39 +2,45 @@
set -ex
BASEDIR="$(dirname "$0")"
cd "$BASEDIR/.."
TARGET_VERSION="12.16.1"
TARGET_PLATFORM="$1"
TARGET_ARCH="$2"
echo "Target version: $TARGET_VERSION"
echo "Target platform: $TARGET_PLATFORM"
echo "Target arch: $TARGET_ARCH"
if [ "$TARGET_ARCH" == "x64" ]; then
GCC_ARCH="x86-64"
else
echo "Unsupported architecture: $TARGET_ARCH"
exit 1
fi
git clean -fdx
mkdir -p vendor
cd vendor
TARGET="node-v$TARGET_VERSION-$TARGET_PLATFORM-$TARGET_ARCH"
TARBALL="$TARGET.tar.gz"
URL="https://nodejs.org/dist/v$TARGET_VERSION/$TARBALL"
TARGET_NODE="$TARGET/bin/node"
get_tgz () {
TARGET_PLATFORM="$1"
TARGET="node-v$TARGET_VERSION-$TARGET_PLATFORM-x64"
ARCHIVE="$TARGET.tar.gz"
URL="https://nodejs.org/dist/v$TARGET_VERSION/$ARCHIVE"
TARGET_NODE="$TARGET/bin/node"
wget "$URL"
tar -xvf "$ARCHIVE" "$TARGET_NODE"
rm -rf "$ARCHIVE"
}
get_zip () {
TARGET_PLATFORM="$1"
TARGET="node-v$TARGET_VERSION-$TARGET_PLATFORM-x64"
ARCHIVE="$TARGET.zip"
URL="https://nodejs.org/dist/v$TARGET_VERSION/$ARCHIVE"
TARGET_NODE="$TARGET/node.exe"
wget "$URL"
unzip "$ARCHIVE" "$TARGET_NODE"
rm -rf "$ARCHIVE"
}
get_tgz darwin
get_tgz linux
get_zip win
wget "$URL"
tar -xvf "$TARBALL" "$TARGET_NODE"
rm -rf "$TARBALL"
cd ..
# Avoid building anything from source.
@ -42,19 +48,31 @@ npm ci --only=prod --ignore-scripts --no-audit --no-fund
# More trouble than it's worth :)
rm -rf ./node_modules/sharp
BINARY_NAME="oasis"
# Darwin (shell script)
cat << EOF > oasis-darwin-x64
#!/bin/sh
exec vendor/node-v$TARGET_VERSION-darwin-x64/bin/node src "\$@"
EOF
chmod +x oasis-darwin-x64
VENDOR_NODE="vendor/$TARGET_NODE"
# Linux (ELF executable)
clang scripts/oasis.c -Wall -g -no-pie -o "oasis-linux-x64" --target=x86_64-linux-unknown -D "NODE=\"vendor/node-v$TARGET_VERSION-linux-x64/bin/node\""
chmod +x oasis-linux-x64
gcc scripts/oasis.c -Wall -g -no-pie -o "$BINARY_NAME" -march="$GCC_ARCH" -D "NODE=\"$VENDOR_NODE\""
# Windows (batch file)
cat << EOF > oasis-win-x64.bat
vendor\\node-v$TARGET_VERSION-darwin-x64\\bin\\node src "\$@"
EOF
chmod +x "$BINARY_NAME"
chmod +x oasis-win-x64.bat
# I think if the zip already exists it's adding files to the existing archive?
ZIP_PATH="/tmp/oasis-$TARGET_PLATFORM-$TARGET_ARCH.zip"
ZIP_PATH="/tmp/oasis-x64.zip"
rm -f "$ZIP_PATH"
zip -r "$ZIP_PATH" . -x ".git/**"
rm -f oasis
rm -f oasis-darwin-x64
rm -f oasis-linux-x64
rm -f oasis-win-x64.bat
rm -rf vendor