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

huangapple go评论84阅读模式
英文:

How to ignore gotvm when building ARM64 binary

问题

我想要构建我的 Go 仓库,当 GOARCH=arm64 时忽略 myrepo/gotvm/*,在其他情况下构建整个仓库。

项目的文件夹结构如下:

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

在 AMD64 架构上构建时一切正常:
GOOS=linux GOARCH=amd64 go build -o amdbuild main.go

在 ARM64 架构上构建时,会出现以下错误,并且不会创建二进制文件:

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

当我在每个文件的顶部添加以下行时(array.godevice.gogotvm.go):

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

问题仍然存在。

我尝试过的其他步骤:

  • go clean -modcache
  • 在所有属于 package gotvm 的文件之前添加以下构建标志(分别):
// go:build (darwin && cgo) || (linux && cgo)
//go:build amd64
// +build amd64`

这里有一个类似的问题:(Mock packages),但是它忽略了整个文件夹。我想要在 ARCH 不是 arm64 的情况下忽略该文件夹。

TL;DR; 有没有办法将我的仓库交叉编译为 ARM64 和 AMD64?我想要在 ARM64 构建中忽略 TVM。

规格:

  • Go 版本:go1.20.4 linux/amd64
  • 系统:Ubuntu 20.04.5 LTS
英文:

I want to build my Go repo such that it ignores myrepo/gotvm/* when GOARCH=arm64 and builds the full repo 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

When building on AMD64 everything works fine:
GOOS=linux GOARCH=amd64 go build -o amdbuild main.go

Building on ARM64
GOOS=linux GOARCH=arm64 go build -o armbuild main.go
gives the following error before not creating the binary:

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

This is also the case when I add the following lines to the top of each file with package gotvm (array.go, device.go, gotvm.go):

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

Other steps I have tried:

  • go clean -modcache
  • Adding the following build flags (seperately) above all files which are part of the package gotvm
// go:build (darwin && cgo) || (linux && cgo)
//go:build amd64
// +build amd64`

There's a similar question on here: (Mock packages)
, but this ignores the entire folder. I want to ignore the folder conditional on ARCH not being arm64.

TL;DR; Is there any way to cross-compile my repo to both ARM64 and AMD64? I want to ignore TVM for my ARM64 build.

Specs:

  • Go version: go1.20.4 linux/amd64
  • System: Ubuntu 20.04.5 LTS

答案1

得分: 0

将一个只包含包声明的go文件添加到gotvm文件夹中可以解决这个问题。例如,一个doc.go文件:

// 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

但是当你遇到这个问题时,大多数情况下意味着至少有一个文件导入了这个包,并且这个文件没有被排除在GOARCH=arm64之外。也许你应该从这个文件中移除导入(或者也将这个文件排除在GOARCH=arm64之外)。

我将添加一个演示来说明这个问题。以下是这些文件:

├── 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!")
}

对于这个演示,要么从main.go中删除_ "example.com/m/gotvm",要么添加前面提到的doc.go文件都可以解决这个问题。在我看来,最好从main.go中删除_ "example.com/m/gotvm"

英文:

Adding a go file with only the package clause into the gotvm folder could workaround the issue. For example, a 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 issue, most of the time, it means that there is at least one file importing this package and this file is not excluded for GOARCH=arm64. Maybe you should remove the import from this file instead (or exclude this file too for GOARCH=arm64).

I will add a demo to illustrate the issue. 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, either remove _ "example.com/m/gotvm" from main.go or add a doc.go file mentioned before will fix the issue. In my opinion, it's better to remove _ "example.com/m/gotvm" from main.go.

huangapple
  • 本文由 发表于 2023年5月10日 16:03:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定