-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
316 lines (283 loc) · 8.73 KB
/
cmd.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
// Copyright 2023 Tomas Machalek <[email protected]>
// Copyright 2023 Institute of the Czech National Corpus,
// Faculty of Arts, Charles University
// This file is part of CNC-MASM.
//
// CNC-MASM is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CNC-MASM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CNC-MASM. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/czcorpus/cnc-gokit/collections"
"github.com/fatih/color"
)
var (
version string
buildDate string
gitCommit string
KnownVersions = []string{
"2.167.8",
"2.167.10",
"2.208",
"2.214.1",
"2.223.6",
"2.225.8",
}
)
func showVersionMismatch(found, expected Version) {
fmt.Fprintf(os.Stderr, "\nERROR: Found Manatee %s, you require %s.\n", found.Semver(), expected.Semver())
fmt.Fprintln(os.Stderr, "\nA) If you prefer a different installed version of Manatee")
fmt.Fprintln(os.Stderr, "then please specify a path where a respective libmanatee.so")
fmt.Fprintf(os.Stderr, "can be found (manabuild %s --manatee-lib /path/to/libmanatee.so/dir\n\n", expected.Semver())
fmt.Fprintln(os.Stderr, "B) If you want to use the detected installed version then run")
fmt.Fprintf(os.Stderr, "this script with proper version (manabuild %s)", found.Semver())
}
func mkHeader() {
repeatStr := func(str string, n int) string {
b := strings.Builder{}
for i := 0; i < n; i++ {
b.WriteString(str)
}
return b.String()
}
verInfo := fmt.Sprintf(
"| manabuild %s, build date: %s, last commit: %s |", version, buildDate, gitCommit)
hd := fmt.Sprintf("+%s+", repeatStr("-", len(verInfo)-2))
fmt.Fprintln(os.Stderr, hd)
fmt.Fprintln(os.Stderr, verInfo)
fmt.Fprintln(os.Stderr, hd)
}
func clearPreviousBinaries(workingDir, binaryName string) {
binPath := path.Join(workingDir, fmt.Sprintf("%s.bin", binaryName))
rsPath := path.Join(workingDir, binaryName)
os.Remove(binPath)
os.Remove(rsPath)
}
func generateBootstrapScript(
ctx *OperationSequence,
needsLDScript bool,
manateeLib, workingDir, binaryName string,
) error {
binPath := path.Join(workingDir, fmt.Sprintf("%s.bin", binaryName))
rsPath := path.Join(workingDir, binaryName)
if needsLDScript {
if err := os.Rename(rsPath, binPath); err != nil {
return err
}
fw, err := os.OpenFile(rsPath, os.O_WRONLY|os.O_CREATE, 0775)
if err != nil {
return err
}
fw.WriteString("#!/usr/bin/env bash\n")
fw.WriteString("MYSELF=`which \"$0\" 2>/dev/null`\n")
fw.WriteString(fmt.Sprintf("export LD_LIBRARY_PATH=\"%s\"\n", manateeLib))
fw.WriteString(fmt.Sprintf("`dirname $0`/%s.bin \"${@:1}\"\n", binaryName))
fw.Close()
ctx.WithPausedOutput(func() {
fmt.Fprint(
os.Stderr, "\nGenerated run script to handle non-standard libmanatee.so location.")
fmt.Fprintf(
os.Stderr,
"\nTo install the application, copy files %s.bin and %s", binaryName, binaryName,
)
fmt.Fprint(os.Stderr, " to a system searched path (e.g. /usr/local/bin).")
})
} else {
fmt.Fprintf(os.Stderr, "\nTo install the application, copy file %s", binaryName)
fmt.Fprint(os.Stderr, " to a system searched path (e.g. /usr/local/bin)")
}
return nil
}
func main() {
flag.Usage = func() {
fmt.Fprint(
os.Stderr,
"Manabuild - a tool for building Go programs with Manatee-open dependency\n",
fmt.Sprintf("usage: %s [binary name] (in case .manabuild.json or -no-build is enabled)\n", filepath.Base(os.Args[0])),
fmt.Sprintf(" %s [binary name] [version]\n", filepath.Base(os.Args[0])),
fmt.Sprintf(" %s version", filepath.Base(os.Args[0])),
"\n")
flag.PrintDefaults()
}
conf := new(Conf)
workingDir := flag.String("project-path", ".", "A path where a target project is located")
err := TryConfig(*workingDir, conf)
if err != nil && err != ErrNoConfig {
log.Fatal(err)
}
shouldRunTests := flag.Bool("test", false, "Specify whether to run unit tests")
buildCmdDir := flag.String("cmd-dir", "", "A subdirectory of `cmd` to be used for build.")
noBuild := flag.Bool("no-build", false, "Just check and prepare Manatee sources and define CGO variables")
manateeSrc := flag.String("manatee-src", "", "Location of Manatee source files")
manateeLib := flag.String("manatee-lib", "", "Location of libmanatee.so")
flag.Parse()
if flag.Arg(0) == "version" {
fmt.Fprintf(
os.Stderr,
"Manabuild %s\nbuild date: %s\nlast commit: %s\n",
version, buildDate, gitCommit,
)
os.Exit(0)
return
}
if !conf.IsLoaded() && !*noBuild && (flag.NArg() < 1 || flag.NArg() > 2) {
flag.Usage()
os.Exit(1)
return
}
mkHeader()
if conf.SrcPath() != "" {
color.New(color.FgHiYellow).Fprintf(os.Stderr, "\n \u24D8 Using %s\n", conf.SrcPath())
}
var shouldGenerateRunScript bool
detectedVersion, err := AutodetectManateeVersion(*manateeLib, KnownVersions)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to find manatee-open or determine its version: %s\n", err)
os.Exit(1)
} else if detectedVersion.IsZero() {
fmt.Fprintf(os.Stderr, "Autodetection has not found any suitable Manatee version. Please select one manually\n")
os.Exit(1)
}
specifiedVersion := detectedVersion
if flag.Arg(1) != "" {
specifiedVersion, err = ParseManateeVersion(flag.Arg(1))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse specified version")
os.Exit(1)
}
} else {
color.New(color.FgHiYellow).Fprintf(
os.Stderr,
"\n \u24D8 No explicit Manatee version specified. Found %s\n",
detectedVersion,
)
}
if flag.Arg(0) != "" {
conf.TargetBinaryName = flag.Arg(0)
}
if !collections.SliceContains(KnownVersions, specifiedVersion.Semver()) {
fmt.Fprintf(
os.Stderr,
"Unsupported version: %s. Please use one of: %s\n",
specifiedVersion, strings.Join(KnownVersions, ", "),
)
os.Exit(1)
}
timeLocation, err := time.LoadLocation("Europe/Prague")
if err != nil {
fmt.Fprintln(os.Stderr, "failed to load time location")
os.Exit(1)
}
seq := NewOperationSequence(timeLocation)
seq.RunOperation("searching for manatee-open", func(ctx *OperationSequence) {
if *manateeSrc == "" {
*manateeSrc, err = downloadManateeSrc(specifiedVersion)
if err != nil {
ctx.Fail(func() {
fmt.Fprintln(os.Stderr, err)
})
}
} else {
ctx.WithPausedOutput(func() {
fmt.Fprintf(
os.Stderr,
"\nAssuming that provided Manatee src path matches required version %s",
specifiedVersion.Semver(),
)
})
}
if *manateeLib == "" {
*manateeLib = findManatee(specifiedVersion)
if *manateeLib == "" {
ctx.Fail(func() {
fmt.Fprintln(
os.Stderr,
"Manatee not found in system searched paths. Please run the script with --manatee-lib argument")
})
}
if !specifiedVersion.Eq(detectedVersion) {
ctx.Fail(func() {
showVersionMismatch(detectedVersion, specifiedVersion)
})
} else {
ctx.WithPausedOutput(func() {
fmt.Fprintf(
os.Stderr,
"\nUsing system-installed %s\n",
detectedVersion.Semver(),
)
})
}
} else {
ctx.WithPausedOutput(func() {
fmt.Fprintf(
os.Stderr,
"\nAssuming that provided %s/libmanatee.so matches required version %s",
*manateeLib, specifiedVersion.Semver(),
)
})
}
if !strings.HasPrefix(*manateeLib, "/usr/local/lib") { // TODO maybe we should test more paths known by LD
shouldGenerateRunScript = true
}
})
clearPreviousBinaries(*workingDir, conf.TargetBinaryName)
seq.RunOperation("preparing manatee-open sources", func(ctx *OperationSequence) {
err = initManateeSources(specifiedVersion, *manateeSrc)
if err != nil {
ctx.Fail(func() {
fmt.Fprintf(os.Stderr, "Failed to init manatee-open sources: %s", err)
})
}
})
msg := "building target project"
if *noBuild {
msg = "exporting CGO variables"
}
seq.RunOperation(msg, func(ctx *OperationSequence) {
err = buildProject(
ctx,
specifiedVersion,
*workingDir,
*manateeSrc,
*manateeLib,
*shouldRunTests,
conf.TargetBinaryName,
*buildCmdDir,
*noBuild,
)
if err != nil {
ctx.Fail(func() {
fmt.Fprintf(os.Stderr, "\U0001F4A5 Failed to build: %s\n", err)
})
}
})
if !*noBuild {
seq.RunOperation("generating executable", func(ctx *OperationSequence) {
generateBootstrapScript(
ctx,
shouldGenerateRunScript,
*manateeLib,
*workingDir,
conf.TargetBinaryName,
)
})
}
}