libogg 1.3.6 and libvorbis 1.3.7 declare cmake_minimum_required below 3.5, which CMake 4 rejects. ziran_audio_vorbis sets CMAKE_POLICY_VERSION_MINIMUM 3.5 for the fetched projects only; CMake 3 ignores it.
77 lines
3.3 KiB
CMake
77 lines
3.3 KiB
CMake
# Native MP3 implementation for std/audio_mp3. No UI or audio package needed.
|
|
include_guard(GLOBAL)
|
|
function(ziran_audio_mp3)
|
|
if(TARGET ziran_mp3)
|
|
return()
|
|
endif()
|
|
set(dr_mp3_dir "${CMAKE_CURRENT_BINARY_DIR}/dr_mp3")
|
|
file(MAKE_DIRECTORY "${dr_mp3_dir}")
|
|
set(dr_mp3_ref f12e197fd462e4c22873fbd70d11b1d2f2c1dc3c)
|
|
foreach(file IN ITEMS dr_mp3.h LICENSE)
|
|
if(file STREQUAL "dr_mp3.h")
|
|
set(hash 7a6cc38b0afdb55cfc9eec991d9169d67beb8fd910be2995121510cdaa99c356)
|
|
else()
|
|
set(hash dd1c647e6f767f8ff4b2dfae0fed314726600a01e0cf1ef556afddd5fa96ff15)
|
|
endif()
|
|
if(EXISTS "${dr_mp3_dir}/${file}")
|
|
file(SHA256 "${dr_mp3_dir}/${file}" actual)
|
|
else()
|
|
set(actual "")
|
|
endif()
|
|
if(NOT actual STREQUAL hash)
|
|
file(DOWNLOAD "https://raw.githubusercontent.com/mackron/dr_libs/${dr_mp3_ref}/${file}"
|
|
"${dr_mp3_dir}/${file}" EXPECTED_HASH SHA256=${hash} TLS_VERIFY ON)
|
|
endif()
|
|
endforeach()
|
|
add_library(ziran_mp3 STATIC "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/ziran_mp3.c")
|
|
target_include_directories(ziran_mp3 PRIVATE "${dr_mp3_dir}")
|
|
target_compile_options(ziran_mp3 PRIVATE -ffunction-sections -fdata-sections)
|
|
set_target_properties(ziran_mp3 PROPERTIES POSITION_INDEPENDENT_CODE ON C_STANDARD 99)
|
|
endfunction()
|
|
|
|
# Reusable native Vorbis decoder. Callers link vorbisfile; its public target
|
|
# dependencies supply vorbis and ogg. Sources and licenses stay in the build.
|
|
function(ziran_audio_vorbis)
|
|
if(TARGET vorbisfile)
|
|
return()
|
|
endif()
|
|
include(FetchContent)
|
|
if(POLICY CMP0135)
|
|
cmake_policy(SET CMP0135 NEW)
|
|
endif()
|
|
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
|
|
set(CMAKE_POLICY_VERSION_MINIMUM 3.5)
|
|
set(BUILD_SHARED_LIBS OFF)
|
|
set(BUILD_TESTING OFF)
|
|
set(INSTALL_DOCS OFF)
|
|
set(INSTALL_PKG_CONFIG_MODULE OFF)
|
|
set(INSTALL_CMAKE_PACKAGE_MODULE OFF)
|
|
FetchContent_Declare(ziran_core_ogg
|
|
URL https://downloads.xiph.org/releases/ogg/libogg-1.3.6.tar.xz
|
|
URL_HASH SHA256=5c8253428e181840cd20d41f3ca16557a9cc04bad4a3d04cce84808677fa1061)
|
|
FetchContent_MakeAvailable(ziran_core_ogg)
|
|
# Vorbis's FindOgg must use this target, including generated config_types.h,
|
|
# rather than a host library when cross compiling.
|
|
if(NOT TARGET Ogg::ogg)
|
|
add_library(Ogg::ogg ALIAS ogg)
|
|
endif()
|
|
set(OGG_INCLUDE_DIR "${ziran_core_ogg_SOURCE_DIR}/include")
|
|
set(OGG_LIBRARY ogg)
|
|
FetchContent_Declare(ziran_core_vorbis
|
|
URL https://downloads.xiph.org/releases/vorbis/libvorbis-1.3.7.tar.xz
|
|
URL_HASH SHA256=b33cc4934322bcbf6efcbacf49e3ca01aadbea4114ec9589d1b1e9d20f72954b)
|
|
FetchContent_MakeAvailable(ziran_core_vorbis)
|
|
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
|
|
# Upstream libvorbis 1.3.7 sharedbook.c:425 shifts signed codebook bits.
|
|
target_compile_options(vorbis PRIVATE -fno-sanitize=shift)
|
|
endif()
|
|
set_target_properties(vorbisenc PROPERTIES EXCLUDE_FROM_ALL TRUE)
|
|
foreach(target IN ITEMS ogg vorbis vorbisfile)
|
|
set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON
|
|
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
|
|
if(NOT MSVC)
|
|
target_compile_options(${target} PRIVATE -ffunction-sections -fdata-sections)
|
|
endif()
|
|
endforeach()
|
|
endfunction()
|