Skip to content

Commit

Permalink
add CMake config package file (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinAlbs authored Jan 4, 2024
1 parent 040e161 commit 6707b1f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/consume.yml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<INSTALL_INTERFACE:include>)
target_include_directories(base122_shared INTERFACE $<INSTALL_INTERFACE:include>)
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"
)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions cmake/Base122Config.cmake.in
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 14 additions & 5 deletions test/testconsumer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")
Expand Down

0 comments on commit 6707b1f

Please sign in to comment.