Skip to content

Commit d92a2fe

Browse files
author
MazTheMan
committed
Bump CMake version, use target_xyz functions instead of global add_xyz
1 parent 69098a1 commit d92a2fe

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

CMakeLists.txt

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# CMake versions greater than the JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION policies will
1313
# continue to generate policy warnings "CMake Warning (dev)...Policy CMP0XXX is not set:"
1414
#
15-
set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.8.0")
16-
set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.13.2")
15+
set(JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION "3.21..")
16+
set(JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION "3.29")
1717
cmake_minimum_required(VERSION ${JSONCPP_OLDEST_VALIDATED_POLICIES_VERSION})
1818
if("${CMAKE_VERSION}" VERSION_LESS "${JSONCPP_NEWEST_VALIDATED_POLICIES_VERSION}")
1919
#Set and use the newest available cmake policies that are validated to work
@@ -40,12 +40,6 @@ foreach(pold "") # Currently Empty
4040
endif()
4141
endforeach()
4242

43-
# Build the library with C++11 standard support, independent from other including
44-
# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
45-
set(CMAKE_CXX_STANDARD 11)
46-
set(CMAKE_CXX_EXTENSIONS OFF)
47-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
48-
4943
# Ensure that CMAKE_BUILD_TYPE has a value specified for single configuration generators.
5044
if(NOT DEFINED CMAKE_BUILD_TYPE AND NOT DEFINED CMAKE_CONFIGURATION_TYPES)
5145
set(CMAKE_BUILD_TYPE Release CACHE STRING
@@ -75,6 +69,15 @@ project(jsoncpp
7569
VERSION 1.9.5 # <major>[.<minor>[.<patch>[.<tweak>]]]
7670
LANGUAGES CXX)
7771

72+
73+
if (PROJECT_IS_TOP_LEVEL)
74+
# Build the library with C++11 standard support, independent from other including
75+
# software which may use a different CXX_STANDARD or CMAKE_CXX_STANDARD.
76+
set(CMAKE_CXX_STANDARD 11)
77+
set(CMAKE_CXX_EXTENSIONS OFF)
78+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
79+
endif()
80+
7881
message(STATUS "JsonCpp Version: ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
7982
set(PROJECT_SOVERSION 25)
8083

@@ -105,6 +108,8 @@ endif()
105108

106109
set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
107110

111+
add_library(project_options INTERFACE)
112+
108113
configure_file("${PROJECT_SOURCE_DIR}/version.in"
109114
"${PROJECT_BINARY_DIR}/version"
110115
NEWLINE_STYLE UNIX)
@@ -113,53 +118,53 @@ macro(use_compilation_warning_as_error)
113118
if(MSVC)
114119
# Only enabled in debug because some old versions of VS STL generate
115120
# warnings when compiled in release configuration.
116-
add_compile_options($<$<CONFIG:Debug>:/WX>)
121+
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:/WX>)
117122
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
118-
add_compile_options(-Werror)
123+
target_compile_options(project_options INTERFACE -Werror)
119124
if(JSONCPP_WITH_STRICT_ISO)
120-
add_compile_options(-pedantic-errors)
125+
target_compile_options(project_options INTETFACE -pedantic-errors)
121126
endif()
122127
endif()
123128
endmacro()
124129

125130
# Include our configuration header
126-
include_directories(${jsoncpp_SOURCE_DIR}/include)
131+
target_include_directories(project_options INTERFACE ${jsoncpp_SOURCE_DIR}/include)
127132

128133
if(MSVC)
129134
# Only enabled in debug because some old versions of VS STL generate
130135
# unreachable code warning when compiled in release configuration.
131-
add_compile_options($<$<CONFIG:Debug>:/W4>)
136+
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:/W4>)
132137
if (JSONCPP_STATIC_WINDOWS_RUNTIME)
133138
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
134139
endif()
135140
endif()
136141

137142
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
138143
# using regular Clang or AppleClang
139-
add_compile_options(-Wall -Wconversion -Wshadow)
144+
target_compile_options(project_options INTERFACE -Wall -Wconversion -Wshadow)
140145

141146
if(JSONCPP_WITH_WARNING_AS_ERROR)
142-
add_compile_options(-Werror=conversion -Werror=sign-compare)
147+
target_compile_options(project_options INTERFACE -Werror=conversion -Werror=sign-compare)
143148
endif()
144149
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
145150
# using GCC
146-
add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
151+
target_compile_options(project_options INTERFACE -Wall -Wconversion -Wshadow -Wextra)
147152
# not yet ready for -Wsign-conversion
148153

149154
if(JSONCPP_WITH_STRICT_ISO)
150-
add_compile_options(-Wpedantic)
155+
target_compile_options(project_options INTERFACE -Wpedantic)
151156
endif()
152157
if(JSONCPP_WITH_WARNING_AS_ERROR)
153-
add_compile_options(-Werror=conversion)
158+
target_compile_options(project_options INTERFACE -Werror=conversion)
154159
endif()
155160
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
156161
# using Intel compiler
157-
add_compile_options(-Wall -Wconversion -Wshadow -Wextra)
162+
target_compile_options(project_options INTERFACE -Wall -Wconversion -Wshadow -Wextra)
158163

159164
if(JSONCPP_WITH_WARNING_AS_ERROR)
160-
add_compile_options(-Werror=conversion)
165+
target_compile_options(project_options INTERFACE -Werror=conversion)
161166
elseif(JSONCPP_WITH_STRICT_ISO)
162-
add_compile_options(-Wpedantic)
167+
target_compile_options(project_options INTERFACE -Wpedantic)
163168
endif()
164169
endif()
165170

example/CMakeLists.txt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ set(EXAMPLES
55
stringWrite
66
streamWrite
77
)
8-
add_definitions(-D_GLIBCXX_USE_CXX11_ABI)
98

10-
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
11-
add_compile_options(-Wall -Wextra)
12-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
13-
add_definitions(
14-
-D_SCL_SECURE_NO_WARNINGS
15-
-D_CRT_SECURE_NO_WARNINGS
16-
-D_WIN32_WINNT=0x601
17-
-D_WINSOCK_DEPRECATED_NO_WARNINGS
18-
)
9+
macro(add_example_options target)
10+
11+
target_compile_definitions(${target} -D_GLIBCXX_USE_CXX11_ABI)
12+
13+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
14+
target_compile_options(${target} PRIVATE -Wall -Wextra)
15+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
16+
target_compile_definitions(${target}
17+
PRIVATE
18+
-D_SCL_SECURE_NO_WARNINGS
19+
-D_CRT_SECURE_NO_WARNINGS
20+
-D_WIN32_WINNT=0x601
21+
-D_WINSOCK_DEPRECATED_NO_WARNINGS
22+
)
23+
endif()
1924
endif()
2025

2126
foreach(example ${EXAMPLES})
2227
add_executable(${example} ${example}/${example}.cpp)
28+
add_example_options(${example})
2329
target_include_directories(${example} PUBLIC ${CMAKE_SOURCE_DIR}/include)
2430
target_link_libraries(${example} jsoncpp_lib)
2531
endforeach()

src/jsontestrunner/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ add_executable(jsontestrunner_exe
1515

1616
if(BUILD_SHARED_LIBS)
1717
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
18-
add_compile_definitions( JSON_DLL )
18+
target_compile_definitions(jsontestrunner_exe PUBLIC JSON_DLL )
1919
else()
2020
add_definitions(-DJSON_DLL)
2121
endif()

src/lib_json/CMakeLists.txt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
add_library(jsonlib_options INTERFACE)
12
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.1.2)
23
#-Werror=* was introduced -after- GCC 4.1.2
3-
add_compile_options("-Werror=strict-aliasing")
4+
target_compile_options(jsonlib_options INTERFACE "-Werror=strict-aliasing")
45
endif()
56

67
include(CheckIncludeFileCXX)
78
include(CheckTypeSize)
89
include(CheckStructHasMember)
910
include(CheckCXXSymbolExists)
1011

12+
# might have to do this: list(APPEND CMAKE_REQUIRED_INCLUDES ${ASIO_PATH}) <-- with some path that is missing...?
13+
1114
check_include_file_cxx(clocale HAVE_CLOCALE)
1215
check_cxx_symbol_exists(localeconv clocale HAVE_LOCALECONV)
1316

@@ -19,7 +22,7 @@ check_struct_has_member(lconv decimal_point clocale HAVE_DECIMAL_POINT LANGUAGE
1922
if(NOT (HAVE_CLOCALE AND HAVE_LCONV_SIZE AND HAVE_DECIMAL_POINT AND HAVE_LOCALECONV))
2023
message(WARNING "Locale functionality is not supported")
2124
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
22-
add_compile_definitions(JSONCPP_NO_LOCALE_SUPPORT)
25+
target_compile_definitions(jsonlib_options PUBLIC JSONCPP_NO_LOCALE_SUPPORT)
2326
else()
2427
add_definitions(-DJSONCPP_NO_LOCALE_SUPPORT)
2528
endif()
@@ -107,14 +110,13 @@ list(APPEND REQUIRED_FEATURES
107110

108111

109112
if(BUILD_SHARED_LIBS)
113+
set(SHARED_LIB ${PROJECT_NAME}_lib)
114+
add_library(${SHARED_LIB} SHARED ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
110115
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
111-
add_compile_definitions(JSON_DLL_BUILD)
116+
target_compile_definitions(${SHARED_LIB} PUBLIC JSON_DLL_BUILD)
112117
else()
113118
add_definitions(-DJSON_DLL_BUILD)
114119
endif()
115-
116-
set(SHARED_LIB ${PROJECT_NAME}_lib)
117-
add_library(${SHARED_LIB} SHARED ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
118120
set_target_properties(${SHARED_LIB} PROPERTIES
119121
OUTPUT_NAME jsoncpp
120122
VERSION ${PROJECT_VERSION}
@@ -128,6 +130,10 @@ if(BUILD_SHARED_LIBS)
128130
endif()
129131

130132
target_compile_features(${SHARED_LIB} PUBLIC ${REQUIRED_FEATURES})
133+
target_link_libraries(${SHARED_LIB} PUBLIC
134+
$<BUILD_INTERFACE:project_options>
135+
$<BUILD_INTERFACE:jsonlib_options>
136+
)
131137

132138
target_include_directories(${SHARED_LIB} PUBLIC
133139
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
@@ -162,6 +168,10 @@ if(BUILD_STATIC_LIBS)
162168
endif()
163169

164170
target_compile_features(${STATIC_LIB} PUBLIC ${REQUIRED_FEATURES})
171+
target_link_libraries(${STATIC_LIB} PUBLIC
172+
$<BUILD_INTERFACE:project_options>
173+
$<BUILD_INTERFACE:jsonlib_options>
174+
)
165175

166176
target_include_directories(${STATIC_LIB} PUBLIC
167177
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
@@ -189,6 +199,10 @@ if(BUILD_OBJECT_LIBS)
189199
endif()
190200

191201
target_compile_features(${OBJECT_LIB} PUBLIC ${REQUIRED_FEATURES})
202+
target_link_libraries(${OBJECT_LIB} PUBLIC
203+
$<BUILD_INTERFACE:project_options>
204+
$<BUILD_INTERFACE:jsonlib_options>
205+
)
192206

193207
target_include_directories(${OBJECT_LIB} PUBLIC
194208
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>

src/test_lib_json/CMakeLists.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ add_executable(jsoncpp_test
1111

1212
if(BUILD_SHARED_LIBS)
1313
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
14-
add_compile_definitions( JSON_DLL )
14+
target_compile_definitions(jsoncpp_test PRIVATE JSON_DLL )
1515
else()
16-
add_definitions( -DJSON_DLL )
16+
target_compile_definitions(jsoncpp_test PRIVATE -DJSON_DLL )
1717
endif()
18-
target_link_libraries(jsoncpp_test jsoncpp_lib)
19-
else()
20-
target_link_libraries(jsoncpp_test jsoncpp_static)
18+
target_link_libraries(jsoncpp_test jsoncpp_lib
19+
$<BUILD_INTERFACE:project_options>
20+
)
21+
else()
22+
target_link_libraries(jsoncpp_test jsoncpp_static $<BUILD_INTERFACE:project_options>)
2123
endif()
2224

2325
# another way to solve issue #90

0 commit comments

Comments
 (0)