Skip to content

Commit 5d320c5

Browse files
committed
internal: add OnLinux constant
Add a constant which indicates whether we are building for Linux. This is so that we can write if !internal.OnLinux { instead of if runtime.GOOS != "linux" { This makes it easy to remove platform dependent code by removing the constant and then fixing the breakage. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 2a70bf2 commit 5d320c5

File tree

7 files changed

+15
-13
lines changed

7 files changed

+15
-13
lines changed

internal/feature.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func NewFeatureTest(name string, fn FeatureTestFn, versions ...string) func() er
9393
break
9494
}
9595

96-
if runtime.GOOS == "linux" && !strings.ContainsRune(version, ':') {
96+
if OnLinux && !strings.ContainsRune(version, ':') {
9797
// Allow version numbers without a GOOS prefix on Linux.
9898
ft.Version = version
9999
break

internal/feature_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestFeatureTestNotSupportedOnOS(t *testing.T) {
8383
qt.Assert(t, qt.IsNotNil(NewFeatureTest("foo", fn)()))
8484
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "froz:1.0.0")(), ErrNotSupportedOnOS))
8585
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, runtime.GOOS+":1.0")(), sentinel))
86-
if runtime.GOOS == "linux" {
86+
if OnLinux {
8787
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "1.0")(), sentinel))
8888
}
8989
}

internal/goos.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package internal
2+
3+
import "runtime"
4+
5+
const (
6+
OnLinux = runtime.GOOS == "linux"
7+
)

internal/kallsyms/kallsyms.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"os"
8-
"runtime"
98
"slices"
109
"strconv"
1110
"strings"
@@ -50,7 +49,7 @@ func Module(name string) (string, error) {
5049
// Any symbols missing in the kernel are ignored. Returns an error if multiple
5150
// symbols with a given name were found.
5251
func AssignModules(symbols map[string]string) error {
53-
if runtime.GOOS != "linux" {
52+
if !internal.OnLinux {
5453
return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS)
5554
}
5655

@@ -169,7 +168,7 @@ func Address(symbol string) (uint64, error) {
169168
// Any symbols missing in the kernel are ignored. Returns an error if multiple
170169
// addresses were found for a symbol.
171170
func AssignAddresses(symbols map[string]uint64) error {
172-
if runtime.GOOS != "linux" {
171+
if !internal.OnLinux {
173172
return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS)
174173
}
175174

internal/kallsyms/kallsyms_test.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package kallsyms
22

33
import (
44
"bytes"
5-
"errors"
65
"os"
7-
"runtime"
86
"testing"
97

108
"github.com/go-quicktest/qt"
119

10+
"github.com/cilium/ebpf/internal"
1211
"github.com/cilium/ebpf/internal/testutils"
1312
)
1413

@@ -177,13 +176,11 @@ func BenchmarkAssignAddresses(b *testing.B) {
177176
func mustOpenProcKallsyms(tb testing.TB) *os.File {
178177
tb.Helper()
179178

180-
if runtime.GOOS != "linux" {
179+
if !internal.OnLinux {
181180
tb.Skip("/proc/kallsyms is a Linux concept")
182181
}
183182

184183
f, err := os.Open("/proc/kallsyms")
185-
if runtime.GOOS != "linux" && errors.Is(err, os.ErrNotExist) {
186-
}
187184
qt.Assert(tb, qt.IsNil(err))
188185
tb.Cleanup(func() { f.Close() })
189186
return f

internal/linux/auxv.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"runtime"
87
_ "unsafe"
98

109
"github.com/cilium/ebpf/internal"
@@ -51,7 +50,7 @@ func (r *auxvRuntimeReader) ReadAuxvPair() (uint64, uint64, error) {
5150
}
5251

5352
func newAuxvRuntimeReader() (auxvPairReader, error) {
54-
if runtime.GOOS != "linux" {
53+
if !internal.OnLinux {
5554
return nil, fmt.Errorf("read auxv from runtime: %w", internal.ErrNotSupportedOnOS)
5655
}
5756

internal/tracefs/kprobe.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func sanitizeTracefsPath(path ...string) (string, error) {
113113
// but may be also be available at /sys/kernel/debug/tracing if debugfs is mounted.
114114
// The available tracefs paths will depends on distribution choices.
115115
var getTracefsPath = sync.OnceValues(func() (string, error) {
116-
if runtime.GOOS != "linux" {
116+
if !internal.OnLinux {
117117
return "", fmt.Errorf("tracefs: %w", internal.ErrNotSupportedOnOS)
118118
}
119119

0 commit comments

Comments
 (0)