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.
This commit is contained in:
parent
51981ebfdb
commit
9b53ee94dc
|
@ -21,7 +21,7 @@ get_tgz () {
|
||||||
|
|
||||||
wget "$URL"
|
wget "$URL"
|
||||||
tar -xvf "$ARCHIVE" "$TARGET_NODE"
|
tar -xvf "$ARCHIVE" "$TARGET_NODE"
|
||||||
rm -rf "$ARCHIVE"
|
rm -f "$ARCHIVE"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_zip () {
|
get_zip () {
|
||||||
|
@ -33,7 +33,7 @@ get_zip () {
|
||||||
|
|
||||||
wget "$URL"
|
wget "$URL"
|
||||||
unzip "$ARCHIVE" "$TARGET_NODE"
|
unzip "$ARCHIVE" "$TARGET_NODE"
|
||||||
rm -rf "$ARCHIVE"
|
rm -f "$ARCHIVE"
|
||||||
}
|
}
|
||||||
|
|
||||||
get_tgz darwin
|
get_tgz darwin
|
||||||
|
@ -47,24 +47,25 @@ npm ci --only=prod --ignore-scripts --no-audit --no-fund
|
||||||
# More trouble than it's worth :)
|
# More trouble than it's worth :)
|
||||||
rm -rf ./node_modules/sharp
|
rm -rf ./node_modules/sharp
|
||||||
|
|
||||||
|
export GOARCH="amd64"
|
||||||
|
|
||||||
# Darwin (shell script)
|
# Darwin (shell script)
|
||||||
cat << EOF > oasis-darwin-x64
|
export GOOS="darwin"
|
||||||
#!/bin/sh
|
OUTFILE="oasis-$GOOS-$GOARCH"
|
||||||
BASEDIR="\$(dirname "\$0")"
|
go build -ldflags "-X main.node=vendor/node-v$TARGET_VERSION-darwin-x64/bin/node" -o "$OUTFILE" scripts/oasis.go
|
||||||
exec "\$BASEDIR/vendor/node-v$TARGET_VERSION-darwin-x64/bin/node" "\$BASEDIR/src" "\$@"
|
chmod +x "$OUTFILE"
|
||||||
EOF
|
|
||||||
chmod +x oasis-darwin-x64
|
|
||||||
|
|
||||||
# Linux (ELF executable)
|
# 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\""
|
export GOOS="linux"
|
||||||
chmod +x oasis-linux-x64
|
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)
|
# Windows (batch file)
|
||||||
cat << EOF > oasis-win-x64.bat
|
export GOOS="windows"
|
||||||
vendor\\node-v$TARGET_VERSION-win-x64\\bin\\node src %*
|
OUTFILE="oasis-$GOOS-$GOARCH.exe"
|
||||||
EOF
|
go build -ldflags "-X main.node=vendor\\node-v$TARGET_VERSION-win-x64\\bin\\node" -o "$OUTFILE" scripts/oasis.go
|
||||||
|
chmod +x "$OUTFILE"
|
||||||
chmod +x oasis-win-x64.bat
|
|
||||||
|
|
||||||
# I think if the zip already exists it's adding files to the existing archive?
|
# I think if the zip already exists it's adding files to the existing archive?
|
||||||
ZIP_PATH="/tmp/oasis-x64.zip"
|
ZIP_PATH="/tmp/oasis-x64.zip"
|
||||||
|
@ -73,3 +74,4 @@ rm -f "$ZIP_PATH"
|
||||||
zip -r "$ZIP_PATH" . -x ".git/**"
|
zip -r "$ZIP_PATH" . -x ".git/**"
|
||||||
|
|
||||||
git clean -fdx
|
git clean -fdx
|
||||||
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
#ifndef NODE
|
|
||||||
#define NODE "/usr/bin/node"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue