CI: move grafana-build into pkg/build (#105640)
* move grafana-build into pkg/build
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
type LDFlag struct {
|
||||
Name string
|
||||
Values []string
|
||||
}
|
||||
|
||||
func GoLDFlags(flags []LDFlag) string {
|
||||
ldflags := strings.Builder{}
|
||||
for _, v := range flags {
|
||||
if v.Values == nil {
|
||||
ldflags.WriteString(v.Name + " ")
|
||||
continue
|
||||
}
|
||||
|
||||
for _, value := range v.Values {
|
||||
// For example, "-X 'main.version=v1.0.0'"
|
||||
ldflags.WriteString(fmt.Sprintf(`%s \"%s\" `, v.Name, value))
|
||||
}
|
||||
}
|
||||
|
||||
return ldflags.String()
|
||||
}
|
||||
|
||||
// GoBuildCommand returns the arguments for go build to be used in 'WithExec'.
|
||||
func GoBuildCommand(output string, ldflags []LDFlag, tags []string, main string) []string {
|
||||
args := []string{"go", "build", "-v", "-x",
|
||||
fmt.Sprintf("-ldflags=\"%s\"", GoLDFlags(ldflags)),
|
||||
fmt.Sprintf("-o=%s", output),
|
||||
"-trimpath",
|
||||
fmt.Sprintf("-tags=%s", strings.Join(tags, ",")),
|
||||
// Go is weird and paths referring to packages within a module to be prefixed with "./".
|
||||
// Otherwise, the path is assumed to be relative to $GOROOT
|
||||
"./" + main,
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
func Build(
|
||||
d *dagger.Client,
|
||||
builder *dagger.Container,
|
||||
src *dagger.Directory,
|
||||
distro Distribution,
|
||||
out string,
|
||||
opts *BuildOpts,
|
||||
) *dagger.Directory {
|
||||
vcsinfo := GetVCSInfo(src, opts.Version, opts.Enterprise)
|
||||
builder = WithVCSInfo(builder, vcsinfo, opts.Enterprise)
|
||||
|
||||
ldflags := LDFlagsDynamic(vcsinfo)
|
||||
|
||||
if opts.Static {
|
||||
ldflags = LDFlagsStatic(vcsinfo)
|
||||
}
|
||||
|
||||
cmd := []string{
|
||||
"grafana",
|
||||
"grafana-server",
|
||||
"grafana-cli",
|
||||
"grafana-example-apiserver",
|
||||
}
|
||||
|
||||
os, _ := OSAndArch(distro)
|
||||
|
||||
for _, v := range cmd {
|
||||
// Some CLI packages such as grafana-example-apiserver don't exist in earlier Grafana Versions <10.3
|
||||
// Below check skips building them as needed
|
||||
pkgPath := path.Join("pkg", "cmd", v)
|
||||
out := path.Join(out, v)
|
||||
if os == "windows" {
|
||||
out += ".exe"
|
||||
}
|
||||
|
||||
cmd := GoBuildCommand(out, ldflags, opts.Tags, pkgPath)
|
||||
|
||||
script := fmt.Sprintf(`if [ -d %s ]; then %s; fi`, pkgPath, strings.Join(cmd, " "))
|
||||
log.Printf("Building with command '%s'", script)
|
||||
|
||||
builder = builder.
|
||||
WithExec([]string{"/bin/sh", "-c", script})
|
||||
}
|
||||
|
||||
return builder.Directory(out)
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"dagger.io/dagger"
|
||||
"github.com/grafana/grafana/pkg/build/daggerbuild/containers"
|
||||
"github.com/grafana/grafana/pkg/build/daggerbuild/golang"
|
||||
)
|
||||
|
||||
// BuildOpts are general options that can change the way Grafana is compiled regardless of distribution.
|
||||
type BuildOpts struct {
|
||||
Version string
|
||||
ExperimentalFlags []string
|
||||
Tags []string
|
||||
WireTag string
|
||||
Static bool
|
||||
Enterprise bool
|
||||
}
|
||||
|
||||
func distroOptsFunc(log *slog.Logger, distro Distribution) (DistroBuildOptsFunc, error) {
|
||||
if val, ok := DistributionGoOpts[distro]; ok {
|
||||
return DistroOptsLogger(log, val), nil
|
||||
}
|
||||
return nil, errors.New("unrecognized distribution")
|
||||
}
|
||||
|
||||
func WithGoEnv(log *slog.Logger, container *dagger.Container, distro Distribution, opts *BuildOpts) (*dagger.Container, error) {
|
||||
fn, err := distroOptsFunc(log, distro)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bopts := fn(distro, opts.ExperimentalFlags, opts.Tags)
|
||||
|
||||
return containers.WithEnv(container, GoBuildEnv(bopts)), nil
|
||||
}
|
||||
|
||||
func WithViceroyEnv(log *slog.Logger, container *dagger.Container, distro Distribution, opts *BuildOpts) (*dagger.Container, error) {
|
||||
fn, err := distroOptsFunc(log, distro)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bopts := fn(distro, opts.ExperimentalFlags, opts.Tags)
|
||||
|
||||
return containers.WithEnv(container, ViceroyEnv(bopts)), nil
|
||||
}
|
||||
|
||||
func ViceroyContainer(
|
||||
d *dagger.Client,
|
||||
log *slog.Logger,
|
||||
distro Distribution,
|
||||
goVersion string,
|
||||
viceroyVersion string,
|
||||
opts *BuildOpts,
|
||||
) (*dagger.Container, error) {
|
||||
containerOpts := dagger.ContainerOpts{
|
||||
Platform: "linux/amd64",
|
||||
}
|
||||
|
||||
// Instead of directly using the `arch` variable here to substitute in the GoURL, we have to be careful with the Go releases.
|
||||
// Supported releases (in the names):
|
||||
// * amd64
|
||||
// * armv6l
|
||||
// * arm64
|
||||
goURL := golang.DownloadURL(goVersion, "amd64")
|
||||
container := d.Container(containerOpts).From(fmt.Sprintf("rfratto/viceroy:%s", viceroyVersion))
|
||||
|
||||
// Install Go manually, and install make, git, and curl from the package manager.
|
||||
container = container.WithExec([]string{"apt-get", "update"}).
|
||||
WithExec([]string{"apt-get", "install", "-yq", "curl", "make", "git"}).
|
||||
WithExec([]string{"/bin/sh", "-c", fmt.Sprintf("curl -L %s | tar -C /usr/local -xzf -", goURL)}).
|
||||
WithEnvVariable("PATH", "/bin:/usr/bin:/usr/local/bin:/usr/local/go/bin:/usr/osxcross/bin")
|
||||
|
||||
return WithViceroyEnv(log, container, distro, opts)
|
||||
}
|
||||
|
||||
func GolangContainer(
|
||||
d *dagger.Client,
|
||||
log *slog.Logger,
|
||||
goVersion string,
|
||||
viceroyVersion string,
|
||||
platform dagger.Platform,
|
||||
distro Distribution,
|
||||
opts *BuildOpts,
|
||||
) (*dagger.Container, error) {
|
||||
os, _ := OSAndArch(distro)
|
||||
// Only use viceroy for all darwin and only windows/amd64
|
||||
if os == "darwin" || distro == DistWindowsAMD64 {
|
||||
return ViceroyContainer(d, log, distro, goVersion, viceroyVersion, opts)
|
||||
}
|
||||
|
||||
container := golang.Container(d, platform, goVersion).
|
||||
WithExec([]string{"apk", "add", "--update", "wget", "build-base", "alpine-sdk", "musl", "musl-dev", "xz"}).
|
||||
WithExec([]string{"wget", "https://ziglang.org/download/0.11.0/zig-linux-x86_64-0.11.0.tar.xz"}).
|
||||
WithExec([]string{"tar", "--strip-components=1", "-C", "/", "-xf", "zig-linux-x86_64-0.11.0.tar.xz"}).
|
||||
WithExec([]string{"mv", "/zig", "/bin/zig"}).
|
||||
// Install the toolchain specifically for armv7 until we figure out why it's crashing w/ zig container = container.
|
||||
WithExec([]string{"mkdir", "/toolchain"}).
|
||||
WithExec([]string{"wget", "http://musl.cc/arm-linux-musleabihf-cross.tgz", "-P", "/toolchain"}).
|
||||
WithExec([]string{"tar", "-xvf", "/toolchain/arm-linux-musleabihf-cross.tgz", "-C", "/toolchain"}).
|
||||
WithExec([]string{"wget", "https://musl.cc/s390x-linux-musl-cross.tgz", "-P", "/toolchain"}).
|
||||
WithExec([]string{"tar", "-xvf", "/toolchain/s390x-linux-musl-cross.tgz", "-C", "/toolchain"})
|
||||
|
||||
return WithGoEnv(log, container, distro, opts)
|
||||
}
|
||||
|
||||
func withCue(c *dagger.Container, src *dagger.Directory) *dagger.Container {
|
||||
return c.
|
||||
WithDirectory("/src/cue.mod", src.Directory("cue.mod")).
|
||||
WithDirectory("/src/kinds", src.Directory("kinds")).
|
||||
WithDirectory("/src/packages/grafana-schema", src.Directory("packages/grafana-schema"), dagger.ContainerWithDirectoryOpts{
|
||||
Include: []string{"**/*.cue"},
|
||||
}).
|
||||
WithDirectory("/src/public/app/plugins", src.Directory("public/app/plugins"), dagger.ContainerWithDirectoryOpts{
|
||||
Include: []string{"**/*.cue", "**/plugin.json"},
|
||||
}).
|
||||
WithFile("/src/embed.go", src.File("embed.go"))
|
||||
}
|
||||
|
||||
// Builder returns the container that is used to build the Grafana backend binaries.
|
||||
// The build container:
|
||||
// * Will be based on rfratto/viceroy for Darwin or Windows
|
||||
// * Will be based on golang:x.y.z-alpine for all other ditsros
|
||||
// * Will download & cache the downloaded Go modules
|
||||
// * Will run `make gen-go` on the provided Grafana source
|
||||
// - With the linux/amd64 arch/os combination, regardless of what the requested distro is.
|
||||
//
|
||||
// * And will have all of the environment variables necessary to run `go build`.
|
||||
func Builder(
|
||||
d *dagger.Client,
|
||||
log *slog.Logger,
|
||||
distro Distribution,
|
||||
opts *BuildOpts,
|
||||
platform dagger.Platform,
|
||||
src *dagger.Directory,
|
||||
goVersion string,
|
||||
viceroyVersion string,
|
||||
goBuildCache *dagger.CacheVolume,
|
||||
goModCache *dagger.CacheVolume,
|
||||
) (*dagger.Container, error) {
|
||||
var (
|
||||
version = opts.Version
|
||||
)
|
||||
|
||||
// for some distros we use the golang official iamge. For others, we use viceroy.
|
||||
builder, err := GolangContainer(d, log, goVersion, viceroyVersion, platform, distro, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
builder = builder.
|
||||
WithMountedCache("/root/.cache/go", goBuildCache).
|
||||
WithEnvVariable("GOCACHE", "/root/.cache/go")
|
||||
|
||||
commitInfo := GetVCSInfo(src, version, opts.Enterprise)
|
||||
|
||||
builder = withCue(builder, src).
|
||||
WithDirectory("/src/", src, dagger.ContainerWithDirectoryOpts{
|
||||
Include: []string{"**/*.mod", "**/*.sum", "**/*.work"},
|
||||
}).
|
||||
WithDirectory("/src/pkg", src.Directory("pkg")).
|
||||
WithDirectory("/src/apps", src.Directory("apps")).
|
||||
WithDirectory("/src/emails", src.Directory("emails")).
|
||||
WithFile("/src/pkg/server/wire_gen.go", Wire(d, src, platform, goVersion, opts.WireTag)).
|
||||
WithFile("/src/.buildinfo.commit", commitInfo.Commit).
|
||||
WithWorkdir("/src")
|
||||
|
||||
if opts.Enterprise {
|
||||
builder = builder.WithFile("/src/.buildinfo.enterprise-commit", commitInfo.EnterpriseCommit)
|
||||
}
|
||||
|
||||
builder = golang.WithCachedGoDependencies(
|
||||
builder,
|
||||
goModCache,
|
||||
)
|
||||
|
||||
return builder, nil
|
||||
}
|
||||
|
||||
func Wire(d *dagger.Client, src *dagger.Directory, platform dagger.Platform, goVersion string, wireTag string) *dagger.File {
|
||||
// withCue is only required during `make gen-go` in 9.5.x or older.
|
||||
return withCue(golang.Container(d, platform, goVersion), src).
|
||||
WithExec([]string{"apk", "add", "make"}).
|
||||
WithDirectory("/src/", src, dagger.ContainerWithDirectoryOpts{
|
||||
Include: []string{"**/*.mod", "**/*.sum", "**/*.work"},
|
||||
}).
|
||||
WithDirectory("/src/pkg", src.Directory("pkg")).
|
||||
WithDirectory("/src/apps", src.Directory("apps")).
|
||||
WithDirectory("/src/.bingo", src.Directory(".bingo")).
|
||||
WithFile("/src/Makefile", src.File("Makefile")).
|
||||
WithWorkdir("/src").
|
||||
WithExec([]string{"make", "gen-go", fmt.Sprintf("WIRE_TAGS=%s", wireTag)}).
|
||||
File("/src/pkg/server/wire_gen.go")
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
// Distribution is a string that represents the GOOS and GOARCH environment variables joined by a "/".
|
||||
// Optionally, if there is an extra argument specific to that architecture, it will be the last segment of the string.
|
||||
// Examples:
|
||||
// - "linux/arm/v6" = GOOS=linux, GOARCH=arm, GOARM=6
|
||||
// - "linux/arm/v7" = GOOS=linux, GOARCH=arm, GOARM=7
|
||||
// - "linux/amd64/v7" = GOOS=linux, GOARCH=arm, GOARM=7
|
||||
// - "linux/amd64/v2" = GOOS=linux, GOARCH=amd64, GOAMD64=v2
|
||||
// The list of distributions is built from the command "go tool dist list".
|
||||
// While not all are used, it at least represents the possible combinations.
|
||||
type Distribution string
|
||||
|
||||
const (
|
||||
DistDarwinAMD64 Distribution = "darwin/amd64"
|
||||
DistDarwinAMD64v1 Distribution = "darwin/amd64/v1"
|
||||
DistDarwinAMD64v2 Distribution = "darwin/amd64/v2"
|
||||
DistDarwinAMD64v3 Distribution = "darwin/amd64/v3"
|
||||
DistDarwinAMD64v4 Distribution = "darwin/amd64/v4"
|
||||
DistDarwinARM64 Distribution = "darwin/arm64"
|
||||
)
|
||||
|
||||
const (
|
||||
DistFreeBSD386 Distribution = "freebsd/386"
|
||||
DistFreeBSD386SSE2 Distribution = "freebsd/386/sse2"
|
||||
DistFreeBSD386SoftFloat Distribution = "freebsd/386/softfloat"
|
||||
DistFreeBSDAMD64 Distribution = "freebsd/amd64"
|
||||
DistFreeBSDAMD64v1 Distribution = "freebsd/amd64/v1"
|
||||
DistFreeBSDAMD64v2 Distribution = "freebsd/amd64/v2"
|
||||
DistFreeBSDAMD64v3 Distribution = "freebsd/amd64/v3"
|
||||
DistFreeBSDAMD64v4 Distribution = "freebsd/amd64/v4"
|
||||
DistFreeBSDARM Distribution = "freebsd/arm"
|
||||
DistFreeBSDARM64 Distribution = "freebsd/arm64"
|
||||
DistFreeBSDRISCV Distribution = "freebsd/riscv64"
|
||||
)
|
||||
|
||||
const (
|
||||
DistIllumosAMD64 Distribution = "illumos/amd64"
|
||||
DistIllumosAMD64v1 Distribution = "illumos/amd64/v1"
|
||||
DistIllumosAMD64v2 Distribution = "illumos/amd64/v2"
|
||||
DistIllumosAMD64v3 Distribution = "illumos/amd64/v3"
|
||||
DistIllumosAMD64v4 Distribution = "illumos/amd64/v4"
|
||||
)
|
||||
const (
|
||||
DistLinux386 Distribution = "linux/386"
|
||||
DistLinux386SSE2 Distribution = "linux/386/sse2"
|
||||
DistLinux386SoftFloat Distribution = "linux/386/softfloat"
|
||||
DistLinuxAMD64 Distribution = "linux/amd64"
|
||||
DistLinuxAMD64v1 Distribution = "linux/amd64/v1"
|
||||
DistLinuxAMD64v2 Distribution = "linux/amd64/v2"
|
||||
DistLinuxAMD64v3 Distribution = "linux/amd64/v3"
|
||||
DistLinuxAMD64v4 Distribution = "linux/amd64/v4"
|
||||
DistLinuxAMD64Dynamic Distribution = "linux/amd64/dynamic"
|
||||
DistLinuxAMD64DynamicMusl Distribution = "linux/amd64/dynamic-musl"
|
||||
DistLinuxARM Distribution = "linux/arm"
|
||||
DistLinuxARMv6 Distribution = "linux/arm/v6"
|
||||
DistLinuxARMv7 Distribution = "linux/arm/v7"
|
||||
DistLinuxARM64 Distribution = "linux/arm64"
|
||||
DistLinuxARM64Dynamic Distribution = "linux/arm64/dynamic"
|
||||
DistLinuxLoong64 Distribution = "linux/loong64"
|
||||
DistLinuxMips Distribution = "linux/mips"
|
||||
DistLinuxMips64 Distribution = "linux/mips64"
|
||||
DistLinuxMips64le Distribution = "linux/mips64le"
|
||||
DistLinuxMipsle Distribution = "linux/mipsle"
|
||||
DistLinuxPPC64 Distribution = "linux/ppc64"
|
||||
DistLinuxPPC64le Distribution = "linux/ppc64le"
|
||||
DistLinuxRISCV64 Distribution = "linux/riscv64"
|
||||
DistLinuxS390X Distribution = "linux/s390x"
|
||||
)
|
||||
|
||||
const (
|
||||
DistOpenBSD386 Distribution = "openbsd/386"
|
||||
DistOpenBSD386SSE2 Distribution = "openbsd/386/sse2"
|
||||
DistOpenBSD386SoftFLoat Distribution = "openbsd/386/softfloat"
|
||||
DistOpenBSDAMD64 Distribution = "openbsd/amd64"
|
||||
DistOpenBSDAMD64v1 Distribution = "openbsd/amd64/v1"
|
||||
DistOpenBSDAMD64v2 Distribution = "openbsd/amd64/v2"
|
||||
DistOpenBSDAMD64v3 Distribution = "openbsd/amd64/v3"
|
||||
DistOpenBSDAMD64v4 Distribution = "openbsd/amd64/v4"
|
||||
DistOpenBSDARM Distribution = "openbsd/arm"
|
||||
DistOpenBSDARMv6 Distribution = "openbsd/arm/v6"
|
||||
DistOpenBSDARMv7 Distribution = "openbsd/arm/v7"
|
||||
DistOpenBSDARM64 Distribution = "openbsd/arm64"
|
||||
DistOpenBSDMips64 Distribution = "openbsd/mips64"
|
||||
)
|
||||
|
||||
const (
|
||||
DistPlan9386 Distribution = "plan9/386"
|
||||
DistPlan9386SSE2 Distribution = "plan9/386/sse2"
|
||||
DistPlan9386SoftFloat Distribution = "plan9/386/softfloat"
|
||||
DistPlan9AMD64 Distribution = "plan9/amd64"
|
||||
DistPlan9AMD64v1 Distribution = "plan9/amd64/v1"
|
||||
DistPlan9AMD64v2 Distribution = "plan9/amd64/v2"
|
||||
DistPlan9AMD64v3 Distribution = "plan9/amd64/v3"
|
||||
DistPlan9AMD64v4 Distribution = "plan9/amd64/v4"
|
||||
DistPlan9ARM Distribution = "plan9/arm/v6"
|
||||
DistPlan9ARMv6 Distribution = "plan9/arm/v6"
|
||||
DistPlan9ARMv7 Distribution = "plan9/arm/v7"
|
||||
)
|
||||
|
||||
const (
|
||||
DistSolarisAMD64 Distribution = "solaris/amd64"
|
||||
DistSolarisAMD64v1 Distribution = "solaris/amd64/v1"
|
||||
DistSolarisAMD64v2 Distribution = "solaris/amd64/v2"
|
||||
DistSolarisAMD64v3 Distribution = "solaris/amd64/v3"
|
||||
DistSolarisAMD64v4 Distribution = "solaris/amd64/v4"
|
||||
)
|
||||
|
||||
const (
|
||||
DistWindows386 Distribution = "windows/386"
|
||||
DistWindows386SSE2 Distribution = "windows/386/sse2"
|
||||
DistWindows386SoftFloat Distribution = "windows/386/softfloat"
|
||||
DistWindowsAMD64 Distribution = "windows/amd64"
|
||||
DistWindowsAMD64v1 Distribution = "windows/amd64/v1"
|
||||
DistWindowsAMD64v2 Distribution = "windows/amd64/v2"
|
||||
DistWindowsAMD64v3 Distribution = "windows/amd64/v3"
|
||||
DistWindowsAMD64v4 Distribution = "windows/amd64/v4"
|
||||
DistWindowsARM Distribution = "windows/arm"
|
||||
DistWindowsARMv6 Distribution = "windows/arm/v6"
|
||||
DistWindowsARMv7 Distribution = "windows/arm/v7"
|
||||
DistWindowsARM64 Distribution = "windows/arm64"
|
||||
)
|
||||
|
||||
func IsWindows(d Distribution) bool {
|
||||
return strings.Split(string(d), "/")[0] == "windows"
|
||||
}
|
||||
|
||||
func OSAndArch(d Distribution) (string, string) {
|
||||
p := strings.Split(string(d), "/")
|
||||
if len(p) < 2 {
|
||||
return string(d), ""
|
||||
}
|
||||
return p[0], p[1]
|
||||
}
|
||||
|
||||
func FullArch(d Distribution) string {
|
||||
p := strings.Split(string(d), "/")
|
||||
return strings.Join(p[1:], "/")
|
||||
}
|
||||
|
||||
func ArchVersion(d Distribution) string {
|
||||
p := strings.Split(string(d), "/")
|
||||
if len(p) < 3 {
|
||||
return ""
|
||||
}
|
||||
|
||||
// ARM specifically must be specified without a 'v' prefix.
|
||||
// GOAMD64, however, expects a 'v' prefix.
|
||||
// Specifying the ARM version with the 'v' prefix and without is supported in Docker's platform argument, however.
|
||||
if arch := p[1]; arch == "arm" {
|
||||
return strings.TrimPrefix(p[2], "v")
|
||||
}
|
||||
|
||||
return p[2]
|
||||
}
|
||||
|
||||
func PackageArch(d Distribution) string {
|
||||
_, arch := OSAndArch(d)
|
||||
|
||||
if arch == "arm" {
|
||||
return "armhf"
|
||||
}
|
||||
|
||||
return arch
|
||||
}
|
||||
|
||||
// From the distribution, try to assume the docker platform (used in Docker's --platform argument or the (dagger.ContainerOpts).Platform field
|
||||
func Platform(d Distribution) dagger.Platform {
|
||||
p := strings.ReplaceAll(string(d), "/dynamic-musl", "")
|
||||
p = strings.ReplaceAll(p, "/dynamic", "")
|
||||
p = strings.ReplaceAll(p, "arm/v6", "arm/v7")
|
||||
// for now let's just try to use the distro name as the platform and see if that works...
|
||||
return dagger.Platform(p)
|
||||
}
|
||||
|
||||
type DistroBuildOptsFunc func(distro Distribution, experiments []string, tags []string) *GoBuildOpts
|
||||
|
||||
func LDFlagsStatic(info *VCSInfo) []LDFlag {
|
||||
return []LDFlag{
|
||||
{"-w", nil},
|
||||
{"-s", nil},
|
||||
{"-X", info.X()},
|
||||
{"-linkmode=external", nil},
|
||||
{"-extldflags=-static", nil},
|
||||
}
|
||||
}
|
||||
|
||||
func LDFlagsDynamic(info *VCSInfo) []LDFlag {
|
||||
return []LDFlag{
|
||||
{"-X", info.X()},
|
||||
}
|
||||
}
|
||||
|
||||
func ZigCC(distro Distribution) string {
|
||||
target, ok := ZigTargets[distro]
|
||||
if !ok {
|
||||
target = "x86_64-linux-musl" // best guess? should probably retun an error but i don't want to
|
||||
}
|
||||
|
||||
return fmt.Sprintf("zig cc -target %s", target)
|
||||
}
|
||||
|
||||
func ZigCXX(distro Distribution) string {
|
||||
target, ok := ZigTargets[distro]
|
||||
if !ok {
|
||||
target = "x86_64-linux-musl" // best guess? should probably retun an error but i don't want to
|
||||
}
|
||||
|
||||
return fmt.Sprintf("zig c++ -target %s", target)
|
||||
}
|
||||
|
||||
var DefaultBuildOpts = func(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
os, arch := OSAndArch(distro)
|
||||
|
||||
return &GoBuildOpts{
|
||||
CC: ZigCC(distro),
|
||||
CXX: ZigCXX(distro),
|
||||
ExperimentalFlags: experiments,
|
||||
OS: os,
|
||||
Arch: arch,
|
||||
CGOEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildOptsStaticARM builds Grafana statically for the armv6/v7 architectures (not aarch64/arm64)
|
||||
func BuildOptsStaticARM(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
var (
|
||||
os, _ = OSAndArch(distro)
|
||||
arm = ArchVersion(distro)
|
||||
)
|
||||
|
||||
return &GoBuildOpts{
|
||||
CC: "/toolchain/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-gcc",
|
||||
CXX: "/toolchain/arm-linux-musleabihf-cross/bin/arm-linux-musleabihf-cpp",
|
||||
ExperimentalFlags: experiments,
|
||||
OS: os,
|
||||
Arch: "arm",
|
||||
GoARM: GoARM(arm),
|
||||
CGOEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
// BuildOptsStaticS390X builds Grafana statically for the s390x arch
|
||||
func BuildOptsStaticS390X(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
var (
|
||||
os, _ = OSAndArch(distro)
|
||||
)
|
||||
|
||||
return &GoBuildOpts{
|
||||
CC: "/toolchain/s390x-linux-musl-cross/bin/s390x-linux-musl-gcc",
|
||||
CXX: "/toolchain/s390x-linux-musl-cross/bin/s390x-linux-musl-cpp",
|
||||
ExperimentalFlags: experiments,
|
||||
OS: os,
|
||||
Arch: "s390x",
|
||||
CGOEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
func StdZigBuildOpts(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
var (
|
||||
os, arch = OSAndArch(distro)
|
||||
)
|
||||
|
||||
return &GoBuildOpts{
|
||||
CC: ZigCC(distro),
|
||||
CXX: ZigCXX(distro),
|
||||
ExperimentalFlags: experiments,
|
||||
OS: os,
|
||||
Arch: arch,
|
||||
CGOEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
func BuildOptsWithoutZig(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
var (
|
||||
os, arch = OSAndArch(distro)
|
||||
)
|
||||
|
||||
return &GoBuildOpts{
|
||||
ExperimentalFlags: experiments,
|
||||
OS: os,
|
||||
Arch: arch,
|
||||
CGOEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
func ViceroyBuildOpts(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
var (
|
||||
os, arch = OSAndArch(distro)
|
||||
)
|
||||
|
||||
return &GoBuildOpts{
|
||||
CC: "viceroycc",
|
||||
ExperimentalFlags: experiments,
|
||||
OS: os,
|
||||
Arch: arch,
|
||||
CGOEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
var ZigTargets = map[Distribution]string{
|
||||
DistLinuxAMD64: "x86_64-linux-musl",
|
||||
DistLinuxAMD64Dynamic: "x86_64-linux-gnu",
|
||||
DistLinuxAMD64DynamicMusl: "x86_64-linux-musl",
|
||||
DistLinuxARM64: "aarch64-linux-musl",
|
||||
DistLinuxARM64Dynamic: "aarch64-linux-musl",
|
||||
DistLinuxARM: "arm-linux-musleabihf",
|
||||
DistLinuxARMv6: "arm-linux-musleabihf",
|
||||
DistLinuxARMv7: "arm-linux-musleabihf",
|
||||
DistLinuxRISCV64: "riscv64-linux-musl",
|
||||
DistWindowsAMD64: "x86_64-windows-gnu",
|
||||
DistWindowsARM64: "aarch64-windows-gnu",
|
||||
}
|
||||
|
||||
var DistributionGoOpts = map[Distribution]DistroBuildOptsFunc{
|
||||
// The Linux distros should all have an equivalent zig target in the ZigTargets map
|
||||
DistLinuxARM: BuildOptsStaticARM,
|
||||
DistLinuxARMv6: BuildOptsStaticARM,
|
||||
DistLinuxARMv7: BuildOptsStaticARM,
|
||||
DistLinuxS390X: BuildOptsStaticS390X,
|
||||
DistLinuxARM64: StdZigBuildOpts,
|
||||
DistLinuxARM64Dynamic: StdZigBuildOpts,
|
||||
DistLinuxAMD64: StdZigBuildOpts,
|
||||
DistLinuxAMD64Dynamic: StdZigBuildOpts,
|
||||
DistPlan9AMD64: StdZigBuildOpts,
|
||||
DistLinuxRISCV64: StdZigBuildOpts,
|
||||
|
||||
// Non-Linux distros can have whatever they want in CC and CXX; it'll get overridden
|
||||
// but it's probably not best to rely on that.
|
||||
DistWindowsAMD64: ViceroyBuildOpts,
|
||||
DistWindowsARM64: StdZigBuildOpts,
|
||||
DistDarwinAMD64: ViceroyBuildOpts,
|
||||
DistDarwinARM64: ViceroyBuildOpts,
|
||||
|
||||
DistLinuxAMD64DynamicMusl: BuildOptsWithoutZig,
|
||||
}
|
||||
|
||||
func DistroOptsLogger(log *slog.Logger, fn DistroBuildOptsFunc) func(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
return func(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
|
||||
opts := fn(distro, experiments, tags)
|
||||
log.Debug("Building with options", "distribution", distro, "experiments", experiments, "tags", tags, "os", opts.OS, "arch", opts.Arch, "arm", opts.GoARM, "CGO", opts.CGOEnabled, "386", opts.Go386, "CC", opts.CC, "CXX", opts.CXX)
|
||||
return opts
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package backend holds the functions that create containers, files, and directories for building Grafana's backend binaries.
|
||||
package backend
|
||||
@@ -0,0 +1,140 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/build/daggerbuild/containers"
|
||||
)
|
||||
|
||||
type (
|
||||
BuildMode string
|
||||
GoARM string
|
||||
GoAMD64 string
|
||||
Go386 string
|
||||
LibC int
|
||||
)
|
||||
|
||||
const (
|
||||
BuildModeDefault BuildMode = "default"
|
||||
BuildModeExe BuildMode = "exe"
|
||||
)
|
||||
|
||||
const (
|
||||
GOARM5 GoARM = "5"
|
||||
GOARM6 GoARM = "6"
|
||||
GOARM7 GoARM = "7"
|
||||
)
|
||||
|
||||
const (
|
||||
Go386SSE2 Go386 = "sse2"
|
||||
Go386SoftFloat Go386 = "softfloat"
|
||||
)
|
||||
|
||||
const (
|
||||
Musl LibC = iota
|
||||
GLibC
|
||||
)
|
||||
|
||||
type GoBuildOpts struct {
|
||||
// OS is value supplied to the GOOS environment variable
|
||||
OS string
|
||||
|
||||
// Arch is value supplied to the GOARCH environment variable
|
||||
Arch string
|
||||
|
||||
// ExperimentalFlags are Go build-time feature flags in the "GOEXPERIMENT" environment variable that enable experimental features.
|
||||
ExperimentalFlags []string
|
||||
|
||||
// CGOEnabled defines whether or not the CGO_ENABLED flag is set.
|
||||
CGOEnabled bool
|
||||
|
||||
// GOARM: For GOARCH=arm, the ARM architecture for which to compile.
|
||||
// Valid values are 5, 6, 7.
|
||||
GoARM GoARM
|
||||
|
||||
// GO386: For GOARCH=386, how to implement floating point instructions.
|
||||
// Valid values are sse2 (default), softfloat.
|
||||
Go386 Go386
|
||||
|
||||
// CC is the command to use to compile C code when CGO is enabled. (Sets the "CC" environment variable)
|
||||
CC string
|
||||
|
||||
// CXX is the command to use to compile C++ code when CGO is enabled. (Sets the "CXX" environment variable)
|
||||
CXX string
|
||||
}
|
||||
|
||||
// GoBuildEnv returns the environment variables that must be set for a 'go build' command given the provided 'GoBuildOpts'.
|
||||
func GoBuildEnv(opts *GoBuildOpts) []containers.Env {
|
||||
var (
|
||||
os = opts.OS
|
||||
arch = opts.Arch
|
||||
)
|
||||
|
||||
env := []containers.Env{containers.EnvVar("GOOS", os), containers.EnvVar("GOARCH", arch)}
|
||||
|
||||
if arch == "arm" {
|
||||
env = append(env, containers.EnvVar("GOARM", string(opts.GoARM)))
|
||||
}
|
||||
|
||||
if opts.CGOEnabled {
|
||||
env = append(env, containers.EnvVar("GOARM", string(opts.GoARM)))
|
||||
env = append(env, containers.EnvVar("CGO_ENABLED", "1"))
|
||||
|
||||
// https://github.com/mattn/go-sqlite3/issues/1164#issuecomment-1635253695
|
||||
env = append(env, containers.EnvVar("CGO_CFLAGS", "-D_LARGEFILE64_SOURCE"))
|
||||
} else {
|
||||
env = append(env, containers.EnvVar("CGO_ENABLED", "0"))
|
||||
}
|
||||
|
||||
if opts.ExperimentalFlags != nil {
|
||||
env = append(env, containers.EnvVar("GOEXPERIMENT", strings.Join(opts.ExperimentalFlags, ",")))
|
||||
}
|
||||
|
||||
if opts.CC != "" {
|
||||
env = append(env, containers.EnvVar("CC", opts.CC))
|
||||
}
|
||||
|
||||
if opts.CXX != "" {
|
||||
env = append(env, containers.EnvVar("CXX", opts.CXX))
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
|
||||
// ViceroyEnv returns the environment variables that must be set for a 'go build' command given the provided 'GoBuildOpts'.
|
||||
func ViceroyEnv(opts *GoBuildOpts) []containers.Env {
|
||||
var (
|
||||
os = opts.OS
|
||||
arch = opts.Arch
|
||||
)
|
||||
|
||||
env := []containers.Env{
|
||||
containers.EnvVar("VICEROYOS", os),
|
||||
containers.EnvVar("GOOS", os),
|
||||
containers.EnvVar("VICEROYARCH", arch),
|
||||
containers.EnvVar("GOARCH", arch),
|
||||
}
|
||||
|
||||
if arch == "arm" {
|
||||
env = append(env, containers.EnvVar("VICEROYARM", string(opts.GoARM)))
|
||||
}
|
||||
|
||||
if opts.CGOEnabled {
|
||||
env = append(env, containers.EnvVar("CGO_ENABLED", "1"))
|
||||
|
||||
// https://github.com/mattn/go-sqlite3/issues/1164#issuecomment-1635253695
|
||||
env = append(env, containers.EnvVar("CGO_CFLAGS", "-D_LARGEFILE64_SOURCE"))
|
||||
} else {
|
||||
env = append(env, containers.EnvVar("CGO_ENABLED", "0"))
|
||||
}
|
||||
|
||||
if opts.ExperimentalFlags != nil {
|
||||
env = append(env, containers.EnvVar("GOEXPERIMENT", strings.Join(opts.ExperimentalFlags, ",")))
|
||||
}
|
||||
|
||||
if opts.CC != "" {
|
||||
env = append(env, containers.EnvVar("CC", "viceroycc"))
|
||||
}
|
||||
|
||||
return env
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
type VCSInfo struct {
|
||||
Version string
|
||||
Commit *dagger.File
|
||||
EnterpriseCommit *dagger.File
|
||||
Branch *dagger.File
|
||||
}
|
||||
|
||||
func WithVCSInfo(c *dagger.Container, info *VCSInfo, enterprise bool) *dagger.Container {
|
||||
c = c.
|
||||
WithFile(".buildinfo.commit", info.Commit).
|
||||
WithFile(".buildinfo.branch", info.Branch)
|
||||
|
||||
if enterprise {
|
||||
return c.WithFile(".buildinfo.enterprise-commit", info.EnterpriseCommit)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// VCSInfo gets the VCS data from the directory 'src', writes them to a file on the given container, and returns the files which can be used in other containers.
|
||||
func GetVCSInfo(src *dagger.Directory, version string, enterprise bool) *VCSInfo {
|
||||
info := &VCSInfo{
|
||||
Version: version,
|
||||
Commit: src.File(".buildinfo.commit"),
|
||||
Branch: src.File(".buildinfo.branch"),
|
||||
}
|
||||
|
||||
if enterprise {
|
||||
info.EnterpriseCommit = src.File(".buildinfo.enterprise-commit")
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
func (v *VCSInfo) X() []string {
|
||||
flags := []string{
|
||||
fmt.Sprintf("main.version=%s", strings.TrimPrefix(v.Version, "v")),
|
||||
`main.commit=$(cat ./.buildinfo.commit)`,
|
||||
`main.buildBranch=$(cat ./.buildinfo.branch)`,
|
||||
}
|
||||
|
||||
if v.EnterpriseCommit != nil {
|
||||
flags = append(flags, `main.enterpriseCommit=$(cat ./.buildinfo.enterprise-commit)`)
|
||||
}
|
||||
|
||||
return flags
|
||||
}
|
||||
Reference in New Issue
Block a user