Skip to content

Commit

Permalink
Added CMake commands to install library for find_package. (#3)
Browse files Browse the repository at this point in the history
- Updated README with developer instructions.
- Added action to check for correct installation.
  • Loading branch information
LTLA committed Aug 10, 2023
1 parent 3a139ce commit 7ed234d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 6 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/check-install.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
on:
push:
branches:
- master
pull_request:

name: Check CMake install

jobs:
install:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Get latest CMake
uses: lukka/get-cmake@latest

- name: Configure the build
run: cmake -S . -B build -DMILLIJSON_TESTS=OFF

- name: Install the library
run: sudo cmake --install build

- name: Test downstream usage
run: |
mkdir _downstream
touch _downstream/source.cpp
cat << EOF > _downstream/CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(test_install)
add_executable(whee source.cpp)
find_package(ltla_millijson)
target_link_libraries(whee ltla::millijson)
EOF
cd _downstream && cmake -S . -B build
33 changes: 31 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,44 @@ project(millijson
set(CMAKE_CXX_STANDARD 17)

add_library(millijson INTERFACE)
add_library(ltla::millijson ALIAS millijson)

target_include_directories(millijson INTERFACE include)
include(GNUInstallDirs)
target_include_directories(millijson INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/ltla_millijson>")

# Building the test-related machinery, if we are compiling this library directly.
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
option(INSTALL_GTEST OFF)
option(MILLIJSON_TESTS "Build millijson's test suite." ON)
else()
option(MILLIJSON_TESTS "Build millijson's test suite." OFF)
endif()

if(MILLIJSON_TESTS)
include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
endif()

# Setting up the installation commands.
include(CMakePackageConfigHelpers)

install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ltla_millijson)

install(TARGETS millijson
EXPORT millijsonTargets)

install(EXPORT millijsonTargets
FILE ltla_millijsonTargets.cmake
NAMESPACE ltla::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ltla_millijson)

configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/ltla_millijsonConfig.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ltla_millijson)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ltla_millijsonConfig.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ltla_millijson)
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Yet another JSON parser

![Unit tests](https://github.com/LTLA/millijson/actions/workflows/run-tests.yaml/badge.svg)
![Documentation](https://github.com/LTLA/millijson/actions/workflows/doxygenate.yaml/badge.svg)
[![codecov](https://codecov.io/gh/LTLA/millijson/branch/master/graph/badge.svg?token=DLTMWKJAG3)](https://codecov.io/gh/LTLA/millijson)
![Unit tests](https://github.com/ArtifactDB/millijson/actions/workflows/run-tests.yaml/badge.svg)
![Documentation](https://github.com/ArtifactDB/millijson/actions/workflows/doxygenate.yaml/badge.svg)
[![codecov](https://codecov.io/gh/ArtifactDB/millijson/branch/master/graph/badge.svg?token=DLTMWKJAG3)](https://codecov.io/gh/ArtifactDB/millijson)

## Overview

Expand Down Expand Up @@ -54,6 +54,8 @@ See the [reference documentation](https://ltla.github.io/millijson) for more det
## Building projects
### CMake with `FetchContent`
If you're using CMake, you just need to add something like this to your `CMakeLists.txt`:
```
Expand All @@ -78,7 +80,27 @@ target_link_libraries(myexe millijson)
target_link_libraries(mylib INTERFACE millijson)
```
Or you can just copy the [`millijson.hpp`](include/millijson/millijson.hpp) file into your source directory.
### CMake with `find_package()`
You can install the library by cloning a suitable version of this repository and running the following commands:
```sh
mkdir build && cd build
cmake .. -DMILLIJSON_TESTS=OFF
cmake --build . --target install
```

Then you can use `find_package()` as usual:

```cmake
find_package(ltla_millijson CONFIG REQUIRED)
target_link_libraries(mylib INTERFACE ltla::millijson)
```

### Manual

If you're not using CMake, the simple approach is to just copy the files in the `include/` subdirectory -
either directly or with Git submodules - and include their path during compilation with, e.g., GCC's `-I`.

## Links

Expand Down
3 changes: 3 additions & 0 deletions cmake/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/ltla_millijsonTargets.cmake")
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ FetchContent_Declare(

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

# Avoid installing GoogleTest when installing this project.
option(INSTALL_GTEST "Enable installation of googletest." OFF)

FetchContent_MakeAvailable(googletest)

enable_testing()
Expand Down

0 comments on commit 7ed234d

Please sign in to comment.