[release-11.6.7] Fix windows CGO issue (#113205)

* [release-12.1.4] pkg/build: Add nocgo option (#112893)

* pkg/build: Add nocgo option (#112834)

Add nocgo option

(cherry picked from commit 2a0f149a63)

* CI: release windows no cgo (#112870)

* fix nocgo option

* set nocgo for windows and darwin

(cherry picked from commit 6b2b9bd7c2)
(cherry picked from commit 9aaa364229)

* [release-12.1.4] CI: Windows builds with CGO cross-compiler toolchain (#112935)

CI: Windows builds with CGO cross-compiler toolchain (#112922)

* CI: Windows builds with CGO cross-compiler toolchain

* fix comments

(cherry picked from commit e7a49fc472)
(cherry picked from commit 444e7f42a8)
This commit is contained in:
Kevin Minehart
2025-10-30 09:12:05 +00:00
committed by GitHub
parent 64a8007e4d
commit 8e559f58ec
7 changed files with 121 additions and 29 deletions
+10 -1
View File
@@ -132,6 +132,7 @@ type NewBackendOpts struct {
Tags []string
Static bool
WireTag string
CGOEnabled bool
GoBuildCache *dagger.CacheVolume
GoModCache *dagger.CacheVolume
}
@@ -198,6 +199,12 @@ func NewBackendFromString(ctx context.Context, log *slog.Logger, artifact string
goCacheProg = val
}
cgoDisabled, err := options.Bool(flags.CGODisabled)
if err != nil {
return nil, err
}
cgoEnabled := !cgoDisabled
bopts := &backend.BuildOpts{
Version: p.Version,
Enterprise: p.Enterprise,
@@ -206,6 +213,7 @@ func NewBackendFromString(ctx context.Context, log *slog.Logger, artifact string
Static: static,
WireTag: wireTag,
Tags: tags,
CGOEnabled: cgoEnabled,
}
return pipeline.ArtifactWithLogging(ctx, log, &pipeline.Artifact{
@@ -233,9 +241,10 @@ func NewBackend(ctx context.Context, log *slog.Logger, artifact string, opts *Ne
Tags: opts.Tags,
Static: opts.Static,
WireTag: opts.WireTag,
CGOEnabled: opts.CGOEnabled,
}
log.Info("Initializing backend artifact with options", "static", opts.Static, "version", opts.Version, "name", opts.Name, "distro", opts.Distribution)
log.Info("Initializing backend artifact with options", "static", opts.Static, "version", opts.Version, "name", opts.Name, "distro", opts.Distribution, "cgo enabled", opts.CGOEnabled)
return pipeline.ArtifactWithLogging(ctx, log, &pipeline.Artifact{
ArtifactString: artifact,
Type: pipeline.ArtifactTypeDirectory,
@@ -121,7 +121,12 @@ func NewTarballFromString(ctx context.Context, log *slog.Logger, artifact string
if err != nil {
return nil, err
}
return NewTarball(ctx, log, artifact, p.Distribution, p.Enterprise, p.Name, p.Version, p.BuildID, src, yarnCache, goModCache, goBuildCache, static, wireTag, tags, goVersion, viceroyVersion, experiments)
cgoDisabled, err := options.Bool(flags.CGODisabled)
if err != nil {
return nil, err
}
cgoEnabled := !cgoDisabled
return NewTarball(ctx, log, artifact, p.Distribution, p.Enterprise, p.Name, p.Version, p.BuildID, src, yarnCache, goModCache, goBuildCache, static, wireTag, tags, goVersion, viceroyVersion, experiments, cgoEnabled)
}
// NewTarball returns a properly initialized Tarball artifact.
@@ -145,6 +150,7 @@ func NewTarball(
goVersion string,
viceroyVersion string,
experiments []string,
cgoEnabled bool,
) (*pipeline.Artifact, error) {
backendArtifact, err := NewBackend(ctx, log, artifact, &NewBackendOpts{
Name: name,
@@ -160,6 +166,7 @@ func NewTarball(
Enterprise: enterprise,
GoBuildCache: goBuildCache,
GoModCache: goModCache,
CGOEnabled: cgoEnabled,
})
if err != nil {
return nil, err
+1 -1
View File
@@ -62,7 +62,7 @@ func Build(
ldflags := LDFlagsDynamic(vcsinfo)
if opts.Static {
if opts.Static && opts.CGOEnabled {
ldflags = LDFlagsStatic(vcsinfo)
}
+37 -16
View File
@@ -19,9 +19,23 @@ type BuildOpts struct {
GoCacheProg string
Static bool
Enterprise bool
CGOEnabled bool
}
func distroOptsFunc(log *slog.Logger, distro Distribution) (DistroBuildOptsFunc, error) {
func distroOptsFunc(log *slog.Logger, distro Distribution, opts *BuildOpts) (DistroBuildOptsFunc, error) {
if !opts.CGOEnabled {
return func(distro Distribution, experiments, tags []string) *GoBuildOpts {
os, arch := OSAndArch(distro)
archv := ArchVersion(distro)
return &GoBuildOpts{
OS: os,
Arch: arch,
GoARM: GoARM(archv),
CGOEnabled: false,
}
}, nil
}
if val, ok := DistributionGoOpts[distro]; ok {
return DistroOptsLogger(log, val), nil
}
@@ -29,7 +43,7 @@ func distroOptsFunc(log *slog.Logger, distro Distribution) (DistroBuildOptsFunc,
}
func WithGoEnv(log *slog.Logger, container *dagger.Container, distro Distribution, opts *BuildOpts) (*dagger.Container, error) {
fn, err := distroOptsFunc(log, distro)
fn, err := distroOptsFunc(log, distro, opts)
if err != nil {
return nil, err
}
@@ -39,7 +53,7 @@ func WithGoEnv(log *slog.Logger, container *dagger.Container, distro Distributio
}
func WithViceroyEnv(log *slog.Logger, container *dagger.Container, distro Distribution, opts *BuildOpts) (*dagger.Container, error) {
fn, err := distroOptsFunc(log, distro)
fn, err := distroOptsFunc(log, distro, opts)
if err != nil {
return nil, err
}
@@ -91,23 +105,30 @@ func GolangContainer(
opts *BuildOpts,
) (*dagger.Container, error) {
os, _ := OSAndArch(distro)
// Only use viceroy for all darwin and only windows/amd64
if os == "darwin" || distro == DistWindowsAMD64 {
// Only use viceroy for all darwin builds
if opts.CGOEnabled && os == "darwin" {
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", "-q", "https://dl.grafana.com/ci/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", "-q", "http://dl.grafana.com/ci/arm-linux-musleabihf-cross.tgz", "-P", "/toolchain"}).
WithExec([]string{"tar", "-xf", "/toolchain/arm-linux-musleabihf-cross.tgz", "-C", "/toolchain"}).
WithExec([]string{"wget", "-q", "https://dl.grafana.com/ci/s390x-linux-musl-cross.tgz", "-P", "/toolchain"}).
WithExec([]string{"tar", "-xf", "/toolchain/s390x-linux-musl-cross.tgz", "-C", "/toolchain"})
container := golang.Container(d, platform, goVersion)
if opts.CGOEnabled {
container = container.
WithExec([]string{"apk", "add", "--update", "wget", "build-base", "alpine-sdk", "musl", "musl-dev", "xz"}).
WithExec([]string{"wget", "-q", "https://dl.grafana.com/ci/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", "-q", "http://dl.grafana.com/ci/arm-linux-musleabihf-cross.tgz", "-P", "/toolchain"}).
WithExec([]string{"tar", "-xf", "/toolchain/arm-linux-musleabihf-cross.tgz", "-C", "/toolchain"}).
WithExec([]string{"wget", "-q", "https://dl.grafana.com/ci/s390x-linux-musl-cross.tgz", "-P", "/toolchain"}).
WithExec([]string{"tar", "-xf", "/toolchain/s390x-linux-musl-cross.tgz", "-C", "/toolchain"}).
WithExec([]string{"wget", "-q", "https://dl.grafana.com/ci/riscv64-linux-musl-cross.tgz", "-P", "/toolchain"}).
WithExec([]string{"tar", "-xf", "/toolchain/riscv64-linux-musl-cross.tgz", "-C", "/toolchain"}).
WithExec([]string{"wget", "-q", "https://dl.grafana.com/ci/x86_64-w64-mingw32-cross.tgz", "-P", "/toolchain"}).
WithExec([]string{"tar", "-xf", "/toolchain/x86_64-w64-mingw32-cross.tgz", "-C", "/toolchain"})
}
return WithGoEnv(log, container, distro, opts)
}
+47 -2
View File
@@ -264,6 +264,38 @@ func BuildOptsStaticS390X(distro Distribution, experiments []string, tags []stri
}
}
// BuildOptsStaticRiscv64 builds Grafana statically for the riscv64 arch
func BuildOptsStaticRiscv64(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
var (
os, _ = OSAndArch(distro)
)
return &GoBuildOpts{
CC: "/toolchain/riscv64-linux-musl-cross/bin/riscv64-linux-musl-gcc",
CXX: "/toolchain/riscv64-linux-musl-cross/bin/riscv64-linux-musl-cpp",
ExperimentalFlags: experiments,
OS: os,
Arch: "riscv64",
CGOEnabled: true,
}
}
// BuildOptsStaticWindows builds Grafana statically for Windows on amd64
func BuildOptsStaticWindows(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
var (
os, _ = OSAndArch(distro)
)
return &GoBuildOpts{
CC: "/toolchain/x86_64-w64-mingw32-cross/bin/x86_64-w64-mingw32-gcc",
CXX: "/toolchain/x86_64-w64-mingw32-cross/bin/x86_64-w64-mingw32-cpp",
ExperimentalFlags: experiments,
OS: os,
Arch: "amd64",
CGOEnabled: true,
}
}
func StdZigBuildOpts(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
var (
os, arch = OSAndArch(distro)
@@ -306,6 +338,19 @@ func ViceroyBuildOpts(distro Distribution, experiments []string, tags []string)
}
}
func BuildOptsNoCGO(distro Distribution, experiments []string, tags []string) *GoBuildOpts {
var (
os, arch = OSAndArch(distro)
)
return &GoBuildOpts{
ExperimentalFlags: experiments,
OS: os,
Arch: arch,
CGOEnabled: false,
}
}
var ZigTargets = map[Distribution]string{
DistLinuxAMD64: "x86_64-linux-musl",
DistLinuxAMD64Dynamic: "x86_64-linux-gnu",
@@ -331,11 +376,11 @@ var DistributionGoOpts = map[Distribution]DistroBuildOptsFunc{
DistLinuxAMD64: StdZigBuildOpts,
DistLinuxAMD64Dynamic: StdZigBuildOpts,
DistPlan9AMD64: StdZigBuildOpts,
DistLinuxRISCV64: StdZigBuildOpts,
DistLinuxRISCV64: BuildOptsStaticRiscv64,
// 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,
DistWindowsAMD64: BuildOptsStaticWindows,
DistWindowsARM64: StdZigBuildOpts,
DistDarwinAMD64: ViceroyBuildOpts,
DistDarwinARM64: ViceroyBuildOpts,
+7 -8
View File
@@ -82,6 +82,13 @@ func GoBuildEnv(opts *GoBuildOpts) []containers.Env {
// https://github.com/mattn/go-sqlite3/issues/1164#issuecomment-1635253695
env = append(env, containers.EnvVar("CGO_CFLAGS", "-D_LARGEFILE64_SOURCE"))
if opts.CC != "" {
env = append(env, containers.EnvVar("CC", opts.CC))
}
if opts.CXX != "" {
env = append(env, containers.EnvVar("CXX", opts.CXX))
}
} else {
env = append(env, containers.EnvVar("CGO_ENABLED", "0"))
}
@@ -90,14 +97,6 @@ func GoBuildEnv(opts *GoBuildOpts) []containers.Env {
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
}
+11
View File
@@ -19,6 +19,7 @@ const (
GoTags pipeline.FlagOption = "go-tag"
GoExperiments pipeline.FlagOption = "go-experiments"
Sign pipeline.FlagOption = "sign"
CGODisabled pipeline.FlagOption = "nocgo"
// Pretty much only used to set the deb or RPM internal package name (and file name) to `{}-nightly` and/or `{}-rpi`
Nightly pipeline.FlagOption = "nightly"
@@ -81,6 +82,13 @@ var SignFlag = pipeline.Flag{
},
}
var CGODisabledFlag = pipeline.Flag{
Name: "nocgo",
Options: map[pipeline.FlagOption]any{
CGODisabled: true,
},
}
var NightlyFlag = pipeline.Flag{
Name: "nightly",
Options: map[pipeline.FlagOption]any{
@@ -95,5 +103,8 @@ func StdPackageFlags() []pipeline.Flag {
return JoinFlags(
distros,
names,
[]pipeline.Flag{
CGODisabledFlag,
},
)
}