-
Notifications
You must be signed in to change notification settings - Fork 33
/
build.go
87 lines (76 loc) · 1.68 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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/nokia/ntt/internal/compdb"
"github.com/nokia/ntt/project"
"github.com/spf13/cobra"
)
var (
BuildCommand = &cobra.Command{
Use: "build",
Short: "Build test suite and its dependencies",
RunE: Build,
}
ErrNoSources = fmt.Errorf("no sources available")
CompDB bool
)
func init() {
BuildCommand.Flags().BoolVar(&CompDB, "compdb", false, "generate compilation database")
}
func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func Build(cmd *cobra.Command, args []string) error {
tasks, err := project.BuildTasks(Project)
if err != nil {
return err
}
if CompDB {
var db []compdb.Command
for _, t := range tasks {
// NOTE(5nord) K3 does not a dedicated command building
// libraries. There we just skip the CompDB output,
// when the input list is the same as the output list.
//
// This approach is not perfect, but sufficient for now.
if equal(t.Inputs(), t.Outputs()) {
continue
}
cmd := t.String()
for _, in := range t.Inputs() {
for _, out := range t.Outputs() {
db = append(db, compdb.Command{
Command: cmd,
File: in,
Output: out,
})
}
}
}
if len(db) > 0 {
b, err := json.MarshalIndent(db, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal compdb: %w", err)
}
if err := os.WriteFile("compile_commands.json", b, 0644); err != nil {
return fmt.Errorf("failed to write compile_commands.json: %w", err)
}
}
}
for _, t := range tasks {
if err := t.Run(); err != nil {
return err
}
}
return nil
}