Skip to content

Commit

Permalink
Refactor CMakeLists
Browse files Browse the repository at this point in the history
* Overhaul of dealing with CMake use cases
* Put target property setup into a function
* Fix test standalone_with_boost for CMake
* Other minor fixes/edits
  • Loading branch information
grisumbras committed Jun 28, 2021
1 parent 445efad commit 745be88
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 157 deletions.
228 changes: 115 additions & 113 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
# Copyright (c) 2019 Vinnie Falco ([email protected])
# Copyright (c) 2021 DMitry Arkhipov ([email protected])
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/json
#

cmake_minimum_required(VERSION 3.5...3.16)
cmake_minimum_required(VERSION 3.8...3.16)

set(BOOST_JSON_VERSION 2)
if(BOOST_SUPERPROJECT_VERSION)
Expand All @@ -16,152 +17,153 @@ endif()

project(boost_json VERSION "${BOOST_JSON_VERSION}" LANGUAGES CXX)

option(BOOST_JSON_STANDALONE "Build boost::json as a standalone library" OFF)
option(BOOST_JSON_BUILD_TESTS "Build boost::json tests" ON)
option(BOOST_JSON_BUILD_FUZZERS "Build boost::json fuzzers" ON)
option(BOOST_JSON_BUILD_EXAMPLES "Build boost::json examples" ON)
option(BOOST_JSON_BUILD_BENCHMARKS "Build boost::json benchmarks" OFF)

file(GLOB_RECURSE BOOST_JSON_HEADERS $<$<VERSION_GREATER_EQUAL:${CMAKE_VERSION},3.12>:CONFIGURE_DEPENDS>
set(BOOST_JSON_IS_ROOT OFF)
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(BOOST_JSON_IS_ROOT ON)
endif()

if(BOOST_JSON_IS_ROOT)
include(CTest)
endif()
if(NOT BOOST_SUPERPROJECT_VERSION)
option(BOOST_JSON_STANDALONE "Build boost::json as a standalone library" OFF)
option(BOOST_JSON_INSTALL "Install boost::json files" ON)
option(BOOST_JSON_BUILD_TESTS "Build boost::json tests" ${BUILD_TESTING})
option(BOOST_JSON_BUILD_FUZZERS "Build boost::json fuzzers" ${BOOST_JSON_IS_ROOT})
option(BOOST_JSON_BUILD_EXAMPLES "Build boost::json examples" ${BOOST_JSON_IS_ROOT})
option(BOOST_JSON_BUILD_BENCHMARKS "Build boost::json benchmarks" OFF)
else()
set(BOOST_JSON_BUILD_TESTS ${BUILD_TESTING})
endif()


include(GNUInstallDirs)
if(BOOST_JSON_IS_ROOT AND NOT BOOST_JSON_STANDALONE)
#
# Building inside Boost tree, but as a separate project e.g. on Travis or
# other CI, or when producing Visual Studio Solution and Projects. This is
# when we have to add dependencies to the project manually.
foreach(lib
# direct dependencies
align
assert
config
container
exception
mp11
system
throw_exception
utility

# indirect dependencies
container_hash
core
detail
integer
intrusive
io
move
predef
preprocessor
smart_ptr
static_assert
tuple
type_traits
winapi
)
add_subdirectory(../${lib} libs/${lib} EXCLUDE_FROM_ALL)
set(target boost_${lib})
if(TARGET ${target})
get_target_property(lib_type ${target} TYPE)
if(NOT ${lib_type} STREQUAL INTERFACE_LIBRARY)
set_property(TARGET ${target} PROPERTY FOLDER _deps/${lib})
endif()
endif()
endforeach()
endif()


function(boost_json_setup_properties target)
target_compile_features(${target} PUBLIC cxx_constexpr)
target_compile_definitions(${target} PUBLIC BOOST_JSON_NO_LIB=1)

if(BOOST_SUPERPROJECT_VERSION)
target_include_directories(${target} PUBLIC "${PROJECT_SOURCE_DIR}/include")
else()
target_include_directories(${target}
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
endif()

if(BOOST_JSON_STANDALONE)
target_compile_definitions(${target} PUBLIC BOOST_JSON_STANDALONE)
target_compile_features(${target} PUBLIC cxx_std_17)
else()
target_link_libraries(${target}
PUBLIC
Boost::align
Boost::assert
Boost::config
Boost::container
Boost::exception
Boost::mp11
Boost::system
Boost::throw_exception
Boost::utility
)
endif()
endfunction()


file(GLOB_RECURSE BOOST_JSON_HEADERS CONFIGURE_DEPENDS
include/boost/*.hpp
include/boost/*.ipp
include/boost/*.natvis
)

set(BOOST_JSON_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/src.cpp
)
set(BOOST_JSON_SOURCES src/src.cpp)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/boost PREFIX "" FILES ${BOOST_JSON_HEADERS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "" FILES ${BOOST_JSON_SOURCES})

# TODO: For Boost superproject, do we want to support header-only mode?
# Then, this needs to read `add_library(boost_json INTERFACE)`
# and related settings need to be INTERFACE-ed as well.

add_library(boost_json ${BOOST_JSON_HEADERS} ${BOOST_JSON_SOURCES})
add_library(Boost::json ALIAS boost_json)

target_compile_features(boost_json PUBLIC cxx_constexpr)

# TODO: For Boost superproject, this may need to be INTERFACE setting.
include(GNUInstallDirs)
if(BOOST_SUPERPROJECT_VERSION)
target_include_directories(boost_json PUBLIC include)
else()
target_include_directories(boost_json
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
endif()

target_compile_definitions(boost_json PUBLIC BOOST_JSON_NO_LIB=1)
boost_json_setup_properties(boost_json)

if(BUILD_SHARED_LIBS)
target_compile_definitions(boost_json PUBLIC BOOST_JSON_DYN_LINK=1)
else()
target_compile_definitions(boost_json PUBLIC BOOST_JSON_STATIC_LINK=1)
endif()

if(BOOST_JSON_STANDALONE)
#
# Building out of Boost superproject tree, without Boost as dependency.
# e.g. for packaging or added with add_subdirectory.
#
target_compile_definitions(boost_json PUBLIC BOOST_JSON_STANDALONE)
target_compile_features(boost_json PUBLIC cxx_std_17)

elseif(BOOST_SUPERPROJECT_VERSION)
#
# Building as part of Boost superproject tree, with Boost as dependency.
#
target_link_libraries(boost_json
PUBLIC
Boost::align
Boost::assert
Boost::config
Boost::container
Boost::exception
Boost::mp11
Boost::system
Boost::throw_exception
Boost::utility
)

elseif(BOOST_JSON_IN_BOOST_TREE)
#
# Building inside Boost tree, out of Boost superproject tree, with Boost as dependency.
# e.g. on Travis or other CI, or when producing Visual Studio Solution and Projects.
#
get_filename_component(BOOST_ROOT ../.. ABSOLUTE)
target_include_directories(boost_json PUBLIC ${BOOST_ROOT})
target_link_directories(boost_json PUBLIC ${BOOST_ROOT}/stage/lib)

else()
#
# Building out of Boost tree, out of Boost superproject tree, with Boost as dependency.
# e.g. for packaging or added with add_subdirectory.
#
find_package(Boost REQUIRED COMPONENTS container system)
target_link_libraries(boost_json
PUBLIC
Boost::container
Boost::system
)
endif()

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND NOT BOOST_JSON_IN_BOOST_TREE)
set_target_properties(boost_json PROPERTIES EXPORT_NAME json)
if(BOOST_JSON_INSTALL AND NOT BOOST_SUPERPROJECT_VERSION)
install(TARGETS boost_json
EXPORT boost_json_targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)

install(EXPORT boost_json_targets
FILE boost_json-targets.cmake
NAMESPACE Boost::
DESTINATION lib/cmake/boost_json
)

include(CMakePackageConfigHelpers)

configure_package_config_file(cmake/config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/boost_json-config.cmake
INSTALL_DESTINATION lib/cmake/boost_json
)

write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/boost_json-config-version.cmake
VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion
)

install(FILES
${CMAKE_CURRENT_BINARY_DIR}/boost_json-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/boost_json-config-version.cmake
DESTINATION lib/cmake/boost_json
)

install(DIRECTORY include/ DESTINATION include)
endif()


if(BOOST_JSON_BUILD_TESTS)
include(CTest)
if(BUILD_TESTING)
add_subdirectory(test)
endif()
add_subdirectory(test)
endif()

if(BOOST_JSON_BUILD_FUZZERS AND NOT BOOST_SUPERPROJECT_VERSION)
if(BOOST_JSON_BUILD_FUZZERS)
add_subdirectory(fuzzing)
endif()

if(BOOST_JSON_BUILD_EXAMPLES AND NOT BOOST_SUPERPROJECT_VERSION)
if(BOOST_JSON_BUILD_EXAMPLES)
add_subdirectory(example)
endif()

if(BOOST_JSON_BUILD_BENCHMARKS AND NOT BOOST_SUPERPROJECT_VERSION)
if(BOOST_JSON_BUILD_BENCHMARKS)
add_subdirectory(bench)
endif()
10 changes: 0 additions & 10 deletions cmake/config.cmake.in

This file was deleted.

58 changes: 24 additions & 34 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,63 +1,53 @@
#
# Copyright (c) 2019 Vinnie Falco ([email protected])
# Copyright (c) 2021 DMitry Arkhipov ([email protected])
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#
# Official repository: https://github.com/boostorg/json
#

file(GLOB_RECURSE BOOST_JSON_TESTS_FILES CONFIGURE_DEPENDS Jamfile *.cpp *.hpp)
list(FILTER BOOST_JSON_TESTS_FILES EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/cmake_install_test/.*$")
if(BOOST_JSON_STANDALONE)
list(FILTER BOOST_JSON_TESTS_FILES EXCLUDE REGEX "standalone_with_boost.cpp")

if(NOT TARGET tests)
add_custom_target(tests)
set_property(TARGET tests PROPERTY FOLDER _deps)
endif()


file(GLOB_RECURSE BOOST_JSON_TESTS_FILES CONFIGURE_DEPENDS Jamfile *.cpp *.hpp)
list(FILTER BOOST_JSON_TESTS_FILES EXCLUDE REGEX cmake_install_test/.*$)
list(FILTER BOOST_JSON_TESTS_FILES EXCLUDE REGEX standalone_with_boost.cpp)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES ${BOOST_JSON_TESTS_FILES})
add_executable(boost_json-tests ${BOOST_JSON_TESTS_FILES})
target_include_directories(boost_json-tests PRIVATE .)
target_link_libraries(boost_json-tests PRIVATE Boost::json)
add_test(NAME boost_json-tests COMMAND boost_json-tests)
add_dependencies(tests boost_json-tests)


source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES limits.cpp main.cpp)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../src PREFIX "" FILES ../src/src.cpp)
add_executable(boost_json-limits limits.cpp main.cpp ../src/src.cpp Jamfile)
boost_json_setup_properties(boost_json-limits)

target_compile_features(boost_json-limits PUBLIC cxx_constexpr)

target_include_directories(boost_json-limits PRIVATE ../include .)
target_compile_definitions(boost_json-limits PRIVATE
BOOST_JSON_MAX_STRING_SIZE=1000
BOOST_JSON_MAX_STRUCTURED_SIZE=20
BOOST_JSON_STACK_BUFFER_SIZE=256
BOOST_JSON_NO_LIB=1
)

if(BOOST_JSON_STANDALONE)
target_compile_definitions(boost_json-limits PRIVATE BOOST_JSON_STANDALONE)
target_compile_features(boost_json-limits PRIVATE cxx_std_17)
elseif(BOOST_SUPERPROJECT_VERSION)
target_link_libraries(boost_json-limits
PRIVATE
Boost::align
Boost::assert
Boost::config
Boost::container
Boost::exception
Boost::mp11
Boost::system
Boost::throw_exception
Boost::utility
)
elseif(BOOST_JSON_IN_BOOST_TREE)
target_include_directories(boost_json-limits PRIVATE ${BOOST_ROOT})
target_link_directories(boost_json-limits PRIVATE ${BOOST_ROOT}/stage/lib)
else()
target_link_libraries(boost_json-limits
PRIVATE
Boost::system
Boost::container
)
endif()

add_test(NAME boost_json-limits COMMAND boost_json-limits)
add_dependencies(tests boost_json-limits)


if(NOT BOOST_JSON_STANDALONE)
add_library(boost_json-standalone_with_boost STATIC standalone_with_boost.cpp)
target_compile_definitions(boost_json-standalone_with_boost PRIVATE BOOST_JSON_STANDALONE)
target_compile_features(boost_json-standalone_with_boost PRIVATE cxx_std_17)
target_link_libraries(boost_json-standalone_with_boost PRIVATE Boost::assert)
boost_json_setup_properties(boost_json-standalone_with_boost)
add_dependencies(tests boost_json-standalone_with_boost)
endif()

0 comments on commit 745be88

Please sign in to comment.