/
make.go
378 lines (335 loc) · 11 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
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
//go:build ignore
/*
Copyright 2013 The Perkeep Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// This program builds Perkeep.
//
// $ go run make.go
//
// See the BUILDING file.
//
// The output binaries go into the usual go install directory:
// $GOBIN or $GOPATH/bin.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
)
var (
race = flag.Bool("race", false, "Build race-detector version of binaries (they will run slowly)")
verbose = flag.Bool("v", strings.Contains(os.Getenv("CAMLI_DEBUG_X"), "makego"), "Verbose mode")
targets = flag.String("targets", "", "Optional comma-separated list of targets (i.e go packages) to build and install. '*' builds everything. Empty builds defaults for this platform. Example: perkeep.org/server/perkeepd,perkeep.org/cmd/pk-put")
quiet = flag.Bool("quiet", false, "Don't print anything unless there's a failure.")
buildARCH = flag.String("arch", runtime.GOARCH, "Architecture to build for.")
buildOS = flag.String("os", runtime.GOOS, "Operating system to build for.")
buildARM = flag.String("arm", "7", "ARM version to use if building for ARM. Note that this version applies even if the host arch is ARM too (and possibly of a different version).")
stampVersion = flag.Bool("stampversion", true, "Stamp version into buildinfo.GitInfo")
website = flag.Bool("website", false, "Just build the website.")
static = flag.Bool("static", false, "Build a static binary, so it can run in an empty container.")
offline = flag.Bool("offline", false, "Do not fetch the JS code for the web UI from perkeep.org. If not rebuilding the web UI, just trust the files on disk (if they exist).")
)
var (
// pkRoot is the Perkeep project root
pkRoot string
binDir string // $GOBIN or $GOPATH/bin, based on user setting or default Go value.
)
func main() {
log.SetFlags(0)
flag.Parse()
if *buildARCH == "386" && *buildOS == "darwin" {
if ok, _ := strconv.ParseBool(os.Getenv("CAMLI_FORCE_OSARCH")); !ok {
log.Fatalf("You're trying to build a 32-bit binary for a Mac. That is almost always a mistake.\nTo do it anyway, set env CAMLI_FORCE_OSARCH=1 and run again.\n")
}
}
failIfCamlistoreOrgDir()
verifyGoModules()
verifyGoVersion()
verifyPerkeepRoot()
version := getVersion()
gitRev := getGitVersion()
if *verbose {
log.Printf("Perkeep version = %q, git = %q", version, gitRev)
log.Printf("Project source: %s", pkRoot)
log.Printf("Output binaries: %s", actualBinDir())
}
buildAll := false
targs := []string{
"perkeep.org/dev/devcam",
"perkeep.org/cmd/pk-get",
"perkeep.org/cmd/pk-put",
"perkeep.org/cmd/pk",
"perkeep.org/server/perkeepd",
"perkeep.org/app/hello",
"perkeep.org/app/scanningcabinet",
"perkeep.org/app/scanningcabinet/scancab",
}
switch *targets {
case "*":
buildAll = true
case "":
// Add pk-mount to default build targets on OSes that support FUSE.
switch *buildOS {
case "linux":
targs = append(targs, "perkeep.org/cmd/pk-mount")
}
default:
if *website {
log.Fatal("--targets and --website are mutually exclusive")
}
if t := strings.Split(*targets, ","); len(t) != 0 {
targs = t
}
}
if *website {
buildAll = false
targs = []string{"perkeep.org/website/pk-web"}
}
tags := []string{"purego"} // for cznic/zappy
if *static {
tags = append(tags, "netgo", "osusergo")
}
baseArgs := []string{"install", "-v"}
if *race {
baseArgs = append(baseArgs, "-race")
}
if *verbose {
log.Printf("version to stamp is %q, %q", version, gitRev)
}
var ldFlags string
if *static {
ldFlags = "-w -d -linkmode internal"
}
if *stampVersion {
if ldFlags != "" {
ldFlags += " "
}
ldFlags += "-X \"perkeep.org/pkg/buildinfo.GitInfo=" + gitRev + "\""
ldFlags += "-X \"perkeep.org/pkg/buildinfo.Version=" + version + "\""
}
if ldFlags != "" {
baseArgs = append(baseArgs, "--ldflags="+ldFlags)
}
baseArgs = append(baseArgs, "--tags="+strings.Join(tags, ","))
// First install command: build just the final binaries, installed to a GOBIN
// under <perkeep_root>/bin:
args := append(baseArgs, targs...)
if buildAll {
args = append(args,
"perkeep.org/app/...",
"perkeep.org/pkg/...",
"perkeep.org/server/...",
"perkeep.org/internal/...",
)
}
cmd := exec.Command("go", args...)
cmd.Env = goEnv()
if *static {
cmd.Env = append(cmd.Env, "CGO_ENABLED=0")
}
if *verbose {
log.Printf("Running go %q with Env %q", args, cmd.Env)
}
var output bytes.Buffer
if *quiet {
cmd.Stdout = &output
cmd.Stderr = &output
} else {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
if *verbose {
log.Printf("Running go install of main binaries with args %s", cmd.Args)
}
if err := cmd.Run(); err != nil {
log.Fatalf("Error building main binaries: %v\n%s", err, output.String())
}
if !*quiet {
log.Printf("Success. Binaries are in %s", actualBinDir())
}
}
func actualBinDir() string {
cmd := exec.Command("go", "list", "-f", "{{.Target}}", "perkeep.org/cmd/pk")
cmd.Env = goEnv()
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Fatalf("Could not run go list to guess install dir: %v, %v", err, out)
}
return filepath.Dir(strings.TrimSpace(string(out)))
}
func goPathBinDir() (string, error) {
cmd := exec.Command("go", "env", "GOPATH")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("could not get GOPATH: %v, %s", err, out)
}
paths := filepath.SplitList(strings.TrimSpace(string(out)))
if len(paths) < 1 {
return "", errors.New("no GOPATH")
}
return filepath.Join(paths[0], "bin"), nil
}
// Create an environment variable of the form key=value.
func envPair(key, value string) string {
return fmt.Sprintf("%s=%s", key, value)
}
func goEnv() (ret []string) {
ret = slices.Clone(os.Environ())
var cross bool
if *buildOS != runtime.GOOS {
ret = append(ret, envPair("GOOS", *buildOS))
cross = true
}
if *buildARCH != runtime.GOARCH {
ret = append(ret, envPair("GOARCH", *buildARCH))
cross = true
}
if cross {
ret = append(ret, envPair("GOBIN", ""))
}
// If we're building for ARM (regardless of cross-compiling or not), we reset GOARM
if *buildARCH == "arm" {
ret = append(ret, envPair("GOARM", *buildARM))
}
return ret
}
// fullSrcPath returns the full path concatenation
// of pkRoot with fromSrc.
func fullSrcPath(fromSrc string) string {
return filepath.Join(pkRoot, filepath.FromSlash(fromSrc))
}
// getVersion returns the version of Perkeep found in a VERSION file at the root.
func getVersion() string {
slurp, err := os.ReadFile(filepath.Join(pkRoot, "VERSION"))
v := strings.TrimSpace(string(slurp))
if err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
if v == "" {
return "unknown"
}
return v
}
var gitVersionRx = regexp.MustCompile(`\b\d\d\d\d-\d\d-\d\d-[0-9a-f]{10,10}\b`)
// getGitVersion returns the git version of the git repo at pkRoot as a
// string of the form "yyyy-mm-dd-xxxxxxx", with an optional trailing
// '+' if there are any local uncommitted modifications to the tree.
func getGitVersion() string {
if _, err := exec.LookPath("git"); err != nil {
return ""
}
if _, err := os.Stat(filepath.Join(pkRoot, ".git")); os.IsNotExist(err) {
return ""
}
cmd := exec.Command("git", "rev-list", "--max-count=1", "--pretty=format:'%ad-%h'",
"--date=short", "--abbrev=10", "HEAD")
cmd.Dir = pkRoot
out, err := cmd.Output()
if err != nil {
log.Fatalf("Error running git rev-list in %s: %v", pkRoot, err)
}
v := strings.TrimSpace(string(out))
if m := gitVersionRx.FindStringSubmatch(v); m != nil {
v = m[0]
} else {
panic("Failed to find git version in " + v)
}
cmd = exec.Command("git", "diff", "--exit-code")
cmd.Dir = pkRoot
if err := cmd.Run(); err != nil {
v += "+"
}
return v
}
// verifyPerkeepRoot sets pkRoot and crashes if dir isn't the Perkeep root directory.
func verifyPerkeepRoot() {
var err error
pkRoot, err = os.Getwd()
if err != nil {
log.Fatalf("Failed to get current directory: %v", err)
}
testFile := filepath.Join(pkRoot, "pkg", "blob", "ref.go")
if _, err := os.Stat(testFile); err != nil {
log.Fatalf("make.go must be run from the Perkeep src root directory (where make.go is). Current working directory is %s", pkRoot)
}
// we can't rely on perkeep.org/cmd/pk with modules on as we have no assurance
// the current dir is $GOPATH/src/perkeep.org, so we use ./cmd/pk instead.
cmd := exec.Command("go", "list", "-f", "{{.Target}}", "perkeep.org/cmd/pk")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Fatalf("Could not run go list to find install dir: %v, %s", err, out)
}
binDir = filepath.Dir(strings.TrimSpace(string(out)))
}
const (
goVersionMinor = 21
)
var validVersionRx = regexp.MustCompile(`go version go1\.(\d+)`)
// verifyGoModules ensures that "GO111MODULE" isn't set to "off"
func verifyGoModules() {
gomodules := os.Getenv("GO11MODULE")
if gomodules == "off" {
log.Fatalf("GO11MODULE is set to 'off'. Please enable it to continue.")
}
}
// verifyGoVersion runs "go version" and parses the output. If the version is
// acceptable a check for gopherjs versions are also done. If problems
// are found a message is logged and we abort.
func verifyGoVersion() {
_, err := exec.LookPath("go")
if err != nil {
log.Fatalf("Go doesn't appear to be installed ('go' isn't in your PATH). Install Go 1.%d or newer.", goVersionMinor)
}
out, err := exec.Command("go", "version").Output()
if err != nil {
log.Fatalf("Error checking Go version with the 'go' command: %v", err)
}
version := string(out)
// Handle non-versioned binaries
// ex: "go version devel +c26fac8 Thu Feb 15 21:41:39 2018 +0000 linux/amd64"
if strings.HasPrefix(version, "go version devel ") {
return
}
m := validVersionRx.FindStringSubmatch(version)
if m == nil {
log.Fatalf("Unexpected output while checking 'go version': %q", version)
}
minorVersion, err := strconv.Atoi(m[1])
if err != nil {
log.Fatalf("Unexpected error while parsing version string %q: %v", m[1], err)
}
if minorVersion < goVersionMinor {
log.Fatalf("Your version of Go (%s) is too old. Perkeep requires Go 1.%d or later.", string(out), goVersionMinor)
}
}
func failIfCamlistoreOrgDir() {
dir, _ := os.Getwd()
if strings.HasSuffix(dir, "camlistore.org") {
log.Fatalf(`Camlistore was renamed to Perkeep. Your current directory (%s) looks like a camlistore.org directory.
We're expecting you to be in a perkeep.org directory now.
See https://github.com/perkeep/perkeep/issues/981#issuecomment-354690313 for details.
You need to rename your "camlistore.org" parent directory to "perkeep.org"
`, dir)
}
}