Skip to content

Commit 24e2a19

Browse files
authored
Merge pull request #260 from kevinAlbs/add-cmake-config-file-package
Add CMake Config-file package
2 parents a1b99da + 60205e4 commit 24e2a19

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

.github/workflows/cmake.yml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Build
2222
run: |
2323
mkdir build
24-
cmake -S . -B build -DBUILD_SHARED_LIBS=${{ matrix.shared }} -DUTF8PROC_ENABLE_TESTING=ON
24+
cmake -S . -B build -DBUILD_SHARED_LIBS=${{ matrix.shared }} -DUTF8PROC_ENABLE_TESTING=ON -DCMAKE_INSTALL_PREFIX=tmp/install
2525
cmake --build build
2626
- name: Run Test
2727
run: ctest --test-dir build -V
@@ -33,6 +33,21 @@ jobs:
3333
path: |
3434
build/libutf8proc.*
3535
build/Debug/utf8proc.*
36+
- name: Test Consuming (Windows)
37+
if: runner.os == 'Windows'
38+
run: |
39+
cmake --install build --config Debug
40+
cmake -S test/app -B test/app/build -DCMAKE_INSTALL_PREFIX=tmp/install
41+
cmake --build test/app/build
42+
$Env:PATH = "$PWD\tmp\install\bin;$Env:PATH"
43+
test/app/build/Debug/app.exe
44+
- name: Test Consuming (Unix)
45+
if: runner.os != 'Windows'
46+
run: |
47+
cmake --install build
48+
cmake -S test/app -B test/app/build -DCMAKE_INSTALL_PREFIX=tmp/install
49+
cmake --build test/app/build
50+
test/app/build/app
3651
3752
mingw:
3853
strategy:
@@ -52,7 +67,7 @@ jobs:
5267
- name: Build
5368
run: |
5469
mkdir build
55-
cmake -S . -B build -DBUILD_SHARED_LIBS=${{ matrix.shared }} -DUTF8PROC_ENABLE_TESTING=ON -G'MSYS Makefiles'
70+
cmake -S . -B build -DBUILD_SHARED_LIBS=${{ matrix.shared }} -DUTF8PROC_ENABLE_TESTING=ON -G'MSYS Makefiles' -DCMAKE_INSTALL_PREFIX=tmp/install
5671
cmake --build build
5772
- name: Run Test
5873
run: ctest --test-dir build -V
@@ -62,3 +77,10 @@ jobs:
6277
with:
6378
name: windows-mingw64
6479
path: build/libutf8proc.*
80+
- name: Test Consuming
81+
run: |
82+
cmake --install build
83+
cmake -S test/app -B test/app/build -DCMAKE_INSTALL_PREFIX=tmp/install -G'MSYS Makefiles'
84+
cmake --build test/app/build
85+
PATH="$(pwd)/tmp/install/bin:$PATH"
86+
test/app/build/app.exe

CMakeLists.txt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ add_library (utf8proc
2424
)
2525

2626
# expose header path, for when this is part of a larger cmake project
27-
target_include_directories(utf8proc PUBLIC .)
27+
target_include_directories(utf8proc PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<INSTALL_INTERFACE:include>)
2828

2929
if (BUILD_SHARED_LIBS)
3030
# Building shared library
@@ -53,14 +53,33 @@ set_target_properties (utf8proc PROPERTIES
5353

5454
if (UTF8PROC_INSTALL)
5555
include(GNUInstallDirs)
56-
install(FILES utf8proc.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}")
56+
install(FILES utf8proc.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
5757
install(TARGETS utf8proc
58-
ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
59-
LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}"
60-
RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}"
58+
EXPORT utf8proc-targets
59+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
60+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
61+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
6162
)
6263
configure_file(libutf8proc.pc.cmakein libutf8proc.pc @ONLY)
63-
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libutf8proc.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig")
64+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libutf8proc.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
65+
# Install CMake targets file.
66+
install(EXPORT utf8proc-targets FILE utf8proc-targets.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/utf8proc" NAMESPACE utf8proc::)
67+
include(CMakePackageConfigHelpers)
68+
configure_package_config_file(
69+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/utf8proc-config.cmake.in"
70+
"${CMAKE_CURRENT_BINARY_DIR}/utf8proc-config.cmake"
71+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/utf8proc"
72+
NO_SET_AND_CHECK_MACRO
73+
)
74+
write_basic_package_version_file(
75+
"${CMAKE_CURRENT_BINARY_DIR}/utf8proc-config-version.cmake"
76+
COMPATIBILITY SameMajorVersion
77+
)
78+
install(FILES
79+
"${CMAKE_CURRENT_BINARY_DIR}/utf8proc-config.cmake"
80+
"${CMAKE_CURRENT_BINARY_DIR}/utf8proc-config-version.cmake"
81+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/utf8proc"
82+
)
6483
endif()
6584

6685
if(UTF8PROC_ENABLE_TESTING)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ gmake CC=/opt/aCC/bin/aCC CFLAGS="+O2" PICFLAG="+z" C99FLAG="-Ae" WCFLAGS="+w" L
5353
```
5454
To run `gmake install` you will need GNU coreutils for the `install` command, and you may want to pass `prefix=/opt libdir=/opt/lib/hpux32` or similar to change the installation location.
5555

56+
### Using with CMake
57+
58+
A CMake Config-file package is provided. To use utf8proc in a CMake project:
59+
60+
```cmake
61+
add_executable (app app.c)
62+
find_package (utf8proc 2.9.0 REQUIRED)
63+
target_link_libraries (app PRIVATE utf8proc::utf8proc)
64+
```
65+
5666
## General Information
5767

5868
The C library is found in this directory after successful compilation

cmake/utf8proc-config.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
include("${CMAKE_CURRENT_LIST_DIR}/utf8proc-targets.cmake")
3+
# `check_required_components` is used to ensure consumer did not erroneously request components.
4+
# No components are currently defined.
5+
check_required_components(utf8proc)

test/app/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This is a test app to test consuming utf8proc with CMake.
2+
cmake_minimum_required(VERSION 3.16)
3+
project(utf8proc-test)
4+
find_package(utf8proc REQUIRED)
5+
add_executable(app app.c)
6+
target_link_libraries(app utf8proc::utf8proc)

test/app/app.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <stdio.h>
2+
#include <utf8proc.h>
3+
4+
int
5+
main(void)
6+
{
7+
printf("%s\n", utf8proc_version());
8+
return 0;
9+
}

0 commit comments

Comments
 (0)