forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
220 lines (183 loc) · 4.52 KB
/
magefile.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//+build mage
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"www.velocidex.com/golang/velociraptor/constants"
)
var (
assets = map[string]string{
"artifacts/b0x.yaml": "artifacts/assets/ab0x.go",
"config/b0x.yaml": "config/ab0x.go",
"gui/b0x.yaml": "gui/assets/ab0x.go",
}
mingw_xcompiler = "x86_64-w64-mingw32-gcc"
name = "velociraptor"
version = "v" + constants.VERSION
)
func Xgo() error {
return sh.RunV(
"xgo", "-out", filepath.Join("output", "velociraptor-"+version), "-v",
"--targets", "windows/amd64,windows/x86,darwin/amd64,linux/amd64",
"-tags", "release cgo",
"-ldflags=-s -w "+flags(),
"./bin/")
}
func Linux() error {
if err := os.Mkdir("output", 0700); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create output: %v", err)
}
err := ensure_assets()
if err != nil {
return err
}
env := make(map[string]string)
err = sh.RunWith(
env,
mg.GoCmd(), "build",
"-o", filepath.Join("output", name),
"-tags", "release",
"-ldflags=-s -w "+flags(),
"./bin/")
if err != nil {
return err
}
return err
}
// Builds a Development binary. This does not embed things like GUI
// resources to allow them to be loaded from the local directory.
func Dev() error {
if err := os.Mkdir("output", 0700); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create output: %v", err)
}
err := ensure_assets()
if err != nil {
return err
}
env := make(map[string]string)
err = sh.RunWith(
env,
mg.GoCmd(), "build",
"-o", filepath.Join("output", name),
"-tags", "devel",
"-ldflags=-s -w "+flags(),
"./bin/")
if err != nil {
return err
}
return err
}
// Cross compile the windows binary using mingw
func Windows() error {
if err := os.Mkdir("output", 0700); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create output: %v", err)
}
err := ensure_assets()
if err != nil {
return err
}
env := make(map[string]string)
if mingwxcompiler_exists() {
env["CC"] = mingw_xcompiler
env["CGO_ENABLED"] = "1"
} else {
fmt.Printf("Windows cross compiler not found. Disabling cgo modules.")
env["CGO_ENABLED"] = "0"
}
env["GOOS"] = "windows"
env["GOARCH"] = "amd64"
err = sh.RunWith(
env,
mg.GoCmd(), "build",
"-o", filepath.Join("output", name+".exe"),
"-tags", "release",
"-ldflags=-s -w "+flags(),
"./bin/")
if err != nil {
return err
}
return err
}
// We have to compile darwin executables with xgo otherwise many of
// the cgo plugins wont work.
func Darwin() error {
return sh.RunV(
"xgo", "-out", filepath.Join("output", "velociraptor-"+version), "-v",
"--targets", "darwin/amd64",
"-tags", "release",
"-ldflags=-s -w "+flags(),
"./bin/")
}
func Clean() error {
for _, target := range assets {
err := sh.Rm(target)
if err != nil {
return err
}
}
return nil
}
func flags() string {
timestamp := time.Now().Format(time.RFC3339)
return fmt.Sprintf(`-X "www.velocidex.com/golang/velociraptor/config.build_time=%s" -X "www.velocidex.com/golang/velociraptor/config.commit_hash=%s"`, timestamp, hash())
}
// hash returns the git hash for the current repo or "" if none.
func hash() string {
hash, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return hash
}
func fileb0x(asset string) error {
err := sh.Run("fileb0x", asset)
if err != nil {
err = sh.Run(mg.GoCmd(), "get", "github.com/UnnoTed/fileb0x")
if err != nil {
return err
}
err = sh.Run("fileb0x", asset)
}
return err
}
func ensure_assets() error {
for asset, target := range assets {
before := timestamp_of(target)
err := fileb0x(asset)
if err != nil {
return err
}
// Only do this if the file has changed.
if before != timestamp_of(target) {
err = replace_string_in_file(target, "func init()", "func Init()")
if err != nil {
return err
}
}
}
return nil
}
func mingwxcompiler_exists() bool {
err := sh.Run(mingw_xcompiler, "--version")
return err == nil
}
// Temporarily manipulate the generated file until
// https://github.com/UnnoTed/fileb0x/pull/46 goes in.
func replace_string_in_file(filename string, old string, new string) error {
read, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
newContents := strings.Replace(string(read), old, new, -1)
return ioutil.WriteFile(filename, []byte(newContents), 0)
}
func timestamp_of(path string) int64 {
stat, err := os.Stat(path)
if os.IsNotExist(err) || err != nil {
return 0
}
return stat.ModTime().UnixNano()
}