CMake improvements

* Fix Ogg and Opus imported targets
* Better SndFileConfig.cmake module with dependency checks
* Fix config-file package installation directory
* Prefer config-file package mode for Windows CI builds
* Update README.md
* Allow CMAKE_MSVC_RUNTIME_LIBRARY option to set MSVC ABI
* Fix some bugs in FindFLAC.cmake (closes #505)
This commit is contained in:
evpobr 2020-03-08 10:28:46 +05:00 committed by Erik de Castro Lopo
parent a7e9fb5f7c
commit e6909192ce
11 changed files with 86 additions and 37 deletions

View File

@ -1,5 +1,9 @@
cmake_minimum_required (VERSION 3.1.3) cmake_minimum_required (VERSION 3.1.3)
if (POLICY CMP0091)
cmake_policy(SET CMP0091 NEW)
endif ()
project(sndfile VERSION 1.0.29) project(sndfile VERSION 1.0.29)
# #
@ -304,7 +308,7 @@ target_link_libraries (sndfile
$<$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>:Vorbis::VorbisEnc> $<$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>:Vorbis::VorbisEnc>
$<$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>:FLAC::FLAC> $<$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>:FLAC::FLAC>
$<$<AND:$<BOOL:${ENABLE_EXPERIMENTAL}>,$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>,$<BOOL:${HAVE_SPEEX}>>:Speex::Speex> $<$<AND:$<BOOL:${ENABLE_EXPERIMENTAL}>,$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>,$<BOOL:${HAVE_SPEEX}>>:Speex::Speex>
$<$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>:Opus::Opus> $<$<BOOL:${HAVE_EXTERNAL_XIPH_LIBS}>:Opus::opus>
) )
set_target_properties (sndfile PROPERTIES set_target_properties (sndfile PROPERTIES
PUBLIC_HEADER "${sndfile_HDRS}" PUBLIC_HEADER "${sndfile_HDRS}"
@ -600,15 +604,15 @@ endif ()
if (ENABLE_PACKAGE_CONFIG) if (ENABLE_PACKAGE_CONFIG)
if (UNIX) if (WIN32 AND (NOT MINGW) AND (NOT CYGWIN))
set (CMAKE_INSTALL_PACKAGEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/SndFile) set (CMAKE_INSTALL_PACKAGEDIR cmake)
elseif(WIN32) else ()
set (CMAKE_INSTALL_PACKAGEDIR cmake) set (CMAKE_INSTALL_PACKAGEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/SndFile)
endif() endif()
install (TARGETS sndfile ${SNDFILE_PROGRAM_TARGETS} install (TARGETS sndfile ${SNDFILE_PROGRAM_TARGETS}
EXPORT SndFileConfig EXPORT SndFileTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
@ -617,14 +621,27 @@ if (ENABLE_PACKAGE_CONFIG)
include (CMakePackageConfigHelpers) include (CMakePackageConfigHelpers)
if (ENABLE_EXTERNAL_LIBS)
set (SndFile_WITH_EXTERNAL_LIBS 1)
else ()
set (SndFile_WITH_EXTERNAL_LIBS 0)
endif ()
set (INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR})
configure_package_config_file(cmake/SndFileConfig.cmake.in SndFileConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_PACKAGEDIR}
PATH_VARS INCLUDE_INSTALL_DIR
)
write_basic_package_version_file (SndFileConfigVersion.cmake COMPATIBILITY SameMajorVersion) write_basic_package_version_file (SndFileConfigVersion.cmake COMPATIBILITY SameMajorVersion)
install(EXPORT SndFileConfig install(EXPORT SndFileTargets
NAMESPACE SndFile:: NAMESPACE SndFile::
DESTINATION ${CMAKE_INSTALL_PACKAGEDIR} DESTINATION ${CMAKE_INSTALL_PACKAGEDIR}
) )
install( install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/SndFileConfigVersion.cmake FILES
${CMAKE_CURRENT_BINARY_DIR}/SndFileConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/SndFileConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_PACKAGEDIR} DESTINATION ${CMAKE_INSTALL_PACKAGEDIR}
) )

View File

@ -22,7 +22,7 @@ cmake_files = cmake/ClipMode.cmake cmake/FindFLAC.cmake \
cmake/FindSpeex.cmake cmake/FindSQLite3.cmake cmake/FindVorbisEnc.cmake \ cmake/FindSpeex.cmake cmake/FindSQLite3.cmake cmake/FindVorbisEnc.cmake \
cmake/SndFileChecks.cmake cmake/TestInline.cmake \ cmake/SndFileChecks.cmake cmake/TestInline.cmake \
cmake/TestLargeFiles.cmake cmake/TestInline.c.in \ cmake/TestLargeFiles.cmake cmake/TestInline.c.in \
cmake/FindOpus.cmake cmake/FindOpus.cmake cmake/SndFileConfig.cmake.in
pkgconfig_DATA = sndfile.pc pkgconfig_DATA = sndfile.pc

View File

@ -137,6 +137,9 @@ You can pass additional options with `/D<parameter>=<value>` when you run
This option is `ON` by default. This option is `ON` by default.
* `ENABLE_STATIC_RUNTIME` - enable static runtime on Windows platform, `OFF` by * `ENABLE_STATIC_RUNTIME` - enable static runtime on Windows platform, `OFF` by
default. default.
**Note**: For MSVC compiler this option is depecated. Use `CMAKE_MSVC_RUNTIME_LIBRARY`
option instead (CMake >= 3.15).
* `ENABLE_COMPATIBLE_LIBSNDFILE_NAME` - set DLL name to `libsndfile-1.dll` * `ENABLE_COMPATIBLE_LIBSNDFILE_NAME` - set DLL name to `libsndfile-1.dll`
(canonical name) on Windows platform, `sndfile.dll` otherwise, `OFF` by (canonical name) on Windows platform, `sndfile.dll` otherwise, `OFF` by
default. Library name can be different depending on platform. The well known default. Library name can be different depending on platform. The well known
@ -159,8 +162,16 @@ Deprecated options:
### Linking from CMake projects ### Linking from CMake projects
When `libsndfile` built and installed with `ENABLE_PACKAGE_CONFIG` option set First you need to add `FindOgg.cmake`, `FindVorbis.cmake`, `FindVorbisEnc.cmake`,
to `ON`, you can find library from your `CMakeLists.txt` with this command: `FindFLAC.cmake` and `FindOpus.cmake` files to some directory inside your CMake
project (usually `cmake`) and add it to `CMAKE_MODULE_PATH`:
project(SomeApplication)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
Now you can search `libsndfile` library from your `CMakeLists.txt`
with this command:
find_package(SndFile) find_package(SndFile)
@ -191,14 +202,14 @@ To link `libsndfile` library use:
First advice - set `ENABLE_STATIC_RUNTIME` to ON. This will remove dependencies First advice - set `ENABLE_STATIC_RUNTIME` to ON. This will remove dependencies
on runtime DLLs. on runtime DLLs.
Second advice is about Ogg, Vorbis and FLAC support. Searching external Second advice is about Ogg, Vorbis FLAC and Opus support. Searching external
libraries under Windows is a little bit tricky. The best way is to use libraries under Windows is a little bit tricky. The best way is to use
[Vcpkg](https://github.com/Microsoft/vcpkg). You need to install static libogg, [Vcpkg](https://github.com/Microsoft/vcpkg). You need to install static libogg,
libvorbis, libflac and libopus libraries: libvorbis, libflac and libopus libraries:
vcpkg install libogg:x64-windows-static libvorbis:x64-windows-static vcpkg install libogg:x64-windows-static libvorbis:x64-windows-static
libflac:x64-windows-static libogg:x86-windows-static libflac:x64-windows-static opus:x64-windows-static libogg:x86-windows-static
libvorbis:x86-windows-static libflac:x86-windows-static libvorbis:x86-windows-static libflac:x86-windows-static opus:x86-windows-static
Then and add this parameter to cmake command line: Then and add this parameter to cmake command line:

View File

@ -13,7 +13,7 @@ before_build:
- cd CMakeBuild - cd CMakeBuild
- if %platform%==Win32 set CMAKE_GENERATOR=Visual Studio 15 2017 - if %platform%==Win32 set CMAKE_GENERATOR=Visual Studio 15 2017
- if %platform%==x64 set CMAKE_GENERATOR=Visual Studio 15 2017 Win64 - if %platform%==x64 set CMAKE_GENERATOR=Visual Studio 15 2017 Win64
- cmake .. -G"%CMAKE_GENERATOR%" -DCMAKE_TOOLCHAIN_FILE=C:/Tools/vcpkg/scripts/buildsystems/vcpkg.cmake - cmake .. -G"%CMAKE_GENERATOR%" -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON -DCMAKE_TOOLCHAIN_FILE=C:/Tools/vcpkg/scripts/buildsystems/vcpkg.cmake
build: build:
project: c:\projects\libsndfile\CMakeBuild\sndfile.sln project: c:\projects\libsndfile\CMakeBuild\sndfile.sln
verbosity: minimal verbosity: minimal

View File

@ -55,7 +55,7 @@ jobs:
- task: CMake@1 - task: CMake@1
inputs: inputs:
workingDirectory: 'build' workingDirectory: 'build'
cmakeArgs: ' -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..' cmakeArgs: ' -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..'
- script: | - script: |
cd build cd build
cmake --build . cmake --build .
@ -81,7 +81,7 @@ jobs:
- task: CMake@1 - task: CMake@1
inputs: inputs:
workingDirectory: 'build' workingDirectory: 'build'
cmakeArgs: ' -G "Visual Studio 14 2015" -A x64 -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..' cmakeArgs: ' -G "Visual Studio 14 2015" -A x64 -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..'
- script: | - script: |
cd build cd build
cmake --build . cmake --build .
@ -107,7 +107,7 @@ jobs:
- task: CMake@1 - task: CMake@1
inputs: inputs:
workingDirectory: 'build' workingDirectory: 'build'
cmakeArgs: ' -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..' cmakeArgs: ' -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..'
- script: | - script: |
cd build cd build
cmake --build . cmake --build .
@ -133,7 +133,7 @@ jobs:
- task: CMake@1 - task: CMake@1
inputs: inputs:
workingDirectory: 'build' workingDirectory: 'build'
cmakeArgs: ' -G "Visual Studio 15 2017" -A x64 -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..' cmakeArgs: ' -G "Visual Studio 15 2017" -A x64 -DCMAKE_FIND_PACKAGE_PREFER_CONFIG=ON -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake ..'
- script: | - script: |
cd build cd build
cmake --build . cmake --build .

View File

@ -54,22 +54,12 @@ find_package_handle_standard_args (FLAC
if (FLAC_FOUND) if (FLAC_FOUND)
set (FLAC_INCLUDE_DIRS ${FLAC_INCLUDE_DIR}) set (FLAC_INCLUDE_DIRS ${FLAC_INCLUDE_DIR})
set (FLAC_LIBRARIES ${FLAC_LIBRARY} ${OGG_LIBRARIES}) set (FLAC_LIBRARIES ${FLAC_LIBRARY} ${OGG_LIBRARIES})
if (WIN32)
set (FLAC_LIBRARIES ${FLAC_LIBRARIES} wsock32)
get_filename_component (FLAC_LIBRARY_FILENAME ${FLAC_LIBRARY} NAME_WE)
if (FLAC_LIBRARY_FILENAME MATCHES "libFLAC_static")
set (FLAC_DEFINITIONS -DFLAC__NO_DLL)
endif ()
endif ()
if (NOT TARGET FLAC::FLAC) if (NOT TARGET FLAC::FLAC)
add_library(FLAC::FLAC UNKNOWN IMPORTED) add_library(FLAC::FLAC UNKNOWN IMPORTED)
set_target_properties(FLAC::FLAC PROPERTIES set_target_properties(FLAC::FLAC PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${FLAC_INCLUDE_DIR}" INTERFACE_INCLUDE_DIRECTORIES "${FLAC_INCLUDE_DIR}"
IMPORTED_LOCATION "${FLAC_LIBRARY}" IMPORTED_LOCATION "${FLAC_LIBRARY}"
INTERFACE_LINK_LIBRARIES INTERFACE_LINK_LIBRARIES Ogg::ogg
Ogg::Ogg
$<$<BOOL:${WIN32}>:wsock32>
INTERFACE_COMPILE_DEFINITIONS ${FLAC_DEFINITIONS}
) )
endif () endif ()
endif () endif ()

View File

@ -49,9 +49,9 @@ if (OGG_FOUND)
set (OGG_LIBRARIES ${OGG_LIBRARY}) set (OGG_LIBRARIES ${OGG_LIBRARY})
set (OGG_INCLUDE_DIRS ${OGG_INCLUDE_DIR}) set (OGG_INCLUDE_DIRS ${OGG_INCLUDE_DIR})
if(NOT TARGET Ogg::Ogg) if(NOT TARGET Ogg::ogg)
add_library(Ogg::Ogg UNKNOWN IMPORTED) add_library(Ogg::ogg UNKNOWN IMPORTED)
set_target_properties(Ogg::Ogg PROPERTIES set_target_properties(Ogg::ogg PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${OGG_INCLUDE_DIRS}" INTERFACE_INCLUDE_DIRECTORIES "${OGG_INCLUDE_DIRS}"
IMPORTED_LOCATION "${OGG_LIBRARIES}" IMPORTED_LOCATION "${OGG_LIBRARIES}"
) )

View File

@ -55,9 +55,9 @@ if (OPUS_FOUND)
set (OPUS_LIBRARIES ${OPUS_LIBRARY}) set (OPUS_LIBRARIES ${OPUS_LIBRARY})
set (OPUS_INCLUDE_DIRS ${OPUS_INCLUDE_DIR}) set (OPUS_INCLUDE_DIRS ${OPUS_INCLUDE_DIR})
if (NOT TARGET Opus::Opus) if (NOT TARGET Opus::opus)
add_library (Opus::Opus UNKNOWN IMPORTED) add_library (Opus::opus UNKNOWN IMPORTED)
set_target_properties (Opus::Opus PROPERTIES set_target_properties (Opus::opus PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${OPUS_INCLUDE_DIRS}" INTERFACE_INCLUDE_DIRECTORIES "${OPUS_INCLUDE_DIRS}"
IMPORTED_LOCATION "${OPUS_LIBRARIES}" IMPORTED_LOCATION "${OPUS_LIBRARIES}"
) )

View File

@ -56,7 +56,7 @@ if (VORBIS_FOUND)
set_target_properties (Vorbis::Vorbis PROPERTIES set_target_properties (Vorbis::Vorbis PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${VORBIS_INCLUDE_DIR}" INTERFACE_INCLUDE_DIRECTORIES "${VORBIS_INCLUDE_DIR}"
IMPORTED_LOCATION "${VORBIS_LIBRARY}" IMPORTED_LOCATION "${VORBIS_LIBRARY}"
INTERFACE_LINK_LIBRARIES Ogg::Ogg INTERFACE_LINK_LIBRARIES Ogg::ogg
) )
endif () endif ()
endif () endif ()

View File

@ -27,6 +27,7 @@ if (NOT WIN32)
find_package (Sndio) find_package (Sndio)
endif () endif ()
find_package(Ogg 1.3)
find_package (VorbisEnc) find_package (VorbisEnc)
find_package (FLAC) find_package (FLAC)
find_package (Opus) find_package (Opus)

View File

@ -0,0 +1,30 @@
set(SndFile_VERSION @PROJECT_VERSION@)
set(SndFile_VERSION_MAJOR @PROJECT_VERSION_MAJOR@)
set(SndFile_VERSION_MINOR @PROJECT_VERSION_MINOR@)
set(SndFile_VERSION_PATCH @PROJECT_VERSION_PATCH@)
set (SndFile_WITH_EXTERNAL_LIBS @SndFile_WITH_EXTERNAL_LIBS@)
@PACKAGE_INIT@
include (CMakeFindDependencyMacro)
find_dependency (Ogg 1.3)
find_dependency (VorbisEnc)
find_dependency (FLAC)
find_dependency (Opus)
include (${CMAKE_CURRENT_LIST_DIR}/SndFileTargets.cmake)
set_and_check (SndFile_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@")
set (SNDFILE_INCLUDE_DIR ${SndFile_INCLUDE_DIR})
set (SndFile_LIBRARY SndFile::sndfile)
set (SNDFILE_LIBRARY SndFile::sndfile)
set (SndFile_LIBRARIES SndFile::sndfile)
set (SNDFILE_LIBRARIES SndFile::sndfile)
check_required_components(SndFile)
set (SNDFILE_FOUND 1)