Skip to content

Improve architecture detection in buildinputs tool for qemu-user s390x builds #1373

@coderabbitai

Description

@coderabbitai

Problem Description

The recent change in scripts/buildinputs/dockerfile.go (PR #1320) replaced hardcoded "amd64" with runtime.GOARCH to support multi-architecture builds. While this works for native arm64 builds, it fails for qemu-user s390x builds because runtime.GOARCH returns the host architecture rather than the target architecture.

Current Implementation

// Current code in dockerfile.go
platform: "linux/" + runtime.GOARCH,
BuildPlatforms: []ocispecs.Platform{{OS: "linux", Architecture: runtime.GOARCH}},

Impact Analysis

  • Native builds: Works correctly (amd64 → amd64, arm64 → arm64)
  • QEMU user emulation: Fails for s390x builds (host amd64/arm64 → incorrectly detects as amd64/arm64 instead of s390x)
  • Cross-compilation scenarios: May not detect target architecture correctly

Root Cause

When using qemu-user emulation for s390x builds:

  1. The build runs on a host machine (typically amd64 or arm64)
  2. runtime.GOARCH returns the host architecture
  3. The tool incorrectly configures the platform as "linux/amd64" or "linux/arm64" instead of "linux/s390x"
  4. This leads to incorrect dependency resolution and build configuration

Solution Options

Option 1: Environment Variable Detection

Detect target architecture from environment variables commonly used in cross-compilation:

  • TARGETARCH (Docker BuildKit)
  • GOARCH (Go cross-compilation)
  • BUILD_ARCH (custom CI variables)

Option 2: Configuration Parameter

Add a command-line parameter or configuration option to explicitly specify the target architecture:

--target-arch s390x

Option 3: Platform Detection Logic

Implement intelligent platform detection that considers:

  • BuildKit platform specifications
  • Docker build context
  • QEMU emulation indicators

Option 4: Hybrid Approach

Combine multiple detection methods with fallback logic:

  1. Check for explicit target architecture parameters
  2. Detect from environment variables
  3. Fall back to runtime.GOARCH for native builds

Acceptance Criteria

  • s390x builds using qemu-user emulation correctly detect target architecture as "linux/s390x"
  • Native amd64 and arm64 builds continue to work correctly
  • Cross-compilation scenarios are properly handled
  • Solution is backwards compatible with existing workflows
  • Architecture detection is configurable when needed
  • Clear error messages when architecture cannot be determined

Implementation Guidance

  1. Phase 1: Add environment variable detection for common cross-compilation variables
  2. Phase 2: Implement configuration parameter support
  3. Phase 3: Add intelligent platform detection logic
  4. Phase 4: Comprehensive testing across all supported architectures

Related Files

  • scripts/buildinputs/dockerfile.go - Main implementation
  • CI configuration files that use buildinputs tool
  • Documentation for cross-architecture builds

References

Context

This issue was identified during the review of PR #1320 where multi-architecture CUDA support was added. The current solution works for native arm64 builds but needs enhancement for qemu-user s390x builds.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

📋 Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions