...

Source file src/github.com/chaos-mesh/chaos-mesh/cmd/generate-makefile/generate_binary_makefile.go

Documentation: github.com/chaos-mesh/chaos-mesh/cmd/generate-makefile

     1  // Copyright 2023 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    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  // binaryGeneratedMkTemplate is the template for the file binary.generated.mk, use binaryGeneratedMkOptions as the context.
    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  // binaryRecipeTemplate is the template for one target, use binaryRecipeOptions as the context.
    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  	// TargetName is the name of the makefile target.
    55  	TargetName string
    56  	// SourcePath is the path to the source file.
    57  	SourcePath string
    58  	// OutputPath is the path to the output file.
    59  	OutputPath string
    60  	// UseCGO introduces a CGO_ENABLED=1 environment variable to the build command.
    61  	UseCGO bool
    62  	// DependencyTargets are the targets that this target depends on.
    63  	DependencyTargets []string
    64  	// Comment is the comment for the target, do not need to include the leading `##`
    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  // binaryRecipes is the list of binaryRecipes to generate, edit here to build new binaries.
   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