more error messages
This commit is contained in:
parent
a5861ca989
commit
003af32f46
59
compile.go
Normal file
59
compile.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
|
||||||
|
filename := fmt.Sprintf("%s_%s_%s%s", ProjectName, cfg.OS, cfg.Arch, cfg.FileExt)
|
||||||
|
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateCompiling}
|
||||||
|
|
||||||
|
compileCmd := exec.Command("go", "build", "-o", filename)
|
||||||
|
compileCmd.Dir = ModulePath
|
||||||
|
|
||||||
|
if err := compileCmd.Start(); err != nil {
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateCompileError}
|
||||||
|
wg.Done()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := compileCmd.Wait(); err != nil {
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateCompileError}
|
||||||
|
wg.Done()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Compress(filename, cfg, ch, wg)
|
||||||
|
|
||||||
|
// uncomment for independent compile and compress tasks
|
||||||
|
// (slightly slower in tests, most likely because of more context switches)
|
||||||
|
/*
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateWaiting}
|
||||||
|
go Runner.Run(func() { Compress(filename, cfg, ch, wg) })
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
func Compress(filename string, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateCompressing}
|
||||||
|
|
||||||
|
if !*NoCompress && cfg.Compress {
|
||||||
|
compressCmd := exec.Command("upx", "--best", filename)
|
||||||
|
compressCmd.Dir = ModulePath
|
||||||
|
|
||||||
|
if err := compressCmd.Start(); err != nil {
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateCompressError}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := compressCmd.Wait(); err != nil {
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateCompressError}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ch <- &CompileReport{Config: cfg, State: StateDone}
|
||||||
|
}
|
76
main.go
76
main.go
@ -9,15 +9,21 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.milar.in/milarin/channel"
|
"git.milar.in/milarin/channel"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ModulePath string
|
// globals
|
||||||
var ProjectName string
|
var (
|
||||||
|
ModulePath string
|
||||||
|
ProjectName string
|
||||||
|
Runner channel.Runner
|
||||||
|
)
|
||||||
|
|
||||||
|
// command line arguments
|
||||||
var (
|
var (
|
||||||
OS = flag.String("os", "", "comma-separated list of operating systems to compile for (empty for all)")
|
OS = flag.String("os", "", "comma-separated list of operating systems to compile for (empty for all)")
|
||||||
Arch = flag.String("arch", "", "comma-separated list of architectures to compile for (empty for all)")
|
Arch = flag.String("arch", "", "comma-separated list of architectures to compile for (empty for all)")
|
||||||
@ -27,8 +33,6 @@ var (
|
|||||||
NumThreads = flag.Int("t", runtime.NumCPU(), "amount of threads (0 = infinite)")
|
NumThreads = flag.Int("t", runtime.NumCPU(), "amount of threads (0 = infinite)")
|
||||||
)
|
)
|
||||||
|
|
||||||
var Runner channel.Runner
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@ -37,7 +41,6 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
ProjectName = filepath.Base(ModulePath)
|
|
||||||
|
|
||||||
if _, err := exec.LookPath("go"); err != nil {
|
if _, err := exec.LookPath("go"); err != nil {
|
||||||
ColorError.Fprintln(os.Stderr, "go not found in PATH. compilation not possible")
|
ColorError.Fprintln(os.Stderr, "go not found in PATH. compilation not possible")
|
||||||
@ -61,6 +64,11 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := DetermineProjectName(); err != nil {
|
||||||
|
ColorError.Fprintln(os.Stderr, fmt.Errorf("project name could not be determined: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ch := make(chan *CompileReport, len(CompileConfigs))
|
ch := make(chan *CompileReport, len(CompileConfigs))
|
||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
|
|
||||||
@ -85,58 +93,26 @@ func main() {
|
|||||||
<-doneCh // wait for Watch
|
<-doneCh // wait for Watch
|
||||||
|
|
||||||
if !*Silent {
|
if !*Silent {
|
||||||
fmt.Printf("compilation took %s (using %s threads)\n", end.Sub(start), GetThreadCount())
|
fmt.Printf("compilation took %s (using %s threads)\n", end.Sub(start), GetThreadCountString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Compile(cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
|
func DetermineProjectName() error {
|
||||||
filename := fmt.Sprintf("%s_%s_%s%s", ProjectName, cfg.OS, cfg.Arch, cfg.FileExt)
|
data, err := os.ReadFile(filepath.Join(ModulePath, "go.mod"))
|
||||||
|
if err != nil {
|
||||||
ch <- &CompileReport{Config: cfg, State: StateCompiling}
|
return err
|
||||||
|
|
||||||
compileCmd := exec.Command("go", "build", "-o", filename)
|
|
||||||
compileCmd.Dir = ModulePath
|
|
||||||
if err := compileCmd.Start(); err != nil {
|
|
||||||
ch <- &CompileReport{Config: cfg, State: StateCompileError}
|
|
||||||
wg.Done()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err := compileCmd.Wait(); err != nil {
|
|
||||||
ch <- &CompileReport{Config: cfg, State: StateCompileError}
|
|
||||||
wg.Done()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Compress(filename, cfg, ch, wg)
|
for _, line := range strings.Split(string(data), "\n") {
|
||||||
|
if strings.HasPrefix(line, "module ") {
|
||||||
// uncomment for independent compile and compress tasks
|
fullName := strings.TrimPrefix(line, "module ")
|
||||||
/*
|
parts := strings.Split(fullName, "/")
|
||||||
ch <- &CompileReport{Config: cfg, State: StateWaiting}
|
ProjectName = parts[len(parts)-1]
|
||||||
go Runner.Run(func() { Compress(filename, cfg, ch, wg) })
|
return nil
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
func Compress(filename string, cfg *CompileConfig, ch chan<- *CompileReport, wg *sync.WaitGroup) {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
ch <- &CompileReport{Config: cfg, State: StateCompressing}
|
|
||||||
|
|
||||||
if !*NoCompress && cfg.Compress {
|
|
||||||
compressCmd := exec.Command("upx", "--best", filename)
|
|
||||||
compressCmd.Dir = ModulePath
|
|
||||||
|
|
||||||
if err := compressCmd.Start(); err != nil {
|
|
||||||
ch <- &CompileReport{Config: cfg, State: StateCompressError}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := compressCmd.Wait(); err != nil {
|
|
||||||
ch <- &CompileReport{Config: cfg, State: StateCompressError}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ch <- &CompileReport{Config: cfg, State: StateDone}
|
return errors.New("malformed syntax in go.mod file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRunner() channel.Runner {
|
func GetRunner() channel.Runner {
|
||||||
@ -147,7 +123,7 @@ func GetRunner() channel.Runner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetThreadCount() string {
|
func GetThreadCountString() string {
|
||||||
if *NumThreads == 0 {
|
if *NumThreads == 0 {
|
||||||
return "infinitely many"
|
return "infinitely many"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user