Skip to content

Commit

Permalink
internal: add OnLinux constant
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
lmb committed Jan 27, 2025
1 parent 2a70bf2 commit 5d320c5
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion internal/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewFeatureTest(name string, fn FeatureTestFn, versions ...string) func() er
break
}

if runtime.GOOS == "linux" && !strings.ContainsRune(version, ':') {
if OnLinux && !strings.ContainsRune(version, ':') {
// Allow version numbers without a GOOS prefix on Linux.
ft.Version = version
break
Expand Down
2 changes: 1 addition & 1 deletion internal/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestFeatureTestNotSupportedOnOS(t *testing.T) {
qt.Assert(t, qt.IsNotNil(NewFeatureTest("foo", fn)()))
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "froz:1.0.0")(), ErrNotSupportedOnOS))
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, runtime.GOOS+":1.0")(), sentinel))
if runtime.GOOS == "linux" {
if OnLinux {
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "1.0")(), sentinel))
}
}
7 changes: 7 additions & 0 deletions internal/goos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package internal

import "runtime"

const (
OnLinux = runtime.GOOS == "linux"
)
5 changes: 2 additions & 3 deletions internal/kallsyms/kallsyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"runtime"
"slices"
"strconv"
"strings"
Expand Down Expand Up @@ -50,7 +49,7 @@ func Module(name string) (string, error) {
// Any symbols missing in the kernel are ignored. Returns an error if multiple
// symbols with a given name were found.
func AssignModules(symbols map[string]string) error {
if runtime.GOOS != "linux" {
if !internal.OnLinux {
return fmt.Errorf("read /proc/kallsyms: %w", internal.ErrNotSupportedOnOS)
}

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

Expand Down
7 changes: 2 additions & 5 deletions internal/kallsyms/kallsyms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package kallsyms

import (
"bytes"
"errors"
"os"
"runtime"
"testing"

"github.com/go-quicktest/qt"

"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)

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

if runtime.GOOS != "linux" {
if !internal.OnLinux {
tb.Skip("/proc/kallsyms is a Linux concept")
}

f, err := os.Open("/proc/kallsyms")
if runtime.GOOS != "linux" && errors.Is(err, os.ErrNotExist) {
}
qt.Assert(tb, qt.IsNil(err))
tb.Cleanup(func() { f.Close() })
return f
Expand Down
3 changes: 1 addition & 2 deletions internal/linux/auxv.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"runtime"
_ "unsafe"

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

func newAuxvRuntimeReader() (auxvPairReader, error) {
if runtime.GOOS != "linux" {
if !internal.OnLinux {
return nil, fmt.Errorf("read auxv from runtime: %w", internal.ErrNotSupportedOnOS)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tracefs/kprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func sanitizeTracefsPath(path ...string) (string, error) {
// but may be also be available at /sys/kernel/debug/tracing if debugfs is mounted.
// The available tracefs paths will depends on distribution choices.
var getTracefsPath = sync.OnceValues(func() (string, error) {
if runtime.GOOS != "linux" {
if !internal.OnLinux {
return "", fmt.Errorf("tracefs: %w", internal.ErrNotSupportedOnOS)
}

Expand Down

0 comments on commit 5d320c5

Please sign in to comment.