Newer
Older
multiCompile / multiCompile.go
root on 13 Aug 2021 1 KB Initial commit
package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"

	"github.com/akamensky/argparse"
)

func main() {
	parser := argparse.NewParser("print", "Multi-OS Golang compiler ("+runtime.GOOS+")")
	p := parser.String("p", "path", &argparse.Options{Required: true, Help: "path/to/program.go t compile"})
	err := parser.Parse(os.Args)
	if err != nil {
		fmt.Print(parser.Usage(err))
		os.Exit(1)
	}
	// convert CRLF to LF
	fullPath := strings.Replace(*p, "\n", "", -1)
	dir, file := filepath.Split(fullPath)
	fmt.Println("Dir: " + dir)
	fmt.Println("File: " + file)

	if _, err := os.Stat(fullPath); os.IsNotExist(err) {
		fmt.Println("[-] file does not exist!")
		os.Exit(1)
	}

	osASM := [4]string{"windows-386", "windows-amd64", "linux-386", "linux-amd64"}
	for index, element := range osASM {
		fmt.Printf("%v) Compiling %v - ", index+1, element)
		elemSplit := strings.Split(element, "-")

		fileNoExt := strings.TrimSuffix(file, filepath.Ext(file))
		var ext string
		if elemSplit[0] == "windows" {
			ext = ".exe"
		} else {
			ext = ""
		}
		if runtime.GOOS == "windows" {
			fmt.Print("(windows compiler)... ")
			exec.Command("go", "env", "-w", "GOOS="+elemSplit[0]).Output()   // windows
			exec.Command("go", "env", "-w", "GOARCH="+elemSplit[1]).Output() // windows
			exec.Command("go", "build", "-o", dir+fileNoExt+"-"+element+ext, fullPath).Output()
		}
		if runtime.GOOS == "linux" {
			fmt.Print("(linux compiler)... ")
			exec.Command("env", "GOOS="+elemSplit[0], "GOARC="+elemSplit[1], "go", "build", "-o", dir+fileNoExt+"-"+element+ext, fullPath).Output() // linux
		}
		fmt.Printf("Built %v\n", dir+fileNoExt+"-"+element+ext)
	}
}