diff --git a/.github/workflows/cmake-tests.yml b/.github/workflows/cmake-tests.yml index 4af2463569c..af629af4baf 100644 --- a/.github/workflows/cmake-tests.yml +++ b/.github/workflows/cmake-tests.yml @@ -84,6 +84,13 @@ jobs: cmake -S "${{ env.SRC_DIR }}/build/cmake" -B build -DBUILD_TESTING=ON -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE=Release --install-prefix "${{ runner.temp }}/install" cmake --build build --config Release cmake --install build --config Release + - name: Configure subdirectory consumers + run: | + cmake -S "${{ env.SRC_DIR }}/build/cmake/tests/consumer" -B consumer-build -G "${{ matrix.generator }}" -DCMAKE_BUILD_TYPE=Release -DCMAKE_COMPILE_WARNING_AS_ERROR=ON + - name: Build subdirectory consumers + run: cmake --build consumer-build --config Release + - name: Test subdirectory consumers + run: ctest --test-dir consumer-build -C Release --output-on-failure # Windows-specific cmake testing with Visual Studio 2022 # Tests multiple generators and toolchains to ensure broad Windows compatibility diff --git a/build/cmake/tests/consumer/CMakeLists.txt b/build/cmake/tests/consumer/CMakeLists.txt new file mode 100644 index 00000000000..b51ca208f98 --- /dev/null +++ b/build/cmake/tests/consumer/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# This source code is licensed under both the BSD-style license (found in the +# LICENSE file in the root directory of this source tree) and the GPLv2 (found +# in the COPYING file in the root directory of this source tree). +# You may select, at your option, one of the above-listed licenses. + +cmake_minimum_required(VERSION 3.10) +project(zstd_consumer C CXX) + +set(ZSTD_BUILD_PROGRAMS OFF CACHE BOOL "Build command-line programs") +set(ZSTD_BUILD_TESTS OFF CACHE BOOL "Build zstd's test suite") +add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../.." zstd) + +enable_testing() +foreach(kind IN ITEMS static shared) + add_executable(consumer_${kind} main.cpp) + # Consumers must receive the public headers through the linked target, + # without inheriting zstd's directory-scoped include paths. + target_link_libraries(consumer_${kind} PRIVATE libzstd_${kind}) + set_target_properties(consumer_${kind} PROPERTIES CXX_STANDARD 98 CXX_STANDARD_REQUIRED ON) + add_test(NAME consumer_${kind} COMMAND consumer_${kind}) +endforeach() + +if(WIN32) + add_custom_command(TARGET consumer_shared POST_BUILD + COMMAND "${CMAKE_COMMAND}" -E copy_if_different + "$" "$" + VERBATIM) +endif() diff --git a/build/cmake/tests/consumer/main.cpp b/build/cmake/tests/consumer/main.cpp new file mode 100644 index 00000000000..a04ce35fef9 --- /dev/null +++ b/build/cmake/tests/consumer/main.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * This source code is licensed under both the BSD-style license (found in the + * LICENSE file in the root directory of this source tree) and the GPLv2 (found + * in the COPYING file in the root directory of this source tree). + * You may select, at your option, one of the above-listed licenses. + */ + +#include +#include +#include +#include + +int main() +{ + const char input[] = "CMake subdirectory consumer round trip"; + char compressed[128]; + char decompressed[sizeof(input)]; + const size_t compressedSize = ZSTD_compress(compressed, sizeof(compressed), input, sizeof(input), 1); + if (ZSTD_isError(compressedSize)) return 1; + + const size_t decompressedSize = ZSTD_decompress(decompressed, sizeof(decompressed), compressed, compressedSize); + if (ZSTD_isError(decompressedSize) || decompressedSize != sizeof(input)) return 1; + if (memcmp(input, decompressed, sizeof(input)) != 0) return 1; + + if (ZSTD_getErrorCode(decompressedSize) != ZSTD_error_no_error) return 1; + if (ZDICT_getDictID(input, sizeof(input)) != 0) return 1; + return 0; +}