diff --git a/.github/workflows/build-tests.yml b/.github/workflows/build-tests.yml index 846a7b4..6842a63 100644 --- a/.github/workflows/build-tests.yml +++ b/.github/workflows/build-tests.yml @@ -10,19 +10,45 @@ jobs: name: Build Tests runs-on: ${{ matrix.os }} strategy: + fail-fast: false matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] include: - os: ubuntu-latest name: linux + runTest: true testExe: build/sst-effects-test + - os: macos-latest - name: mac + name: mac-x86 + runTest: true testExe: build/sst-effects-test + cmakeArgs: -DCMAKE_OSX_ARCHITECTURES=x86_64 + + - os: macos-latest + name: mac-arm + cmakeArgs: -DCMAKE_OSX_ARCHITECTURES=arm64 + + - os: macos-latest + name: mac-arm-nonative + cmakeArgs: -DCMAKE_OSX_ARCHITECTURES=arm64 -DSST_BASIC_BLOCKS_SIMD_OMIT_NATIVE_ALIASES=TRUE + - os: windows-latest - name: win + name: win-x86 + runTest: true testExe: build/Release/sst-effects-test.exe + - os: windows-latest + name: win-arm64 + cmakeArgs: -G"Visual Studio 17 2022" -A arm64 -DCMAKE_SYSTEM_VERSION=10 + + - os: windows-latest + name: win-arm64ec + cmakeArgs: -G"Visual Studio 17 2022" -A arm64ec -DCMAKE_SYSTEM_VERSION=10 + + - os: windows-latest + name: win-arm64-non-native + cmakeArgs: -G"Visual Studio 17 2022" -A arm64 -DCMAKE_SYSTEM_VERSION=10 -DSST_BASIC_BLOCKS_SIMD_OMIT_NATIVE_ALIASES=TRUE + steps: - name: Checkout code @@ -32,10 +58,11 @@ jobs: - name: Build Smoke test run: | - cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Release -DSST_EFFECTS_BUILD_TESTS=TRUE -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" + cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Release -DSST_EFFECTS_BUILD_TESTS=TRUE ${{ matrix.cmakeArgs }} cmake --build ./build --config Release - name: Run Smoke Test + if: ${{ matrix.runTest }} run: | ls ${{ matrix.testExe }} ${{ matrix.testExe }} diff --git a/CMakeLists.txt b/CMakeLists.txt index e1e7d53..2c8c2a0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,15 @@ if (${SST_EFFECTS_BUILD_TESTS}) message(STATUS "Building tests") include(cmake/CPM.cmake) + if (NOT TARGET simde) + CPMAddPackage(NAME simde + GITHUB_REPOSITORY simd-everywhere/simde + VERSION 0.7.2 + ) + add_library(simde INTERFACE) + target_include_directories(simde INTERFACE ${simde_SOURCE_DIR}) + endif () + if (NOT TARGET sst-basic-blocks) CPMAddPackage(NAME sst-basic-blocks GITHUB_REPOSITORY surge-synthesizer/sst-basic-blocks @@ -53,14 +62,6 @@ if (${SST_EFFECTS_BUILD_TESTS}) ) endif () - if (NOT TARGET simde) - CPMAddPackage(NAME simde - GITHUB_REPOSITORY simd-everywhere/simde - VERSION 0.7.2 - ) - add_library(simde INTERFACE) - target_include_directories(simde INTERFACE ${simde_SOURCE_DIR}) - endif () add_executable(${PROJECT_NAME}-test tests/sst-effects-test.cpp diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index eaa397b..8269a8b 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -1,32 +1,1269 @@ -set(CPM_DOWNLOAD_VERSION 0.36.0) +# CPM.cmake - CMake's missing package manager +# =========================================== +# See https://github.com/cpm-cmake/CPM.cmake for usage and update instructions. +# +# MIT License +# ----------- +#[[ + Copyright (c) 2019-2023 Lars Melchior and contributors -if(CPM_SOURCE_CACHE) - set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -elseif(DEFINED ENV{CPM_SOURCE_CACHE}) - set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +]] + +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +# Initialize logging prefix +if(NOT CPM_INDENT) + set(CPM_INDENT + "CPM:" + CACHE INTERNAL "" + ) +endif() + +if(NOT COMMAND cpm_message) + function(cpm_message) + message(${ARGV}) + endfunction() +endif() + +set(CURRENT_CPM_VERSION 0.40.2) + +get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH) +if(CPM_DIRECTORY) + if(NOT CPM_DIRECTORY STREQUAL CPM_CURRENT_DIRECTORY) + if(CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) + message( + AUTHOR_WARNING + "${CPM_INDENT} \ +A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ +It is recommended to upgrade CPM to the most recent version. \ +See https://github.com/cpm-cmake/CPM.cmake for more information." + ) + endif() + if(${CMAKE_VERSION} VERSION_LESS "3.17.0") + include(FetchContent) + endif() + return() + endif() + + get_property( + CPM_INITIALIZED GLOBAL "" + PROPERTY CPM_INITIALIZED + SET + ) + if(CPM_INITIALIZED) + return() + endif() +endif() + +if(CURRENT_CPM_VERSION MATCHES "development-version") + message( + WARNING "${CPM_INDENT} Your project is using an unstable development version of CPM.cmake. \ +Please update to a recent release if possible. \ +See https://github.com/cpm-cmake/CPM.cmake for details." + ) +endif() + +set_property(GLOBAL PROPERTY CPM_INITIALIZED true) + +macro(cpm_set_policies) + # the policy allows us to change options without caching + cmake_policy(SET CMP0077 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) + + # the policy allows us to change set(CACHE) without caching + if(POLICY CMP0126) + cmake_policy(SET CMP0126 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) + endif() + + # The policy uses the download time for timestamp, instead of the timestamp in the archive. This + # allows for proper rebuilds when a projects url changes + if(POLICY CMP0135) + cmake_policy(SET CMP0135 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0135 NEW) + endif() + + # treat relative git repository paths as being relative to the parent project's remote + if(POLICY CMP0150) + cmake_policy(SET CMP0150 NEW) + set(CMAKE_POLICY_DEFAULT_CMP0150 NEW) + endif() +endmacro() +cpm_set_policies() + +option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" + $ENV{CPM_USE_LOCAL_PACKAGES} +) +option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" + $ENV{CPM_LOCAL_PACKAGES_ONLY} +) +option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) +option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" + $ENV{CPM_DONT_UPDATE_MODULE_PATH} +) +option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" + $ENV{CPM_DONT_CREATE_PACKAGE_LOCK} +) +option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK + "Add all packages added through CPM.cmake to the package lock" + $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK} +) +option(CPM_USE_NAMED_CACHE_DIRECTORIES + "Use additional directory of package name in cache on the most nested level." + $ENV{CPM_USE_NAMED_CACHE_DIRECTORIES} +) + +set(CPM_VERSION + ${CURRENT_CPM_VERSION} + CACHE INTERNAL "" +) +set(CPM_DIRECTORY + ${CPM_CURRENT_DIRECTORY} + CACHE INTERNAL "" +) +set(CPM_FILE + ${CMAKE_CURRENT_LIST_FILE} + CACHE INTERNAL "" +) +set(CPM_PACKAGES + "" + CACHE INTERNAL "" +) +set(CPM_DRY_RUN + OFF + CACHE INTERNAL "Don't download or configure dependencies (for testing)" +) + +if(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) else() - set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") + set(CPM_SOURCE_CACHE_DEFAULT OFF) endif() -# Expand relative path. This is important if the provided path contains a tilde (~) -get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) +set(CPM_SOURCE_CACHE + ${CPM_SOURCE_CACHE_DEFAULT} + CACHE PATH "Directory to download CPM dependencies" +) + +if(NOT CPM_DONT_UPDATE_MODULE_PATH) + set(CPM_MODULE_PATH + "${CMAKE_BINARY_DIR}/CPM_modules" + CACHE INTERNAL "" + ) + # remove old modules + file(REMOVE_RECURSE ${CPM_MODULE_PATH}) + file(MAKE_DIRECTORY ${CPM_MODULE_PATH}) + # locally added CPM modules should override global packages + set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") +endif() -function(download_cpm) - message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") - file(DOWNLOAD - https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake - ${CPM_DOWNLOAD_LOCATION} +if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + set(CPM_PACKAGE_LOCK_FILE + "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" + CACHE INTERNAL "" ) + file(WRITE ${CPM_PACKAGE_LOCK_FILE} + "# CPM Package Lock\n# This file should be committed to version control\n\n" + ) +endif() + +include(FetchContent) + +# Try to infer package name from git repository uri (path or url) +function(cpm_package_name_from_git_uri URI RESULT) + if("${URI}" MATCHES "([^/:]+)/?.git/?$") + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + else() + unset(${RESULT} PARENT_SCOPE) + endif() endfunction() -if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) - download_cpm() -else() - # resume download if it previously failed - file(READ ${CPM_DOWNLOAD_LOCATION} check) - if("${check}" STREQUAL "") - download_cpm() +# Try to infer package name and version from a url +function(cpm_package_name_and_ver_from_url url outName outVer) + if(url MATCHES "[/\\?]([a-zA-Z0-9_\\.-]+)\\.(tar|tar\\.gz|tar\\.bz2|zip|ZIP)(\\?|/|$)") + # We matched an archive + set(filename "${CMAKE_MATCH_1}") + + if(filename MATCHES "([a-zA-Z0-9_\\.-]+)[_-]v?(([0-9]+\\.)*[0-9]+[a-zA-Z0-9]*)") + # We matched - (ie foo-1.2.3) + set(${outName} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + set(${outVer} + "${CMAKE_MATCH_2}" + PARENT_SCOPE + ) + elseif(filename MATCHES "(([0-9]+\\.)+[0-9]+[a-zA-Z0-9]*)") + # We couldn't find a name, but we found a version + # + # In many cases (which we don't handle here) the url would look something like + # `irrelevant/ACTUAL_PACKAGE_NAME/irrelevant/1.2.3.zip`. In such a case we can't possibly + # distinguish the package name from the irrelevant bits. Moreover if we try to match the + # package name from the filename, we'd get bogus at best. + unset(${outName} PARENT_SCOPE) + set(${outVer} + "${CMAKE_MATCH_1}" + PARENT_SCOPE + ) + else() + # Boldly assume that the file name is the package name. + # + # Yes, something like `irrelevant/ACTUAL_NAME/irrelevant/download.zip` will ruin our day, but + # such cases should be quite rare. No popular service does this... we think. + set(${outName} + "${filename}" + PARENT_SCOPE + ) + unset(${outVer} PARENT_SCOPE) + endif() + else() + # No ideas yet what to do with non-archives + unset(${outName} PARENT_SCOPE) + unset(${outVer} PARENT_SCOPE) + endif() +endfunction() + +function(cpm_find_package NAME VERSION) + string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") + find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) + if(${CPM_ARGS_NAME}_FOUND) + if(DEFINED ${CPM_ARGS_NAME}_VERSION) + set(VERSION ${${CPM_ARGS_NAME}_VERSION}) + endif() + cpm_message(STATUS "${CPM_INDENT} Using local package ${CPM_ARGS_NAME}@${VERSION}") + CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") + set(CPM_PACKAGE_FOUND + YES + PARENT_SCOPE + ) + else() + set(CPM_PACKAGE_FOUND + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Create a custom FindXXX.cmake module for a CPM package This prevents `find_package(NAME)` from +# finding the system library +function(cpm_create_module_file Name) + if(NOT CPM_DONT_UPDATE_MODULE_PATH) + # erase any previous modules + file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake + "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)" + ) + endif() +endfunction() + +# Find a package locally or fallback to CPMAddPackage +function(CPMFindPackage) + set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + set(downloadPackage ${CPM_DOWNLOAD_ALL}) + if(DEFINED CPM_DOWNLOAD_${CPM_ARGS_NAME}) + set(downloadPackage ${CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + elseif(DEFINED ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + set(downloadPackage $ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) + endif() + if(downloadPackage) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(NOT CPM_PACKAGE_FOUND) + CPMAddPackage(${ARGN}) + cpm_export_variables(${CPM_ARGS_NAME}) + endif() + +endfunction() + +# checks if a package has been added before +function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION) + if("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) + CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) + if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}") + message( + WARNING + "${CPM_INDENT} Requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})." + ) + endif() + cpm_get_fetch_properties(${CPM_ARGS_NAME}) + set(${CPM_ARGS_NAME}_ADDED NO) + set(CPM_PACKAGE_ALREADY_ADDED + YES + PARENT_SCOPE + ) + cpm_export_variables(${CPM_ARGS_NAME}) + else() + set(CPM_PACKAGE_ALREADY_ADDED + NO + PARENT_SCOPE + ) + endif() +endfunction() + +# Parse the argument of CPMAddPackage in case a single one was provided and convert it to a list of +# arguments which can then be parsed idiomatically. For example gh:foo/bar@1.2.3 will be converted +# to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 +function(cpm_parse_add_package_single_arg arg outArgs) + # Look for a scheme + if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") + string(TOLOWER "${CMAKE_MATCH_1}" scheme) + set(uri "${CMAKE_MATCH_2}") + + # Check for CPM-specific schemes + if(scheme STREQUAL "gh") + set(out "GITHUB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "gl") + set(out "GITLAB_REPOSITORY;${uri}") + set(packageType "git") + elseif(scheme STREQUAL "bb") + set(out "BITBUCKET_REPOSITORY;${uri}") + set(packageType "git") + # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine + # type + elseif(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Fall back to a URL + set(out "URL;${arg}") + set(packageType "archive") + + # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. + # We just won't bother with the additional complexity it will induce in this function. SVN is + # done by multi-arg + endif() + else() + if(arg MATCHES ".git/?(@|#|$)") + set(out "GIT_REPOSITORY;${arg}") + set(packageType "git") + else() + # Give up + message(FATAL_ERROR "${CPM_INDENT} Can't determine package type of '${arg}'") + endif() + endif() + + # For all packages we interpret @... as version. Only replace the last occurrence. Thus URIs + # containing '@' can be used + string(REGEX REPLACE "@([^@]+)$" ";VERSION;\\1" out "${out}") + + # Parse the rest according to package type + if(packageType STREQUAL "git") + # For git repos we interpret #... as a tag or branch or commit hash + string(REGEX REPLACE "#([^#]+)$" ";GIT_TAG;\\1" out "${out}") + elseif(packageType STREQUAL "archive") + # For archives we interpret #... as a URL hash. + string(REGEX REPLACE "#([^#]+)$" ";URL_HASH;\\1" out "${out}") + # We don't try to parse the version if it's not provided explicitly. cpm_get_version_from_url + # should do this at a later point + else() + # We should never get here. This is an assertion and hitting it means there's a problem with the + # code above. A packageType was set, but not handled by this if-else. + message(FATAL_ERROR "${CPM_INDENT} Unsupported package type '${packageType}' of '${arg}'") + endif() + + set(${outArgs} + ${out} + PARENT_SCOPE + ) +endfunction() + +# Check that the working directory for a git repo is clean +function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) + + find_package(Git REQUIRED) + + if(NOT GIT_EXECUTABLE) + # No git executable, assume directory is clean + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + # check for uncommitted changes + execute_process( + COMMAND ${GIT_EXECUTABLE} status --porcelain + RESULT_VARIABLE resultGitStatus + OUTPUT_VARIABLE repoStatus + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET + WORKING_DIRECTORY ${repoPath} + ) + if(resultGitStatus) + # not supposed to happen, assume clean anyway + message(WARNING "${CPM_INDENT} Calling git status on folder ${repoPath} failed") + set(${isClean} + TRUE + PARENT_SCOPE + ) + return() + endif() + + if(NOT "${repoStatus}" STREQUAL "") + set(${isClean} + FALSE + PARENT_SCOPE + ) + return() + endif() + + # check for committed changes + execute_process( + COMMAND ${GIT_EXECUTABLE} diff -s --exit-code ${gitTag} + RESULT_VARIABLE resultGitDiff + OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_QUIET + WORKING_DIRECTORY ${repoPath} + ) + + if(${resultGitDiff} EQUAL 0) + set(${isClean} + TRUE + PARENT_SCOPE + ) + else() + set(${isClean} + FALSE + PARENT_SCOPE + ) + endif() + +endfunction() + +# Add PATCH_COMMAND to CPM_ARGS_UNPARSED_ARGUMENTS. This method consumes a list of files in ARGN +# then generates a `PATCH_COMMAND` appropriate for `ExternalProject_Add()`. This command is appended +# to the parent scope's `CPM_ARGS_UNPARSED_ARGUMENTS`. +function(cpm_add_patches) + # Return if no patch files are supplied. + if(NOT ARGN) + return() + endif() + + # Find the patch program. + find_program(PATCH_EXECUTABLE patch) + if(WIN32 AND NOT PATCH_EXECUTABLE) + # The Windows git executable is distributed with patch.exe. Find the path to the executable, if + # it exists, then search `../usr/bin` and `../../usr/bin` for patch.exe. + find_package(Git QUIET) + if(GIT_EXECUTABLE) + get_filename_component(extra_search_path ${GIT_EXECUTABLE} DIRECTORY) + get_filename_component(extra_search_path_1up ${extra_search_path} DIRECTORY) + get_filename_component(extra_search_path_2up ${extra_search_path_1up} DIRECTORY) + find_program( + PATCH_EXECUTABLE patch HINTS "${extra_search_path_1up}/usr/bin" + "${extra_search_path_2up}/usr/bin" + ) + endif() + endif() + if(NOT PATCH_EXECUTABLE) + message(FATAL_ERROR "Couldn't find `patch` executable to use with PATCHES keyword.") + endif() + + # Create a temporary + set(temp_list ${CPM_ARGS_UNPARSED_ARGUMENTS}) + + # Ensure each file exists (or error out) and add it to the list. + set(first_item True) + foreach(PATCH_FILE ${ARGN}) + # Make sure the patch file exists, if we can't find it, try again in the current directory. + if(NOT EXISTS "${PATCH_FILE}") + if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") + message(FATAL_ERROR "Couldn't find patch file: '${PATCH_FILE}'") + endif() + set(PATCH_FILE "${CMAKE_CURRENT_LIST_DIR}/${PATCH_FILE}") + endif() + + # Convert to absolute path for use with patch file command. + get_filename_component(PATCH_FILE "${PATCH_FILE}" ABSOLUTE) + + # The first patch entry must be preceded by "PATCH_COMMAND" while the following items are + # preceded by "&&". + if(first_item) + set(first_item False) + list(APPEND temp_list "PATCH_COMMAND") + else() + list(APPEND temp_list "&&") + endif() + # Add the patch command to the list + list(APPEND temp_list "${PATCH_EXECUTABLE}" "-p1" "<" "${PATCH_FILE}") + endforeach() + + # Move temp out into parent scope. + set(CPM_ARGS_UNPARSED_ARGUMENTS + ${temp_list} + PARENT_SCOPE + ) + +endfunction() + +# method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload +# FetchContent calls. As these are internal cmake properties, this method should be used carefully +# and may need modification in future CMake versions. Source: +# https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake#L1152 +function(cpm_override_fetchcontent contentName) + cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "") + if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "") + message(FATAL_ERROR "${CPM_INDENT} Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}") + endif() + + string(TOLOWER ${contentName} contentNameLower) + set(prefix "_FetchContent_${contentNameLower}") + + set(propertyName "${prefix}_sourceDir") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}") + + set(propertyName "${prefix}_binaryDir") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}") + + set(propertyName "${prefix}_populated") + define_property( + GLOBAL + PROPERTY ${propertyName} + BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" + FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" + ) + set_property(GLOBAL PROPERTY ${propertyName} TRUE) +endfunction() + +# Download and add a package from source +function(CPMAddPackage) + cpm_set_policies() + + list(LENGTH ARGN argnLength) + if(argnLength EQUAL 1) + cpm_parse_add_package_single_arg("${ARGN}" ARGN) + + # The shorthand syntax implies EXCLUDE_FROM_ALL and SYSTEM + set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES;SYSTEM;YES;") + endif() + + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + FIND_PACKAGE_ARGUMENTS + NO_CACHE + SYSTEM + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + CUSTOM_CACHE_KEY + ) + + set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND PATCHES) + + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") + + # Set default values for arguments + + if(NOT DEFINED CPM_ARGS_VERSION) + if(DEFINED CPM_ARGS_GIT_TAG) + cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) + endif() + endif() + + if(CPM_ARGS_DOWNLOAD_ONLY) + set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) + else() + set(DOWNLOAD_ONLY NO) + endif() + + if(DEFINED CPM_ARGS_GITHUB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_GITLAB_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") + elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) + set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") endif() -endif() -include(${CPM_DOWNLOAD_LOCATION}) + if(DEFINED CPM_ARGS_GIT_REPOSITORY) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) + if(NOT DEFINED CPM_ARGS_GIT_TAG) + set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) + endif() + + # If a name wasn't provided, try to infer it from the git repo + if(NOT DEFINED CPM_ARGS_NAME) + cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME) + endif() + endif() + + set(CPM_SKIP_FETCH FALSE) + + if(DEFINED CPM_ARGS_GIT_TAG) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) + # If GIT_SHALLOW is explicitly specified, honor the value. + if(DEFINED CPM_ARGS_GIT_SHALLOW) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW}) + endif() + endif() + + if(DEFINED CPM_ARGS_URL) + # If a name or version aren't provided, try to infer them from the URL + list(GET CPM_ARGS_URL 0 firstUrl) + cpm_package_name_and_ver_from_url(${firstUrl} nameFromUrl verFromUrl) + # If we fail to obtain name and version from the first URL, we could try other URLs if any. + # However multiple URLs are expected to be quite rare, so for now we won't bother. + + # If the caller provided their own name and version, they trump the inferred ones. + if(NOT DEFINED CPM_ARGS_NAME) + set(CPM_ARGS_NAME ${nameFromUrl}) + endif() + if(NOT DEFINED CPM_ARGS_VERSION) + set(CPM_ARGS_VERSION ${verFromUrl}) + endif() + + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}") + endif() + + # Check for required arguments + + if(NOT DEFINED CPM_ARGS_NAME) + message( + FATAL_ERROR + "${CPM_INDENT} 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'" + ) + endif() + + # Check if package has been added before + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + if(CPM_PACKAGE_ALREADY_ADDED) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for manual overrides + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") + set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) + set(CPM_${CPM_ARGS_NAME}_SOURCE "") + CPMAddPackage( + NAME "${CPM_ARGS_NAME}" + SOURCE_DIR "${PACKAGE_SOURCE}" + EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" + SYSTEM "${CPM_ARGS_SYSTEM}" + PATCHES "${CPM_ARGS_PATCHES}" + OPTIONS "${CPM_ARGS_OPTIONS}" + SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" + DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" + FORCE True + ) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + # Check for available declaration + if(NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") + set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) + set(CPM_DECLARATION_${CPM_ARGS_NAME} "") + CPMAddPackage(${declaration}) + cpm_export_variables(${CPM_ARGS_NAME}) + # checking again to ensure version and option compatibility + cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") + return() + endif() + + if(NOT CPM_ARGS_FORCE) + if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) + cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) + + if(CPM_PACKAGE_FOUND) + cpm_export_variables(${CPM_ARGS_NAME}) + return() + endif() + + if(CPM_LOCAL_PACKAGES_ONLY) + message( + SEND_ERROR + "${CPM_INDENT} ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})" + ) + endif() + endif() + endif() + + CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") + + if(DEFINED CPM_ARGS_GIT_TAG) + set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") + else() + set(PACKAGE_INFO "${CPM_ARGS_VERSION}") + endif() + + if(DEFINED FETCHCONTENT_BASE_DIR) + # respect user's FETCHCONTENT_BASE_DIR if set + set(CPM_FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR}) + else() + set(CPM_FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps) + endif() + + cpm_add_patches(${CPM_ARGS_PATCHES}) + + if(DEFINED CPM_ARGS_DOWNLOAD_COMMAND) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) + elseif(DEFINED CPM_ARGS_SOURCE_DIR) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) + if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR}) + # Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work + # for relative paths. + get_filename_component( + source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR} + ) + else() + set(source_directory ${CPM_ARGS_SOURCE_DIR}) + endif() + if(NOT EXISTS ${source_directory}) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild") + endif() + elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) + string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) + set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) + list(SORT origin_parameters) + if(CPM_ARGS_CUSTOM_CACHE_KEY) + # Application set a custom unique directory name + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${CPM_ARGS_CUSTOM_CACHE_KEY}) + elseif(CPM_USE_NAMED_CACHE_DIRECTORIES) + string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG") + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME}) + else() + string(SHA1 origin_hash "${origin_parameters}") + set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) + endif() + # Expand `download_directory` relative path. This is important because EXISTS doesn't work for + # relative paths. + get_filename_component(download_directory ${download_directory} ABSOLUTE) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) + + if(CPM_SOURCE_CACHE) + file(LOCK ${download_directory}/../cmake.lock) + endif() + + if(EXISTS ${download_directory}) + if(CPM_SOURCE_CACHE) + file(LOCK ${download_directory}/../cmake.lock RELEASE) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} "${download_directory}" + "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" + ) + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + + if(DEFINED CPM_ARGS_GIT_TAG AND NOT (PATCH_COMMAND IN_LIST CPM_ARGS_UNPARSED_ARGUMENTS)) + # warn if cache has been changed since checkout + cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN) + if(NOT ${IS_CLEAN}) + message( + WARNING "${CPM_INDENT} Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty" + ) + endif() + endif() + + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" + ) + set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") + + # As the source dir is already cached/populated, we override the call to FetchContent. + set(CPM_SKIP_FETCH TRUE) + cpm_override_fetchcontent( + "${lower_case_name}" SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" + ) + + else() + # Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but + # it should guarantee no commit hash get mis-detected. + if(NOT DEFINED CPM_ARGS_GIT_SHALLOW) + cpm_is_git_tag_commit_hash("${CPM_ARGS_GIT_TAG}" IS_HASH) + if(NOT ${IS_HASH}) + list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE) + endif() + endif() + + # remove timestamps so CMake will re-download the dependency + file(REMOVE_RECURSE ${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild) + set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}") + endif() + endif() + + cpm_create_module_file(${CPM_ARGS_NAME} "CPMAddPackage(\"${ARGN}\")") + + if(CPM_PACKAGE_LOCK_ENABLED) + if((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) + cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + elseif(CPM_ARGS_SOURCE_DIR) + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") + else() + cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") + endif() + endif() + + cpm_message( + STATUS "${CPM_INDENT} Adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})" + ) + + if(NOT CPM_SKIP_FETCH) + # CMake 3.28 added EXCLUDE, SYSTEM (3.25), and SOURCE_SUBDIR (3.18) to FetchContent_Declare. + # Calling FetchContent_MakeAvailable will then internally forward these options to + # add_subdirectory. Up until these changes, we had to call FetchContent_Populate and + # add_subdirectory separately, which is no longer necessary and has been deprecated as of 3.30. + set(fetchContentDeclareExtraArgs "") + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.28.0") + if(${CPM_ARGS_EXCLUDE_FROM_ALL}) + list(APPEND fetchContentDeclareExtraArgs EXCLUDE_FROM_ALL) + endif() + if(${CPM_ARGS_SYSTEM}) + list(APPEND fetchContentDeclareExtraArgs SYSTEM) + endif() + if(DEFINED CPM_ARGS_SOURCE_SUBDIR) + list(APPEND fetchContentDeclareExtraArgs SOURCE_SUBDIR ${CPM_ARGS_SOURCE_SUBDIR}) + endif() + # For CMake version <3.28 OPTIONS are parsed in cpm_add_subdirectory + if(CPM_ARGS_OPTIONS AND NOT DOWNLOAD_ONLY) + foreach(OPTION ${CPM_ARGS_OPTIONS}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") + endforeach() + endif() + endif() + cpm_declare_fetch( + "${CPM_ARGS_NAME}" ${fetchContentDeclareExtraArgs} "${CPM_ARGS_UNPARSED_ARGUMENTS}" + ) + + cpm_fetch_package("${CPM_ARGS_NAME}" ${DOWNLOAD_ONLY} populated ${CPM_ARGS_UNPARSED_ARGUMENTS}) + if(CPM_SOURCE_CACHE AND download_directory) + file(LOCK ${download_directory}/../cmake.lock RELEASE) + endif() + if(${populated} AND ${CMAKE_VERSION} VERSION_LESS "3.28.0") + cpm_add_subdirectory( + "${CPM_ARGS_NAME}" + "${DOWNLOAD_ONLY}" + "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" + "${${CPM_ARGS_NAME}_BINARY_DIR}" + "${CPM_ARGS_EXCLUDE_FROM_ALL}" + "${CPM_ARGS_SYSTEM}" + "${CPM_ARGS_OPTIONS}" + ) + endif() + cpm_get_fetch_properties("${CPM_ARGS_NAME}") + endif() + + set(${CPM_ARGS_NAME}_ADDED YES) + cpm_export_variables("${CPM_ARGS_NAME}") +endfunction() + +# Fetch a previously declared package +macro(CPMGetPackage Name) + if(DEFINED "CPM_DECLARATION_${Name}") + CPMAddPackage(NAME ${Name}) + else() + message(SEND_ERROR "${CPM_INDENT} Cannot retrieve package ${Name}: no declaration available") + endif() +endmacro() + +# export variables available to the caller to the parent scope expects ${CPM_ARGS_NAME} to be set +macro(cpm_export_variables name) + set(${name}_SOURCE_DIR + "${${name}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${name}_BINARY_DIR + "${${name}_BINARY_DIR}" + PARENT_SCOPE + ) + set(${name}_ADDED + "${${name}_ADDED}" + PARENT_SCOPE + ) + set(CPM_LAST_PACKAGE_NAME + "${name}" + PARENT_SCOPE + ) +endmacro() + +# declares a package, so that any call to CPMAddPackage for the package name will use these +# arguments instead. Previous declarations will not be overridden. +macro(CPMDeclarePackage Name) + if(NOT DEFINED "CPM_DECLARATION_${Name}") + set("CPM_DECLARATION_${Name}" "${ARGN}") + endif() +endmacro() + +function(cpm_add_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN false ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name}\n${PRETTY_ARGN})\n") + endif() +endfunction() + +function(cpm_add_comment_to_package_lock Name) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + cpm_prettify_package_arguments(PRETTY_ARGN true ${ARGN}) + file(APPEND ${CPM_PACKAGE_LOCK_FILE} + "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name}\n${PRETTY_ARGN}#)\n" + ) + endif() +endfunction() + +# includes the package lock file if it exists and creates a target `cpm-update-package-lock` to +# update it +macro(CPMUsePackageLock file) + if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) + get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) + if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) + endif() + if(NOT TARGET cpm-update-package-lock) + add_custom_target( + cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} + ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH} + ) + endif() + set(CPM_PACKAGE_LOCK_ENABLED true) + endif() +endmacro() + +# registers a package that has been added to CPM +function(CPMRegisterPackage PACKAGE VERSION) + list(APPEND CPM_PACKAGES ${PACKAGE}) + set(CPM_PACKAGES + ${CPM_PACKAGES} + CACHE INTERNAL "" + ) + set("CPM_PACKAGE_${PACKAGE}_VERSION" + ${VERSION} + CACHE INTERNAL "" + ) +endfunction() + +# retrieve the current version of the package to ${OUTPUT} +function(CPMGetPackageVersion PACKAGE OUTPUT) + set(${OUTPUT} + "${CPM_PACKAGE_${PACKAGE}_VERSION}" + PARENT_SCOPE + ) +endfunction() + +# declares a package in FetchContent_Declare +function(cpm_declare_fetch PACKAGE) + if(${CPM_DRY_RUN}) + cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)") + return() + endif() + + FetchContent_Declare(${PACKAGE} ${ARGN}) +endfunction() + +# returns properties for a package previously defined by cpm_declare_fetch +function(cpm_get_fetch_properties PACKAGE) + if(${CPM_DRY_RUN}) + return() + endif() + + set(${PACKAGE}_SOURCE_DIR + "${CPM_PACKAGE_${PACKAGE}_SOURCE_DIR}" + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + "${CPM_PACKAGE_${PACKAGE}_BINARY_DIR}" + PARENT_SCOPE + ) +endfunction() + +function(cpm_store_fetch_properties PACKAGE source_dir binary_dir) + if(${CPM_DRY_RUN}) + return() + endif() + + set(CPM_PACKAGE_${PACKAGE}_SOURCE_DIR + "${source_dir}" + CACHE INTERNAL "" + ) + set(CPM_PACKAGE_${PACKAGE}_BINARY_DIR + "${binary_dir}" + CACHE INTERNAL "" + ) +endfunction() + +# adds a package as a subdirectory if viable, according to provided options +function( + cpm_add_subdirectory + PACKAGE + DOWNLOAD_ONLY + SOURCE_DIR + BINARY_DIR + EXCLUDE + SYSTEM + OPTIONS +) + + if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) + set(addSubdirectoryExtraArgs "") + if(EXCLUDE) + list(APPEND addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) + endif() + if("${SYSTEM}" AND "${CMAKE_VERSION}" VERSION_GREATER_EQUAL "3.25") + # https://cmake.org/cmake/help/latest/prop_dir/SYSTEM.html#prop_dir:SYSTEM + list(APPEND addSubdirectoryExtraArgs SYSTEM) + endif() + if(OPTIONS) + foreach(OPTION ${OPTIONS}) + cpm_parse_option("${OPTION}") + set(${OPTION_KEY} "${OPTION_VALUE}") + endforeach() + endif() + set(CPM_OLD_INDENT "${CPM_INDENT}") + set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") + add_subdirectory(${SOURCE_DIR} ${BINARY_DIR} ${addSubdirectoryExtraArgs}) + set(CPM_INDENT "${CPM_OLD_INDENT}") + endif() +endfunction() + +# downloads a previously declared package via FetchContent and exports the variables +# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope +function(cpm_fetch_package PACKAGE DOWNLOAD_ONLY populated) + set(${populated} + FALSE + PARENT_SCOPE + ) + if(${CPM_DRY_RUN}) + cpm_message(STATUS "${CPM_INDENT} Package ${PACKAGE} not fetched (dry run)") + return() + endif() + + FetchContent_GetProperties(${PACKAGE}) + + string(TOLOWER "${PACKAGE}" lower_case_name) + + if(NOT ${lower_case_name}_POPULATED) + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.28.0") + if(DOWNLOAD_ONLY) + # MakeAvailable will call add_subdirectory internally which is not what we want when + # DOWNLOAD_ONLY is set. Populate will only download the dependency without adding it to the + # build + FetchContent_Populate( + ${PACKAGE} + SOURCE_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-src" + BINARY_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" + SUBBUILD_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild" + ${ARGN} + ) + else() + FetchContent_MakeAvailable(${PACKAGE}) + endif() + else() + FetchContent_Populate(${PACKAGE}) + endif() + set(${populated} + TRUE + PARENT_SCOPE + ) + endif() + + cpm_store_fetch_properties( + ${CPM_ARGS_NAME} ${${lower_case_name}_SOURCE_DIR} ${${lower_case_name}_BINARY_DIR} + ) + + set(${PACKAGE}_SOURCE_DIR + ${${lower_case_name}_SOURCE_DIR} + PARENT_SCOPE + ) + set(${PACKAGE}_BINARY_DIR + ${${lower_case_name}_BINARY_DIR} + PARENT_SCOPE + ) +endfunction() + +# splits a package option +function(cpm_parse_option OPTION) + string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") + string(LENGTH "${OPTION}" OPTION_LENGTH) + string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) + if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) + # no value for key provided, assume user wants to set option to "ON" + set(OPTION_VALUE "ON") + else() + math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") + string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) + endif() + set(OPTION_KEY + "${OPTION_KEY}" + PARENT_SCOPE + ) + set(OPTION_VALUE + "${OPTION_VALUE}" + PARENT_SCOPE + ) +endfunction() + +# guesses the package version from a git tag +function(cpm_get_version_from_git_tag GIT_TAG RESULT) + string(LENGTH ${GIT_TAG} length) + if(length EQUAL 40) + # GIT_TAG is probably a git hash + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) + set(${RESULT} + ${CMAKE_MATCH_1} + PARENT_SCOPE + ) + endif() +endfunction() + +# guesses if the git tag is a commit hash or an actual tag or a branch name. +function(cpm_is_git_tag_commit_hash GIT_TAG RESULT) + string(LENGTH "${GIT_TAG}" length) + # full hash has 40 characters, and short hash has at least 7 characters. + if(length LESS 7 OR length GREATER 40) + set(${RESULT} + 0 + PARENT_SCOPE + ) + else() + if(${GIT_TAG} MATCHES "^[a-fA-F0-9]+$") + set(${RESULT} + 1 + PARENT_SCOPE + ) + else() + set(${RESULT} + 0 + PARENT_SCOPE + ) + endif() + endif() +endfunction() + +function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) + set(oneValueArgs + NAME + FORCE + VERSION + GIT_TAG + DOWNLOAD_ONLY + GITHUB_REPOSITORY + GITLAB_REPOSITORY + BITBUCKET_REPOSITORY + GIT_REPOSITORY + SOURCE_DIR + FIND_PACKAGE_ARGUMENTS + NO_CACHE + SYSTEM + GIT_SHALLOW + EXCLUDE_FROM_ALL + SOURCE_SUBDIR + ) + set(multiValueArgs URL OPTIONS DOWNLOAD_COMMAND) + cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + foreach(oneArgName ${oneValueArgs}) + if(DEFINED CPM_ARGS_${oneArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + if(${oneArgName} STREQUAL "SOURCE_DIR") + string(REPLACE ${CMAKE_SOURCE_DIR} "\${CMAKE_SOURCE_DIR}" CPM_ARGS_${oneArgName} + ${CPM_ARGS_${oneArgName}} + ) + endif() + string(APPEND PRETTY_OUT_VAR " ${oneArgName} ${CPM_ARGS_${oneArgName}}\n") + endif() + endforeach() + foreach(multiArgName ${multiValueArgs}) + if(DEFINED CPM_ARGS_${multiArgName}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ${multiArgName}\n") + foreach(singleOption ${CPM_ARGS_${multiArgName}}) + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " \"${singleOption}\"\n") + endforeach() + endif() + endforeach() + + if(NOT "${CPM_ARGS_UNPARSED_ARGUMENTS}" STREQUAL "") + if(${IS_IN_COMMENT}) + string(APPEND PRETTY_OUT_VAR "#") + endif() + string(APPEND PRETTY_OUT_VAR " ") + foreach(CPM_ARGS_UNPARSED_ARGUMENT ${CPM_ARGS_UNPARSED_ARGUMENTS}) + string(APPEND PRETTY_OUT_VAR " ${CPM_ARGS_UNPARSED_ARGUMENT}") + endforeach() + string(APPEND PRETTY_OUT_VAR "\n") + endif() + + set(${OUT_VAR} + ${PRETTY_OUT_VAR} + PARENT_SCOPE + ) + +endfunction() diff --git a/examples/CLIExample.cpp b/examples/CLIExample.cpp index b2c10f8..31f39d4 100644 --- a/examples/CLIExample.cpp +++ b/examples/CLIExample.cpp @@ -30,7 +30,7 @@ #define DR_WAV_IMPLEMENTATION #include "dr_wav.h" -#include "../tests/simd-test-include.h" +#include "sst/basic-blocks/simd/setup.h" #include "sst/voice-effects/distortion/BitCrusher.h" #include "sst/voice-effects/utilities/VolumeAndPan.h" #include "sst/voice-effects/dynamics/Compressor.h" @@ -136,9 +136,7 @@ struct ConcreteConfig DbToLinearProvider dbtlp; // It is painful that sst-filters makes us over-adapt // this class - GS(double sr) : sampleRate(sr) { - dbtlp.init(); - } + GS(double sr) : sampleRate(sr) { dbtlp.init(); } }; struct ES { @@ -170,12 +168,18 @@ struct ConcreteConfig static inline double sampleRate(GlobalStorage *s) { return s->sampleRate; } static inline double sampleRateInv(GlobalStorage *s) { return 1.0 / s->sampleRate; } static inline float noteToPitch(GlobalStorage *s, float p) { return pow(2.0, p / 12); } - static inline float noteToPitchIgnoringTuning(GlobalStorage *s, float p) { return noteToPitch(s, p); } - static inline float noteToPitchInv(GlobalStorage *s, float p) { return 1.0 / noteToPitch(s, p); } + static inline float noteToPitchIgnoringTuning(GlobalStorage *s, float p) + { + return noteToPitch(s, p); + } + static inline float noteToPitchInv(GlobalStorage *s, float p) + { + return 1.0 / noteToPitch(s, p); + } static inline float dbToLinear(GlobalStorage *s, float f) { return s->dbtlp.dbToLinear(f); } }; -int writeOutfile(std::string filename, int sampleRate, uint32_t sample_count, float* samples) +int writeOutfile(std::string filename, int sampleRate, uint32_t sample_count, float *samples) { drwav wav; drwav_data_format format; @@ -196,16 +200,19 @@ int writeOutfile(std::string filename, int sampleRate, uint32_t sample_count, fl return 0; } -int writeDatfile(std::string filename, uint32_t sample_count, float* samples) +int writeDatfile(std::string filename, uint32_t sample_count, float *samples) { FILE *datFile = fopen(filename.c_str(), "w"); - + if (!datFile) { std::cout << "Datfile not open at '" << filename << "'" << std::endl; return 4; - } else { - for (size_t s = 0; s < sample_count; s++) { + } + else + { + for (size_t s = 0; s < sample_count; s++) + { fprintf(datFile, "%d %f %f\n", s, samples[s * 2], samples[s * 2 + 1]); } fclose(datFile); @@ -307,9 +314,11 @@ template int voiceEffectExampleHarness(const CLIArgBundle &arg) } int err; - if (err = writeOutfile(arg.outfileName, sampleRate, sample_count, outputSamples)) return err; + if (err = writeOutfile(arg.outfileName, sampleRate, sample_count, outputSamples)) + return err; - if (!arg.datfileName.empty() && (err = writeDatfile(arg.datfileName, sample_count, outputSamples))) + if (!arg.datfileName.empty() && + (err = writeDatfile(arg.datfileName, sample_count, outputSamples))) return err; if (arg.launchGnuplot) @@ -348,7 +357,7 @@ template int effectExampleHarness(const CLIArgBundle &arg) auto fx = std::make_unique(&gs, &es, nullptr); for (int i = 0; i < FXT::numParams; ++i) - fx->paramStorage[i] = fx->paramAt(i).defaultVal; + fx->paramStorage[i] = fx->paramAt(i).defaultVal; int ai{0}; for (const auto &f : arg.fArgs) @@ -400,9 +409,11 @@ template int effectExampleHarness(const CLIArgBundle &arg) } int err; - if (err = writeOutfile(arg.outfileName, sampleRate, sample_count, outputSamples)) return err; + if (err = writeOutfile(arg.outfileName, sampleRate, sample_count, outputSamples)) + return err; - if (!arg.datfileName.empty() && (err = writeDatfile(arg.datfileName, sample_count, outputSamples))) + if (!arg.datfileName.empty() && + (err = writeDatfile(arg.datfileName, sample_count, outputSamples))) return err; if (arg.launchGnuplot) @@ -438,7 +449,7 @@ int main(int argc, char const *argv[]) // - Add a ringout option CLI11_PARSE(app, argc, argv); - + if (arg.launchGnuplot && arg.datfileName.empty()) { std::cout << "To launch gnuplot you need to specify a datfile with -d" << std::endl; @@ -446,21 +457,21 @@ int main(int argc, char const *argv[]) } std::vector types; -#define ADD_VFX_TYPE(key, cls) \ +#define ADD_VFX_TYPE(key, cls) \ { \ types.push_back(std::string(key) + " -> " + #cls); \ if (fxType == std::string(key)) \ - return voiceEffectExampleHarness>(arg); \ + return voiceEffectExampleHarness>(arg); \ } -#define ADD_FX_TYPE(key, cls) \ +#define ADD_FX_TYPE(key, cls) \ { \ types.push_back(std::string(key) + " -> " + #cls); \ if (fxType == std::string(key)) \ - return effectExampleHarness>(arg); \ + return effectExampleHarness>(arg); \ } ADD_VFX_TYPE("volpan", utilities::VolumeAndPan); ADD_VFX_TYPE("bitcrush", distortion::BitCrusher); - + ADD_FX_TYPE("reverb2", reverb2::Reverb2); ADD_FX_TYPE("floaty", floatydelay::FloatyDelay); ADD_FX_TYPE("bonsai", bonsai::Bonsai); diff --git a/include/sst/effects/Delay.h b/include/sst/effects/Delay.h index fc51ef5..ba8bb34 100644 --- a/include/sst/effects/Delay.h +++ b/include/sst/effects/Delay.h @@ -342,22 +342,24 @@ template inline void Delay::processBlock(float *da std::clamp((int)(sincTable.FIRipol_M * (float(i_dtimeR + 1) - timeR.v)), 0, sincTable.FIRipol_M - 1); - __m128 L, R; - L = _mm_mul_ps(_mm_load_ps(&sincTable.sinctable1X[sincL]), _mm_loadu_ps(&buffer[0][rpL])); - L = _mm_add_ps(L, _mm_mul_ps(_mm_load_ps(&sincTable.sinctable1X[sincL + 4]), - _mm_loadu_ps(&buffer[0][rpL + 4]))); - L = _mm_add_ps(L, _mm_mul_ps(_mm_load_ps(&sincTable.sinctable1X[sincL + 8]), - _mm_loadu_ps(&buffer[0][rpL + 8]))); + SIMD_M128 L, R; + L = SIMD_MM(mul_ps)(SIMD_MM(load_ps)(&sincTable.sinctable1X[sincL]), + SIMD_MM(loadu_ps)(&buffer[0][rpL])); + L = SIMD_MM(add_ps)(L, SIMD_MM(mul_ps)(SIMD_MM(load_ps)(&sincTable.sinctable1X[sincL + 4]), + SIMD_MM(loadu_ps)(&buffer[0][rpL + 4]))); + L = SIMD_MM(add_ps)(L, SIMD_MM(mul_ps)(SIMD_MM(load_ps)(&sincTable.sinctable1X[sincL + 8]), + SIMD_MM(loadu_ps)(&buffer[0][rpL + 8]))); L = sst::basic_blocks::mechanics::sum_ps_to_ss(L); - R = _mm_mul_ps(_mm_load_ps(&sincTable.sinctable1X[sincR]), _mm_loadu_ps(&buffer[1][rpR])); - R = _mm_add_ps(R, _mm_mul_ps(_mm_load_ps(&sincTable.sinctable1X[sincR + 4]), - _mm_loadu_ps(&buffer[1][rpR + 4]))); - R = _mm_add_ps(R, _mm_mul_ps(_mm_load_ps(&sincTable.sinctable1X[sincR + 8]), - _mm_loadu_ps(&buffer[1][rpR + 8]))); + R = SIMD_MM(mul_ps)(SIMD_MM(load_ps)(&sincTable.sinctable1X[sincR]), + SIMD_MM(loadu_ps)(&buffer[1][rpR])); + R = SIMD_MM(add_ps)(R, SIMD_MM(mul_ps)(SIMD_MM(load_ps)(&sincTable.sinctable1X[sincR + 4]), + SIMD_MM(loadu_ps)(&buffer[1][rpR + 4]))); + R = SIMD_MM(add_ps)(R, SIMD_MM(mul_ps)(SIMD_MM(load_ps)(&sincTable.sinctable1X[sincR + 8]), + SIMD_MM(loadu_ps)(&buffer[1][rpR + 8]))); R = sst::basic_blocks::mechanics::sum_ps_to_ss(R); - _mm_store_ss(&tbufferL[k], L); - _mm_store_ss(&tbufferR[k], R); + SIMD_MM(store_ss)(&tbufferL[k], L); + SIMD_MM(store_ss)(&tbufferR[k], R); } // negative feedback diff --git a/include/sst/effects/Reverb1.h b/include/sst/effects/Reverb1.h index 03c22b5..246f31f 100644 --- a/include/sst/effects/Reverb1.h +++ b/include/sst/effects/Reverb1.h @@ -239,13 +239,13 @@ inline void Reverb1::processBlock(float *__restrict dataL, float *__re this->noteToPitchIgnoringTuning(12 * this->floatValue(rev1_predelay)) * this->temposyncRatio(rev1_predelay); - const __m128 one4 = _mm_set1_ps(1.f); + const auto one4 = SIMD_MM(set1_ps)(1.f); float dv = this->floatValue(rev1_damping); dv = std::clamp(dv, 0.01f, 0.99f); // this is a simple one-pole damper, w * y[n] + ( 1-w ) // y[n-1] so to be stable has to stay in range - __m128 damp4 = _mm_load1_ps(&dv); - __m128 damp4m1 = _mm_sub_ps(one4, damp4); + auto damp4 = SIMD_MM(load1_ps)(&dv); + auto damp4m1 = SIMD_MM(sub_ps)(one4, damp4); for (int k = 0; k < FXConfig::blockSize; k++) { @@ -253,57 +253,62 @@ inline void Reverb1::processBlock(float *__restrict dataL, float *__re { int dp = (delay_pos - (delay_time[t] >> 8)); // float newa = delay[t + ((dp & (max_rev_dly-1))<> 8)); - __m128 newb = _mm_load_ss(&delay[t + 1 + ((dp & (max_rev_dly - 1)) << rev_tap_bits)]); + auto newb = + SIMD_MM(load_ss)(&delay[t + 1 + ((dp & (max_rev_dly - 1)) << rev_tap_bits)]); dp = (delay_pos - (delay_time[t + 2] >> 8)); - newa = _mm_unpacklo_ps(newa, newb); // a,b,0,0 - __m128 newc = _mm_load_ss(&delay[t + 2 + ((dp & (max_rev_dly - 1)) << rev_tap_bits)]); + newa = SIMD_MM(unpacklo_ps)(newa, newb); // a,b,0,0 + auto newc = + SIMD_MM(load_ss)(&delay[t + 2 + ((dp & (max_rev_dly - 1)) << rev_tap_bits)]); dp = (delay_pos - (delay_time[t + 3] >> 8)); - __m128 newd = _mm_load_ss(&delay[t + 3 + ((dp & (max_rev_dly - 1)) << rev_tap_bits)]); - newc = _mm_unpacklo_ps(newc, newd); // c,d,0,0 - __m128 new4 = _mm_movelh_ps(newa, newc); // a,b,c,d - - __m128 out_tap4 = _mm_load_ps(&out_tap[t]); - out_tap4 = _mm_add_ps(_mm_mul_ps(out_tap4, damp4), _mm_mul_ps(new4, damp4m1)); - _mm_store_ps(&out_tap[t], out_tap4); + auto newd = + SIMD_MM(load_ss)(&delay[t + 3 + ((dp & (max_rev_dly - 1)) << rev_tap_bits)]); + newc = SIMD_MM(unpacklo_ps)(newc, newd); // c,d,0,0 + auto new4 = SIMD_MM(movelh_ps)(newa, newc); // a,b,c,d + + auto out_tap4 = SIMD_MM(load_ps)(&out_tap[t]); + out_tap4 = + SIMD_MM(add_ps)(SIMD_MM(mul_ps)(out_tap4, damp4), SIMD_MM(mul_ps)(new4, damp4m1)); + SIMD_MM(store_ps)(&out_tap[t], out_tap4); // out_tap[t] = this->floatValue(rev1_damping]*out_tap[t) + (1- // this->floatValue(rev1_damping))*newa; } - __m128 fb = _mm_add_ps(_mm_add_ps(_mm_load_ps(out_tap), _mm_load_ps(out_tap + 4)), - _mm_add_ps(_mm_load_ps(out_tap + 8), _mm_load_ps(out_tap + 12))); + auto fb = SIMD_MM(add_ps)( + SIMD_MM(add_ps)(SIMD_MM(load_ps)(out_tap), SIMD_MM(load_ps)(out_tap + 4)), + SIMD_MM(add_ps)(SIMD_MM(load_ps)(out_tap + 8), SIMD_MM(load_ps)(out_tap + 12))); fb = mech::sum_ps_to_ss(fb); /*pd_floator(int t=0; tisDeactivated(rev1_lowcut)) diff --git a/include/sst/effects/RotarySpeaker.h b/include/sst/effects/RotarySpeaker.h index 21f8497..605a322 100644 --- a/include/sst/effects/RotarySpeaker.h +++ b/include/sst/effects/RotarySpeaker.h @@ -183,7 +183,7 @@ template inline void RotarySpeaker::setvars(bool i mix.instantize(); for (int i = 0; i < sst::waveshapers::n_waveshaper_registers; ++i) - wsState.R[i] = _mm_setzero_ps(); + wsState.R[i] = SIMD_MM(setzero_ps)(); } } @@ -337,10 +337,10 @@ inline void RotarySpeaker::processBlock(float *__restrict dataL, float drive_factor = 1.f + (drive.v * drive.v * 15.f); if (useSSEShaper) { - auto inp = _mm_set1_ps(0.5 * (dataL[k] + dataR[k])); - auto wsres = wsop(&wsState, inp, _mm_set1_ps(drive_factor)); + auto inp = SIMD_MM(set1_ps)(0.5 * (dataL[k] + dataR[k])); + auto wsres = wsop(&wsState, inp, SIMD_MM(set1_ps)(drive_factor)); float r[4]; - _mm_store_ps(r, wsres); + SIMD_MM(store_ps)(r, wsres); input = r[0] * gain_tweak; } else diff --git a/include/sst/voice-effects/eq/TiltEQ.h b/include/sst/voice-effects/eq/TiltEQ.h index 87cd030..46b6737 100644 --- a/include/sst/voice-effects/eq/TiltEQ.h +++ b/include/sst/voice-effects/eq/TiltEQ.h @@ -75,7 +75,7 @@ template struct TiltEQ : core::VoiceEffectTemplateBasenote_to_pitch_ignoring_tuning(this->getFloatParam(fpFreq)); float slope = std::clamp(this->getFloatParam(fpTilt), -18.f, 18.f) / 2.f; - + float posGain = this->dbToLinear(slope); float negGain = this->dbToLinear(-1 * slope); float res = .07f; diff --git a/include/sst/voice-effects/filter/SSTFilters.h b/include/sst/voice-effects/filter/SSTFilters.h index 664dc9d..8c99757 100644 --- a/include/sst/voice-effects/filter/SSTFilters.h +++ b/include/sst/voice-effects/filter/SSTFilters.h @@ -154,8 +154,8 @@ template struct SSTFilters : core::VoiceEffectTemplateBase< void resetFilter(bool stereo) { - std::fill(qfus.R, &qfus.R[sst::filters::n_filter_registers], _mm_setzero_ps()); - std::fill(qfus.C, &qfus.C[sst::filters::n_cm_coeffs], _mm_setzero_ps()); + std::fill(qfus.R, &qfus.R[sst::filters::n_filter_registers], SIMD_MM(setzero_ps)()); + std::fill(qfus.C, &qfus.C[sst::filters::n_cm_coeffs], SIMD_MM(setzero_ps)()); auto type = (sst::filters::FilterType)this->getIntParam(ipType); @@ -224,7 +224,7 @@ template struct SSTFilters : core::VoiceEffectTemplateBase< for (int f = 0; f < sst::filters::n_cm_coeffs; ++f) { float res alignas(16)[4]; - _mm_store_ps(res, qfus.C[f]); + SIMD_MM(store_ps)(res, qfus.C[f]); coefMaker.C[f] = res[0]; } @@ -252,9 +252,9 @@ template struct SSTFilters : core::VoiceEffectTemplateBase< r[2] = 0.f; r[3] = 0.f; - auto resS = filterUnitPtr(&qfus, _mm_load_ps(r)); + auto resS = filterUnitPtr(&qfus, SIMD_MM(load_ps)(r)); float res alignas(16)[4]; - _mm_store_ps(res, resS); + SIMD_MM(store_ps)(res, resS); dataoutL[i] = res[0]; dataoutR[i] = res[1]; } @@ -280,9 +280,9 @@ template struct SSTFilters : core::VoiceEffectTemplateBase< r[2] = 0.f; r[3] = 0.f; - auto resS = filterUnitPtr(&qfus, _mm_load_ps(r)); + auto resS = filterUnitPtr(&qfus, SIMD_MM(load_ps)(r)); float res alignas(16)[4]; - _mm_store_ps(res, resS); + SIMD_MM(store_ps)(res, resS); dataoutL[i] = res[0]; } } diff --git a/include/sst/voice-effects/modulation/PhaseMod.h b/include/sst/voice-effects/modulation/PhaseMod.h index 80c3e43..3561f99 100644 --- a/include/sst/voice-effects/modulation/PhaseMod.h +++ b/include/sst/voice-effects/modulation/PhaseMod.h @@ -148,21 +148,21 @@ template struct PhaseMod : core::VoiceEffectTemplateBase M_PI) phase -= 2 * M_PI; - const auto half = _mm_set1_ps(0.5f); + const auto half = SIMD_MM(set1_ps)(0.5f); for (int k = 0; k < bs2; k += 4) { - auto ph = _mm_load_ps(phVals + k); - auto s0 = _mm_load_ps(OS[0] + k); - auto s1 = _mm_load_ps(OS[1] + k); - auto w0 = sdsp::clampToPiRangeSSE(_mm_add_ps(s0, ph)); - auto w1 = sdsp::clampToPiRangeSSE(_mm_add_ps(s1, ph)); + auto ph = SIMD_MM(load_ps)(phVals + k); + auto s0 = SIMD_MM(load_ps)(OS[0] + k); + auto s1 = SIMD_MM(load_ps)(OS[1] + k); + auto w0 = sdsp::clampToPiRangeSSE(SIMD_MM(add_ps)(s0, ph)); + auto w1 = sdsp::clampToPiRangeSSE(SIMD_MM(add_ps)(s1, ph)); ph = sdsp::clampToPiRangeSSE(ph); auto sph = sdsp::fastsinSSE(ph); - auto r0 = _mm_mul_ps(half, _mm_sub_ps(sdsp::fastsinSSE(w0), sph)); - auto r1 = _mm_mul_ps(half, _mm_sub_ps(sdsp::fastsinSSE(w1), sph)); - _mm_store_ps(OS[0] + k, r0); - _mm_store_ps(OS[1] + k, r1); + auto r0 = SIMD_MM(mul_ps)(half, SIMD_MM(sub_ps)(sdsp::fastsinSSE(w0), sph)); + auto r1 = SIMD_MM(mul_ps)(half, SIMD_MM(sub_ps)(sdsp::fastsinSSE(w1), sph)); + SIMD_MM(store_ps)(OS[0] + k, r0); + SIMD_MM(store_ps)(OS[1] + k, r1); } post.process_block_D2(OS[0], OS[1], bs2, dataoutL, dataoutR); @@ -200,17 +200,17 @@ template struct PhaseMod : core::VoiceEffectTemplateBase M_PI) phase -= 2 * M_PI; - const auto half = _mm_set1_ps(0.5f); + const auto half = SIMD_MM(set1_ps)(0.5f); for (int k = 0; k < bs2; k += 4) { - auto ph = _mm_load_ps(phVals + k); - auto s0 = _mm_load_ps(OS[0] + k); - auto w0 = sdsp::clampToPiRangeSSE(_mm_add_ps(s0, ph)); + auto ph = SIMD_MM(load_ps)(phVals + k); + auto s0 = SIMD_MM(load_ps)(OS[0] + k); + auto w0 = sdsp::clampToPiRangeSSE(SIMD_MM(add_ps)(s0, ph)); ph = sdsp::clampToPiRangeSSE(ph); auto sph = sdsp::fastsinSSE(ph); - auto r0 = _mm_mul_ps(half, _mm_sub_ps(sdsp::fastsinSSE(w0), sph)); - _mm_store_ps(OS[0] + k, r0); + auto r0 = SIMD_MM(mul_ps)(half, SIMD_MM(sub_ps)(sdsp::fastsinSSE(w0), sph)); + SIMD_MM(store_ps)(OS[0] + k, r0); } post.process_block_D2(OS[0], OS[0], bs2, dataoutL, 0); diff --git a/include/sst/voice-effects/waveshaper/WaveShaper.h b/include/sst/voice-effects/waveshaper/WaveShaper.h index 3170a27..c06e720 100644 --- a/include/sst/voice-effects/waveshaper/WaveShaper.h +++ b/include/sst/voice-effects/waveshaper/WaveShaper.h @@ -150,21 +150,21 @@ template struct WaveShaper : core::VoiceEffectTemplateBase< float resa alignas(16)[4]; for (auto i = 0U; i < VFXConfig::blockSize; ++i) { - auto mdrv = _mm_set1_ps(mDriveLerp.v); + auto mdrv = SIMD_MM(set1_ps)(mDriveLerp.v); - __m128 val; + SIMD_M128 val; if constexpr (stereo) { - val = _mm_set_ps(0.f, 0.f, datainR[i] + mBiasLerp.v, datainL[i] + mBiasLerp.v); + val = SIMD_MM(set_ps)(0.f, 0.f, datainR[i] + mBiasLerp.v, datainL[i] + mBiasLerp.v); } else { - val = _mm_set_ps(0.f, 0.f, 0.f, datainL[i] + mBiasLerp.v); + val = SIMD_MM(set_ps)(0.f, 0.f, 0.f, datainL[i] + mBiasLerp.v); } auto res = mWSOp(&mWss, val, mdrv); - res = _mm_mul_ps(res, _mm_set1_ps(mGainLerp.v)); - _mm_store_ps(resa, res); + res = SIMD_MM(mul_ps)(res, SIMD_MM(set1_ps)(mGainLerp.v)); + SIMD_MM(store_ps)(resa, res); dataoutL[i] = resa[0]; if constexpr (stereo) { @@ -294,9 +294,9 @@ template struct WaveShaper : core::VoiceEffectTemplateBase< sst::waveshapers::initializeWaveshaperRegister(mWSType, R); for (int i = 0; i < sst::waveshapers::n_waveshaper_registers; ++i) { - mWss.R[i] = _mm_set1_ps(R[i]); + mWss.R[i] = SIMD_MM(set1_ps)(R[i]); } - mWss.init = _mm_cmpeq_ps(_mm_setzero_ps(), _mm_setzero_ps()); + mWss.init = SIMD_MM(cmpeq_ps)(SIMD_MM(setzero_ps)(), SIMD_MM(setzero_ps)()); mWSOp = sst::waveshapers::GetQuadWaveshaper(mWSType); } } diff --git a/tests/concrete-runs.cpp b/tests/concrete-runs.cpp index 63b9377..43c58d8 100644 --- a/tests/concrete-runs.cpp +++ b/tests/concrete-runs.cpp @@ -21,7 +21,7 @@ #include #include "catch2.hpp" -#include "simd-test-include.h" +#include "sst/basic-blocks/simd/setup.h" #include "sst/effects/ConcreteConfig.h" diff --git a/tests/create-effect.cpp b/tests/create-effect.cpp index 82ba19e..b1998d7 100644 --- a/tests/create-effect.cpp +++ b/tests/create-effect.cpp @@ -21,7 +21,7 @@ #include #include #include "catch2.hpp" -#include "simd-test-include.h" +#include "sst/basic-blocks/simd/setup.h" #include "sst/effects/Delay.h" #include "sst/effects/FloatyDelay.h" diff --git a/tests/create-voice-effect.cpp b/tests/create-voice-effect.cpp index 0d53420..1f7509e 100644 --- a/tests/create-voice-effect.cpp +++ b/tests/create-voice-effect.cpp @@ -19,7 +19,7 @@ */ #include "catch2.hpp" -#include "simd-test-include.h" +#include "sst/basic-blocks/simd/setup.h" #include "sst/voice-effects/distortion/BitCrusher.h" #include "sst/voice-effects/delay/Microgate.h" @@ -190,7 +190,10 @@ TEST_CASE("Can Create Voice FX") { VTester>::TestVFX(); } - SECTION("Noise AM") { VTester>::TestVFX(); } + SECTION("Noise AM") + { + VTester>::TestVFX(); + } SECTION("VolumeAndPan") { VTester>::TestVFX(); diff --git a/tests/sfinae-test.cpp b/tests/sfinae-test.cpp index 25ff104..ad775c6 100644 --- a/tests/sfinae-test.cpp +++ b/tests/sfinae-test.cpp @@ -21,7 +21,8 @@ #include #include #include "catch2.hpp" -#include "simd-test-include.h" + +#include "sst/basic-blocks/simd/setup.h" #include "sst/effects/EffectCore.h" diff --git a/tests/simd-test-include.h b/tests/simd-test-include.h deleted file mode 100644 index 12e4fe9..0000000 --- a/tests/simd-test-include.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * sst-effects - an open source library of audio effects - * built by Surge Synth Team. - * - * Copyright 2018-2023, various authors, as described in the GitHub - * transaction log. - * - * sst-effects is released under the GNU General Public Licence v3 - * or later (GPL-3.0-or-later). The license is found in the "LICENSE" - * file in the root of this repository, or at - * https://www.gnu.org/licenses/gpl-3.0.en.html - * - * The majority of these effects at initiation were factored from - * Surge XT, and so git history prior to April 2023 is found in the - * surge repo, https://github.com/surge-synthesizer/surge - * - * All source in sst-effects available at - * https://github.com/surge-synthesizer/sst-effects - */ - -#ifndef SST_EFFECTS_TESTS_SIMD_TEST_INCLUDE_H -#define SST_EFFECTS_TESTS_SIMD_TEST_INCLUDE_H - -#if defined(__arm64__) -#define SIMDE_ENABLE_NATIVE_ALIASES -#include "simde/x86/sse2.h" -#else -#include -#endif - -#endif // SURGE_SIMD_TEST_INCLUDE_H