-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmake.go
executable file
·136 lines (114 loc) · 3.48 KB
/
make.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//go:build ignore
// +build ignore
// SILVER - Service Wrapper
//
// Copyright (c) 2014-2021 PaperCut Software http://www.papercut.com/
// Use of this source code is governed by an MIT or GPL Version 2 license.
// See the project's LICENSE file for more information.
//
// This Go make file builds Silver directly from a code checkout, bypassing
// the need to configure/setup a Go workspace.
//
// Run on the command line with:
//
// $ go run make.go
//
// Other options:
//
// Run tests:
// $ go run make.go test
//
// Concepts loosely based on concepts in Camlistore
//
// https://github.com/bradfitz/camlistore
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const (
rootNamespace = "github.com/papercutsoftware/silver"
)
var (
// The project root where this file is located
projectRoot string
//
buildOutputDir string
)
func usage() {
fmt.Println("Usage: go run make.go [flagged args] [non-flagged args]")
fmt.Println("-goos=<operating system> target operating system for silver executable. Default is taken from runtime")
fmt.Println("-goarch=<architecture> target architecture for silver executable. Default is taken from runtime")
fmt.Println("Build action. Can be either 'all'(build all) or 'test'(test all). Default is 'all'")
os.Exit(1)
}
func main() {
goos := flag.String("goos", runtime.GOOS, "Specify target operating system for cross compilation")
goarch := flag.String("goarch", runtime.GOARCH, "Specify target architecture for cross compilation")
flag.Parse()
_ = os.Setenv("GOOS", *goos)
_ = os.Setenv("GOARCH", *goarch)
var err error
projectRoot, err = os.Getwd()
if err != nil {
panic(fmt.Sprintf("Failed to get current directory: %v\n", err))
}
buildOutputDir = filepath.Join(projectRoot, "build", *goos)
action := "all"
if len(flag.Args()) > 0 {
action = flag.Arg(0)
}
switch action {
case "all":
buildAll()
case "test":
testAll()
default:
usage()
}
}
func buildAll() {
makeDir(buildOutputDir)
goos := os.Getenv("GOOS")
goarch := os.Getenv("GOARCH")
fmt.Printf("Building binaries for %s/%s ...\n", goos, goarch)
_ = runCmd("go", "build", "-ldflags", "-s -w", "-o", makeOutputPath(buildOutputDir, "updater"), rootNamespace+"/updater")
_ = runCmd("go", "build", "-ldflags", "-s -w", "-o", makeOutputPath(buildOutputDir, "service"), rootNamespace+"/service")
_ = runCmd("go", "build", "-tags", "nohttp", "-ldflags", "-s -w", "-o", makeOutputPath(buildOutputDir, "service-no-http"), rootNamespace+"/service")
if goos == "windows" {
_ = runCmd("go", "build", "-tags", "nohttp", "-ldflags", "-s -w -H=windowsgui", "-o", makeOutputPath(buildOutputDir, "service-no-window"), rootNamespace+"/service")
_ = runCmd("go", "build", "-ldflags", "-s -w -H=windowsgui", "-o", makeOutputPath(buildOutputDir, "updater-no-window"), rootNamespace+"/updater")
}
fmt.Printf("\nCOMPLETE. You'll find the files in:\n '%s'\n", buildOutputDir)
}
func testAll() {
_ = runCmd("go", "test", rootNamespace+"/...")
}
func runCmd(cmd string, arg ...string) error {
c := exec.Command(cmd, arg...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
return fmt.Errorf("error running command %s: %v", cmd, err)
}
return nil
}
func makeDir(dir string) {
if err := os.MkdirAll(dir, 0755); err != nil {
panic(err)
}
}
func makeOutputPath(dir, name string) string {
goos := os.Getenv("GOOS")
if goos == "windows" {
if !strings.HasSuffix(name, ".exe") {
name = name + ".exe"
}
}
return filepath.Join(dir, name)
}