Skip to content

Commit

Permalink
resolve strip base on target platform
Browse files Browse the repository at this point in the history
  • Loading branch information
moukoublen committed Aug 1, 2024
1 parent 48ca1ca commit 57afb7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 48 deletions.
53 changes: 10 additions & 43 deletions dev-tools/mage/crossbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,

Expand All @@ -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) {
Expand Down
25 changes: 20 additions & 5 deletions x-pack/osquerybeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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"
}

0 comments on commit 57afb7f

Please sign in to comment.