Merge pull request #150 from jonpetri/jonpetri/cmake-improvements

This commit is contained in:
Tristan Penman 2022-01-31 19:54:45 +11:00 committed by GitHub
commit 80afdef597
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 6 deletions

View File

@ -3,9 +3,8 @@ project(valijson)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
option(valijson_INSTALL_HEADERS "Install valijson headers." FALSE)
option(valijson_BUILD_EXAMPLES "Build valijson examples." FALSE)
option(valijson_BUILD_TESTS "Build valijson test suite." TRUE)
option(valijson_BUILD_TESTS "Build valijson test suite." FALSE)
option(valijson_EXCLUDE_BOOST "Exclude Boost when building test suite." FALSE)
option(valijson_USE_EXCEPTIONS "Use exceptions in valijson and included libs." TRUE)
@ -35,10 +34,21 @@ target_include_directories(valijson INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(valijson_INSTALL_HEADERS)
install(DIRECTORY include/ DESTINATION include)
if(valijson_USE_EXCEPTIONS)
target_compile_definitions(valijson INTERFACE -DVALIJSON_USE_EXCEPTIONS=1)
endif()
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS valijson
EXPORT valijsonConfig
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(EXPORT valijsonConfig
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/valijson"
)
if(NOT valijson_BUILD_TESTS AND NOT valijson_BUILD_EXAMPLES)
return()
endif()

View File

@ -65,6 +65,7 @@ Note that Valijson's `SchemaParser` and `Validator` classes expect you to pass i
### Exceptions
By default, Valijson classes will not throw exceptions (e.g. when failing to parse a schema). To enable exceptions for these cases, `VALIJSON_USE_EXCEPTIONS` must be defined.
However note that `VALIJSON_USE_EXCEPTIONS` is defined as interface compile definition of the cmake target, and the definition populates all the targets linking Valijson with cmake.
### Strong vs Weak Types
@ -141,13 +142,17 @@ The examples and test suite can be built using cmake:
# Build examples and test suite
mkdir build
cd build
cmake ..
cmake .. -Dvalijson_BUILD_TESTS=ON -Dvalijson_BUILD_EXAMPLES=ON
make
# Run test suite (from build directory)
./test_suite
```
#### How to add this library to your cmake target ####
## How to add this library to your cmake target ##
Valijson can be integrated either as git submodule or with find_package().
### Valijson as git submodule ###
Download this repository into your project
@ -172,7 +177,24 @@ add_executable(your-executable ...)
target_link_libraries(your-executable ValiJSON::valijson)
```
### Install Valijson and import it ###
It is possible to install headers by running cmake's install command from the build tree. Once Valijson is installed, use it from other CMake projects using `find_package(Valijson)` in your CMakeLists.txt.
```bash
# Install Valijson
git clone --depth=1 git@github.com:tristanpenman/valijson.git
cd valijson
mkdir build
cd build
cmake ..
cmake --install .
```
```cmake
# Import installed valijson and link it to your executable
find_package(valijson REQUIRED)
add_executable(executable main.cpp)
target_link_libraries(executable valijson)
```
### Xcode ###
An Xcode project has also been provided, in the 'xcode' directory. Note that in order to run the test suite, you may need to configure the working directory for the 'test\_suite' scheme. It is recommended that you use the 'xcode' directory as the working directory.