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

How to ignore gotvm when building ARM64 binaries

PHPz
Release: 2024-02-05 21:27:07
forward
549 people have browsed it

如何在构建 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
Copy after login

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
Copy after login

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`
Copy after login

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`
Copy after login

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
Copy after login

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
Copy after login

go.mod

module example.com/m

go 1.20
Copy after login

gotvm/gotvm.go

//go:build amd64

package gotvm

import "fmt"

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

main.go

package main

import (
    "fmt"

    _ "example.com/m/gotvm"
)

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

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template