Skip to content

Commit

Permalink
fix(action/linux): fix cross-compilation detection
Browse files Browse the repository at this point in the history
- the code to detect if cross-compilation is needed did not work on
  arm64
- additionally, since everyone is naming architectures differently, I
  made 2 new functions to normalize (translate) into some common naming
  scheme

Signed-off-by: AtomicFS <vojtech.vesely@9elements.com>
  • Loading branch information
AtomicFS committed Nov 12, 2024
1 parent e95fabc commit 5e9f8f3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
39 changes: 21 additions & 18 deletions action/recipes/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ type LinuxOpts struct {

// Specifies target architecture, such as 'x86' or 'arm64'.
// Supported options:
// - 'x86'
// - 'x86_64'
// - 'i386'
// - 'amd64'
// - 'arm'
// - 'arm64'
Arch string `json:"arch"`
Expand Down Expand Up @@ -134,26 +134,29 @@ func (opts LinuxOpts) buildFirmware(ctx context.Context, client *dagger.Client,
// Setup environment variables in the container
// Handle cross-compilation: Map architecture to cross-compiler
crossCompile := map[string]string{
"x86": "i686-linux-gnu-",
"x86_64": "",
"arm": "arm-linux-gnueabi-",
"arm64": "aarch64-linux-gnu-",
"i386": "i686-linux-gnu-",
"amd64": "x86-64-linux-gnu-",
"arm": "arm-linux-gnueabi-",
"arm64": "aarch64-linux-gnu-",
}
envVars := map[string]string{
"ARCH": opts.Arch,
"ARCH": NormalizeArchitectureForLinux(opts.Arch),
}

val, ok := crossCompile[opts.Arch]
if !ok {
err = errUnknownArchCrossCompile
slog.Error(
"Selected unknown cross compilation target architecture",
slog.Any("error", err),
)
return nil, err
}
if val != "" {
envVars["CROSS_COMPILE"] = val
// Check if cross-compilation is needed
if NormalizeArchitecture(runtime.GOARCH) != NormalizeArchitecture(opts.Arch) {
val, ok := crossCompile[opts.Arch]
if !ok {
err = errUnknownArchCrossCompile
slog.Error(
"Selected unknown cross compilation target architecture",
slog.Any("error", err),
)
return nil, err
}
if val != "" {
envVars["CROSS_COMPILE"] = val
}
}

for key, value := range envVars {
Expand Down
4 changes: 2 additions & 2 deletions action/recipes/linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func TestLinux(t *testing.T) {
{
name: "normal build for x86 64bit",
linuxVersion: "6.1.45",
arch: "x86_64",
arch: "amd64",
wantErr: nil,
},
{
name: "normal build for x86 32bit",
linuxVersion: "6.1.45",
arch: "x86",
arch: "i386",
wantErr: nil,
},
{
Expand Down
42 changes: 42 additions & 0 deletions action/recipes/recipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,45 @@ func Execute(ctx context.Context, target string, config *Config, interactive boo
}
return ErrTargetMissing
}

// NormalizeArchitecture will translate various architecture strings into expected format
func NormalizeArchitecture(arch string) string {
archMap := map[string]string{
// x86 32-bit
"IA-32": "i386", // Intel
"IA32": "i386", // Intel
"i686": "i386", // common on Linux
"386": "i386", // GOARCH
"x86": "i386", // common on Windows
"x86-32": "i386", // rare
"x86_32": "i386", // rare
// x86 64-bit
"AMD64": "amd64",
"x64": "amd64", // common on Windows
"x86-64": "amd64",
"x86_64": "amd64",
}
result, ok := archMap[arch]
if result != "" && ok {
return result
}
// fallback
return arch
}

// NormalizeArchitectureForLinux will translate various architecture strings into format expected by Linux
func NormalizeArchitectureForLinux(arch string) string {
normalArch := NormalizeArchitecture(arch)
archMap := map[string]string{
// x86 32-bit
"i386": "x86",
// x86 64-bit
"amd64": "x86_64",
}
result, ok := archMap[normalArch]
if result != "" && ok {
return result
}
// fallback
return arch
}

0 comments on commit 5e9f8f3

Please sign in to comment.