From: Joel Rosdahl Date: Sat, 30 Mar 2024 19:45:30 +0000 (+0100) Subject: build: Add support for using system blake3 X-Git-Tag: v4.10~42^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04dad2827736749cd828d7c6b1d95e3994735e3e;p=thirdparty%2Fccache.git build: Add support for using system blake3 --- diff --git a/.clang-format b/.clang-format index db62f47a..92da3a9d 100644 --- a/.clang-format +++ b/.clang-format @@ -31,7 +31,7 @@ IncludeCategories: - Regex: '^$' + - Regex: '^<(blake3/blake3\.h|cxxurl/url\.hpp|doctest/.*|fmt/.*|hiredis/.*|httplib\.h|nonstd/.*|tl/expected\.hpp|xx(hash|h_x86dispatch)\.h|zstd\.h)>$' Priority: 3 # System headers: - Regex: '\.h.*>$' diff --git a/LICENSE.adoc b/LICENSE.adoc index 543a3a10..2f20e08b 100644 --- a/LICENSE.adoc +++ b/LICENSE.adoc @@ -48,7 +48,7 @@ the GPL: that is, if separated from the ccache sources, they may be usable under less restrictive terms. -=== src/third_party/blake3/blake3_* +=== src/third_party/blake3/blake3/* This is a subset of https://github.com/BLAKE3-team/BLAKE3[BLAKE3] 1.5.1 with the following license: diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake index 2cb17560..f162b2dd 100644 --- a/cmake/Dependencies.cmake +++ b/cmake/Dependencies.cmake @@ -19,6 +19,7 @@ endif() set(DEPS AUTO CACHE STRING "How to retrieve third party dependencies") set_property(CACHE DEPS PROPERTY STRINGS AUTO DOWNLOAD LOCAL) +find_package(Blake3 1.4.0 MODULE REQUIRED) find_package(CppHttplib 0.10.6 MODULE REQUIRED) find_package(Fmt 8.0.0 MODULE REQUIRED) find_package(NonstdSpan 0.10.3 MODULE REQUIRED) diff --git a/cmake/FindBlake3.cmake b/cmake/FindBlake3.cmake new file mode 100644 index 00000000..501353c9 --- /dev/null +++ b/cmake/FindBlake3.cmake @@ -0,0 +1,30 @@ +mark_as_advanced(BLAKE3_INCLUDE_DIR) +mark_as_advanced(BLAKE3_LIBRARY) + +if(DEP_BLAKE3 STREQUAL "BUNDLED") + message(STATUS "Using bundled Blake3 as requested") +else() + find_path(BLAKE3_INCLUDE_DIR blake3.h) + find_library(BLAKE3_LIBRARY blake3) + if(BLAKE3_INCLUDE_DIR) + file(READ "${BLAKE3_INCLUDE_DIR}/blake3.h" _blake3_h) + string(REGEX MATCH "#define _blake3_version_string \"([0-9]+).([0-9]+).*([0-9]+)\"" _ "${_blake3_h}") + set(_blake3_version_string "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}") + if(NOT "${CMAKE_MATCH_0}" STREQUAL "" AND "${_blake3_version_string}" VERSION_GREATER_EQUAL "${Blake3_FIND_VERSION}") + if(BLAKE3_LIBRARY) + message(STATUS "Using system Blake3 (${BLAKE3_LIBRARY})") + add_library(dep_blake3 UNKNOWN IMPORTED) + set_target_properties( + dep_blake3 + PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${BLAKE3_INCLUDE_DIR}" + IMPORTED_LOCATION "${BLAKE3_LIBRARY}" + ) + endif() + register_dependency(Blake3 "SYSTEM (${BLAKE3_LIBRARY})" "${_blake3_version_string}") + endif() + endif() + if(NOT TARGET dep_blake3) + message(STATUS "Using bundled Blake3 since Blake3>=${Blake3_FIND_VERSION} was not found locally") + endif() +endif() diff --git a/doc/INSTALL.md b/doc/INSTALL.md index 7e24680c..56e0d6b0 100644 --- a/doc/INSTALL.md +++ b/doc/INSTALL.md @@ -39,6 +39,7 @@ should be located or retrieved: ### Dependencies +- [BLAKE3](https://github.com/BLAKE3-team/BLAKE3)[^1] - [cpp-httplib](https://github.com/yhirose/cpp-httplib)[^1] - [doctest](https://github.com/doctest/doctest)[^2] (optional, disable with `-D ENABLE_TESTING=OFF`) diff --git a/src/ccache/Hash.hpp b/src/ccache/Hash.hpp index 990b77bc..df95b66c 100644 --- a/src/ccache/Hash.hpp +++ b/src/ccache/Hash.hpp @@ -18,7 +18,7 @@ #pragma once -#include +#include #include #include diff --git a/src/third_party/CMakeLists.txt b/src/third_party/CMakeLists.txt index fb614ca8..cda49305 100644 --- a/src/third_party/CMakeLists.txt +++ b/src/third_party/CMakeLists.txt @@ -1,7 +1,9 @@ -add_subdirectory(blake3) add_subdirectory(cxxurl) add_subdirectory(win32-compat) +if(NOT TARGET dep_blake3) + add_subdirectory(blake3) +endif() if(NOT TARGET dep_cpphttplib) add_subdirectory(cpp-httplib) endif() diff --git a/src/third_party/blake3/CMakeLists.txt b/src/third_party/blake3/CMakeLists.txt index 7ab255f2..8d0e9620 100644 --- a/src/third_party/blake3/CMakeLists.txt +++ b/src/third_party/blake3/CMakeLists.txt @@ -1,17 +1,25 @@ -add_library(dep_blake3 STATIC blake3.c blake3_dispatch.c blake3_portable.c) +register_dependency(Blake3 BUNDLED 1.5.1) -target_include_directories(dep_blake3 INTERFACE "${CMAKE_SOURCE_DIR}/src/third_party/blake3") +add_library( + dep_blake3 STATIC + "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3.c" + "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_dispatch.c" + "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_portable.c" +) + +target_include_directories(dep_blake3 INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") target_link_libraries(dep_blake3 PRIVATE standard_settings) if(MSVC) - # No object file is created if masm is passed the compile options from standard_settings, - # so don't pass any flags at all to assembler (as no flags are needed anyway). + # No object file is created if masm is passed the compile options from + # standard_settings, so don't pass any flags at all to assembler (as no + # flags are needed anyway). string(REPLACE " " "" CMAKE_ASM_MASM_COMPILE_OBJECT "${CMAKE_ASM_MASM_COMPILE_OBJECT}") endif() include(CheckCSourceCompiles) -function(add_source_if_enabled feature msvc_flags others_flags intrinsic) +function(_add_blake3_source_if_enabled feature msvc_flags others_flags intrinsic) if(MSVC) set(compile_flags "${msvc_flags}") else() @@ -29,8 +37,8 @@ function(add_source_if_enabled feature msvc_flags others_flags intrinsic) # First check if it's possible to use the assembler variant for the feature. string(TOUPPER "have_asm_${feature}" have_feature) if(NOT DEFINED "${have_feature}" AND CMAKE_SIZEOF_VOID_P EQUAL 8 -# Force intrinsic version for msbuild because of a bug in the cmake generator -# or msbuild itself with masm flags. + # Force intrinsic version for msbuild because of a bug in the CMake + # generator or msbuild itself with masm flags. AND NOT CMAKE_GENERATOR MATCHES "Visual Studio") if(NOT CMAKE_REQUIRED_QUIET) @@ -39,14 +47,16 @@ function(add_source_if_enabled feature msvc_flags others_flags intrinsic) set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) - # Must set CMAKE_ASM_MASM_CREATE_STATIC_LIBRARY explicitly otherwise try_compile - # fails, see https://discourse.cmake.org/t/building-lib-file-from-asm-cmake-bug/1959 + # Must set CMAKE_ASM_MASM_CREATE_STATIC_LIBRARY explicitly otherwise + # try_compile fails, see + # . try_compile( ${have_feature} ${CMAKE_CURRENT_BINARY_DIR} - "${CMAKE_CURRENT_SOURCE_DIR}/blake3_${feature}${suffix}" + "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_${feature}${suffix}" CMAKE_FLAGS -DCMAKE_ASM_MASM_CREATE_STATIC_LIBRARY=${CMAKE_C_CREATE_STATIC_LIBRARY} - COMPILE_DEFINITIONS ${compile_flags}) + COMPILE_DEFINITIONS ${compile_flags} + ) unset(CMAKE_TRY_COMPILE_TARGET_TYPE) @@ -59,7 +69,7 @@ function(add_source_if_enabled feature msvc_flags others_flags intrinsic) endif() endif() - # If the assembler variant didn't work, try the c variant. + # If the assembler variant didn't work, try the C variant. if(NOT ${have_feature}) string(TOUPPER "have_c_${feature}" have_feature) set(suffix ".c") @@ -75,36 +85,35 @@ function(add_source_if_enabled feature msvc_flags others_flags intrinsic) endif() if(${have_feature}) - target_sources(dep_blake3 PRIVATE blake3_${feature}${suffix}) + target_sources( + dep_blake3 + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_${feature}${suffix}" + ) if(suffix STREQUAL ".c") if(MINGW AND feature STREQUAL "avx512") - # Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65782. + # Workaround for . # Taken from blake3's build.rs. set(compile_flags "${compile_flags} -fno-asynchronous-unwind-tables") endif() set_property( - SOURCE blake3_${feature}${suffix} + SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_${feature}${suffix}" APPEND PROPERTY COMPILE_FLAGS ${compile_flags}) elseif(NOT MSVC) set_property( - SOURCE blake3_${feature}${suffix} + SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_${feature}${suffix}" PROPERTY COMPILE_FLAGS ${compile_flags}) endif() else() string(TOUPPER "blake3_no_${feature}" no_feature) - target_compile_definitions(blake3 PRIVATE ${no_feature}) + target_compile_definitions(dep_blake3 PRIVATE ${no_feature}) endif() endfunction() # https://software.intel.com/sites/landingpage/IntrinsicsGuide/ -add_source_if_enabled(sse2 "" "-msse2" - "_mm_set1_epi32(42)") -add_source_if_enabled(sse41 "" "-msse4.1" - "_mm_test_all_ones(_mm_set1_epi32(42))") -add_source_if_enabled(avx2 "/arch:AVX2" "-mavx2" - "_mm256_abs_epi8(_mm256_set1_epi32(42))") -add_source_if_enabled(avx512 "/arch:AVX512" "-mavx512f -mavx512vl" - "_mm256_abs_epi64(_mm256_set1_epi32(42))") +_add_blake3_source_if_enabled(sse2 "" "-msse2" "_mm_set1_epi32(42)") +_add_blake3_source_if_enabled(sse41 "" "-msse4.1" "_mm_test_all_ones(_mm_set1_epi32(42))") +_add_blake3_source_if_enabled(avx2 "/arch:AVX2" "-mavx2" "_mm256_abs_epi8(_mm256_set1_epi32(42))") +_add_blake3_source_if_enabled(avx512 "/arch:AVX512" "-mavx512f -mavx512vl" "_mm256_abs_epi64(_mm256_set1_epi32(42))") # Neon is always available on AArch64 if(CMAKE_SIZEOF_VOID_P EQUAL 8) @@ -116,8 +125,8 @@ if(CMAKE_SIZEOF_VOID_P EQUAL 8) ]=] HAVE_NEON) if(HAVE_NEON) - target_sources(blake3 PRIVATE blake3_neon.c) - target_compile_definitions(blake3 PRIVATE BLAKE3_USE_NEON) + target_sources(dep_blake3 PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/blake3/blake3_neon.c") + target_compile_definitions(dep_blake3 PRIVATE BLAKE3_USE_NEON) endif() endif() diff --git a/src/third_party/blake3/blake3.c b/src/third_party/blake3/blake3/blake3.c similarity index 100% rename from src/third_party/blake3/blake3.c rename to src/third_party/blake3/blake3/blake3.c diff --git a/src/third_party/blake3/blake3.h b/src/third_party/blake3/blake3/blake3.h similarity index 100% rename from src/third_party/blake3/blake3.h rename to src/third_party/blake3/blake3/blake3.h diff --git a/src/third_party/blake3/blake3_avx2.c b/src/third_party/blake3/blake3/blake3_avx2.c similarity index 100% rename from src/third_party/blake3/blake3_avx2.c rename to src/third_party/blake3/blake3/blake3_avx2.c diff --git a/src/third_party/blake3/blake3_avx2_x86-64_unix.S b/src/third_party/blake3/blake3/blake3_avx2_x86-64_unix.S similarity index 100% rename from src/third_party/blake3/blake3_avx2_x86-64_unix.S rename to src/third_party/blake3/blake3/blake3_avx2_x86-64_unix.S diff --git a/src/third_party/blake3/blake3_avx2_x86-64_windows_gnu.S b/src/third_party/blake3/blake3/blake3_avx2_x86-64_windows_gnu.S similarity index 100% rename from src/third_party/blake3/blake3_avx2_x86-64_windows_gnu.S rename to src/third_party/blake3/blake3/blake3_avx2_x86-64_windows_gnu.S diff --git a/src/third_party/blake3/blake3_avx2_x86-64_windows_msvc.asm b/src/third_party/blake3/blake3/blake3_avx2_x86-64_windows_msvc.asm similarity index 100% rename from src/third_party/blake3/blake3_avx2_x86-64_windows_msvc.asm rename to src/third_party/blake3/blake3/blake3_avx2_x86-64_windows_msvc.asm diff --git a/src/third_party/blake3/blake3_avx512.c b/src/third_party/blake3/blake3/blake3_avx512.c similarity index 100% rename from src/third_party/blake3/blake3_avx512.c rename to src/third_party/blake3/blake3/blake3_avx512.c diff --git a/src/third_party/blake3/blake3_avx512_x86-64_unix.S b/src/third_party/blake3/blake3/blake3_avx512_x86-64_unix.S similarity index 100% rename from src/third_party/blake3/blake3_avx512_x86-64_unix.S rename to src/third_party/blake3/blake3/blake3_avx512_x86-64_unix.S diff --git a/src/third_party/blake3/blake3_avx512_x86-64_windows_gnu.S b/src/third_party/blake3/blake3/blake3_avx512_x86-64_windows_gnu.S similarity index 100% rename from src/third_party/blake3/blake3_avx512_x86-64_windows_gnu.S rename to src/third_party/blake3/blake3/blake3_avx512_x86-64_windows_gnu.S diff --git a/src/third_party/blake3/blake3_avx512_x86-64_windows_msvc.asm b/src/third_party/blake3/blake3/blake3_avx512_x86-64_windows_msvc.asm similarity index 100% rename from src/third_party/blake3/blake3_avx512_x86-64_windows_msvc.asm rename to src/third_party/blake3/blake3/blake3_avx512_x86-64_windows_msvc.asm diff --git a/src/third_party/blake3/blake3_dispatch.c b/src/third_party/blake3/blake3/blake3_dispatch.c similarity index 100% rename from src/third_party/blake3/blake3_dispatch.c rename to src/third_party/blake3/blake3/blake3_dispatch.c diff --git a/src/third_party/blake3/blake3_impl.h b/src/third_party/blake3/blake3/blake3_impl.h similarity index 100% rename from src/third_party/blake3/blake3_impl.h rename to src/third_party/blake3/blake3/blake3_impl.h diff --git a/src/third_party/blake3/blake3_neon.c b/src/third_party/blake3/blake3/blake3_neon.c similarity index 100% rename from src/third_party/blake3/blake3_neon.c rename to src/third_party/blake3/blake3/blake3_neon.c diff --git a/src/third_party/blake3/blake3_portable.c b/src/third_party/blake3/blake3/blake3_portable.c similarity index 100% rename from src/third_party/blake3/blake3_portable.c rename to src/third_party/blake3/blake3/blake3_portable.c diff --git a/src/third_party/blake3/blake3_sse2.c b/src/third_party/blake3/blake3/blake3_sse2.c similarity index 100% rename from src/third_party/blake3/blake3_sse2.c rename to src/third_party/blake3/blake3/blake3_sse2.c diff --git a/src/third_party/blake3/blake3_sse2_x86-64_unix.S b/src/third_party/blake3/blake3/blake3_sse2_x86-64_unix.S similarity index 100% rename from src/third_party/blake3/blake3_sse2_x86-64_unix.S rename to src/third_party/blake3/blake3/blake3_sse2_x86-64_unix.S diff --git a/src/third_party/blake3/blake3_sse2_x86-64_windows_gnu.S b/src/third_party/blake3/blake3/blake3_sse2_x86-64_windows_gnu.S similarity index 100% rename from src/third_party/blake3/blake3_sse2_x86-64_windows_gnu.S rename to src/third_party/blake3/blake3/blake3_sse2_x86-64_windows_gnu.S diff --git a/src/third_party/blake3/blake3_sse2_x86-64_windows_msvc.asm b/src/third_party/blake3/blake3/blake3_sse2_x86-64_windows_msvc.asm similarity index 100% rename from src/third_party/blake3/blake3_sse2_x86-64_windows_msvc.asm rename to src/third_party/blake3/blake3/blake3_sse2_x86-64_windows_msvc.asm diff --git a/src/third_party/blake3/blake3_sse41.c b/src/third_party/blake3/blake3/blake3_sse41.c similarity index 100% rename from src/third_party/blake3/blake3_sse41.c rename to src/third_party/blake3/blake3/blake3_sse41.c diff --git a/src/third_party/blake3/blake3_sse41_x86-64_unix.S b/src/third_party/blake3/blake3/blake3_sse41_x86-64_unix.S similarity index 100% rename from src/third_party/blake3/blake3_sse41_x86-64_unix.S rename to src/third_party/blake3/blake3/blake3_sse41_x86-64_unix.S diff --git a/src/third_party/blake3/blake3_sse41_x86-64_windows_gnu.S b/src/third_party/blake3/blake3/blake3_sse41_x86-64_windows_gnu.S similarity index 100% rename from src/third_party/blake3/blake3_sse41_x86-64_windows_gnu.S rename to src/third_party/blake3/blake3/blake3_sse41_x86-64_windows_gnu.S diff --git a/src/third_party/blake3/blake3_sse41_x86-64_windows_msvc.asm b/src/third_party/blake3/blake3/blake3_sse41_x86-64_windows_msvc.asm similarity index 100% rename from src/third_party/blake3/blake3_sse41_x86-64_windows_msvc.asm rename to src/third_party/blake3/blake3/blake3_sse41_x86-64_windows_msvc.asm