Skip to content

Commit 9f1da11

Browse files
committed
chore: use the compile-time GOAMD64 flag in the updater
1 parent 63ad95e commit 9f1da11

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

component/updater/cpu_amd64.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package updater
2+
3+
// getGOAMD64level is implemented in cpu_amd64.s. Returns number in [1,4].
4+
func getGOAMD64level() int32

component/updater/cpu_amd64.s

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
// func getGOAMD64level() int32
8+
TEXT ·getGOAMD64level(SB),NOSPLIT,$0-4
9+
#ifdef GOAMD64_v4
10+
MOVL $4, ret+0(FP)
11+
#else
12+
#ifdef GOAMD64_v3
13+
MOVL $3, ret+0(FP)
14+
#else
15+
#ifdef GOAMD64_v2
16+
MOVL $2, ret+0(FP)
17+
#else
18+
MOVL $1, ret+0(FP)
19+
#endif
20+
#endif
21+
#endif
22+
RET

component/updater/cpu_others.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !amd64
2+
3+
package updater
4+
5+
// getGOAMD64level is always return 0 when not in amd64 platfrom.
6+
func getGOAMD64level() int32 {
7+
return 0
8+
}

component/updater/cpu_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package updater
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestGOAMD64level(t *testing.T) {
12+
level := getGOAMD64level()
13+
fmt.Printf("GOAMD64=%d\n", level)
14+
if runtime.GOARCH == "amd64" {
15+
assert.True(t, level > 0)
16+
assert.True(t, level <= 4)
17+
} else {
18+
assert.Equal(t, level, int32(0))
19+
}
20+
}

component/updater/update_core.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
mihomoHttp "github.com/metacubex/mihomo/component/http"
1919
C "github.com/metacubex/mihomo/constant"
2020
"github.com/metacubex/mihomo/log"
21-
22-
"github.com/klauspost/cpuid/v2"
2321
)
2422

2523
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/updater/updater.go
@@ -48,7 +46,7 @@ var (
4846
)
4947

5048
func init() {
51-
if runtime.GOARCH == "amd64" && cpuid.CPU.X64Level() < 3 {
49+
if runtime.GOARCH == "amd64" && getGOAMD64level() < 3 {
5250
amd64Compatible = "-compatible"
5351
}
5452
if !strings.HasPrefix(C.Version, "alpha") {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/gofrs/uuid/v5 v5.3.2
1515
github.com/insomniacslk/dhcp v0.0.0-20250109001534-8abf58130905
1616
github.com/klauspost/compress v1.17.9 // lastest version compatible with golang1.20
17-
github.com/klauspost/cpuid/v2 v2.2.9 // lastest version compatible with golang1.20
1817
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40
1918
github.com/mdlayher/netlink v1.7.2
2019
github.com/metacubex/amneziawg-go v0.0.0-20240922133038-fdf3a4d5a4ab
@@ -86,6 +85,7 @@ require (
8685
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
8786
github.com/hashicorp/yamux v0.1.2 // indirect
8887
github.com/josharian/native v1.1.0 // indirect
88+
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
8989
github.com/kr/text v0.2.0 // indirect
9090
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
9191
github.com/mailru/easyjson v0.7.7 // indirect

0 commit comments

Comments
 (0)