From 5e8595c0e2861ad9c8b0b9e4ba9a8acc29fbfdb1 Mon Sep 17 00:00:00 2001 From: Peter Spiess-Knafl Date: Tue, 27 Jan 2015 20:01:42 +0000 Subject: [PATCH] added cmake option to build static and shared libraries at once See #147 and #149. --- README.md | 2 +- src/jsontestrunner/CMakeLists.txt | 8 +++- src/lib_json/CMakeLists.txt | 72 +++++++++++++++++++------------ src/test_lib_json/CMakeLists.txt | 7 ++- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 099f17f..cf8b3db 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Alternatively, from the command-line on Unix in the source directory: mkdir -p build/debug cd build/debug - cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../.. + cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../.. make Running `cmake -`" will display the list of available generators (passed using diff --git a/src/jsontestrunner/CMakeLists.txt b/src/jsontestrunner/CMakeLists.txt index dd8e217..7faf0a5 100644 --- a/src/jsontestrunner/CMakeLists.txt +++ b/src/jsontestrunner/CMakeLists.txt @@ -7,7 +7,13 @@ ENDIF(JSONCPP_LIB_BUILD_SHARED) ADD_EXECUTABLE(jsontestrunner_exe main.cpp ) -TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib) + +IF(JSONCPP_LIB_BUILD_SHARED) + TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib) +ELSE(JSONCPP_LIB_BUILD_SHARED) + TARGET_LINK_LIBRARIES(jsontestrunner_exe jsoncpp_lib_static) +ENDIF(JSONCPP_LIB_BUILD_SHARED) + SET_TARGET_PROPERTIES(jsontestrunner_exe PROPERTIES OUTPUT_NAME jsontestrunner_exe) IF(PYTHONINTERP_FOUND) diff --git a/src/lib_json/CMakeLists.txt b/src/lib_json/CMakeLists.txt index d0f6a5e..133d110 100644 --- a/src/lib_json/CMakeLists.txt +++ b/src/lib_json/CMakeLists.txt @@ -1,15 +1,10 @@ OPTION(JSONCPP_LIB_BUILD_SHARED "Build jsoncpp_lib as a shared library." OFF) +OPTION(JSONCPP_LIB_BUILD_STATIC "Build jsoncpp_lib static library." ON) + IF(BUILD_SHARED_LIBS) SET(JSONCPP_LIB_BUILD_SHARED ON) ENDIF(BUILD_SHARED_LIBS) -IF(JSONCPP_LIB_BUILD_SHARED) - SET(JSONCPP_LIB_TYPE SHARED) - ADD_DEFINITIONS( -DJSON_DLL_BUILD ) -ELSE(JSONCPP_LIB_BUILD_SHARED) - SET(JSONCPP_LIB_TYPE STATIC) -ENDIF(JSONCPP_LIB_BUILD_SHARED) - if( CMAKE_COMPILER_IS_GNUCXX ) #Get compiler version. execute_process( COMMAND ${CMAKE_CXX_COMPILER} -dumpversion @@ -36,25 +31,52 @@ SET( PUBLIC_HEADERS SOURCE_GROUP( "Public API" FILES ${PUBLIC_HEADERS} ) -ADD_LIBRARY( jsoncpp_lib ${JSONCPP_LIB_TYPE} - ${PUBLIC_HEADERS} - json_tool.h - json_reader.cpp - json_batchallocator.h - json_valueiterator.inl - json_value.cpp - json_writer.cpp - version.h.in - ) -SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES OUTPUT_NAME jsoncpp ) -SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR} ) +SET(jsoncpp_sources + json_tool.h + json_reader.cpp + json_batchallocator.h + json_valueiterator.inl + json_value.cpp + json_writer.cpp + version.h.in) -IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) - TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib PUBLIC +IF(JSONCPP_LIB_BUILD_SHARED) + ADD_DEFINITIONS( -DJSON_DLL_BUILD ) + ADD_LIBRARY(jsoncpp_lib SHARED ${PUBLIC_HEADERS} ${jsoncpp_sources}) + SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR}) + SET_TARGET_PROPERTIES( jsoncpp_lib PROPERTIES OUTPUT_NAME jsoncpp ) + + INSTALL( TARGETS jsoncpp_lib ${INSTALL_EXPORT} + RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} + LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR} + ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR}) + + IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) + TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib PUBLIC + $ + $) + ENDIF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) + +ENDIF() + +IF(JSONCPP_LIB_BUILD_STATIC) + ADD_LIBRARY(jsoncpp_lib_static STATIC ${PUBLIC_HEADERS} ${jsoncpp_sources}) + SET_TARGET_PROPERTIES( jsoncpp_lib_static PROPERTIES VERSION ${JSONCPP_VERSION} SOVERSION ${JSONCPP_VERSION_MAJOR}) + SET_TARGET_PROPERTIES( jsoncpp_lib_static PROPERTIES OUTPUT_NAME jsoncpp ) + + INSTALL( TARGETS jsoncpp_lib_static ${INSTALL_EXPORT} + RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} + LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR} + ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR}) + + IF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) + TARGET_INCLUDE_DIRECTORIES( jsoncpp_lib_static PUBLIC $ $ ) -ENDIF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) + ENDIF(NOT CMAKE_VERSION VERSION_LESS 2.8.11) + +ENDIF() # Install instructions for this target IF(JSONCPP_WITH_CMAKE_PACKAGE) @@ -62,9 +84,3 @@ IF(JSONCPP_WITH_CMAKE_PACKAGE) ELSE(JSONCPP_WITH_CMAKE_PACKAGE) SET(INSTALL_EXPORT) ENDIF(JSONCPP_WITH_CMAKE_PACKAGE) - -INSTALL( TARGETS jsoncpp_lib ${INSTALL_EXPORT} - RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR} - LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR} - ARCHIVE DESTINATION ${ARCHIVE_INSTALL_DIR} -) diff --git a/src/test_lib_json/CMakeLists.txt b/src/test_lib_json/CMakeLists.txt index 24dc56f..df5a924 100644 --- a/src/test_lib_json/CMakeLists.txt +++ b/src/test_lib_json/CMakeLists.txt @@ -9,7 +9,12 @@ ADD_EXECUTABLE( jsoncpp_test main.cpp ) -TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib) + +IF(JSONCPP_LIB_BUILD_SHARED) + TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib) +ELSE(JSONCPP_LIB_BUILD_SHARED) + TARGET_LINK_LIBRARIES(jsoncpp_test jsoncpp_lib_static) +ENDIF(JSONCPP_LIB_BUILD_SHARED) # another way to solve issue #90 #set_target_properties(jsoncpp_test PROPERTIES COMPILE_FLAGS -ffloat-store)