1 CMAKE_MINIMUM_REQUIRED(VERSION 2.8.5) 2 PROJECT(jsoncpp) 3 ENABLE_TESTING() 4 5 OPTION(JSONCPP_WITH_TESTS "Compile and run JsonCpp test executables" ON) 6 OPTION(JSONCPP_WITH_POST_BUILD_UNITTEST "Automatically run unit-tests as a post build step" ON) 7 OPTION(JSONCPP_WITH_WARNING_AS_ERROR "Force compilation to fail if a warning occurs" OFF) 8 OPTION(JSONCPP_WITH_PKGCONFIG_SUPPORT "Generate and install .pc files" ON) 9 OPTION(JSONCPP_WITH_CMAKE_PACKAGE "Generate and install cmake package files" OFF) 10 11 # Ensures that CMAKE_BUILD_TYPE is visible in cmake-gui on Unix 12 IF(NOT WIN32) 13 IF(NOT CMAKE_BUILD_TYPE) 14 SET(CMAKE_BUILD_TYPE Release CACHE STRING 15 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage." 16 FORCE) 17 ENDIF(NOT CMAKE_BUILD_TYPE) 18 ENDIF(NOT WIN32) 19 20 SET(RUNTIME_INSTALL_DIR bin 21 CACHE PATH "Install dir for executables and dlls") 22 SET(ARCHIVE_INSTALL_DIR lib 23 CACHE PATH "Install dir for static libraries") 24 SET(LIBRARY_INSTALL_DIR lib 25 CACHE PATH "Install dir for shared libraries") 26 SET(INCLUDE_INSTALL_DIR include 27 CACHE PATH "Install dir for headers") 28 SET(PACKAGE_INSTALL_DIR lib/cmake 29 CACHE PATH "Install dir for cmake package config files") 30 MARK_AS_ADVANCED( RUNTIME_INSTALL_DIR ARCHIVE_INSTALL_DIR INCLUDE_INSTALL_DIR PACKAGE_INSTALL_DIR ) 31 32 # This ensures shared DLL are in the same dir as executable on Windows. 33 # Put all executables / libraries are in a project global directory. 34 SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib 35 CACHE PATH "Single directory for all static libraries.") 36 SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib 37 CACHE PATH "Single directory for all dynamic libraries on Unix.") 38 SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin 39 CACHE PATH "Single directory for all executable and dynamic libraries on Windows.") 40 MARK_AS_ADVANCED( CMAKE_RUNTIME_OUTPUT_DIRECTORY CMAKE_LIBRARY_OUTPUT_DIRECTORY CMAKE_ARCHIVE_OUTPUT_DIRECTORY ) 41 42 # Set variable named ${VAR_NAME} to value ${VALUE} 43 FUNCTION(set_using_dynamic_name VAR_NAME VALUE) 44 SET( "${VAR_NAME}" "${VALUE}" PARENT_SCOPE) 45 ENDFUNCTION(set_using_dynamic_name) 46 47 # Extract major, minor, patch from version text 48 # Parse a version string "X.Y.Z" and outputs 49 # version parts in ${OUPUT_PREFIX}_MAJOR, _MINOR, _PATCH. 50 # If parse succeeds then ${OUPUT_PREFIX}_FOUND is TRUE. 51 MACRO(jsoncpp_parse_version VERSION_TEXT OUPUT_PREFIX) 52 SET(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?") 53 IF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} ) 54 STRING(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT}) 55 LIST(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_MAJOR) 56 LIST(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_MINOR) 57 LIST(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PATCH) 58 set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE ) 59 ELSE( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} ) 60 set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE ) 61 ENDIF( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} ) 62 ENDMACRO(jsoncpp_parse_version) 63 64 # Read out version from "version" file 65 FILE(STRINGS "version" JSONCPP_VERSION) 66 67 jsoncpp_parse_version( ${JSONCPP_VERSION} JSONCPP_VERSION ) 68 IF(NOT JSONCPP_VERSION_FOUND) 69 MESSAGE(FATAL_ERROR "Failed to parse version string properly. Expect X.Y.Z") 70 ENDIF(NOT JSONCPP_VERSION_FOUND) 71 72 MESSAGE(STATUS "JsonCpp Version: ${JSONCPP_VERSION_MAJOR}.${JSONCPP_VERSION_MINOR}.${JSONCPP_VERSION_PATCH}") 73 # File version.h is only regenerated on CMake configure step 74 CONFIGURE_FILE( "${PROJECT_SOURCE_DIR}/src/lib_json/version.h.in" 75 "${PROJECT_SOURCE_DIR}/include/json/version.h" ) 76 77 macro(UseCompilationWarningAsError) 78 if ( MSVC ) 79 # Only enabled in debug because some old versions of VS STL generate 80 # warnings when compiled in release configuration. 81 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /WX ") 82 endif( MSVC ) 83 endmacro() 84 85 # Include our configuration header 86 INCLUDE_DIRECTORIES( ${jsoncpp_SOURCE_DIR}/include ) 87 88 if ( MSVC ) 89 # Only enabled in debug because some old versions of VS STL generate 90 # unreachable code warning when compiled in release configuration. 91 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4 ") 92 endif( MSVC ) 93 94 IF(JSONCPP_WITH_WARNING_AS_ERROR) 95 UseCompilationWarningAsError() 96 ENDIF(JSONCPP_WITH_WARNING_AS_ERROR) 97 98 IF(JSONCPP_WITH_PKGCONFIG_SUPPORT) 99 CONFIGURE_FILE( 100 "pkg-config/jsoncpp.pc.in" 101 "pkg-config/jsoncpp.pc" 102 @ONLY) 103 INSTALL(FILES "${CMAKE_BINARY_DIR}/pkg-config/jsoncpp.pc" 104 DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig") 105 ENDIF(JSONCPP_WITH_PKGCONFIG_SUPPORT) 106 107 IF(JSONCPP_WITH_CMAKE_PACKAGE) 108 INSTALL(EXPORT jsoncpp 109 DESTINATION ${PACKAGE_INSTALL_DIR}/jsoncpp 110 FILE jsoncppConfig.cmake) 111 ENDIF(JSONCPP_WITH_CMAKE_PACKAGE) 112 113 # Build the different applications 114 ADD_SUBDIRECTORY( src ) 115 116 #install the includes 117 ADD_SUBDIRECTORY( include ) 118