diff --git a/.circleci/config.yml b/.circleci/config.yml index a7cb6404..53f7fb91 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -51,6 +51,28 @@ commands: -DCMAKE_BUILD_TYPE=Release \ . - run: make -j 16 VERBOSE=1 + run-linkage-tests: + steps: + - run: + name: Linkage test (static) + command: | + BUILD=$(mktemp -d) INST=$(mktemp -d) LB=$(mktemp -d) + cmake -S . -B "$BUILD" -DCMAKE_BUILD_TYPE=Debug -DSANITIZE=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="$INST" + cmake --build "$BUILD" --parallel + cmake --install "$BUILD" + cmake -S test/linkage -B "$LB" -DCMAKE_PREFIX_PATH="$INST" + cmake --build "$LB" + ctest --test-dir "$LB" -VV + - run: + name: Linkage test (shared) + command: | + BUILD=$(mktemp -d) INST=$(mktemp -d) LB=$(mktemp -d) + cmake -S . -B "$BUILD" -DCMAKE_BUILD_TYPE=Debug -DSANITIZE=OFF -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="$INST" + cmake --build "$BUILD" --parallel + cmake --install "$BUILD" + cmake -S test/linkage -B "$LB" -DCMAKE_PREFIX_PATH="$INST" + cmake --build "$LB" + LD_LIBRARY_PATH="$INST/lib" ctest --test-dir "$LB" -VV test: steps: - run: ctest -VV --output-junit ctest_out.xml @@ -109,6 +131,7 @@ jobs: cat Testing/Temporary/MemoryChecker* exit 1 fi; + - run-linkage-tests build-and-test-clang: machine: @@ -122,6 +145,7 @@ jobs: - linux-setup - build - test + - run-linkage-tests build-and-test-sanitized: machine: diff --git a/test/linkage/CMakeLists.txt b/test/linkage/CMakeLists.txt new file mode 100644 index 00000000..696fd57a --- /dev/null +++ b/test/linkage/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.5) +project(libcbor_linkage_test C) + +find_package(libcbor REQUIRED) + +add_executable(linkage_test main.c) +target_link_libraries(linkage_test libcbor::libcbor) + +enable_testing() +add_test(NAME linkage_test COMMAND linkage_test) diff --git a/test/linkage/main.c b/test/linkage/main.c new file mode 100644 index 00000000..2fc12340 --- /dev/null +++ b/test/linkage/main.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2014-2020 Pavel Kalvoda + * + * libcbor is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +/* + * Standalone linkage test: verifies that libcbor can be found via + * find_package and linked against from an external project, for both + * static and shared library builds. + */ + +#include +#include +#include "cbor.h" + +int main(void) { + /* Encode an integer and decode it back */ + cbor_item_t* item = cbor_build_uint8(42); + assert(item != NULL); + + unsigned char buf[16]; + size_t len = cbor_serialize(item, buf, sizeof(buf)); + assert(len > 0); + + struct cbor_load_result result; + cbor_item_t* decoded = cbor_load(buf, len, &result); + assert(decoded != NULL); + assert(result.error.code == CBOR_ERR_NONE); + assert(cbor_get_uint8(decoded) == 42); + + cbor_decref(&item); + cbor_decref(&decoded); + + printf("libcbor linkage test passed (version %s)\n", CBOR_VERSION); + return 0; +}