[cmake-developers] [PATCH] FindALSA: Create an imported ALSA:ASound target
Sam Thursfield
sam.thursfield at codethink.co.uk
Tue Jan 19 11:50:35 EST 2016
Imported targets are now the recommended way of dealing with external
library dependencies.
The documentation for FindALSA is rewritten slightly.
This commit also adds a test case, you can run it like this:
cmake ... -DCMake_TEST_FindALSA=ON
ctest -R FindALSA --output-on-failure
The testcase requires the libasound library and headers to be installed.
This change is based on the equivalent changes to FindTIFF, found in
commit ebaca6290d2c0be7dec22452389632949a700d28.
---
Modules/FindALSA.cmake | 51 ++++++++++++++++++++++++++++----------
Tests/CMakeLists.txt | 4 +++
Tests/FindALSA/CMakeLists.txt | 10 ++++++++
Tests/FindALSA/Test/CMakeLists.txt | 16 ++++++++++++
Tests/FindALSA/Test/main.c | 12 +++++++++
5 files changed, 80 insertions(+), 13 deletions(-)
create mode 100644 Tests/FindALSA/CMakeLists.txt
create mode 100644 Tests/FindALSA/Test/CMakeLists.txt
create mode 100644 Tests/FindALSA/Test/main.c
diff --git a/Modules/FindALSA.cmake b/Modules/FindALSA.cmake
index 5c30eb9..7f98250 100644
--- a/Modules/FindALSA.cmake
+++ b/Modules/FindALSA.cmake
@@ -2,28 +2,42 @@
# FindALSA
# --------
#
-# Find alsa
+# Find the alsa libraries (asound).
#
-# Find the alsa libraries (asound)
+# Imported targets
+# ^^^^^^^^^^^^^^^^
#
-# ::
+# This module defines the following :prop_tgt:`IMPORTED` target:
#
-# This module defines the following variables:
-# ALSA_FOUND - True if ALSA_INCLUDE_DIR & ALSA_LIBRARY are found
-# ALSA_LIBRARIES - Set when ALSA_LIBRARY is found
-# ALSA_INCLUDE_DIRS - Set when ALSA_INCLUDE_DIR is found
+# ``ALSA::ASound``
+# The asound library, if found.
#
+# Result variables
+# ^^^^^^^^^^^^^^^^
#
+# This module will set the following variables in your project:
#
-# ::
+# ``ALSA_FOUND``
+# true if the ALSA headers and libraries were found
+# ``ALSA_INCLUDE_DIRS``
+# the directory containing the ALSA headers
+# ``ALSA_LIBRARIES``
+# ALSA libraries to be linked.
#
-# ALSA_INCLUDE_DIR - where to find asoundlib.h, etc.
-# ALSA_LIBRARY - the asound library
-# ALSA_VERSION_STRING - the version of alsa found (since CMake 2.8.8)
+# Cache variables
+# ^^^^^^^^^^^^^^^
+#
+# The following cache variables may also be set.
+#
+# ``ALSA_INCLUDE_DIR``
+# the directory containing the ALSA headers
+# ``ALSA_LIBRARY``
+# the path to the ALSA ASound library
#=============================================================================
# Copyright 2009-2011 Kitware, Inc.
# Copyright 2009-2011 Philip Lowman <philip at yhbt.com>
+# Copyright 2016 Raumfeld
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
@@ -58,8 +72,19 @@ FIND_PACKAGE_HANDLE_STANDARD_ARGS(ALSA
VERSION_VAR ALSA_VERSION_STRING)
if(ALSA_FOUND)
- set( ALSA_LIBRARIES ${ALSA_LIBRARY} )
- set( ALSA_INCLUDE_DIRS ${ALSA_INCLUDE_DIR} )
+ set(ALSA_VERSION ${ALSA_VERSION_STRING})
+ set(ALSA_LIBRARIES ${ALSA_LIBRARY})
+ set(ALSA_INCLUDE_DIRS ${ALSA_INCLUDE_DIR})
+
+ if(NOT TARGET ALSA::ASound)
+ add_library(ALSA::ASound UNKNOWN IMPORTED)
+ set_target_properties(ALSA::ASound PROPERTIES
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ IMPORTED_LOCATION "${ALSA_LIBRARY}"
+ INTERFACE_INCLUDE_DIRECTORIES "${ALSA_INCLUDE_DIRS}"
+ INTERFACE_LIBRARIES "${ALSA_LIBRARY}"
+ )
+ endif()
endif()
mark_as_advanced(ALSA_INCLUDE_DIR ALSA_LIBRARY)
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 043b757..26b0cee 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -1355,6 +1355,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release
endif()
endif()
+ if(CMake_TEST_FindALSA)
+ add_subdirectory(FindALSA)
+ endif()
+
if(CMake_TEST_FindBoost)
add_subdirectory(FindBoost)
endif()
diff --git a/Tests/FindALSA/CMakeLists.txt b/Tests/FindALSA/CMakeLists.txt
new file mode 100644
index 0000000..891f7a4
--- /dev/null
+++ b/Tests/FindALSA/CMakeLists.txt
@@ -0,0 +1,10 @@
+add_test(NAME FindALSA.Test COMMAND
+ ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+ --build-and-test
+ "${CMake_SOURCE_DIR}/Tests/FindALSA/Test"
+ "${CMake_BINARY_DIR}/Tests/FindALSA/Test"
+ ${build_generator_args}
+ --build-project TestFindALSA
+ --build-options ${build_options}
+ --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+ )
diff --git a/Tests/FindALSA/Test/CMakeLists.txt b/Tests/FindALSA/Test/CMakeLists.txt
new file mode 100644
index 0000000..2dae457
--- /dev/null
+++ b/Tests/FindALSA/Test/CMakeLists.txt
@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 3.4)
+project(TestFindALSA C)
+include(CTest)
+
+find_package(ALSA REQUIRED)
+
+add_definitions(-DCMAKE_ASOUNDLIB_EXPECTED_VERSION=\"${ALSA_VERSION}\")
+
+add_executable(test_tgt main.c)
+target_link_libraries(test_tgt ALSA::ASound)
+add_test(NAME test_tgt COMMAND test_tgt)
+
+add_executable(test_vars main.c)
+target_include_directories(test_vars PRIVATE ${ALSA_INCLUDE_DIRS})
+target_link_libraries(test_vars PRIVATE ${ALSA_LIBRARIES})
+add_test(NAME test_vars COMMAND test_vars)
diff --git a/Tests/FindALSA/Test/main.c b/Tests/FindALSA/Test/main.c
new file mode 100644
index 0000000..9240c7b
--- /dev/null
+++ b/Tests/FindALSA/Test/main.c
@@ -0,0 +1,12 @@
+#include <assert.h>
+#include <string.h>
+#include <alsa/asoundlib.h>
+
+int main()
+{
+ const char *asoundlib_version;
+ asoundlib_version = snd_asoundlib_version();
+ assert (strcmp(asoundlib_version, CMAKE_ASOUNDLIB_EXPECTED_VERSION) == 0);
+
+ return 0;
+}
--
2.4.3
More information about the cmake-developers
mailing list