From 257a77fa355c3ffe708a4ef6f314206c48023a5a Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Tue, 26 May 2020 21:08:29 +0200
Subject: [PATCH] {android,win32}/build.py: build libmodplug and WildMidi

Closes https://github.com/MusicPlayerDaemon/MPD/issues/866
---
 NEWS                  |  2 ++
 android/build.py      |  2 ++
 python/build/cmake.py | 45 +++++++++++++++++++++++++++++++++++++++++++
 python/build/libs.py  | 24 +++++++++++++++++++++++
 win32/build.py        |  2 ++
 5 files changed, 75 insertions(+)
 create mode 100644 python/build/cmake.py

diff --git a/NEWS b/NEWS
index 30dd81a83..34057bbc2 100644
--- a/NEWS
+++ b/NEWS
@@ -8,8 +8,10 @@ ver 0.21.24 (not yet released)
   - wildmidi: attempt to detect WildMidi using pkg-config
   - wildmidi: fix Windows build failure
 * Android
+  - enable the decoder plugins ModPlug and WildMidi
   - fix build failure with Android NDK r21
 * Windows
+  - enable the decoder plugins ModPlug and WildMidi
   - work around Meson bug breaking the Windows build with GCC 10
 * fix unit test failure
 
diff --git a/android/build.py b/android/build.py
index 91e292683..8716fc1a9 100755
--- a/android/build.py
+++ b/android/build.py
@@ -168,6 +168,8 @@ thirdparty_libs = [
     opus,
     flac,
     libid3tag,
+    libmodplug,
+    wildmidi,
     ffmpeg,
     curl,
     libexpat,
diff --git a/python/build/cmake.py b/python/build/cmake.py
new file mode 100644
index 000000000..76f217715
--- /dev/null
+++ b/python/build/cmake.py
@@ -0,0 +1,45 @@
+import subprocess
+
+from build.project import Project
+
+def configure(toolchain, src, build, args=()):
+    cross_args = []
+
+    if toolchain.is_windows:
+        cross_args.append('-DCMAKE_SYSTEM_NAME=Windows')
+        cross_args.append('-DCMAKE_RC_COMPILER=' + toolchain.windres)
+
+    configure = [
+        'cmake',
+        src,
+
+        '-DCMAKE_INSTALL_PREFIX=' + toolchain.install_prefix,
+        '-DCMAKE_BUILD_TYPE=release',
+
+        '-DCMAKE_C_COMPILER=' + toolchain.cc,
+        '-DCMAKE_CXX_COMPILER=' + toolchain.cxx,
+
+        '-DCMAKE_C_FLAGS=' + toolchain.cflags + ' ' + toolchain.cppflags,
+        '-DCMAKE_CXX_FLAGS=' + toolchain.cxxflags + ' ' + toolchain.cppflags,
+
+        '-GNinja',
+    ] + cross_args + args
+
+    subprocess.check_call(configure, env=toolchain.env, cwd=build)
+
+class CmakeProject(Project):
+    def __init__(self, url, md5, installed, configure_args=[],
+                 **kwargs):
+        Project.__init__(self, url, md5, installed, **kwargs)
+        self.configure_args = configure_args
+
+    def configure(self, toolchain):
+        src = self.unpack(toolchain)
+        build = self.make_build_path(toolchain)
+        configure(toolchain, src, build, self.configure_args)
+        return build
+
+    def build(self, toolchain):
+        build = self.configure(toolchain)
+        subprocess.check_call(['ninja', 'install'],
+                              cwd=build, env=toolchain.env)
diff --git a/python/build/libs.py b/python/build/libs.py
index 0af17b303..c7b2250cd 100644
--- a/python/build/libs.py
+++ b/python/build/libs.py
@@ -4,6 +4,7 @@ from os.path import abspath
 from build.project import Project
 from build.zlib import ZlibProject
 from build.meson import MesonProject
+from build.cmake import CmakeProject
 from build.autotools import AutotoolsProject
 from build.ffmpeg import FfmpegProject
 from build.boost import BoostProject
@@ -111,6 +112,29 @@ liblame = AutotoolsProject(
     ],
 )
 
+libmodplug = AutotoolsProject(
+    'https://downloads.sourceforge.net/modplug-xmms/libmodplug/0.8.9.0/libmodplug-0.8.9.0.tar.gz',
+    '457ca5a6c179656d66c01505c0d95fafaead4329b9dbaa0f997d00a3508ad9de',
+    'lib/libmodplug.a',
+    [
+        '--disable-shared', '--enable-static',
+    ],
+)
+
+wildmidi = CmakeProject(
+    'https://codeload.github.com/Mindwerks/wildmidi/tar.gz/wildmidi-0.4.3',
+    '498e5a96455bb4b91b37188ad6dcb070824e92c44f5ed452b90adbaec8eef3c5',
+    'lib/libWildMidi.a',
+    [
+        '-DBUILD_SHARED_LIBS=OFF',
+        '-DWANT_PLAYER=OFF',
+        '-DWANT_STATIC=ON',
+    ],
+    base='wildmidi-wildmidi-0.4.3',
+    name='wildmidi',
+    version='0.4.3',
+)
+
 ffmpeg = FfmpegProject(
     'http://ffmpeg.org/releases/ffmpeg-4.2.3.tar.xz',
     '9df6c90aed1337634c1fb026fb01c154c29c82a64ea71291ff2da9aacb9aad31',
diff --git a/win32/build.py b/win32/build.py
index 0e09003fe..9e1b6e9e4 100755
--- a/win32/build.py
+++ b/win32/build.py
@@ -96,6 +96,8 @@ thirdparty_libs = [
     zlib,
     libid3tag,
     liblame,
+    libmodplug,
+    wildmidi,
     ffmpeg,
     curl,
     libexpat,