Skip to content
Snippets Groups Projects
CMakeLists.txt 3.65 KiB
project(json-validator
    LANGUAGES CXX)

set(PROJECT_VERSION 2.0.0)

cmake_minimum_required(VERSION 3.2)

option(BUILD_TESTS      "Build tests"    ON)
option(BUILD_EXAMPLES   "Build examples" ON)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules")

find_package(LIBUBOX REQUIRED)

add_library(libubox INTERFACE)
target_include_directories(libubox
INTERFACE
	${LIBUBOX_LIBRARIES}
)

set(NLOHMANN_JSON_DIR "" CACHE STRING "path to json.hpp")

# find nlohmann's json.hpp
find_path(JSON_HPP nlohmann/json.hpp
PATHS
    ${NLOHMANN_JSON_DIR}
    ${CMAKE_BINARY_DIR}/${NLOHMANN_JSON_DIR}) # in case it is a relative path

# get the full, real path
get_filename_component(NLOHMANN_JSON_REALPATH ${JSON_HPP} REALPATH)

if(NOT EXISTS ${NLOHMANN_JSON_REALPATH}/nlohmann/json.hpp)
    message(FATAL_ERROR "please set NLOHMANN_JSON_DIR to a path in which NLohmann's json.hpp can be found. Looking for nlohmann/json.hpp in '${NLOHMANN_JSON_REALPATH}")
endif()

# create an interface-library for simple cmake-linking
add_library(json-hpp INTERFACE)
target_include_directories(json-hpp
INTERFACE
    ${NLOHMANN_JSON_REALPATH})

set(JSON_SCHEMA_DIR "" CACHE STRING "path to json-schema.hpp")

find_path(VALIDATOR_HPP json-schema.hpp
PATHS
    ${JSON_SCHEMA_DIR}
    ${CMAKE_BINARY_DIR}/${JSON_SCHEMA_DIR}) # in case it is a relative path

# get the full, real path
get_filename_component(JSON_SCHEMA_REALPATH ${VALIDATOR_HPP} REALPATH)

if(NOT EXISTS ${JSON_SCHEMA_REALPATH}/json-schema.hpp)
    message(FATAL_ERROR "please set JSON_SCHEMA_DIR to a path in which NLohmann's json.hpp can be found. Looking for nlohmann/json.hpp in '${JSON_SCHEMA_REALPATH}")
endif()

set(PUBLIC_HEADERS ${PROJECT_SOURCE_DIR}/src/json-validator.h)

# create an interface-library for simple cmake-linking
add_library(validator-hpp INTERFACE)
target_include_directories(validator-hpp
INTERFACE
    ${VALIDATOR_REAL_PATH})

# and one for the validator
add_library(json-validator
    SHARED
	src/json-validator.cpp
	src/schema.cpp
	)
set_target_properties(json-validator
                      PROPERTIES
                          VERSION ${PROJECT_VERSION}
                          SOVERSION 1)

install(TARGETS json-validator
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        RUNTIME DESTINATION bin)

install(DIRECTORY src/
        DESTINATION include
        FILES_MATCHING PATTERN "json-validator.h")

target_include_directories(json-validator
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/src)

target_compile_features(json-validator
    PUBLIC
        cxx_range_for) # for C++11 - flags

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    target_compile_options(json-validator
        PRIVATE
            -Wall -Wextra)
endif()

target_link_libraries(json-validator
    PUBLIC
    validator-hpp)

target_link_libraries(json-validator
    PUBLIC
    json-hpp)

target_link_libraries(json-validator
    PUBLIC
    ${LIBUBOX_LIBRARIES})

# regex with boost if gcc < 4.9 - default is std::regex
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0")
        find_package(Boost COMPONENTS regex)
        if(NOT Boost_FOUND)
            message(STATUS "GCC less then 4.9 and boost-regex NOT found - no regex used")
            target_compile_definitions(json-validator)
        else()
            message(STATUS "GCC less then 4.9 and boost-regex FOUND - using boost::regex")
            target_compile_definitions(json-validator)
            target_include_directories(json-validator PRIVATE ${Boost_INCLUDE_DIRS})
            target_link_libraries(json-validator PRIVATE ${Boost_LIBRARIES})
        endif()
    endif()
endif()