Skip to content

Commit b25d794

Browse files
author
cxxsucks
authored
Fix CMake export error when added as sub directory by offering installation option (#107)
1 parent 27cc6a2 commit b25d794

9 files changed

Lines changed: 124 additions & 7 deletions

File tree

.github/workflows/cmake.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,40 @@ jobs:
101101
working-directory: examples/cmake_installed/build
102102
run: ./foo
103103

104+
build_cmake_subdir:
105+
runs-on: ubuntu-latest
106+
strategy:
107+
fail-fast: false
108+
matrix:
109+
BUILD_TYPE: [Release, Debug]
110+
111+
steps:
112+
- uses: actions/checkout@v2
113+
114+
- name: Configure the library dependent on RapidFuzz
115+
working-directory: examples/cmake_export
116+
run: cmake -B build -DCMAKE_BUILD_TYPE=${{matrix.BUILD_TYPE}}
117+
118+
- name: Build the library dependent on RapidFuzz
119+
working-directory: examples/cmake_export
120+
run: cmake --build build --config ${{matrix.BUILD_TYPE}}
121+
122+
- name: Install the library dependent on RapidFuzz
123+
working-directory: examples/cmake_export
124+
run: sudo cmake --build build --target install
125+
126+
- name: Configure the app indirectly dependent on RapidFuzz
127+
working-directory: examples/cmake_export/indirect_app
128+
run: cmake -B build -DCMAKE_BUILD_TYPE=${{matrix.BUILD_TYPE}}
129+
130+
- name: Build the app indirectly dependent on RapidFuzz
131+
working-directory: examples/cmake_export/indirect_app
132+
run: cmake --build build --config ${{matrix.BUILD_TYPE}}
133+
134+
- name: Run the app indirectly dependent on RapidFuzz
135+
working-directory: examples/cmake_export/indirect_app/build
136+
run: ./fooapp
137+
104138
build_cpack_installed:
105139
runs-on: ubuntu-latest
106140

CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ endif()
99
# disable testsuite in that case
1010
if(NOT DEFINED PROJECT_NAME)
1111
set(NOT_SUBPROJECT ON)
12+
# If RapidFuzz is not being used as a subproject via `add_subdirectory`,
13+
# usually installation is required
14+
option(RAPIDFUZZ_INSTALL "Install rapidfuzz" ON)
1215
else()
1316
set(NOT_SUBPROJECT OFF)
17+
# If RapidFuzz is being used as a subproject via `add_subdirectory`,
18+
# chances are that the "main project" does not include RapidFuzz headers
19+
# in any of its headers, in which case installation is not needed.
20+
option(RAPIDFUZZ_INSTALL "Install rapidfuzz (Projects embedding rapidfuzz may want to turn this OFF.)" OFF)
1421
endif()
1522

1623
option(RAPIDFUZZ_BUILD_TESTING "Build tests" OFF)
@@ -73,9 +80,7 @@ if(RAPIDFUZZ_BUILD_FUZZERS)
7380
add_subdirectory(fuzzing)
7481
endif()
7582

76-
# Only perform the installation steps when RapidFuzz is not being used as
77-
# a subproject via `add_subdirectory`
78-
if (NOT_SUBPROJECT)
83+
if (RAPIDFUZZ_INSTALL)
7984
set(RAPIDFUZZ_CMAKE_CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/rapidfuzz")
8085

8186
install(
@@ -135,4 +140,4 @@ if (NOT_SUBPROJECT)
135140
set(CPACK_PACKAGE_CONTACT "https://github.com/maxbachmann/rapidfuzz-cpp")
136141
include(CPack)
137142

138-
endif(NOT_SUBPROJECT)
143+
endif(RAPIDFUZZ_INSTALL)

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ It will be downloaded each time you run CMake in a blank folder.
9898

9999
There are CMake options available:
100100

101-
1. `BUILD_TESTS` : to build test (default OFF and requires [Catch2](https://github.com/catchorg/Catch2))
102-
103-
2. `BUILD_BENCHMARKS` : to build benchmarks (default OFF and requires [Google Benchmark](https://github.com/google/benchmark))
101+
1. `RAPIDFUZZ_BUILD_TESTING` : to build test (default OFF and requires [Catch2](https://github.com/catchorg/Catch2))
102+
2. `RAPIDFUZZ_BUILD_BENCHMARKS` : to build benchmarks (default OFF and requires [Google Benchmark](https://github.com/google/benchmark))
103+
3. `RAPIDFUZZ_INSTALL` : to install the library to local computer
104+
- When configured independently, installation is on.
105+
- When used as a subproject, the installation is turned off by default.
106+
- For library developers, you might want to toggle the behavior depending on your project.
107+
- If your project is exported via `CMake`, turn installation on or export error will result.
108+
- If your project publicly depends on `RapidFuzz` (includes `rapidfuzz.hpp` in header),
109+
turn installation on or apps depending on your project would face include errors.
104110

105111
## Usage
106112
```cpp
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(foo LANGUAGES CXX VERSION 0.0.1)
3+
4+
# The example library publicly dependent on RapidFuzz (includes
5+
# rapidfuzz.hpp in foo_lib.hpp), necessitating RapidFuzz's installation
6+
set(RAPIDFUZZ_INSTALL ON CACHE INTERNAL "")
7+
add_subdirectory(${CMAKE_SOURCE_DIR}/../..
8+
${CMAKE_SOURCE_DIR}/../../build)
9+
10+
add_library(foo foo_lib.cc)
11+
add_library(foo::foo ALIAS foo)
12+
target_link_libraries(foo rapidfuzz)
13+
14+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
15+
include(GNUInstallDirs)
16+
include(CMakePackageConfigHelpers)
17+
18+
set(FOO_CMAKE_CONFIG_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/foo")
19+
install(TARGETS foo EXPORT fooTargs DESTINATION ${CMAKE_INSTALL_LIBDIR})
20+
install(EXPORT fooTargs NAMESPACE foo:: DESTINATION ${FOO_CMAKE_CONFIG_DESTINATION})
21+
22+
configure_package_config_file(
23+
${CMAKE_CURRENT_LIST_DIR}/${PROJECT_NAME}Config.cmake.in
24+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
25+
INSTALL_DESTINATION ${FOO_CMAKE_CONFIG_DESTINATION}
26+
)
27+
write_basic_package_version_file(
28+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
29+
COMPATIBILITY SameMajorVersion
30+
)
31+
32+
install(
33+
FILES
34+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
35+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
36+
DESTINATION
37+
${FOO_CMAKE_CONFIG_DESTINATION}
38+
)
39+
install(FILES foo_lib.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@PACKAGE_INIT@
2+
3+
# Avoid repeatedly including the targets
4+
if(NOT TARGET foo::foo)
5+
find_package(rapidfuzz REQUIRED)
6+
# Provide path for scripts
7+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
8+
9+
include(${CMAKE_CURRENT_LIST_DIR}/fooTargs.cmake)
10+
endif()

examples/cmake_export/foo_lib.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "foo_lib.hpp"
2+
3+
double fooFunc() {
4+
std::string_view a("aaaa"), b("abaa");
5+
FooType cache(a.begin(), a.end());
6+
return cache.similarity(b);
7+
}

examples/cmake_export/foo_lib.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <rapidfuzz/fuzz.hpp>
2+
3+
using FooType = rapidfuzz::fuzz::CachedRatio<char>;
4+
double fooFunc();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(fooapp LANGUAGES CXX VERSION 0.0.1)
3+
find_package(foo REQUIRED)
4+
add_executable(fooapp foo_app.cc)
5+
target_link_libraries(fooapp foo::foo)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <iostream>
2+
#include <foo_lib.hpp>
3+
4+
int main() {
5+
std::cout << fooFunc() << '\n';
6+
return 0;
7+
}

0 commit comments

Comments
 (0)