On Monday, June 21, 2010, I wrote:
> On Monday, June 21, 2010, David Henningsson wrote:
> > Sounds fair enough to me, for now. I think I'll use CMake for a while
> > now and see if I run into trouble. My first impression is that I like it
> > - it seems to be more modern and adjusted for today's needs, compared to
> > the ancient autotools.
> >
> > Btw, I've committed an updated CMakeLists.txt and moved files into
> > "drivers" subdir (just audio and midi drivers) and an "rvoice" dir (for
> > the voice renderer stuff) which will get more files in it soon.
> >
> > // David
>
> Thanks, it builds OK now!
>
> I've noticed that there is a new option to enable D-Bus client support, for
> rtkit. Do you dare to include it in the CMake build system?

Actually, this is an opportunity for explaining some CMake concepts, adding a 
new feature to the CMake build system. So here is a brief HOWTO and a patch. 
Please ask questions if there is something unclear, and feel free to commit 
the changes after some testing and your own customization.

Regards,
Pedro
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 298)
+++ CMakeLists.txt	(working copy)
@@ -61,6 +61,7 @@
 option ( enable-dart "compile DART support (if it is available)" on )
 option ( enable-readline "compile readline lib line editing (if it is available)" on )
 option ( enable-lash "compile LASH support (if it is available)" on )
+option ( enable-dbus "compile DBUS support (if it is available)" on )
 
 # Initialize the library directory name suffix.
 if ( CMAKE_SIZEOF_VOID_P EQUAL 8 )
@@ -325,6 +326,13 @@
   set ( LADCCA_SUPPORT ${LADCCA_FOUND} )
 endif ( enable-ladcca )
 
+set ( DBUS_SUPPORT )
+set ( DBUS_LIBRARIES )
+if ( enable-dbus )
+    pkg_check_modules ( DBUS dbus-1>=1.0.0 )
+    set ( DBUS_SUPPORT ${DBUS_FOUND} )
+endif ( enable-dbus )
+ 
 # General configuration file
 configure_file ( ${CMAKE_SOURCE_DIR}/src/config.cmake 
                  ${CMAKE_BINARY_DIR}/config.h )
Index: cmake_admin/report.cmake
===================================================================
--- cmake_admin/report.cmake	(revision 298)
+++ cmake_admin/report.cmake	(working copy)
@@ -11,6 +11,12 @@
   message ( "libsndfile:            no (raw audio file rendering only)" )
 endif ( LIBSNDFILE_SUPPORT )
 
+if ( DBUS_SUPPORT )
+  message ( "D-Bus:                 yes" )
+else ( DBUS_SUPPORT ) 
+  message ( "D-Bus:                 no" )
+endif ( DBUS_SUPPORT )
+
 if ( PULSE_SUPPORT )
   message ( "PulseAudio:            yes" )
 else ( PULSE_SUPPORT ) 
Index: src/CMakeLists.txt
===================================================================
--- src/CMakeLists.txt	(revision 298)
+++ src/CMakeLists.txt	(working copy)
@@ -54,6 +54,11 @@
   set ( fluid_coremidi_SOURCES drivers/fluid_coremidi.c )
 endif ( COREMIDI_SUPPORT )
 
+if ( DBUS_SUPPORT )
+  set ( fluid_dbus_SOURCES fluid_rtkit.c fluid_rtkit.h )
+  include_directories ( ${DBUS_INCLUDEDIR} ${DBUS_INCLUDE_DIRS} )
+endif ( DBUS_SUPPORT )
+
 if ( JACK_SUPPORT )
   set ( fluid_jack_SOURCES drivers/fluid_jack.c )
   include_directories ( ${JACK_INCLUDEDIR} ${JACK_INCLUDE_DIRS} ) 
@@ -172,6 +177,8 @@
     ${PORTAUDIO_LIBRARY_DIRS} 
     ${LIBSNDFILE_LIBDIR}
     ${LIBSNDFILE_LIBRARY_DIRS}
+    ${DBUS_LIBDIR} 
+    ${DBUS_LIBRARY_DIRS} 
 )
 
 add_library ( libfluidsynth SHARED 
@@ -179,6 +186,7 @@
     ${fluid_coreaudio_SOURCES}
     ${fluid_coremidi_SOURCES}
     ${fluid_dart_SOURCES}
+    ${fluid_dbus_SOURCES}
     ${fluid_jack_SOURCES}
     ${fluid_lash_SOURCES}
     ${fluid_ladspa_SOURCES}
@@ -218,13 +226,14 @@
     ${PULSE_LIBRARIES} 
     ${PORTAUDIO_LIBRARIES} 
     ${LIBSNDFILE_LIBRARIES}
+    ${DBUS_LIBRARIES}
     ${READLINE_LIBS} 
     ${DART_LIBS}  
     ${COREAUDIO_LIBS} 
     ${COREMIDI_LIBS} 
     ${WINDOWS_LIBS}
     ${MidiShare_LIBS}
-    ${LIBFLUID_LIBS} 
+    ${LIBFLUID_LIBS}
 )
 
 # ************ CLI program ************
Index: src/config.cmake
===================================================================
--- src/config.cmake	(revision 298)
+++ src/config.cmake	(working copy)
@@ -19,6 +19,9 @@
 /* Define if building for Mac OS X Darwin */
 #cmakedefine DARWIN @DARWIN@
 
+/* Define if D-Bus support is enabled */
+#cmakedefine DBUS_SUPPORT  @DBUS_SUPPORT@
+
 /* Define to enable FPE checks */
 #cmakedefine FPE_CHECK @FPE_CHECK@
 
How to add a new feature to the CMake build system
==================================================

Let's explain this issue with an example. We are adding D-Bus support to 
FluidSynth as an optional feature, conditionally adding source files that 
require this feature. The first step is to add a macro "option()" to the main 
CMakeLists.txt file, the one that is located at the fluidsynth root directory.

file CMakeLists.txt, line 64:

        option ( enable-dbus "compile DBUS support (if it is available)" on )

Now, let's check if the dbus-1 library and headers are installed, using 
pkg-config:

file CMakeLists.txt, lines 329-334:

        set ( DBUS_SUPPORT )
        set ( DBUS_LIBRARIES )
        if ( enable-dbus )
                pkg_check_modules ( DBUS dbus-1>=1.0.0 )
                set ( DBUS_SUPPORT ${DBUS_FOUND} )
        endif ( enable-dbus )

The two first lines clear the values of the CMake variables DBUS_LIBRARIES and 
DBUS_SUPPORT. If the value of the option "enable-dbus" is true, then the macro 
pkg_check_modules() is used to test a package named "dbus-1" with version 1.0.0 
or later. This macro automatically defines the variables DBUS_LIBRARIES, 
DBUS_INCLUDEDIR, DBUS_FOUND and others. The value of the last one is assigned 
to our variable DBUS_SUPPORT for later use.

There is a report to summarize the performed checks and the enabled features 
after the configuration steps, so let's add a line in this report regarding the 
D-Bus support.

file cmake_admin/report.cmake, lines 14-18:

        if ( DBUS_SUPPORT )
                message ( "D-Bus:                 yes" )
        else ( DBUS_SUPPORT ) 
                message ( "D-Bus:                 no" )
        endif ( DBUS_SUPPORT )

The variable DBUS_SUPPORT is available for the CMake files, but we want to make 
it available to the compilers as well, to conditionally build code using 
"#ifdef DBUS_SUPPORT". This can be done adding a line to the config.cmake file:

file src/config.cmake, lines 22-23:

        /* Define if D-Bus support is enabled */
        #cmakedefine DBUS_SUPPORT @DBUS_SUPPORT@

The file config.cmake will be processed at configure time, producing a header 
file "config.h" in the build directory with this content, if the dbus support 
has been enabled and found:

        /* Define if D-Bus support is enabled */
        #define DBUS_SUPPORT  1

Finally, we can add the new source files to the build system for the compiler 
target with the macro add_library(), and the libraries for the linker target 
with the macros link_directories() and target_link_libraries().

file src/CMakeLists.txt, lines 57-60

        if ( DBUS_SUPPORT )
                set ( fluid_dbus_SOURCES fluid_rtkit.c fluid_rtkit.h )
                include_directories ( ${DBUS_INCLUDEDIR} ${DBUS_INCLUDE_DIRS} )
        endif ( DBUS_SUPPORT )

file src/CMakeLists.txt, lines 163-197

    link_directories (
                ...
                ${DBUS_LIBDIR} 
                ${DBUS_LIBRARY_DIRS} 
        )

        add_library ( libfluidsynth SHARED 
                ...
                ${fluid_dbus_SOURCES}
                ...
        )

file src/CMakeLists.txt, lines 163-197

        target_link_libraries ( libfluidsynth
                ...
                ${DBUS_LIBRARIES}
                ...
        )
_______________________________________________
fluid-dev mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/fluid-dev

Reply via email to