Skip to content

Commit

Permalink
Refactor CMake support:
Browse files Browse the repository at this point in the history
* Add VS CMake integration config
* add cmake 3.10.0 support
* fix compiler and linker flags
* disable verbose build output
* added cmake policy
* Fix source groups
* Group folder related settings
* Add support for the boost superproject and cmake 3.16
* Remove library headers from executable projects
* Fix boost superproject compatibility
  • Loading branch information
Alexej Harm authored and vinniefalco committed Dec 29, 2019
1 parent 61a0691 commit bc95b55
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 214 deletions.
19 changes: 11 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
bin
bin64
*.*#
/bin
/bin64
temp

# Because of CMake and VS2017
Win32/
x64/
out/
.vs/
# Emacs
*#

# Vim
*~

# Visual Studio
/.vs
/out
171 changes: 45 additions & 126 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,148 +7,67 @@
# Official repository: https://github.com/vinniefalco/json
#

cmake_minimum_required (VERSION 3.5.1)
cmake_minimum_required(VERSION 3.5...3.16)

if (POLICY CMP0074)
cmake_policy (SET CMP0074 NEW)
set(BOOST_JSON_VERSION 1)
if(BOOST_SUPERPROJECT_VERSION)
set(BOOST_JSON_VERSION ${BOOST_SUPERPROJECT_VERSION})
endif()

#-------------------------------------------------------------------------------
project(boost_json VERSION ${BOOST_JSON_VERSION} LANGUAGES CXX)

function (DoGroupSources curdir rootdir folder)
file (GLOB children RELATIVE ${PROJECT_SOURCE_DIR}/${curdir} ${PROJECT_SOURCE_DIR}/${curdir}/*)
foreach (child ${children})
if (IS_DIRECTORY ${PROJECT_SOURCE_DIR}/${curdir}/${child})
DoGroupSources (${curdir}/${child} ${rootdir} ${folder})
elseif (${child} STREQUAL "CMakeLists.txt")
source_group("" FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
else()
string (REGEX REPLACE ^${rootdir} ${folder} groupname ${curdir})
string (REPLACE "/" "\\" groupname ${groupname})
source_group (${groupname} FILES ${PROJECT_SOURCE_DIR}/${curdir}/${child})
endif()
endforeach()
endfunction()

function (GroupSources curdir folder)
DoGroupSources (${curdir} ${curdir} ${folder})
endfunction()

#-------------------------------------------------------------------------------
#
# JSON
#
#-------------------------------------------------------------------------------

project (JSON VERSION 1)

set_property (GLOBAL PROPERTY USE_FOLDERS ON)

if (MSVC)
set (CMAKE_VERBOSE_MAKEFILE FALSE)
file(GLOB_RECURSE BOOST_JSON_HEADERS CONFIGURE_DEPENDS
include/boost/*.hpp
include/boost/*.ipp
include/boost/*.natvis
)

add_definitions (
-D_WIN32_WINNT=0x0601
-D_CRT_SECURE_NO_WARNINGS
)
set(BOOST_JSON_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/src.cpp
)

add_compile_options(
/permissive- # strict C++
/W4 # enable all warnings
/MP # Multi-processor compilation
)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

add_link_options(
/INCREMENTAL:NO
)
if(${CMAKE_VERSION} VERSION_GREATER 3.7.2)
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})
endif()

set (Boost_USE_STATIC_LIBS ON)
set (Boost_USE_STATIC_RUNTIME ON)
add_library(boost_json ${BOOST_JSON_HEADERS} ${BOOST_JSON_SOURCES})
add_library(Boost::json ALIAS boost_json)

set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Ob2 /Oi /Ot /GL /MT")
set (CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /Oi /Ot /MT")
target_compile_features(boost_json PUBLIC cxx_constexpr)

set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
set (CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
target_include_directories(boost_json PUBLIC include)

# for RelWithDebInfo builds, disable incremental linking
# since CMake sets it ON by default for that build type and it
# causes warnings
#
string (REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" replacement_flags
${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO})
set (CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO ${replacement_flags})
target_compile_definitions(boost_json PUBLIC BOOST_JSON_NO_LIB=1)

if(BUILD_SHARED_LIBS)
target_compile_definitions(boost_json PUBLIC BOOST_JSON_DYN_LINK=1)
else()
set (THREADS_PREFER_PTHREAD_FLAG ON)
find_package (Threads)

set( CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -Wpedantic -Wno-unused-parameter")

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wrange-loop-analysis")
endif ()
target_compile_definitions(boost_json PUBLIC BOOST_JSON_STATIC_LINK=1)
endif()

# Must come before Boost includes, otherwise the
# IDE sees the wrong file due to boost/ symlinks.
include_directories (include)

#-------------------------------------------------------------------------------
#
# Boost
#
#-------------------------------------------------------------------------------

get_filename_component (BOOST_ROOT ../../ ABSOLUTE)

# VFALCO I want static but "b2 stage" builds a minimal set which excludes static
add_definitions (-DBOOST_ALL_STATIC_LINK=1)

include_directories (${BOOST_ROOT})

link_directories(${BOOST_ROOT}/stage/lib)

#-------------------------------------------------------------------------------

if ("${VARIANT}" STREQUAL "coverage")
if (MSVC)
else()
set (CMAKE_BUILD_TYPE DEBUG)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2 --coverage")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
endif()

elseif ("${VARIANT}" STREQUAL "ubasan")
if (MSVC)
else()
set (CMAKE_BUILD_TYPE RELWITHDEBINFO)
set (CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -msse4.2 -funsigned-char -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=address,undefined -fsanitize-blacklist=${PROJECT_SOURCE_DIR}/tools/blacklist.supp")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,undefined -fno-sanitize-recover=address,undefined")
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
if(${CMAKE_VERSION} VERSION_LESS 3.16)
message(FATAL_ERROR "Boost.JSON development requires CMake 3.16 or newer.")
endif()

elseif ("${VARIANT}" STREQUAL "debug")
set (CMAKE_BUILD_TYPE DEBUG)

elseif ("${VARIANT}" STREQUAL "release")
set (CMAKE_BUILD_TYPE RELEASE)
get_filename_component(BOOST_ROOT ../.. ABSOLUTE)
target_include_directories(boost_json PUBLIC ${BOOST_ROOT})
target_link_directories(boost_json PUBLIC ${BOOST_ROOT}/stage/lib)

add_subdirectory(bench)
add_subdirectory(example)
add_subdirectory(test)
else()
target_link_libraries(boost_json
PUBLIC
Boost::assert
Boost::config
Boost::core
Boost::exception
Boost::system
Boost::utility
)
endif()

#-------------------------------------------------------------------------------
#
# Tests and examples
#

file (GLOB_RECURSE PROJECT_FILES
${PROJECT_SOURCE_DIR}/include/boost/json/*.hpp
${PROJECT_SOURCE_DIR}/include/boost/json/*.ipp
${PROJECT_SOURCE_DIR}/include/boost/json/*.natvis
)

add_subdirectory (bench)
add_subdirectory (example)
add_subdirectory (test)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ The design of the library also achieves these goals:
* Security-aware treatment of untrusted inputs
* Fast compilation performance

## CMake

cmake -G "Visual Studio 16 2019" -A Win32 -B bin -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/msvc.cmake
cmake -G "Visual Studio 16 2019" -A x64 -B bin64 -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains/msvc.cmake

## License

Distributed under the Boost Software License, Version 1.0.
Expand Down
37 changes: 8 additions & 29 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,17 @@
# Official repository: https://github.com/vinniefalco/json
#

source_group (json FILES ${PROJECT_SOURCE_DIR}/include/boost/json.hpp)
source_group (json FILES ${PROJECT_SOURCE_DIR}/include/boost/pilfer.hpp)
source_group (TREE ${PROJECT_SOURCE_DIR}/include/boost/json PREFIX json FILES ${PROJECT_FILES})

GroupSources(bench "/")

# Disable auto-linking
add_definitions(-DBOOST_ALL_NO_LIB=1)

include_directories(../test)

if (MSVC)
if (CMAKE_BUILD_TYPE EQUAL "Debug")
else()
add_compile_options(
/GL # Whole program optimization
/Ob2 # inline any suitable
)

add_link_options(
/LTCG # Link Time Code Generation
)
endif()
endif()
source_group("" FILES
Jamfile
bench.cpp
)

add_executable (bench
${PROJECT_FILES}
${PROJECT_SOURCE_DIR}/include/boost/json.hpp
${PROJECT_SOURCE_DIR}/include/boost/pilfer.hpp
${PROJECT_SOURCE_DIR}/src/src.cpp
add_executable(bench
Jamfile
bench.cpp
)

target_include_directories(bench PRIVATE ../test)
target_link_libraries(bench PRIVATE boost_json)

add_test(json-tests json-tests)
5 changes: 5 additions & 0 deletions cmake/toolchains/clang.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Include gcc options.
include(${CMAKE_CURRENT_LIST_DIR}/gcc.cmake)

# Compiler options.
add_compile_options(-Wrange-loop-analysis)
18 changes: 18 additions & 0 deletions cmake/toolchains/common.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# C++ standard.
set(CMAKE_CXX_STANDARD 11 CACHE STRING "")
set(CMAKE_CXX_STANDARD_REQUIRED ON CACHE STRING "")
set(CMAKE_CXX_EXTENSIONS OFF CACHE STRING "")

# Static library linkage.
set(BUILD_SHARED_LIBS OFF CACHE STRING "")
add_definitions(-DBOOST_ALL_STATIC_LINK=1)

# Interprocedural optimization.
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON CACHE STRING "")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ON CACHE STRING "")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO ON CACHE STRING "")

# Compiler definitions.
if(WIN32)
add_definitions(-D_WIN32_WINNT=0x0601 -D_CRT_SECURE_NO_WARNINGS)
endif()
5 changes: 5 additions & 0 deletions cmake/toolchains/gcc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Include common options.
include(${CMAKE_CURRENT_LIST_DIR}/common.cmake)

# Compiler options.
add_compile_options(-Wall -Wextra -Wpedantic -Wno-unused-parameter)
28 changes: 28 additions & 0 deletions cmake/toolchains/msvc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Include common options.
include(${CMAKE_CURRENT_LIST_DIR}/common.cmake)

# Static runtime linkage.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>" CACHE STRING "")

# Compiler options.
add_compile_options(
/permissive- # strict C++
/W4 # enable all warnings
/MP # multi-processor compilation
)

# Linker options.
add_link_options(
)

# Disable logos.
foreach(lang C CXX ASM_MASM RC)
set(CMAKE_${lang}_FLAGS_INIT "/nologo")
endforeach()
foreach(type EXE SHARED MODULE)
set(CMAKE_${type}_LINKER_FLAGS_INIT "/nologo")
endforeach()

# Silence Visual Studio CMake integration warnings.
set(SILENCE_VS_DEFINITIONS ${CMAKE_TOOLCHAIN_FILE} ${CMAKE_C_COMPILER})
set(SILENCE_VS_DEFINITIONS)
14 changes: 9 additions & 5 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
# Official repository: https://github.com/vinniefalco/json
#

GroupSources(example "/")
source_group("" FILES
file.hpp
pretty.cpp
validate.cpp
)

add_executable (pretty
${PROJECT_SOURCE_DIR}/src/src.cpp
add_executable(pretty
file.hpp
pretty.cpp
)
set_property(TARGET pretty PROPERTY FOLDER "example")
target_link_libraries(pretty PRIVATE boost_json)

add_executable (validate
${PROJECT_SOURCE_DIR}/src/src.cpp
add_executable(validate
file.hpp
validate.cpp
)
set_property(TARGET validate PROPERTY FOLDER "example")
target_link_libraries(validate PRIVATE boost_json)
Loading

0 comments on commit bc95b55

Please sign in to comment.