From 86f1074905ffce25ff5d8e74fc5701f24b36823b Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Sun, 4 Nov 2018 12:21:23 +0100
Subject: [PATCH] lib/xiph/meson.build: the Vorbis encoder requires the Vorbis
 decoder

Without the Vorbis decoder, `libvorbis` is never detected, leading to
linker failures when attempting to build the Vorbis encoder.
---
 NEWS                     |  2 ++
 src/lib/xiph/meson.build | 13 +++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index cb584b220..8b220ec7f 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@ ver 0.21.1 (not yet released)
 * decoder
   - ffmpeg: fix build failure with non-standard FFmpeg installation path
   - flac: fix linker failure when building without FLAC support
+* encoder
+  - vorbis: fix linker failure when building without Vorbis decoder
 * fix build failure on Linux-PowerPC
 * fix build failure on FreeBSD
 * eliminate DLL dependencies on Windows
diff --git a/src/lib/xiph/meson.build b/src/lib/xiph/meson.build
index 19dc64300..a5d6c1929 100644
--- a/src/lib/xiph/meson.build
+++ b/src/lib/xiph/meson.build
@@ -2,10 +2,15 @@ libflac_dep = dependency('flac', version: '>= 1.2', required: get_option('flac')
 libopus_dep = dependency('opus', required: get_option('opus'))
 libvorbis_dep = dependency('vorbis', required: get_option('vorbis'))
 
-if need_encoder
-  libvorbisenc_dep = dependency('vorbisenc', required: get_option('vorbisenc'))
-else
-  libvorbisenc_dep = dependency('', required: false)
+libvorbisenc_dep = dependency('', required: false)
+if need_encoder and not get_option('vorbisenc').disabled()
+  if libvorbis_dep.found()
+    libvorbisenc_dep = dependency('vorbisenc', required: get_option('vorbisenc'))
+  elif get_option('vorbisenc').enabled()
+    error('Cannot build the Vorbis encoder without the Vorbis decoder')
+  else
+    message('Disabling the Vorbis encoder because the Vorbis decoder is disabled as well')
+  endif
 endif
 
 if libopus_dep.found() or libvorbis_dep.found() or libvorbisenc_dep.found()