1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package main
17
18 import (
19 "bytes"
20 "fmt"
21 "os"
22 "text/template"
23
24 "github.com/pkg/errors"
25 )
26
27
28 const binaryGeneratedMkTemplate = `# Generated by ./cmd/generate-makefile. DO NOT EDIT.
29
30 ##@ Generated targets in binary.generated.mk
31
32 {{ .Content -}}
33
34 .PHONY: clean-binary
35 clean-binary:
36 {{- range .Recipes }}
37 rm -f {{ .OutputPath }}
38 {{- end }}
39 `
40
41
42 const binaryRecipeTemplate = `.PHONY: {{ .TargetName }}
43 {{ .TargetName }}: SHELL:=$(RUN_IN_BUILD_SHELL)
44 {{ .TargetName }}: image-build-env {{ StringsJoin .DependencyTargets " " }} ## {{ .Comment }}
45 {{- if .UseCGO }}
46 $(CGO) build -ldflags "$(LDFLAGS)" -tags "${BUILD_TAGS}" -o {{ .OutputPath }} {{ .SourcePath }}
47 {{- else }}
48 $(GO) build -ldflags "$(LDFLAGS)" -tags "${BUILD_TAGS}" -o {{ .OutputPath }} {{ .SourcePath }}
49 {{- end }}
50
51 `
52
53 type binaryRecipeOptions struct {
54
55 TargetName string
56
57 SourcePath string
58
59 OutputPath string
60
61 UseCGO bool
62
63 DependencyTargets []string
64
65 Comment string
66 }
67
68 type binaryGeneratedMkOptions struct {
69 Recipes []binaryRecipeOptions
70 Content string
71 }
72
73 func renderBinaryGeneratedMk(name string, fileTemplate string, binaryRecipeTemplate string, recipes []binaryRecipeOptions) error {
74 targetFile, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
75 if err != nil {
76 return errors.Wrap(err, "open file "+name)
77 }
78 recipeTemplate, err := template.New(name + " recipe").Funcs(defaultFuncMap).Parse(binaryRecipeTemplate)
79 if err != nil {
80 return errors.Wrap(err, "parse "+name+" recipe template")
81 }
82
83 var buffer bytes.Buffer
84 for _, recipe := range recipes {
85 err := recipeTemplate.Execute(&buffer, recipe)
86 if err != nil {
87 return errors.Wrap(err, fmt.Sprintf("render recipe in "+name+", recipe: %s", recipe.TargetName))
88 }
89 }
90 binaryTemplate, err := template.New(name).Parse(fileTemplate)
91 if err != nil {
92 return errors.Wrap(err, "parse "+name+" template")
93 }
94 err = binaryTemplate.Execute(targetFile, binaryGeneratedMkOptions{
95 Recipes: recipes,
96 Content: buffer.String(),
97 })
98 if err != nil {
99 return errors.Wrap(err, "render "+name)
100 }
101 return nil
102 }
103
104
105 var binaryRecipes = []binaryRecipeOptions{
106 {
107 TargetName: "images/chaos-mesh/bin/chaos-controller-manager",
108 SourcePath: "cmd/chaos-controller-manager/main.go",
109 OutputPath: "images/chaos-mesh/bin/chaos-controller-manager",
110 UseCGO: false,
111 DependencyTargets: nil,
112 Comment: "Build binary chaos-controller-manager",
113 }, {
114 TargetName: "images/chaos-daemon/bin/chaos-daemon",
115 SourcePath: "cmd/chaos-daemon/main.go",
116 OutputPath: "images/chaos-daemon/bin/chaos-daemon",
117 UseCGO: true,
118 DependencyTargets: []string{
119 "pkg/time/fakeclock/fake_clock_gettime.o",
120 "pkg/time/fakeclock/fake_gettimeofday.o",
121 },
122 Comment: "Build binary chaos-daemon",
123 }, {
124 TargetName: "images/chaos-dashboard/bin/chaos-dashboard",
125 SourcePath: "cmd/chaos-dashboard/main.go",
126 OutputPath: "images/chaos-dashboard/bin/chaos-dashboard",
127 UseCGO: true,
128 DependencyTargets: []string{
129 "ui",
130 },
131 Comment: "Build binary chaos-dashboard",
132 }, {
133 TargetName: "images/chaos-daemon/bin/cdh",
134 SourcePath: "cmd/chaos-daemon-helper/main.go",
135 OutputPath: "images/chaos-daemon/bin/cdh",
136 UseCGO: true,
137 DependencyTargets: nil,
138 Comment: "Build binary chaos-daemon-helper",
139 },
140 }
141