diff --git a/.github/workflows/consume.yml b/.github/workflows/consume.yml new file mode 100644 index 0000000..05b0e07 --- /dev/null +++ b/.github/workflows/consume.yml @@ -0,0 +1,36 @@ +# Test consuming libbase122. +name: Test Consuming libbase122 +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Configure CMake + run: cmake -B ${{github.workspace}}/cmake-build -DCMAKE_BUILD_TYPE=Debug + + - name: Build + run: cmake --build ${{github.workspace}}/cmake-build --config Debug + + - name: Install + run: cmake --install ${{github.workspace}}/cmake-build --prefix ${{github.workspace}}/.install --config Debug + + - name: Consume with CMake config file + working-directory: ${{github.workspace}}/test/testconsumer + run: | + cmake \ + -DLIBBASE122_USE_CONFIG_FILE=ON \ + -DLIBBASE122_USE_SHARED=ON \ + -DCMAKE_PREFIX_PATH=${{github.workspace}}/.install \ + -S. -Bcmake-build + + cmake --build cmake-build + + ./cmake-build/testconsumer + diff --git a/CMakeLists.txt b/CMakeLists.txt index 494e2c7..afd6810 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,3 +33,26 @@ install (TARGETS base122 b122 base122_shared) install (FILES src/base122.h TYPE INCLUDE) add_executable (example src/example.c) target_link_libraries(example base122) +# Export CMake targets. +set_target_properties(base122 PROPERTIES EXPORT_NAME base122::static) +set_target_properties(base122_shared PROPERTIES EXPORT_NAME base122::shared) +target_include_directories(base122 INTERFACE $) +target_include_directories(base122_shared INTERFACE $) +install (TARGETS base122 base122_shared EXPORT Base122Targets) +install (EXPORT Base122Targets FILE Base122Targets.cmake DESTINATION lib/cmake/Base122) +include (CMakePackageConfigHelpers) +configure_package_config_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Base122Config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/Base122Config.cmake" + INSTALL_DESTINATION "lib/cmake/Base122" + NO_SET_AND_CHECK_MACRO +) +write_basic_package_version_file( + "${CMAKE_CURRENT_BINARY_DIR}/Base122Version.cmake" + COMPATIBILITY AnyNewerVersion +) +install (FILES + "${CMAKE_CURRENT_BINARY_DIR}/Base122Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/Base122Version.cmake" + DESTINATION "lib/cmake/Base122" +) \ No newline at end of file diff --git a/README.md b/README.md index ed5d1e6..257b829 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ cat example.jpg | b122 > encoded.b122 cat encoded.b122 | b122 -d > decoded.jpg ``` +## Using with CMake + +CMake targets are exported. To use libbase122 in a CMake project: + +```cmake +add_executable (app app.c) +find_package (Base122 REQUIRED) +target_link_libraries (app base122::static) # Or base122::shared +``` ## Contributing diff --git a/cmake/Base122Config.cmake.in b/cmake/Base122Config.cmake.in new file mode 100644 index 0000000..50ba223 --- /dev/null +++ b/cmake/Base122Config.cmake.in @@ -0,0 +1,6 @@ +@PACKAGE_INIT@ +include("${CMAKE_CURRENT_LIST_DIR}/Base122Targets.cmake") +set(SOME_DIR @PACKAGE_CMAKE_INSTALL_PREFIX@/foo) +# `check_required_components` is used to ensure consumer did not erroneously request components. +# No components are currently defined. +check_required_components(Base122) \ No newline at end of file diff --git a/test/testconsumer/CMakeLists.txt b/test/testconsumer/CMakeLists.txt index 3399099..d463b05 100644 --- a/test/testconsumer/CMakeLists.txt +++ b/test/testconsumer/CMakeLists.txt @@ -1,15 +1,24 @@ cmake_minimum_required(VERSION 3.23) project(libbase122 LANGUAGES C) add_executable(testconsumer testconsumer.c) -if (NOT DEFINED LIBBASE122_INSTALL_PATH) - message(FATAL_ERROR "LIBBASE122_INSTALL_PATH not defined. Define with install path to libbase122.") -endif () - if (NOT DEFINED LIBBASE122_USE_SHARED) message(FATAL_ERROR "LIBBASE122_USE_SHARED not defined. Define to TRUE or FALSE.") endif () -if (WIN32) +if (LIBBASE122_USE_CONFIG_FILE) + message (STATUS "Trying to find libbase122 with CMake config file...") + find_package (Base122 REQUIRED) + if (LIBBASE122_USE_SHARED) + target_link_libraries (testconsumer base122::shared) + else () + target_link_libraries (testconsumer base122::static) + endif () # LIBBASE122_USE_SHARED +elseif (WIN32) + if (NOT DEFINED LIBBASE122_INSTALL_PATH) + message(FATAL_ERROR "LIBBASE122_INSTALL_PATH not defined. Define with install path to libbase122.") + endif () + + message (STATUS "Trying to find libbase122 at path ${LIBBASE122_INSTALL_PATH}") # Convert a Windows path (e.g. C:\foo\bar) to a CMake path (e.g. C:/foo/bar) file(TO_CMAKE_PATH "${LIBBASE122_INSTALL_PATH}" LIBBASE122_INSTALL_PATH) message(STATUS "${LIBBASE122_INSTALL_PATH}")