Skip to content

Commit

Permalink
opt: fix compilation errors on *BSD (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 authored Apr 24, 2024
1 parent e800fbd commit f6980ec
Show file tree
Hide file tree
Showing 46 changed files with 401 additions and 149 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/cross-compile-bsd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Cross-compile for *BSD

on:
push:
branches:
- master
- dev
- 1.x
paths-ignore:
- '**.md'
- '**.yml'
- '**.yaml'
- '!.github/workflows/cross-compile-bsd.yml'
pull_request:
branches:
- master
- dev
- 1.x
paths-ignore:
- '**.md'
- '**.yml'
- '**.yaml'
- '!.github/workflows/cross-compile-bsd.yml'

env:
GO111MODULE: on
GOPROXY: "https://proxy.golang.org"

jobs:
build:
strategy:
fail-fast: false
matrix:
go: ['1.17', '1.21']
os:
- ubuntu-latest
name: Go ${{ matrix.go }} @ ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- name: Print Go environment
id: go-env
run: |
printf "Using go at: $(which go)\n"
printf "Go version: $(go version)\n"
printf "\n\nGo environment:\n\n"
go env
printf "\n\nSystem environment:\n\n"
env
# Calculate the short SHA1 hash of the git commit
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "GO_CACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT
- name: Cross-compiling for DragonFlyBSD
run: GOOS=dragonfly GOARCH=amd64 go build

- name: Cross-compiling for DragonFlyBSD -tags=poll_opt,gc_opt
run: GOOS=dragonfly GOARCH=amd64 go build -tags=poll_opt,gc_opt

- name: Cross-compiling for FreeBSD
run: GOOS=freebsd GOARCH=amd64 go build

- name: Cross-compiling for FreeBSD -tags=poll_opt,gc_opt
run: GOOS=freebsd GOARCH=amd64 go build -tags=poll_opt,gc_opt

- name: Cross-compiling for NetBSD
run: GOOS=netbsd GOARCH=amd64 go build

- name: Cross-compiling for NetBSD -tags=poll_opt,gc_opt
run: GOOS=netbsd GOARCH=amd64 go build -tags=poll_opt,gc_opt

- name: Cross-compiling for OpenBSD
run: GOOS=openbsd GOARCH=amd64 go build

- name: Cross-compiling for OpenBSD -tags=poll_opt,gc_opt
run: GOOS=openbsd GOARCH=amd64 go build -tags=poll_opt,gc_opt
4 changes: 2 additions & 2 deletions acceptor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin || windows
// +build linux freebsd dragonfly netbsd openbsd darwin windows
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || windows
// +build darwin dragonfly freebsd linux netbsd openbsd windows

package gnet

Expand Down
4 changes: 2 additions & 2 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions conn_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && !gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || linux || netbsd || openbsd) && !gc_opt
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build !gc_opt

package gnet
Expand Down
4 changes: 2 additions & 2 deletions conn_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || linux || netbsd || openbsd) && gc_opt
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build gc_opt

package gnet
Expand Down
4 changes: 2 additions & 2 deletions conn_matrix_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || linux || netbsd || openbsd) && gc_opt
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build gc_opt

package gnet
Expand Down
4 changes: 2 additions & 2 deletions connection_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build freebsd || dragonfly || netbsd || openbsd || darwin
// +build freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions engine_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions eventloop_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require (
github.com/stretchr/testify v1.8.4
github.com/valyala/bytebufferpool v1.0.0
go.uber.org/zap v1.21.0 // don't upgrade this one
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.19.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
4 changes: 2 additions & 2 deletions internal/io/io_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build freebsd || dragonfly || netbsd || openbsd || darwin
// +build freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package io

Expand Down
4 changes: 2 additions & 2 deletions internal/netpoll/defs_bsd_32bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (freebsd || dragonfly || netbsd || openbsd || darwin) && (386 || arm || mips || mipsle)
// +build freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || netbsd || openbsd) && (386 || arm || mips || mipsle)
// +build darwin dragonfly freebsd netbsd openbsd
// +build 386 arm mips mipsle

package netpoll
Expand Down
4 changes: 2 additions & 2 deletions internal/netpoll/defs_bsd_64bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (freebsd || dragonfly || netbsd || openbsd || darwin) && (amd64 || arm64 || ppc64 || ppc64le || mips64 || mips64le || riscv64)
// +build freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || netbsd || openbsd) && (amd64 || arm64 || ppc64 || ppc64le || mips64 || mips64le || riscv64)
// +build darwin dragonfly freebsd netbsd openbsd
// +build amd64 arm64 ppc64 ppc64le mips64 mips64le riscv64

package netpoll
Expand Down
7 changes: 2 additions & 5 deletions internal/netpoll/defs_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package netpoll

// IOFlags represents the flags of IO events.
type IOFlags = uint16

// PollEventHandler is the callback for I/O events notified by the poller.
type PollEventHandler func(int, IOEvent, IOFlags) error

Expand Down
24 changes: 24 additions & 0 deletions internal/netpoll/defs_poller_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2024 The Gnet Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build darwin || dragonfly || freebsd || openbsd
// +build darwin dragonfly freebsd openbsd

package netpoll

// IOFlags represents the flags of IO events.
type IOFlags = uint16

// IOEvent is the integer type of I/O events on BSD's.
type IOEvent = int16
3 changes: 3 additions & 0 deletions internal/netpoll/defs_poller_epoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package netpoll

import "golang.org/x/sys/unix"

// IOFlags represents the flags of IO events.
type IOFlags = uint16

// IOEvent is the integer type of I/O events on Linux.
type IOEvent = uint32

Expand Down
7 changes: 2 additions & 5 deletions internal/netpoll/defs_poller_kqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build freebsd || dragonfly || netbsd || openbsd || darwin
// +build freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package netpoll

import "golang.org/x/sys/unix"

// IOEvent is the integer type of I/O events on BSD's.
type IOEvent = int16

const (
// InitPollEventsCap represents the initial capacity of poller event-list.
InitPollEventsCap = 64
Expand Down
21 changes: 21 additions & 0 deletions internal/netpoll/defs_poller_netbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 The Gnet Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package netpoll

// IOEvent is the integer type of I/O events on BSD's.
type IOEvent = uint32

// IOFlags represents the flags of IO events.
type IOFlags = uint32
6 changes: 2 additions & 4 deletions internal/netpoll/poller_epoll_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ func OpenPoller() (poller *Poller, err error) {

// Close closes the poller.
func (p *Poller) Close() error {
if err := os.NewSyscallError("close", unix.Close(p.fd)); err != nil {
return err
}
return os.NewSyscallError("close", unix.Close(p.efd))
_ = unix.Close(p.efd)
return os.NewSyscallError("close", unix.Close(p.fd))
}

// Make the endianness of bytes compatible with more linux OSs under different processor-architectures,
Expand Down
Loading

0 comments on commit f6980ec

Please sign in to comment.