[AD] acodec build system

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Attached is a patch to make the examples more flexible regarding audio codecs.

In short, it:

* updates cmake.common "add_our_executable" to accept preprocessor
defines (-DFOO)
* updates the examples' cmake file to append -DALLEGRO_CFG_ACODEC_*
for supported types at Allegro compile time
* updates the examples to include acodec.h (NOT a system file) for
common acodec init code

If none of the current three codecs are available, the examples will
still compile with WAV support.

It does NOT expose anything new to the Allegro user regarding which
codecs are available. I'm not even sure that is possible with the
current architecture, since additional audio codecs could be added or
removed after the initial compile.

I had already put some of this together before seeing the comment
regarding add_defines() ... I think it should work as-is without that.
And actually, without these changes, I think -DALLEGRO_STATICLINK
would override -DALLEGRO_STATICLINK in the "add_our_executable".

--
Matthew Leverton
Index: cmake/Common.cmake
===================================================================
--- cmake/Common.cmake	(revision 13205)
+++ cmake/Common.cmake	(working copy)
@@ -162,21 +162,27 @@
     endif()
 endfunction(install_our_headers)
 
-# Arguments after nm should be source files or libraries.  Source files must
-# end with .c or .cpp.  If no source file was explicitly specified, we assume
-# an implied C source file.
+# Arguments after nm should be source files, libraries, or defines (-D).
+# Source files must end with .c or .cpp.  If no source file was explicitly
+# specified, we assume an implied C source file.
 # 
 # Free variable: EXECUTABLE_TYPE
 #
 function(add_our_executable nm)
     set(srcs)
     set(libs)
+    set(defines)
     set(regex "[.](c|cpp)$")
+    set(regexd "^-D")
     foreach(arg ${ARGN})
         if("${arg}" MATCHES "${regex}")
             list(APPEND srcs ${arg})
         else("${arg}" MATCHES "${regex}")
-            list(APPEND libs ${arg})
+            if ("${arg}" MATCHES "${regexd}")
+                list(APPEND defines "${arg}")
+            else("${arg}" MATCHES "${regexd}")
+                list(APPEND libs ${arg})
+            endif("${arg}" MATCHES "${regexd}")
         endif("${arg}" MATCHES "${regex}")
     endforeach(arg ${ARGN})
 
@@ -187,11 +193,15 @@
     add_executable(${nm} ${EXECUTABLE_TYPE} ${srcs})
     target_link_libraries(${nm} ${libs})
     if(WANT_POPUP_EXAMPLES AND SUPPORT_NATIVE_DIALOG)
-        set_target_properties(${nm} PROPERTIES COMPILE_FLAGS "-DALLEGRO_POPUP_EXAMPLES")
+        list(APPEND defines, "-DALLEGRO_POPUP_EXAMPLES")
     endif()
     if(NOT BUILD_SHARED_LIBS)
-        set_target_properties(${nm} PROPERTIES COMPILE_FLAGS "-DALLEGRO_STATICLINK")
+        list(APPEND defines, "-DALLEGRO_STATICLINK")
     endif(NOT BUILD_SHARED_LIBS)
+
+    string(REGEX REPLACE ";" " " defines "${defines}")
+    set_target_properties(${nm} PROPERTIES COMPILE_FLAGS "${defines}")
+
     if(MINGW)
         if(NOT CMAKE_BUILD_TYPE STREQUAL Debug)
             set_target_properties(${nm} PROPERTIES LINK_FLAGS "-Wl,-subsystem,windows")
Index: examples/ex_kcm_direct.c
===================================================================
--- examples/ex_kcm_direct.c	(revision 13205)
+++ examples/ex_kcm_direct.c	(working copy)
@@ -2,9 +2,8 @@
 
 #include <stdio.h>
 #include "allegro5/allegro5.h"
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -24,8 +23,7 @@
       return 1;
    }
 
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
       fprintf(stderr, "Could not init sound!\n");
Index: examples/ex_mixer_chain.c
===================================================================
--- examples/ex_mixer_chain.c	(revision 13205)
+++ examples/ex_mixer_chain.c	(working copy)
@@ -6,9 +6,8 @@
 
 #include <stdio.h>
 #include "allegro5/allegro5.h"
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -33,8 +32,7 @@
       return 1;
    }
 
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
       fprintf(stderr, "Could not init sound!\n");
Index: examples/ex_stream_file.c
===================================================================
--- examples/ex_stream_file.c	(revision 13205)
+++ examples/ex_stream_file.c	(working copy)
@@ -10,12 +10,8 @@
 
 #include <stdio.h>
 #include "allegro5/allegro5.h"
-#ifndef ALLEGRO_IPHONE
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_modaudio.h"
-#endif
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -41,11 +37,7 @@
        return 1;
    }
 
-   al_init_ogg_vorbis_addon();
-#ifndef ALLEGRO_IPHONE
-   al_init_flac_addon();
-   al_init_modaudio_addon();
-#endif
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
        fprintf(stderr, "Could not init sound!\n");
Index: examples/ex_stream_seek.c
===================================================================
--- examples/ex_stream_seek.c	(revision 13205)
+++ examples/ex_stream_seek.c	(working copy)
@@ -7,11 +7,9 @@
 #include <stdio.h>
 #include "allegro5/allegro5.h"
 #include "allegro5/allegro_font.h"
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
 #include "allegro5/allegro_primitives.h"
-#include "allegro5/allegro_modaudio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -41,9 +39,8 @@
       printf("Could not init mouse!\n");
       return 0;
    }
-   al_init_ogg_vorbis_addon();
-   al_init_flac_addon();
-   al_init_modaudio_addon();
+   
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
       printf("Could not init sound!\n");
Index: examples/ex_audio_simple.c
===================================================================
--- examples/ex_audio_simple.c	(revision 13205)
+++ examples/ex_audio_simple.c	(working copy)
@@ -6,9 +6,8 @@
 
 #include <stdio.h>
 #include "allegro5/allegro5.h"
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -46,8 +45,7 @@
    event_queue = al_create_event_queue();
    al_register_event_source(event_queue, al_get_keyboard_event_source());
 
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
 
 Restart:
 
Index: examples/ex_acodec.c
===================================================================
--- examples/ex_acodec.c	(revision 13205)
+++ examples/ex_acodec.c	(working copy)
@@ -6,8 +6,8 @@
 
 #include <stdio.h>
 #include "allegro5/allegro5.h"
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_vorbis.h"
+#include "allegro5/allegro_audio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -28,8 +28,7 @@
       return 1;
    }
 
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
       fprintf(stderr, "Could not init sound!\n");
Index: examples/ex_mixer_pp.c
===================================================================
--- examples/ex_mixer_pp.c	(revision 13205)
+++ examples/ex_mixer_pp.c	(working copy)
@@ -7,10 +7,9 @@
 #include <math.h>
 #include "allegro5/allegro.h"
 #include "allegro5/allegro_audio.h"
-#include "allegro5/allegro_flac.h"
 #include "allegro5/allegro_image.h"
 #include "allegro5/allegro_primitives.h"
-#include "allegro5/allegro_vorbis.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -139,8 +138,8 @@
    }
 
    al_init_image_addon();
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
+   
    al_install_keyboard();
 
    if (!al_create_display(640, 480)) {
Index: examples/acodec.h
===================================================================
--- examples/acodec.h	(revision 0)
+++ examples/acodec.h	(revision 0)
@@ -0,0 +1,28 @@
+#ifdef ALLEGRO_CFG_ACODEC_FLAC
+#include "allegro5/allegro_flac.h"
+#endif
+#ifdef ALLEGRO_CFG_ACODEC_VORBIS
+#include "allegro5/allegro_vorbis.h"
+#endif
+#ifdef ALLEGRO_CFG_ACODEC_MODAUDIO
+#include "allegro5/allegro_modaudio.h"
+#endif
+
+bool init_acodecs()
+{
+   bool ret = true;
+   
+#ifdef ALLEGRO_CFG_ACODEC_FLAC
+   ret &= al_init_flac_addon();
+#endif
+
+#ifdef ALLEGRO_CFG_ACODEC_MODAUDIO
+   ret &= al_init_modaudio_addon();
+#endif
+
+#ifdef ALLEGRO_CFG_ACODEC_VORBIS
+   ret &= al_init_ogg_vorbis_addon();
+#endif
+
+   return ret;
+}
\ No newline at end of file
Index: examples/ex_acodec_multi.c
===================================================================
--- examples/ex_acodec_multi.c	(revision 13205)
+++ examples/ex_acodec_multi.c	(working copy)
@@ -6,9 +6,8 @@
 
 #include <stdio.h>
 #include "allegro5/allegro5.h"
-#include "allegro5/allegro_flac.h"
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
+#include "acodec.h"
 
 #include "common.c"
 
@@ -31,8 +30,7 @@
        return 1;
    }
 
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
        fprintf(stderr, "Could not init sound!\n");
Index: examples/CMakeLists.txt
===================================================================
--- examples/CMakeLists.txt	(revision 13205)
+++ examples/CMakeLists.txt	(working copy)
@@ -45,7 +45,16 @@
 #-----------------------------------------------------------------------------#
 
 set(AUDIO x${AUDIO_LINK_WITH})
-set(ACODEC x${FLAC_LINK_WITH} x${VORBIS_LINK_WITH} x${MODAUDIO_LINK_WITH})
+set(ACODEC)
+if(SUPPORT_FLAC)
+    list(APPEND ACODEC x${FLAC_LINK_WITH} -DALLEGRO_CFG_ACODEC_FLAC)
+endif()
+if(SUPPORT_VORBIS)
+    list(APPEND ACODEC x${VORBIS_LINK_WITH} -DALLEGRO_CFG_ACODEC_VORBIS)
+endif()
+if (SUPPORT_MODAUDIO)
+    list(APPEND ACODEC x${MODAUDIO_LINK_WITH} -DALLEGRO_CFG_ACODEC_MODAUDIO)
+endif()
 set(COLOR x${COLOR_LINK_WITH})
 set(DIALOG x${NATIVE_DIALOG_LINK_WITH})
 set(FONT x${FONT_LINK_WITH})
Index: examples/ex_audio_props.cpp
===================================================================
--- examples/ex_audio_props.cpp	(revision 13205)
+++ examples/ex_audio_props.cpp	(working copy)
@@ -5,10 +5,9 @@
  */
 
 #include "allegro5/allegro5.h"
-#include "allegro5/allegro_flac.h"
 #include "allegro5/allegro_font.h"
-#include "allegro5/allegro_vorbis.h"
 #include "allegro5/allegro_audio.h"
+#include "acodec.h"
 #include "nihgui.hpp"
 #include <cstdio>
 
@@ -107,8 +106,7 @@
 
    al_init_font_addon();
 
-   al_init_flac_addon();
-   al_init_ogg_vorbis_addon();
+   init_acodecs();
 
    if (!al_install_audio(ALLEGRO_AUDIO_DRIVER_AUTODETECT)) {
       abort_example("Could not init sound!\n");


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/