Skip to content

Commit 25a249f

Browse files
ydnarhraban
authored andcommitted
feat: build without libopusfile using go build tag
This patch introduces a new build tag `nolibopusfile` that conditionally compiles out the code that imports `libopusfile`. This enables a static binary build on Alpine Linux, which doesn’t have a static `libopusfile`. Tests still work: ```sh go test -tags nolibopusfile ./... go test ./... ``` We could also have moved all libopusfile related code (i.e. Stream) into a separate sub-package, but that would break backwards compatibility. This feature is too unpopular to justify introducing a new major version. See also: #24
1 parent 558c065 commit 25a249f

File tree

7 files changed

+105
-65
lines changed

7 files changed

+105
-65
lines changed

Diff for: README.md

+21-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Note regarding Forward Error Correction (FEC):
117117
> Note also that in order to use this feature the encoder needs to be configured
118118
> with `SetInBandFEC(true)` and `SetPacketLossPerc(x)` options.
119119
120-
### Streams (and files)
120+
### Streams (and Files)
121121

122122
To decode a .opus file (or .ogg with Opus data), or to decode a "Opus stream"
123123
(which is a Ogg stream with Opus data), use the `Stream` interface. It wraps an
@@ -162,7 +162,7 @@ https://www.opus-codec.org/docs/opus_api-1.1.3/
162162

163163
For more examples, see the `_test.go` files.
164164

165-
## Build & installation
165+
## Build & Installation
166166

167167
This package requires libopus and libopusfile development packages to be
168168
installed on your system. These are available on Debian based systems from
@@ -180,6 +180,25 @@ Mac:
180180
brew install pkg-config opus opusfile
181181
```
182182

183+
### Building Without `libopusfile`
184+
185+
This package can be built without `libopusfile` by using the build tag `nolibopusfile`.
186+
This enables the compilation of statically-linked binaries with no external
187+
dependencies on operating systems without a static `libopusfile`, such as
188+
[Alpine Linux](https://pkgs.alpinelinux.org/contents?branch=edge&name=opusfile-dev&arch=x86_64&repo=main).
189+
190+
**Note:** this will disable all file and `Stream` APIs.
191+
192+
To enable this feature, add `-tags nolibopusfile` to your `go build` or `go test` commands:
193+
194+
```sh
195+
# Build
196+
go build -tags nolibopusfile ...
197+
198+
# Test
199+
go test -tags nolibopusfile ./...
200+
```
201+
183202
### Using in Docker
184203

185204
If your Dockerized app has this library as a dependency (directly or

Diff for: callbacks.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !nolibopusfile
2+
13
// Copyright © Go Opus Authors (see AUTHORS file)
24
//
35
// License for use of this code is detailed in the LICENSE file

Diff for: errors.go

+1-63
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
)
1010

1111
/*
12-
#cgo pkg-config: opus opusfile
12+
#cgo pkg-config: opus
1313
#include <opus.h>
14-
#include <opusfile.h>
1514
*/
1615
import "C"
1716

@@ -35,64 +34,3 @@ const (
3534
func (e Error) Error() string {
3635
return fmt.Sprintf("opus: %s", C.GoString(C.opus_strerror(C.int(e))))
3736
}
38-
39-
type StreamError int
40-
41-
var _ error = StreamError(0)
42-
43-
// Libopusfile errors. The names are copied verbatim from the libopusfile
44-
// library.
45-
const (
46-
ErrStreamFalse = StreamError(C.OP_FALSE)
47-
ErrStreamEOF = StreamError(C.OP_EOF)
48-
ErrStreamHole = StreamError(C.OP_HOLE)
49-
ErrStreamRead = StreamError(C.OP_EREAD)
50-
ErrStreamFault = StreamError(C.OP_EFAULT)
51-
ErrStreamImpl = StreamError(C.OP_EIMPL)
52-
ErrStreamInval = StreamError(C.OP_EINVAL)
53-
ErrStreamNotFormat = StreamError(C.OP_ENOTFORMAT)
54-
ErrStreamBadHeader = StreamError(C.OP_EBADHEADER)
55-
ErrStreamVersion = StreamError(C.OP_EVERSION)
56-
ErrStreamNotAudio = StreamError(C.OP_ENOTAUDIO)
57-
ErrStreamBadPacked = StreamError(C.OP_EBADPACKET)
58-
ErrStreamBadLink = StreamError(C.OP_EBADLINK)
59-
ErrStreamNoSeek = StreamError(C.OP_ENOSEEK)
60-
ErrStreamBadTimestamp = StreamError(C.OP_EBADTIMESTAMP)
61-
)
62-
63-
func (i StreamError) Error() string {
64-
switch i {
65-
case ErrStreamFalse:
66-
return "OP_FALSE"
67-
case ErrStreamEOF:
68-
return "OP_EOF"
69-
case ErrStreamHole:
70-
return "OP_HOLE"
71-
case ErrStreamRead:
72-
return "OP_EREAD"
73-
case ErrStreamFault:
74-
return "OP_EFAULT"
75-
case ErrStreamImpl:
76-
return "OP_EIMPL"
77-
case ErrStreamInval:
78-
return "OP_EINVAL"
79-
case ErrStreamNotFormat:
80-
return "OP_ENOTFORMAT"
81-
case ErrStreamBadHeader:
82-
return "OP_EBADHEADER"
83-
case ErrStreamVersion:
84-
return "OP_EVERSION"
85-
case ErrStreamNotAudio:
86-
return "OP_ENOTAUDIO"
87-
case ErrStreamBadPacked:
88-
return "OP_EBADPACKET"
89-
case ErrStreamBadLink:
90-
return "OP_EBADLINK"
91-
case ErrStreamNoSeek:
92-
return "OP_ENOSEEK"
93-
case ErrStreamBadTimestamp:
94-
return "OP_EBADTIMESTAMP"
95-
default:
96-
return "libopusfile error: %d (unknown code)"
97-
}
98-
}

Diff for: stream.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// License for use of this code is detailed in the LICENSE file
44

5+
// +build !nolibopusfile
6+
57
package opus
68

79
import (

Diff for: stream_errors.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright © 2015-2017 Go Opus Authors (see AUTHORS file)
2+
//
3+
// License for use of this code is detailed in the LICENSE file
4+
5+
// +build !nolibopusfile
6+
7+
package opus
8+
9+
/*
10+
#cgo pkg-config: opusfile
11+
#include <opusfile.h>
12+
*/
13+
import "C"
14+
15+
// StreamError represents an error from libopusfile.
16+
type StreamError int
17+
18+
var _ error = StreamError(0)
19+
20+
// Libopusfile errors. The names are copied verbatim from the libopusfile
21+
// library.
22+
const (
23+
ErrStreamFalse = StreamError(C.OP_FALSE)
24+
ErrStreamEOF = StreamError(C.OP_EOF)
25+
ErrStreamHole = StreamError(C.OP_HOLE)
26+
ErrStreamRead = StreamError(C.OP_EREAD)
27+
ErrStreamFault = StreamError(C.OP_EFAULT)
28+
ErrStreamImpl = StreamError(C.OP_EIMPL)
29+
ErrStreamInval = StreamError(C.OP_EINVAL)
30+
ErrStreamNotFormat = StreamError(C.OP_ENOTFORMAT)
31+
ErrStreamBadHeader = StreamError(C.OP_EBADHEADER)
32+
ErrStreamVersion = StreamError(C.OP_EVERSION)
33+
ErrStreamNotAudio = StreamError(C.OP_ENOTAUDIO)
34+
ErrStreamBadPacked = StreamError(C.OP_EBADPACKET)
35+
ErrStreamBadLink = StreamError(C.OP_EBADLINK)
36+
ErrStreamNoSeek = StreamError(C.OP_ENOSEEK)
37+
ErrStreamBadTimestamp = StreamError(C.OP_EBADTIMESTAMP)
38+
)
39+
40+
func (i StreamError) Error() string {
41+
switch i {
42+
case ErrStreamFalse:
43+
return "OP_FALSE"
44+
case ErrStreamEOF:
45+
return "OP_EOF"
46+
case ErrStreamHole:
47+
return "OP_HOLE"
48+
case ErrStreamRead:
49+
return "OP_EREAD"
50+
case ErrStreamFault:
51+
return "OP_EFAULT"
52+
case ErrStreamImpl:
53+
return "OP_EIMPL"
54+
case ErrStreamInval:
55+
return "OP_EINVAL"
56+
case ErrStreamNotFormat:
57+
return "OP_ENOTFORMAT"
58+
case ErrStreamBadHeader:
59+
return "OP_EBADHEADER"
60+
case ErrStreamVersion:
61+
return "OP_EVERSION"
62+
case ErrStreamNotAudio:
63+
return "OP_ENOTAUDIO"
64+
case ErrStreamBadPacked:
65+
return "OP_EBADPACKET"
66+
case ErrStreamBadLink:
67+
return "OP_EBADLINK"
68+
case ErrStreamNoSeek:
69+
return "OP_ENOSEEK"
70+
case ErrStreamBadTimestamp:
71+
return "OP_EBADTIMESTAMP"
72+
default:
73+
return "libopusfile error: %d (unknown code)"
74+
}
75+
}

Diff for: stream_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// License for use of this code is detailed in the LICENSE file
44

5+
// +build !nolibopusfile
6+
57
package opus
68

79
import (

Diff for: streams_map.go

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// License for use of this code is detailed in the LICENSE file
44

5+
// +build !nolibopusfile
6+
57
package opus
68

79
import (

0 commit comments

Comments
 (0)