Newer
Older
multiCompile / multiCompile.go
root on 13 Aug 2021 1 KB Initial commit
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10.  
  11. "github.com/akamensky/argparse"
  12. )
  13.  
  14. func main() {
  15. parser := argparse.NewParser("print", "Multi-OS Golang compiler ("+runtime.GOOS+")")
  16. p := parser.String("p", "path", &argparse.Options{Required: true, Help: "path/to/program.go t compile"})
  17. err := parser.Parse(os.Args)
  18. if err != nil {
  19. fmt.Print(parser.Usage(err))
  20. os.Exit(1)
  21. }
  22. // convert CRLF to LF
  23. fullPath := strings.Replace(*p, "\n", "", -1)
  24. dir, file := filepath.Split(fullPath)
  25. fmt.Println("Dir: " + dir)
  26. fmt.Println("File: " + file)
  27.  
  28. if _, err := os.Stat(fullPath); os.IsNotExist(err) {
  29. fmt.Println("[-] file does not exist!")
  30. os.Exit(1)
  31. }
  32.  
  33. osASM := [4]string{"windows-386", "windows-amd64", "linux-386", "linux-amd64"}
  34. for index, element := range osASM {
  35. fmt.Printf("%v) Compiling %v - ", index+1, element)
  36. elemSplit := strings.Split(element, "-")
  37.  
  38. fileNoExt := strings.TrimSuffix(file, filepath.Ext(file))
  39. var ext string
  40. if elemSplit[0] == "windows" {
  41. ext = ".exe"
  42. } else {
  43. ext = ""
  44. }
  45. if runtime.GOOS == "windows" {
  46. fmt.Print("(windows compiler)... ")
  47. exec.Command("go", "env", "-w", "GOOS="+elemSplit[0]).Output() // windows
  48. exec.Command("go", "env", "-w", "GOARCH="+elemSplit[1]).Output() // windows
  49. exec.Command("go", "build", "-o", dir+fileNoExt+"-"+element+ext, fullPath).Output()
  50. }
  51. if runtime.GOOS == "linux" {
  52. fmt.Print("(linux compiler)... ")
  53. exec.Command("env", "GOOS="+elemSplit[0], "GOARC="+elemSplit[1], "go", "build", "-o", dir+fileNoExt+"-"+element+ext, fullPath).Output() // linux
  54. }
  55. fmt.Printf("Built %v\n", dir+fileNoExt+"-"+element+ext)
  56. }
  57. }
Buy Me A Coffee