Debian Codegraph @

api/internal/plugins/compiler/compiler.go

raw
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package compiler
5
6 import (
7 "bytes"
8 "fmt"
9 "log"
10 "os"
11 "os/exec"
12 "path/filepath"
13 "strings"
14
15 "sigs.k8s.io/kustomize/api/internal/plugins/utils"
16 "sigs.k8s.io/kustomize/kyaml/errors"
17 )
18
19 // Compiler creates Go plugin object files.
20 type Compiler struct {
21 // pluginRoot is where the user
22 // has her ${g}/${v}/$lower(${k})/${k}.go files.
23 pluginRoot string
24 // Where compilation happens.
25 workDir string
26 // Used as the root file name for src and object.
27 rawKind string
28 // Capture compiler output.
29 stderr bytes.Buffer
30 // Capture compiler output.
31 stdout bytes.Buffer
32 }
33
34 // NewCompiler returns a new compiler instance.
35 func NewCompiler(root string) *Compiler {
36 return &Compiler{pluginRoot: root}
37 }
38
39 // Set GVK converts g,v,k tuples to file path components.
40 func (b *Compiler) SetGVK(g, v, k string) {
41 b.rawKind = k
42 b.workDir = filepath.Join(b.pluginRoot, g, v, strings.ToLower(k))
43 }
44
45 func (b *Compiler) srcPath() string {
46 return filepath.Join(b.workDir, b.rawKind+".go")
47 }
48
49 func (b *Compiler) objFile() string {
50 return b.rawKind + ".so"
51 }
52
53 // Absolute path to the compiler output (the .so file).
54 func (b *Compiler) ObjPath() string {
55 return filepath.Join(b.workDir, b.objFile())
56 }
57
58 // Compile changes its working directory to
59 // ${pluginRoot}/${g}/${v}/$lower(${k} and places
60 // object code next to source code.
61 func (b *Compiler) Compile() error {
62 if !utils.FileExists(b.srcPath()) {
63 return fmt.Errorf("cannot find source at '%s'", b.srcPath())
64 }
65 // If you use an IDE, make sure it's go build and test flags
66 // match those used below. Same goes for Makefile targets.
67 commands := []string{
68 "build",
69 // "-trimpath", This flag used to make it better, now it makes it worse,
70 // see https://github.com/golang/go/issues/31354
71 "-buildmode",
72 "plugin",
73 "-o", b.objFile(),
74 }
75 goBin := utils.GoBin()
76 if !utils.FileExists(goBin) {
77 return fmt.Errorf(
78 "cannot find go compiler %s", goBin)
79 }
80 cmd := exec.Command(goBin, commands...)
81 b.stderr.Reset()
82 cmd.Stderr = &b.stderr
83 b.stdout.Reset()
84 cmd.Stdout = &b.stdout
85 cmd.Env = os.Environ()
86 cmd.Dir = b.workDir
87 if err := cmd.Run(); err != nil {
88 b.report()
89 return errors.WrapPrefixf(
90 err, "cannot compile %s:\nSTDERR\n%s\n",
91 b.srcPath(), b.stderr.String())
92 }
93 result := filepath.Join(b.workDir, b.objFile())
94 if utils.FileExists(result) {
95 log.Printf("compiler created: %s", result)
96 return nil
97 }
98 return fmt.Errorf("post compile, cannot find '%s'", result)
99 }
100
101 func (b *Compiler) report() {
102 log.Println("stdout: -------")
103 log.Println(b.stdout.String())
104 log.Println("----------------")
105 log.Println("stderr: -------")
106 log.Println(b.stderr.String())
107 log.Println("----------------")
108 }