forked from cretz/tor-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
378 lines (364 loc) · 10.5 KB
/
build.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
)
var verbose bool
var autopointPath string
var folders = []string{"openssl", "libevent", "zlib", "xz", "tor"}
var absCurrDir = getAbsCurrDir()
var numJobs = fmt.Sprintf("-j%d", runtime.NumCPU())
func main() {
flag.BoolVar(&verbose, "verbose", false, "Whether to show command output")
flag.StringVar(&autopointPath, "autopoint-path", "/usr/local/opt/gettext/bin", "OSX: Directory that contains autopoint binary")
flag.Parse()
if len(flag.Args()) != 1 {
log.Fatal("Missing command. Can be build-all, build-<folder>, clean-all, clean-<folder>, show-libs, or package-libs")
}
if err := run(flag.Args()[0]); err != nil {
log.Fatal(err)
}
}
func run(cmd string) error {
if err := validateEnvironment(); err != nil {
return err
}
switch {
case strings.HasPrefix(cmd, "build-"):
return build(cmd[6:])
case strings.HasPrefix(cmd, "clean-"):
return clean(cmd[6:])
case cmd == "show-libs":
return showLibs()
case cmd == "package-libs":
return packageLibs()
default:
return fmt.Errorf("Invalid command: %v. Should be build-all, build-<folder>, clean-all, clean-<folder>, show-libs, or package-libs", cmd)
}
}
func getAbsCurrDir() string {
var err error
absCurrDir, err := filepath.Abs(".")
if err != nil {
panic(err)
}
if runtime.GOOS == "windows" {
volume := filepath.VolumeName(absCurrDir)
absCurrDir = "/" + strings.TrimSuffix(volume, ":") + "/" + filepath.ToSlash(absCurrDir[len(volume)+1:])
}
return absCurrDir
}
func validateEnvironment() error {
// Make sure all the folders are there
for _, folder := range folders {
if info, err := os.Stat(folder); err != nil || !info.IsDir() {
return fmt.Errorf("%v is not a dir", folder)
}
}
switch runtime.GOOS {
// On windows, have to verify MinGW
case "windows":
// Confirm it is MinGW 64
if byts, err := exec.Command("uname", "-a").CombinedOutput(); err != nil {
return fmt.Errorf("This has to be run in a MSYS or MinGW shell, uname failed: %v", err)
} else if !bytes.HasPrefix(byts, []byte("MINGW64")) && !bytes.HasPrefix(byts, []byte("MSYS2")) {
return fmt.Errorf("This has to be run in a MSYS or MinGW64 shell, uname output: %v", string(byts))
}
case "linux":
// Make sure it's not MinGW
if byts, err := exec.Command("uname", "-a").CombinedOutput(); err != nil {
return fmt.Errorf("Failed running uname -a")
} else if bytes.HasPrefix(byts, []byte("MINGW")) {
return fmt.Errorf("MinGW should not use Linux Go binary, but instead a Windows go.exe to run the build")
}
}
return nil
}
func build(folder string) error {
log.Printf("*** Building %v ***", folder)
defer log.Printf("*** Done building %v ***", folder)
pwd := absCurrDir + "/" + folder
switch folder {
case "all":
for _, subFolder := range folders {
if err := build(subFolder); err != nil {
return err
}
}
return nil
case "openssl":
cmds := [][]string{
{"sh", "./config", "--prefix=" + pwd + "/dist", "no-shared", "no-dso", "no-zlib"},
{"make", "depend"},
{"make", numJobs},
{"make", "install"},
}
if runtime.GOOS == "windows" {
cmds[0] = append(cmds[0], "mingw64")
cmds[0][1] = "./Configure"
} else if runtime.GOOS == "darwin" {
cmds[0] = append(cmds[0], "darwin64-x86_64-cc")
cmds[0][1] = "./Configure"
}
return runCmds(folder, nil, cmds)
case "libevent":
return runCmds(folder, nil, [][]string{
{"sh", "-l", "./autogen.sh"},
{"sh", "./configure", "--prefix=" + pwd + "/dist",
"--disable-shared", "--enable-static", "--with-pic", "--disable-samples", "--disable-libevent-regress",
"CPPFLAGS=-I../openssl/dist/include", "LDFLAGS=-L../openssl/dist/lib"},
{"make", numJobs},
{"make", "install"},
})
case "zlib":
var env []string
cmds := [][]string{{"sh", "./configure", "--prefix=" + pwd + "/dist"}, {"make", numJobs}, {"make", "install"}}
if runtime.GOOS == "windows" {
env = []string{"PREFIX=" + pwd + "/dist", "BINARY_PATH=" + pwd + "/dist/bin",
"INCLUDE_PATH=" + pwd + "/dist/include", "LIBRARY_PATH=" + pwd + "/dist/lib"}
cmds = [][]string{{"make", "-fwin32/Makefile.gcc"}, {"make", "install", "-fwin32/Makefile.gcc"}}
}
return runCmds(folder, env, cmds)
case "xz":
var env []string
if runtime.GOOS == "darwin" {
env = []string{"PATH=" + autopointPath + ":" + os.Getenv("PATH")}
}
return runCmds(folder, env, [][]string{
{"sh", "-l", "./autogen.sh"},
{"sh", "./configure", "--prefix=" + pwd + "/dist", "--disable-shared", "--enable-static",
"--disable-doc", "--disable-scripts", "--disable-xz", "--disable-xzdec", "--disable-lzmadec",
"--disable-lzmainfo", "--disable-lzma-links"},
{"make", numJobs},
{"make", "install"},
})
case "tor":
var env = []string{"LDFLAGS=-s"}
var torConf []string
if runtime.GOOS == "windows" {
env = append(env, "LIBS=-lcrypt32 -lgdi32")
}
torConf = []string{"sh", "./configure", "--prefix=" + pwd + "/dist",
"--disable-gcc-hardening", "--disable-system-torrc", "--disable-asciidoc",
"--enable-static-libevent", "--with-libevent-dir=" + pwd + "/../libevent/dist",
"--enable-static-openssl", "--with-openssl-dir=" + pwd + "/../openssl/dist",
"--enable-static-zlib", "--with-zlib-dir=" + pwd + "/../zlib/dist",
"--disable-systemd", "--disable-lzma", "--disable-seccomp"}
if runtime.GOOS != "darwin" {
torConf = append(torConf, "--enable-static-tor")
}
if runtime.GOOS == "windows" {
torConf = append(torConf, "--disable-zstd")
}
return runCmds(folder, env, [][]string{
{"sh", "-l", "./autogen.sh"},
torConf,
{"make", numJobs},
{"make", "install"},
})
default:
return fmt.Errorf("Unrecognized folder: %v", folder)
}
}
func clean(folder string) (err error) {
log.Printf("*** Cleaning %v ***", folder)
defer log.Printf("*** Done cleaning %v ***", folder)
switch folder {
case "all":
for _, subFolder := range folders {
if err = clean(subFolder); err != nil {
break
}
}
default:
args := []string{"clean"}
env := []string{}
makefile := "Makefile"
switch folder {
// OpenSSL needs to have the dist folder removed first
case "openssl":
if err := os.RemoveAll("openssl/dist/lib"); err != nil {
return fmt.Errorf("Unable to remove openssl/dist/lib: %v", err)
}
// Zlib needs to have a prefix and needs a special windows makefile
case "zlib":
env = append(env, "PREFIX="+absCurrDir+"/zlib/dist")
if runtime.GOOS == "windows" {
makefile = "win32/Makefile.gcc"
args = append(args, "-fwin32/Makefile.gcc")
}
}
if dir, err := os.Stat(folder); err != nil || !dir.IsDir() {
return fmt.Errorf("%v is not a directory", folder)
} else if _, err := os.Stat(path.Join(folder, makefile)); os.IsNotExist(err) {
log.Printf("Skipping clean, makefile not present")
return nil
}
err = runCmd(folder, env, "make", args...)
}
return err
}
func runCmds(folder string, env []string, cmdsAndArgs [][]string) error {
for _, cmdAndArgs := range cmdsAndArgs {
if err := runCmd(folder, env, cmdAndArgs[0], cmdAndArgs[1:]...); err != nil {
return err
}
}
return nil
}
func runCmd(folder string, env []string, cmd string, args ...string) error {
log.Printf("Running in folder %v: %v %v", folder, cmd, strings.Join(args, " "))
c := exec.Command(cmd, args...)
if len(env) > 0 {
c.Env = append(os.Environ(), env...)
}
c.Dir = folder
if verbose {
c.Stdout = os.Stdout
c.Stderr = os.Stderr
}
return c.Run()
}
type libSet struct {
dir string
libs []string
}
// Results in recommended linker order
func getLibSets() ([]*libSet, error) {
// Ask Tor for their libs
cmd := exec.Command("make", "show-libs")
cmd.Dir = "tor"
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("Failed 'make show-libs' in tor: %v", err)
}
// Load them all
libSets := []*libSet{}
libSetsByDir := map[string]*libSet{}
for _, lib := range strings.Split(strings.TrimSpace(string(out)), " ") {
dir, file := path.Split(lib)
dir = path.Join("tor", dir)
set := libSetsByDir[dir]
if set == nil {
set = &libSet{dir: dir}
libSets = append(libSets, set)
libSetsByDir[dir] = set
}
set.libs = append(set.libs, strings.TrimPrefix(strings.TrimSuffix(file, ".a"), "lib"))
}
// Add the rest of the known libs
libSets = append(libSets,
&libSet{"libevent/dist/lib", []string{"event"}},
&libSet{"xz/dist/lib", []string{"lzma"}},
&libSet{"zlib/dist/lib", []string{"z"}},
&libSet{"openssl/dist/lib", []string{"ssl", "crypto"}},
)
return libSets, nil
}
func showLibs() error {
libSets, err := getLibSets()
if err != nil {
return err
}
for _, libSet := range libSets {
fmt.Print("-L" + libSet.dir)
for _, lib := range libSet.libs {
fmt.Print(" -l" + lib)
}
fmt.Println()
}
return nil
}
func packageLibs() error {
// Make both a libs.tar.gz and a libs.zip...
// Get lib sets
libSets, err := getLibSets()
if err != nil {
return err
}
// Create tar writer
var tw *tar.Writer
if tf, err := os.Create("libs.tar.gz"); err != nil {
return err
} else {
defer tf.Close()
gw := gzip.NewWriter(tf)
defer gw.Close()
tw = tar.NewWriter(gw)
defer tw.Close()
}
tarWrite := func(path string, b []byte, i os.FileInfo) error {
th, err := tar.FileInfoHeader(i, "")
if err == nil {
th.Name = path
if err = tw.WriteHeader(th); err == nil {
_, err = tw.Write(b)
}
}
return err
}
// Create zip writer
var zw *zip.Writer
if zf, err := os.Create("libs.zip"); err != nil {
return err
} else {
defer zf.Close()
zw = zip.NewWriter(zf)
defer zw.Close()
}
zipWrite := func(path string, b []byte, i os.FileInfo) error {
zh, err := zip.FileInfoHeader(i)
if err == nil {
zh.Name = path
zh.Method = zip.Deflate
var w io.Writer
if w, err = zw.CreateHeader(zh); err == nil {
_, err = w.Write(b)
}
}
return err
}
// Copy over each lib
fileBytesAndInfo := func(name string) (b []byte, i os.FileInfo, err error) {
f, err := os.Open(name)
if err != nil {
return nil, nil, err
}
defer f.Close()
if i, err = f.Stat(); err == nil {
b, err = ioutil.ReadAll(f)
}
return
}
copyFile := func(filePath string) error {
if b, i, err := fileBytesAndInfo(filePath); err != nil {
return err
} else if err := tarWrite(filePath, b, i); err != nil {
return err
} else {
return zipWrite(filePath, b, i)
}
}
for _, libSet := range libSets {
for _, lib := range libSet.libs {
if err := copyFile(path.Join(libSet.dir, "lib"+lib+".a")); err != nil {
return err
}
}
}
// Also copy over tor_api.h
return copyFile("tor/src/feature/api/tor_api.h")
}