Skip to content

Commit

Permalink
ModPlug: Make a compile test for ModPlug_Tell() call existance
Browse files Browse the repository at this point in the history
Thanks to @sezero for a first sketch of this, I have polished it to make it working
  • Loading branch information
Wohlstand committed Nov 26, 2019
1 parent a2c0d6b commit 5771fc4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
11 changes: 11 additions & 0 deletions cmake/tests/modplug_tell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifdef MODPLUG_HEADER
#include MODPLUG_HEADER
#else
#include <libmodplug/modplug.h>
#endif

int main()
{
ModPlug_Tell(0);
return 0;
}
22 changes: 8 additions & 14 deletions src/codecs/music_modplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,9 @@ static ModPlug_Settings settings;
#define FUNCTION_LOADER(FUNC, SIG) \
modplug.FUNC = (SIG) SDL_LoadFunction(modplug.handle, #FUNC); \
if (modplug.FUNC == NULL) { SDL_UnloadObject(modplug.handle); return -1; }
#define FUNCTION_LOADER_OPTIONAL(FUNC, SIG) \
modplug.FUNC = (SIG) SDL_LoadFunction(modplug.handle, #FUNC);
#else
#define FUNCTION_LOADER(FUNC, SIG) \
modplug.FUNC = FUNC;
#define FUNCTION_LOADER_OPTIONAL(FUNC, SIG) \
modplug.FUNC = FUNC;
#endif

static int MODPLUG_Load(void)
Expand All @@ -88,15 +84,18 @@ static int MODPLUG_Load(void)
FUNCTION_LOADER(ModPlug_Unload, void (*)(ModPlugFile* file))
FUNCTION_LOADER(ModPlug_Read, int (*)(ModPlugFile* file, void* buffer, int size))
FUNCTION_LOADER(ModPlug_Seek, void (*)(ModPlugFile* file, int millisecond))
#ifdef MODPLUG_HAS_TELL
/* Use optional strategy to support both official and extended ABIs */
FUNCTION_LOADER_OPTIONAL(ModPlug_Tell, int (*)(ModPlugFile* file))
#endif
FUNCTION_LOADER(ModPlug_GetLength, int (*)(ModPlugFile* file))
FUNCTION_LOADER(ModPlug_GetSettings, void (*)(ModPlug_Settings* settings))
FUNCTION_LOADER(ModPlug_SetSettings, void (*)(const ModPlug_Settings* settings))
FUNCTION_LOADER(ModPlug_SetMasterVolume, void (*)(ModPlugFile* file,unsigned int cvol))
FUNCTION_LOADER(ModPlug_GetName, const char* (*)(ModPlugFile* file))
#ifdef MODPLUG_DYNAMIC
modplug.ModPlug_Tell = (int (*)(ModPlugFile* file)) SDL_LoadFunction(modplug.handle, "ModPlug_Tell");
#elif defined(MODPLUG_HAS_TELL)
modplug.ModPlug_Tell = ModPlug_Tell;
#else
modplug.ModPlug_Tell = NULL;
#endif
}
++modplug.loaded;

Expand Down Expand Up @@ -294,17 +293,12 @@ static int MODPLUG_Seek(void *context, double position)

static double MODPLUG_Tell(void *context)
{
#ifdef MODPLUG_HAS_TELL
if (modplug.ModPlug_Tell) {
MODPLUG_Music *music = (MODPLUG_Music *)context;
return (double)(modplug.ModPlug_Tell(music->file)) / 1000.0;
} else {
return -1;
return -1.0;
}
#else
(void)context;
return -1.0;
#endif
}

static double MODPLUG_Length(void *context)
Expand Down
41 changes: 41 additions & 0 deletions src/codecs/music_modplug.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@ if(USE_MODPLUG)
list(APPEND SDL_MIXER_DEFINITIONS -DMODPLUG_DYNAMIC=\"${ModPlug_DYNAMIC_LIBRARY}\")
message("Dynamic ModPlug: ${ModPlug_DYNAMIC_LIBRARY}")
endif()

if(ModPlug_FOUND)
if(USE_MODPLUG_STATIC)
list(APPEND SDL_MIXER_DEFINITIONS -DMODPLUG_STATIC)
if(UNIX AND NOT APPLE AND NOT HAIKU AND NOT EMSCRIPTEN)
find_library(M_LIBRARY m)
else()
set(M_LIBRARY "")
endif()

if(NOT MSVC)
set(STDCPP_LIBRARY "stdc++") # TODO: Verify on FreeBSD/OpenBSD/NetBSD
else()
set(STDCPP_LIBRARY "")
endif()
set(MODPLUG_STATIC_MACRO "-DMODPLUG_STATIC")
else()
set(STDCPP_LIBRARY "")
set(M_LIBRARY "")
set(MODPLUG_STATIC_MACRO "")
endif()

if(UNIX AND NOT APPLE AND NOT HAIKU AND NOT EMSCRIPTEN)
find_library(M_LIBRARY m)
endif()

try_compile(MODPLUG_HAS_TELL
${CMAKE_BINARY_DIR}/compile_tests
${SDLMixerX_SOURCE_DIR}/cmake/tests/modplug_tell.c
COMPILE_DEFINITIONS ${MODPLUG_STATIC_MACRO}
LINK_LIBRARIES ${ModPlug_LIBRARIES} ${STDCPP_LIBRARY} ${M_LIBRARY}
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${ModPlug_INCLUDE_DIRS}"
OUTPUT_VARIABLE MODPLUG_TEST_RESULT
)
message("ModPlug_Tell() compile test result: ${MODPLUG_HAS_TELL}")
endif()

else()
if(DOWNLOAD_AUDIO_CODECS_DEPENDENCY)
set(ModPlug_LIBRARIES modplug)
Expand All @@ -18,6 +55,7 @@ if(USE_MODPLUG)
HINTS "${AUDIO_CODECS_INSTALL_PATH}/lib")
endif()
set(ModPlug_FOUND 1)
set(MODPLUG_HAS_TELL True)
set(ModPlug_INCLUDE_DIRS "${AUDIO_CODECS_PATH}/libmodplug/include")
endif()

Expand All @@ -33,6 +71,9 @@ if(USE_MODPLUG)
list(APPEND SDL_MIXER_INCLUDE_PATHS ${ModPlug_INCLUDE_DIRS})
list(APPEND SDLMixerX_SOURCES
${SDLMixerX_SOURCE_DIR}/src/codecs/music_modplug.c)
if(MODPLUG_HAS_TELL)
list(APPEND SDL_MIXER_DEFINITIONS -DMODPLUG_HAS_TELL)
endif()
else()
message("-- skipping libModPlug --")
endif()
Expand Down

0 comments on commit 5771fc4

Please sign in to comment.