Skip to content

Commit a15fc7b

Browse files
committed
Add support to build shared, static and object lib at the same time
1 parent fa1ae11 commit a15fc7b

File tree

5 files changed

+151
-77
lines changed

5 files changed

+151
-77
lines changed

.travis_scripts/cmake_builder.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ cmake --version
6666
echo ${CXX}
6767
${CXX} --version
6868
_COMPILER_NAME=`basename ${CXX}`
69-
if [ "${BUILD_TYPE}" = "shared" ]; then
69+
if [ "${LIB_TYPE}" = "shared" ]; then
7070
_CMAKE_BUILD_SHARED_LIBS=ON
7171
else
7272
_CMAKE_BUILD_SHARED_LIBS=OFF

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ endif()
5555

5656
# Note: project(VERSION XX) - the VERSION here is number, but VERSION in meson is string.
5757
# Thus, it is better to be consistent.
58-
project(JSONCPP
58+
project(jsoncpp
5959
LANGUAGES CXX)
6060

6161
# Set variable named ${VAR_NAME} to value ${VALUE}
@@ -102,7 +102,9 @@ option(JSONCPP_WITH_STRICT_ISO "Issue all the warnings demanded by strict ISO C
102102
option(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON)
103103
option(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" ON)
104104
option(JSONCPP_WITH_EXAMPLE "Compile JsonCpp example" OFF)
105-
option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." OFF)
105+
option(BUILD_SHARED_LIBS "Build jsoncpp_lib as a shared library." ON)
106+
option(BUILD_STATIC_LIBS "Build jsoncpp_lib as a static library." ON)
107+
option(BUILD_OBJECT_LIBS "Build jsoncpp_lib as a object library." ON)
106108
option(BUILD_WITH_CXX_11 "Build jsoncpp_lib with C++11 standard." ON)
107109

108110
## To compatible with C++0x and C++1x

src/jsontestrunner/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ if(BUILD_SHARED_LIBS)
1919
else()
2020
add_definitions(-DJSON_DLL)
2121
endif()
22+
target_link_libraries(jsontestrunner_exe jsoncpp_lib)
23+
else()
24+
target_link_libraries(jsontestrunner_exe jsoncpp_static)
2225
endif()
23-
target_link_libraries(jsontestrunner_exe jsoncpp_lib)
2426

2527
set_target_properties(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe)
2628

src/lib_json/CMakeLists.txt

Lines changed: 140 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ set(PUBLIC_HEADERS
5050

5151
source_group("Public API" FILES ${PUBLIC_HEADERS})
5252

53-
set(jsoncpp_sources
53+
set(JSONCPP_SOURCES
5454
json_tool.h
5555
json_reader.cpp
5656
json_valueiterator.inl
@@ -65,93 +65,161 @@ else()
6565
set(INSTALL_EXPORT)
6666
endif()
6767

68+
# Specify compiler features required when compiling a given target.
69+
# See https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES
70+
# for complete list of features available
71+
function(COMPILE_FEATURES ARG1)
72+
if(CMAKE_CXX_STANDARD EQUAL "11")
73+
target_compile_features(${ARG1} PUBLIC
74+
cxx_std_11 # Compiler mode is aware of C++ 11.
75+
#MSVC 1900 cxx_alignas # Alignment control alignas, as defined in N2341.
76+
#MSVC 1900 cxx_alignof # Alignment control alignof, as defined in N2341.
77+
#MSVC 1900 cxx_attributes # Generic attributes, as defined in N2761.
78+
cxx_auto_type # Automatic type deduction, as defined in N1984.
79+
#MSVC 1900 cxx_constexpr # Constant expressions, as defined in N2235.
80+
cxx_decltype # Decltype, as defined in N2343.
81+
cxx_default_function_template_args # Default template arguments for function templates, as defined in DR226
82+
cxx_defaulted_functions # Defaulted functions, as defined in N2346.
83+
#MSVC 1900 cxx_defaulted_move_initializers # Defaulted move initializers, as defined in N3053.
84+
cxx_delegating_constructors # Delegating constructors, as defined in N1986.
85+
#MSVC 1900 cxx_deleted_functions # Deleted functions, as defined in N2346.
86+
cxx_enum_forward_declarations # Enum forward declarations, as defined in N2764.
87+
cxx_explicit_conversions # Explicit conversion operators, as defined in N2437.
88+
cxx_extended_friend_declarations # Extended friend declarations, as defined in N1791.
89+
cxx_extern_templates # Extern templates, as defined in N1987.
90+
cxx_final # Override control final keyword, as defined in N2928, N3206 and N3272.
91+
#MSVC 1900 cxx_func_identifier # Predefined __func__ identifier, as defined in N2340.
92+
#MSVC 1900 cxx_generalized_initializers # Initializer lists, as defined in N2672.
93+
#MSVC 1900 cxx_inheriting_constructors # Inheriting constructors, as defined in N2540.
94+
#MSVC 1900 cxx_inline_namespaces # Inline namespaces, as defined in N2535.
95+
cxx_lambdas # Lambda functions, as defined in N2927.
96+
#MSVC 1900 cxx_local_type_template_args # Local and unnamed types as template arguments, as defined in N2657.
97+
cxx_long_long_type # long long type, as defined in N1811.
98+
#MSVC 1900 cxx_noexcept # Exception specifications, as defined in N3050.
99+
#MSVC 1900 cxx_nonstatic_member_init # Non-static data member initialization, as defined in N2756.
100+
cxx_nullptr # Null pointer, as defined in N2431.
101+
cxx_override # Override control override keyword, as defined in N2928, N3206 and N3272.
102+
cxx_range_for # Range-based for, as defined in N2930.
103+
cxx_raw_string_literals # Raw string literals, as defined in N2442.
104+
#MSVC 1900 cxx_reference_qualified_functions # Reference qualified functions, as defined in N2439.
105+
cxx_right_angle_brackets # Right angle bracket parsing, as defined in N1757.
106+
cxx_rvalue_references # R-value references, as defined in N2118.
107+
#MSVC 1900 cxx_sizeof_member # Size of non-static data members, as defined in N2253.
108+
cxx_static_assert # Static assert, as defined in N1720.
109+
cxx_strong_enums # Strongly typed enums, as defined in N2347.
110+
#MSVC 1900 cxx_thread_local # Thread-local variables, as defined in N2659.
111+
cxx_trailing_return_types # Automatic function return type, as defined in N2541.
112+
#MSVC 1900 cxx_unicode_literals # Unicode string literals, as defined in N2442.
113+
cxx_uniform_initialization # Uniform initialization, as defined in N2640.
114+
#MSVC 1900 cxx_unrestricted_unions # Unrestricted unions, as defined in N2544.
115+
#MSVC 1900 cxx_user_literals # User-defined literals, as defined in N2765.
116+
cxx_variadic_macros # Variadic macros, as defined in N1653.
117+
cxx_variadic_templates # Variadic templates, as defined in N2242.
118+
)
119+
else()
120+
set(CMAKE_CXX_STANDARD 98)
121+
target_compile_features(${ARG1} PUBLIC)
122+
endif()
123+
endfunction()
68124

69125
if(BUILD_SHARED_LIBS)
70126
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12.0)
71127
add_compile_definitions(JSON_DLL_BUILD)
72128
else()
73129
add_definitions(-DJSON_DLL_BUILD)
74130
endif()
131+
132+
set(SHARED_LIB ${PROJECT_NAME}_lib)
133+
add_library(${SHARED_LIB} SHARED ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
134+
set_target_properties( ${SHARED_LIB} PROPERTIES
135+
OUTPUT_NAME jsoncpp
136+
VERSION ${JSONCPP_VERSION}
137+
SOVERSION ${JSONCPP_SOVERSION}
138+
POSITION_INDEPENDENT_CODE ON
139+
)
140+
141+
# Set library's runtime search path on OSX
142+
if(APPLE)
143+
set_target_properties(${SHARED_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.")
144+
endif()
145+
146+
COMPILE_FEATURES(${SHARED_LIB})
147+
148+
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
149+
target_include_directories(${SHARED_LIB} PUBLIC
150+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
151+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
152+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
153+
)
154+
endif()
155+
156+
list(APPEND CMAKE_TARGETS ${SHARED_LIB})
75157
endif()
76158

77-
add_library(jsoncpp_lib ${PUBLIC_HEADERS} ${jsoncpp_sources})
78-
set_target_properties( jsoncpp_lib PROPERTIES
79-
OUTPUT_NAME jsoncpp
80-
VERSION ${JSONCPP_VERSION}
81-
SOVERSION ${JSONCPP_SOVERSION}
82-
POSITION_INDEPENDENT_CODE ON
83-
)
159+
if(BUILD_STATIC_LIBS)
160+
set(STATIC_LIB ${PROJECT_NAME}_static)
161+
add_library(${STATIC_LIB} STATIC ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
162+
163+
# avoid name clashes on windows as the shared import lib is also named jsoncpp.lib
164+
if(NOT DEFINED STATIC_SUFFIX AND BUILD_SHARED_LIBS)
165+
set(STATIC_SUFFIX "_static")
166+
endif()
167+
168+
set_target_properties( ${STATIC_LIB} PROPERTIES
169+
OUTPUT_NAME jsoncpp${STATIC_SUFFIX}
170+
VERSION ${JSONCPP_VERSION}
171+
)
172+
173+
# Set library's runtime search path on OSX
174+
if(APPLE)
175+
set_target_properties(${STATIC_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.")
176+
endif()
84177

85-
# Set library's runtime search path on OSX
86-
if(APPLE)
87-
set_target_properties(jsoncpp_lib PROPERTIES INSTALL_RPATH "@loader_path/.")
178+
COMPILE_FEATURES(${STATIC_LIB})
179+
180+
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
181+
target_include_directories(${STATIC_LIB} PUBLIC
182+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
183+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
184+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
185+
)
186+
endif()
187+
188+
list(APPEND CMAKE_TARGETS ${STATIC_LIB})
88189
endif()
89190

90-
# Specify compiler features required when compiling a given target.
91-
# See https://cmake.org/cmake/help/v3.1/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html#prop_gbl:CMAKE_CXX_KNOWN_FEATURES
92-
# for complete list of features available
93-
if(CMAKE_CXX_STANDARD EQUAL "11")
94-
target_compile_features(jsoncpp_lib PUBLIC
95-
cxx_std_11 # Compiler mode is aware of C++ 11.
96-
#MSVC 1900 cxx_alignas # Alignment control alignas, as defined in N2341.
97-
#MSVC 1900 cxx_alignof # Alignment control alignof, as defined in N2341.
98-
#MSVC 1900 cxx_attributes # Generic attributes, as defined in N2761.
99-
cxx_auto_type # Automatic type deduction, as defined in N1984.
100-
#MSVC 1900 cxx_constexpr # Constant expressions, as defined in N2235.
101-
cxx_decltype # Decltype, as defined in N2343.
102-
cxx_default_function_template_args # Default template arguments for function templates, as defined in DR226
103-
cxx_defaulted_functions # Defaulted functions, as defined in N2346.
104-
#MSVC 1900 cxx_defaulted_move_initializers # Defaulted move initializers, as defined in N3053.
105-
cxx_delegating_constructors # Delegating constructors, as defined in N1986.
106-
#MSVC 1900 cxx_deleted_functions # Deleted functions, as defined in N2346.
107-
cxx_enum_forward_declarations # Enum forward declarations, as defined in N2764.
108-
cxx_explicit_conversions # Explicit conversion operators, as defined in N2437.
109-
cxx_extended_friend_declarations # Extended friend declarations, as defined in N1791.
110-
cxx_extern_templates # Extern templates, as defined in N1987.
111-
cxx_final # Override control final keyword, as defined in N2928, N3206 and N3272.
112-
#MSVC 1900 cxx_func_identifier # Predefined __func__ identifier, as defined in N2340.
113-
#MSVC 1900 cxx_generalized_initializers # Initializer lists, as defined in N2672.
114-
#MSVC 1900 cxx_inheriting_constructors # Inheriting constructors, as defined in N2540.
115-
#MSVC 1900 cxx_inline_namespaces # Inline namespaces, as defined in N2535.
116-
cxx_lambdas # Lambda functions, as defined in N2927.
117-
#MSVC 1900 cxx_local_type_template_args # Local and unnamed types as template arguments, as defined in N2657.
118-
cxx_long_long_type # long long type, as defined in N1811.
119-
#MSVC 1900 cxx_noexcept # Exception specifications, as defined in N3050.
120-
#MSVC 1900 cxx_nonstatic_member_init # Non-static data member initialization, as defined in N2756.
121-
cxx_nullptr # Null pointer, as defined in N2431.
122-
cxx_override # Override control override keyword, as defined in N2928, N3206 and N3272.
123-
cxx_range_for # Range-based for, as defined in N2930.
124-
cxx_raw_string_literals # Raw string literals, as defined in N2442.
125-
#MSVC 1900 cxx_reference_qualified_functions # Reference qualified functions, as defined in N2439.
126-
cxx_right_angle_brackets # Right angle bracket parsing, as defined in N1757.
127-
cxx_rvalue_references # R-value references, as defined in N2118.
128-
#MSVC 1900 cxx_sizeof_member # Size of non-static data members, as defined in N2253.
129-
cxx_static_assert # Static assert, as defined in N1720.
130-
cxx_strong_enums # Strongly typed enums, as defined in N2347.
131-
#MSVC 1900 cxx_thread_local # Thread-local variables, as defined in N2659.
132-
cxx_trailing_return_types # Automatic function return type, as defined in N2541.
133-
#MSVC 1900 cxx_unicode_literals # Unicode string literals, as defined in N2442.
134-
cxx_uniform_initialization # Uniform initialization, as defined in N2640.
135-
#MSVC 1900 cxx_unrestricted_unions # Unrestricted unions, as defined in N2544.
136-
#MSVC 1900 cxx_user_literals # User-defined literals, as defined in N2765.
137-
cxx_variadic_macros # Variadic macros, as defined in N1653.
138-
cxx_variadic_templates # Variadic templates, as defined in N2242.
139-
)
140-
else()
141-
set(CMAKE_CXX_STANDARD 98)
142-
target_compile_features(jsoncpp_lib PUBLIC)
191+
if(BUILD_OBJECT_LIBS)
192+
set(OBJECT_LIB ${PROJECT_NAME}_object)
193+
add_library(${OBJECT_LIB} OBJECT ${PUBLIC_HEADERS} ${JSONCPP_SOURCES})
194+
set_target_properties( ${OBJECT_LIB} PROPERTIES
195+
OUTPUT_NAME jsoncpp
196+
VERSION ${JSONCPP_VERSION}
197+
SOVERSION ${JSONCPP_SOVERSION}
198+
POSITION_INDEPENDENT_CODE ON
199+
)
200+
201+
# Set library's runtime search path on OSX
202+
if(APPLE)
203+
set_target_properties(${OBJECT_LIB} PROPERTIES INSTALL_RPATH "@loader_path/.")
204+
endif()
205+
206+
COMPILE_FEATURES(${OBJECT_LIB})
207+
208+
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
209+
target_include_directories(${OBJECT_LIB} PUBLIC
210+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
211+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
212+
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
213+
)
214+
endif()
215+
216+
list(APPEND CMAKE_TARGETS ${OBJECT_LIB})
143217
endif()
144218

145-
install(TARGETS jsoncpp_lib ${INSTALL_EXPORT}
219+
install(TARGETS ${CMAKE_TARGETS} ${INSTALL_EXPORT}
146220
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
147221
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
148222
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
223+
OBJECTS DESTINATION ${CMAKE_INSTALL_LIBDIR}
149224
)
150225

151-
if(NOT CMAKE_VERSION VERSION_LESS 2.8.11)
152-
target_include_directories(jsoncpp_lib PUBLIC
153-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
154-
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/${JSONCPP_INCLUDE_DIR}>
155-
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include/json>
156-
)
157-
endif()

src/test_lib_json/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ if(BUILD_SHARED_LIBS)
1515
else()
1616
add_definitions( -DJSON_DLL )
1717
endif()
18+
target_link_libraries(jsoncpp_test jsoncpp_lib)
19+
else()
20+
target_link_libraries(jsoncpp_test jsoncpp_static)
1821
endif()
19-
target_link_libraries(jsoncpp_test jsoncpp_lib)
2022

2123
# another way to solve issue #90
2224
#set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store)

0 commit comments

Comments
 (0)