From 9b53ee94dcbc2a4c0470ee565b32dbf5848d0286 Mon Sep 17 00:00:00 2001 From: Christian Bundy Date: Wed, 26 Feb 2020 18:07:49 -0800 Subject: [PATCH] Replace C launcher with Golang launcher Problem: I don't know what I'm doing and can't figure out how to cross-compile this simple C program to macOS and Windows Solution: @cryptix reminded me that Go makes cross-compiling easy, so I took a stab at writing my first Go program. It seems to be working on macOS and Linux, but I haven't tested on Windows. --- scripts/build.sh | 32 +++++++++++++++++--------------- scripts/oasis.c | 32 -------------------------------- scripts/oasis.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 47 deletions(-) delete mode 100644 scripts/oasis.c create mode 100644 scripts/oasis.go diff --git a/scripts/build.sh b/scripts/build.sh index 218bb8c..5850869 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -21,7 +21,7 @@ get_tgz () { wget "$URL" tar -xvf "$ARCHIVE" "$TARGET_NODE" - rm -rf "$ARCHIVE" + rm -f "$ARCHIVE" } get_zip () { @@ -33,7 +33,7 @@ get_zip () { wget "$URL" unzip "$ARCHIVE" "$TARGET_NODE" - rm -rf "$ARCHIVE" + rm -f "$ARCHIVE" } get_tgz darwin @@ -47,24 +47,25 @@ npm ci --only=prod --ignore-scripts --no-audit --no-fund # More trouble than it's worth :) rm -rf ./node_modules/sharp +export GOARCH="amd64" + # Darwin (shell script) -cat << EOF > oasis-darwin-x64 -#!/bin/sh -BASEDIR="\$(dirname "\$0")" -exec "\$BASEDIR/vendor/node-v$TARGET_VERSION-darwin-x64/bin/node" "\$BASEDIR/src" "\$@" -EOF -chmod +x oasis-darwin-x64 +export GOOS="darwin" +OUTFILE="oasis-$GOOS-$GOARCH" +go build -ldflags "-X main.node=vendor/node-v$TARGET_VERSION-darwin-x64/bin/node" -o "$OUTFILE" scripts/oasis.go +chmod +x "$OUTFILE" # Linux (ELF executable) -clang scripts/oasis.c -Wall -g -no-pie -o "oasis-linux-x64" --target=x86_64-linux -D "NODE=\"vendor/node-v$TARGET_VERSION-linux-x64/bin/node\"" -chmod +x oasis-linux-x64 +export GOOS="linux" +OUTFILE="oasis-$GOOS-$GOARCH" +go build -ldflags "-X main.node=vendor/node-v$TARGET_VERSION-linux-x64/bin/node" -o "$OUTFILE" scripts/oasis.go +chmod +x "$OUTFILE" # Windows (batch file) -cat << EOF > oasis-win-x64.bat -vendor\\node-v$TARGET_VERSION-win-x64\\bin\\node src %* -EOF - -chmod +x oasis-win-x64.bat +export GOOS="windows" +OUTFILE="oasis-$GOOS-$GOARCH.exe" +go build -ldflags "-X main.node=vendor\\node-v$TARGET_VERSION-win-x64\\bin\\node" -o "$OUTFILE" scripts/oasis.go +chmod +x "$OUTFILE" # I think if the zip already exists it's adding files to the existing archive? ZIP_PATH="/tmp/oasis-x64.zip" @@ -73,3 +74,4 @@ rm -f "$ZIP_PATH" zip -r "$ZIP_PATH" . -x ".git/**" git clean -fdx + diff --git a/scripts/oasis.c b/scripts/oasis.c deleted file mode 100644 index 731a3ca..0000000 --- a/scripts/oasis.c +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef NODE -#define NODE "/usr/bin/node" -#endif - -#include -#include -#include - -int main (int argc, char *argv[]) { - static const char src[] = "src"; - char** new_argv = malloc(((argc + 1) * sizeof *new_argv) + strlen(src)); - - int pad = 0; - for(int i = 0; i < argc; i++) { - size_t length = strlen(argv[i]) + 1; - new_argv[i + pad] = malloc(length); - memcpy(new_argv[i + pad], argv[i], length); - - if (i == 0) { - pad = 1; - size_t length = strlen(src) + 1; - new_argv[i + pad] = malloc(length); - memcpy(new_argv[i + pad], src, length); - } - } - - new_argv[argc + 1] = NULL; - - execv(NODE, new_argv); - return 1; -} - diff --git a/scripts/oasis.go b/scripts/oasis.go new file mode 100644 index 0000000..3eaba20 --- /dev/null +++ b/scripts/oasis.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" +) + +var node string + +func main() { + node := filepath.Join(filepath.Dir(os.Args[0]), node) + src := filepath.Join(filepath.Dir(os.Args[0]), "src") + + args := []string{src} + + for i := 1; i < len(os.Args); i++ { + args = append(args, os.Args[i]) + } + + cmd := exec.Command(node, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + + if err != nil { + fmt.Println(err) + } + + + fmt.Println(args) +}