func (Build) DarwinARM64() error {
	return buildBackend(newBuildConfig("darwin", "arm64"))
}
DarwinARM64 builds the back-end plugin for OSX on ARM (M1).
package build
import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"github.com/grafana/grafana-plugin-sdk-go/internal"
	"github.com/magefile/mage/mg"
	"github.com/magefile/mage/sh"
)
var beforeBuild = func(cfg Config) (Config, error) {
	return cfg, nil
}
func SetBeforeBuildCallback(cb BeforeBuildCallback) error {
	beforeBuild = cb
	return nil
}
var exname string
func getExecutableName(os string, arch string) (string, error) {
	if exname == "" {
		exename, err := internal.GetExecutableFromPluginJSON("src")
		if err != nil {
			return "", err
		}
		exname = exename
	}
	exeName := fmt.Sprintf("%s_%s_%s", exname, os, arch)
	if os == "windows" {
		exeName = fmt.Sprintf("%s.exe", exeName)
	}
	return exeName, nil
}
func buildBackend(cfg Config) error {
	cfg, err := beforeBuild(cfg)
	if err != nil {
		return err
	}
	exeName, err := getExecutableName(cfg.OS, cfg.Arch)
	if err != nil {
		return err
	}
	ldFlags := ""
	if !cfg.EnableCGo {
		ldFlags = `-extldflags "-static"`
	}
	if !cfg.EnableDebug {
		prefix := ""
		if ldFlags != "" {
			prefix = " "
		}
		ldFlags = fmt.Sprintf("-w -s%s%s", prefix, ldFlags)
	}
	outputPath := cfg.OutputBinaryPath
	if outputPath == "" {
		outputPath = "dist"
	}
	args := []string{
		"build", "-o", filepath.Join(outputPath, exeName),
	}
	info := getBuildInfoFromEnvironment()
	version, err := internal.GetStringValueFromJSON("package.json", "version")
	if err == nil && len(version) > 0 {
		info.Version = version
	}
	flags := make(map[string]string, 10)
	info.appendFlags(flags)
	if cfg.CustomVars != nil {
		for k, v := range cfg.CustomVars {
			flags[k] = v
		}
	}
	for k, v := range flags {
		ldFlags = fmt.Sprintf("%s -X '%s=%s'", ldFlags, k, v)
	}
	args = append(args, "-ldflags", ldFlags)
	if cfg.EnableDebug {
		args = append(args, "-gcflags=all=-N -l")
	}
	rootPackage := "./pkg"
	if cfg.RootPackagePath != "" {
		rootPackage = cfg.RootPackagePath
	}
	args = append(args, rootPackage)
	cfg.Env["GOARCH"] = cfg.Arch
	cfg.Env["GOOS"] = cfg.OS
	if !cfg.EnableCGo {
		cfg.Env["CGO_ENABLED"] = "0"
	}
	return sh.RunWith(cfg.Env, "go", args...)
}
func newBuildConfig(os string, arch string) Config {
	return Config{
		OS:          os,
		Arch:        arch,
		EnableDebug: false,
		Env:         map[string]string{},
	}
}
type Build mg.Namespace
func (Build) Linux() error {
	return buildBackend(newBuildConfig("linux", "amd64"))
}
func (Build) LinuxARM() error {
	return buildBackend(newBuildConfig("linux", "arm"))
}
func (Build) LinuxARM64() error {
	return buildBackend(newBuildConfig("linux", "arm64"))
}
func (Build) Windows() error {
	return buildBackend(newBuildConfig("windows", "amd64"))
}
func (Build) Darwin() error {
	return buildBackend(newBuildConfig("darwin", "amd64"))
}