diff --git a/dev-tools/mage/crossbuild.go b/dev-tools/mage/crossbuild.go index bd171bfd725f..972531c25a8d 100644 --- a/dev-tools/mage/crossbuild.go +++ b/dev-tools/mage/crossbuild.go @@ -120,21 +120,12 @@ func AddPlatforms(expressions ...string) func(params *crossBuildParams) { } } -// UseHostPlatform specifies whether the docker crossbuild container -// should use the host platform at runtime or not. -func UseHostPlatform() func(params *crossBuildParams) { - return func(params *crossBuildParams) { - params.UseHostPlatform = true - } -} - type crossBuildParams struct { - Platforms BuildPlatformList - Target string - Serial bool - InDir string - ImageSelector ImageSelectorFunc - UseHostPlatform bool + Platforms BuildPlatformList + Target string + Serial bool + InDir string + ImageSelector ImageSelectorFunc } // CrossBuild executes a given build target once for each target platform. @@ -190,7 +181,7 @@ func CrossBuild(options ...CrossBuildOption) error { if !buildPlatform.Flags.CanCrossBuild() { return fmt.Errorf("unsupported cross build platform %v", buildPlatform.Name) } - builder := GolangCrossBuilder{buildPlatform.Name, params.Target, params.InDir, params.ImageSelector, params.UseHostPlatform} + builder := GolangCrossBuilder{buildPlatform.Name, params.Target, params.InDir, params.ImageSelector} if params.Serial { if err := builder.Build(); err != nil { return fmt.Errorf("failed cross-building target=%s for platform=%s: %w", @@ -264,11 +255,10 @@ func CrossBuildImage(platform string) (string, error) { // GolangCrossBuilder executes the specified mage target inside of the // associated golang-crossbuild container image for the platform. type GolangCrossBuilder struct { - Platform string - Target string - InDir string - ImageSelector ImageSelectorFunc - UseHostPlatform bool + Platform string + Target string + InDir string + ImageSelector ImageSelectorFunc } // Build executes the build inside of Docker. @@ -345,19 +335,6 @@ func (b GolangCrossBuilder) Build() error { "-w", workDir, ) - // Ensure the proper platform is passed - // This fixes an issue where during arm64 linux build for the currently used docker image - // docker.elastic.co/beats-dev/golang-crossbuild:1.22.5-arm the image for amd64 arch is pulled - // and causes problems when using native arch tools on the binaries that are built for arm64 arch. - // Unless the flag UseHostPlatform is set, in which case the host platform is used for container runtime. - if strings.HasPrefix(b.Platform, "linux/") { - platform := b.Platform - if b.UseHostPlatform { - platform = dockerHostPlatform() - } - args = append(args, "--platform", platform) - } - args = append(args, image, @@ -370,16 +347,6 @@ func (b GolangCrossBuilder) Build() error { return dockerRun(args...) } -func dockerHostPlatform() string { - os := runtime.GOOS - // darwin docker runs inside a linux vm - if os == "darwin" { - os = "linux" - } - - return os + "/" + runtime.GOARCH -} - // DockerChown chowns files generated during build. EXEC_UID and EXEC_GID must // be set in the containers environment otherwise this is a noop. func DockerChown(path string) { diff --git a/x-pack/osquerybeat/magefile.go b/x-pack/osquerybeat/magefile.go index d8a9dee310bb..92967a6a8f4c 100644 --- a/x-pack/osquerybeat/magefile.go +++ b/x-pack/osquerybeat/magefile.go @@ -96,7 +96,7 @@ func Clean() error { func execCommand(ctx context.Context, name string, args ...string) error { ps := strings.Join(append([]string{name}, args...), " ") - fmt.Println(ps) + fmt.Println("Executing strip command: ", ps) output, err := command.Execute(ctx, name, args...) if err != nil { fmt.Println(ps, ", failed: ", err) @@ -130,13 +130,13 @@ func stripLinuxOsqueryd() error { osArchs := osquerybeat.OSArchs(devtools.Platforms) - strip := func(oquerydPath string) error { + strip := func(oquerydPath string, target distro.OSArch) error { ok, err := fileutil.FileExists(oquerydPath) if err != nil { return err } if ok { - if err := execCommand(ctx, "strip", oquerydPath); err != nil { + if err := execCommand(ctx, stripCommand(target.OS, target.Arch), oquerydPath); err != nil { return err } } @@ -160,13 +160,13 @@ func stripLinuxOsqueryd() error { // Checking and stripping osqueryd binary and both paths osquerybeat/build and agentbeat/build // because at the moment it's unclear if this step was initiated from osquerybeat or agentbeat build osquerybeatPath := filepath.Clean(filepath.Join(cwd, "../..", querydRelativePath)) - err = strip(osquerybeatPath) + err = strip(osquerybeatPath, osarch) if err != nil { return err } agentbeatPath := filepath.Clean(filepath.Join(cwd, "../../../agentbeat", querydRelativePath)) - err = strip(agentbeatPath) + err = strip(agentbeatPath, osarch) if err != nil { return err } @@ -260,3 +260,18 @@ func Fields() { mg.Deps(osquerybeat.Update.Fields) } // Config is an alias for update:config. This is a workaround for // https://github.com/magefile/mage/issues/217. func Config() { mg.Deps(osquerybeat.Update.Config) } + +func stripCommand(targetOS, targetArch string) string { + if targetOS != "linux" { + return "strip" // fallback + } + + switch targetArch { + case "arm64": + return "aarch64-linux-gnu-strip" + case "amd64": + return "x86_64-linux-gnu-strip" + } + + return "strip" +}