vgmstream/cmake/FetchDependency.cmake
KatieFrogs bf89c3503d Cmake: Fixes for dependencies
- Sets dependencies for targets
  - Fixes not being able to build single targets, like `make vgmstream_cli`
  - Fixes not being able to build with multiple jobs, like `make -j 5`
- Compiles dependencies out of the source directory
- Fixes double lib suffix, renaming liblibvgmstream.a to libvgmstream.a
- Removes zlib dependency from static builds, previously required by ffmpeg
- If ARCHIVE_EXTRACT is not available in cmake, mpg123 will be downloaded through svn, then fall back to git if that is not available either
2021-10-02 09:02:27 +03:00

111 lines
3.1 KiB
CMake

if(NOT WIN32)
find_package(Git QUIET)
find_package(Subversion QUIET)
if(Git_FOUND OR Subversion_FOUND)
include(FetchContent)
endif()
endif()
function(FetchDependency name)
set(value_args
DIR
GIT_REPOSITORY
GIT_TAG
GIT_UNSHALLOW
SVN_REPOSITORY
SVN_REVISION
FILE_DOWNLOAD
FILE_SUBDIR
)
set(multi_value_args
FETCH_PRIORITY
)
cmake_parse_arguments(ARGS "" "${value_args}" "${multi_value_args}" ${ARGN})
if(NOT ARGS_DIR)
set(ARGS_DIR ${name})
endif()
if(NOT ARGS_FETCH_PRIORITY)
set(ARGS_FETCH_PRIORITY git svn file)
endif()
set(${name}_BIN ${VGM_BINARY_DIR}/dependencies/${ARGS_DIR})
set(${name}_BIN ${${name}_BIN} PARENT_SCOPE)
if(${name}_PATH)
if(IS_DIRECTORY "${${name}_PATH}")
set(${name}_SOURCE "(local path)" PARENT_SCOPE)
else()
message(FATAL_ERROR "The provided path to ${ARGS_DIR} does not exist (Use ${name}_PATH)")
endif()
elseif((Subversion_FOUND AND ARGS_SVN_REPOSITORY) OR (Git_FOUND AND ARGS_GIT_REPOSITORY) OR (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.18.0" AND ARGS_FILE_DOWNLOAD))
set(${name}_PATH ${VGM_SOURCE_DIR}/dependencies/${ARGS_DIR})
set(${name}_PATH ${${name}_PATH} PARENT_SCOPE)
set(${name}_SOURCE "(download)" PARENT_SCOPE)
foreach(CURRENT_FETCH ${ARGS_FETCH_PRIORITY})
if(CURRENT_FETCH STREQUAL "git" AND Git_FOUND AND ARGS_GIT_REPOSITORY)
if(ARGS_GIT_UNSHALLOW)
set(shallow OFF)
else()
set(shallow ON)
endif()
FetchContent_Declare(${name}_FETCH
GIT_REPOSITORY ${ARGS_GIT_REPOSITORY}
SOURCE_DIR ${${name}_PATH}
BINARY_DIR ${${name}_BIN}
SUBBUILD_DIR ${${name}_BIN}-sub
GIT_TAG ${ARGS_GIT_TAG}
GIT_SHALLOW ${shallow}
)
FetchContent_GetProperties(${name}_FETCH)
if(NOT ${name}_FETCH_POPULATED)
message("Downloading ${ARGS_DIR} (git)...")
FetchContent_Populate(${name}_FETCH)
endif()
break()
elseif(CURRENT_FETCH STREQUAL "svn" AND Subversion_FOUND AND ARGS_SVN_REPOSITORY)
FetchContent_Declare(${name}_FETCH
SVN_REPOSITORY ${ARGS_SVN_REPOSITORY}
SOURCE_DIR ${${name}_PATH}
BINARY_DIR ${${name}_BIN}
SUBBUILD_DIR ${${name}_BIN}-sub
SVN_REVISION ${ARGS_SVN_REVISION}
)
FetchContent_GetProperties(${name}_FETCH)
if(NOT ${name}_FETCH_POPULATED)
message("Downloading ${ARGS_DIR} (svn)...")
FetchContent_Populate(${name}_FETCH)
endif()
break()
elseif(CURRENT_FETCH STREQUAL "file" AND ${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.18.0" AND ARGS_FILE_DOWNLOAD)
# no ARCHIVE_EXTRACT until 3.18
string(REGEX REPLACE ".*/" "" FILE ${ARGS_FILE_DOWNLOAD})
if(NOT EXISTS ${${name}_PATH}/${FILE})
message("Downloading ${ARGS_DIR} (file)...")
file(DOWNLOAD
${ARGS_FILE_DOWNLOAD}
${${name}_PATH}/${FILE}
)
file(ARCHIVE_EXTRACT
INPUT ${${name}_PATH}/${FILE}
DESTINATION ${${name}_PATH}
)
endif()
if(ARGS_FILE_SUBDIR)
set(${name}_PATH ${${name}_PATH}/${ARGS_FILE_SUBDIR} PARENT_SCOPE)
endif()
break()
endif()
endforeach()
else()
set(${name}_PATH "" PARENT_SCOPE)
set(USE_${name} OFF PARENT_SCOPE)
endif()
endfunction()