Home  >  Article  >  Backend Development  >  How to ignore gotvm when building ARM64 binaries

How to ignore gotvm when building ARM64 binaries

PHPz
PHPzforward
2024-02-05 21:27:07454browse

如何在构建 ARM64 二进制文件时忽略gotvm

Question content

I want to build my go repository to ignore myrepo/ when goarch=arm64 gotvm/* and build the complete repository in any other case.

The project folder structure is as follows:

└── myrepo
    ├── go.mod
    ├── main.go
    ├── gotvm
    │   ├── array.go
    │   ├── device.go
    │   └── gotvm.go
    └── otherstuff
        └── otherstuff.go

Everything works fine when building on amd64:

goos=linux goarch=amd64 go build -o amdbuild main.go

Build based on arm64

goos=linux goarch=arm64 go build -o armbuild main.go

The following error occurs before the binary is created:

package command-line-arguments
    imports github.com/myrepo/gotvm: build constraints exclude all go files in /homedir/myrepo/gotvm

When I use package gotvm (array.go, device.go, gotvm.go) add the following lines to The same goes for the top of each file:

//go:build amd64
// +build amd64`

Other steps I tried:

  • go clean -modcache
  • Add the following build flags above all files belonging to package gotvm
  • (individually)
// go:build (darwin && cgo) || (linux && cgo)
//go:build amd64
// +build amd64`

There is a similar question here: (simulation package) , but this ignores the entire folder. I want to ignore folders if arch is not arm64.

tl;dr; Is there a way to cross compile my repository to arm64 and amd64? I want to ignore tvm in arm64 build.

Specification:

  • go version: go1.20.4 linux/amd64
  • System: ubuntu 20.04.5 lts

Correct answer


Adding a go file containing only the package clause to the gotvm folder can solve this problem. For example, doc.go file:

// this file is a workaround for the following issue when built with goarch=arm64:
//
//   build constraints exclude all go files in /homedir/myrepo/gotvm

package gotvm

But when you encounter this problem, most of the time, it means that there is at least one file that imports this package, and goarch=arm64 does not exclude this file. Maybe you should remove the imports from this file (or for goarch=arm64 exclude this file too).

I'll add a demo to illustrate the problem. These are the files:

├── go.mod
├── gotvm
│   └── gotvm.go
└── main.go

go.mod

module example.com/m

go 1.20

gotvm/gotvm.go

//go:build amd64

package gotvm

import "fmt"

func f() {
    fmt.println("do awesome things using amd64")
}

main.go

package main

import (
    "fmt"

    _ "example.com/m/gotvm"
)

func main() {
    fmt.Println("Hello world!")
}

For this demo, remove _ "example.com/m/gotvm" from main.go or add the previously mentioned doc.go file will solve the problem. I think it would be better to remove _ "example.com/m/gotvm" from main.go.

The above is the detailed content of How to ignore gotvm when building ARM64 binaries. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete