Browse Source

Fancy Cmake dependencies

Jonas Pflanzer 4 years ago
parent
commit
5389331718

+ 129 - 0
.cmake_modules/FindGMock.cmake

@@ -0,0 +1,129 @@
+# Locate the Google C++ Mocking Framework.
+# (This file is almost an identical copy of the original FindGTest.cmake file,
+#  feel free to use it as it is or modify it for your own needs.)
+#
+#
+# Defines the following variables:
+#
+#   GMOCK_FOUND - Found the Google Testing framework
+#   GMOCK_INCLUDE_DIRS - Include directories
+#
+# Also defines the library variables below as normal
+# variables. These contain debug/optimized keywords when
+# a debugging library is found.
+#
+#   GMOCK_BOTH_LIBRARIES - Both libgmock & libgmock-main
+#   GMOCK_LIBRARIES - libgmock
+#   GMOCK_MAIN_LIBRARIES - libgmock-main
+#
+# Accepts the following variables as input:
+#
+#   GMOCK_ROOT - (as a CMake or environment variable)
+#                The root directory of the gmock install prefix
+#
+#   GMOCK_MSVC_SEARCH - If compiling with MSVC, this variable can be set to
+#                       "MD" or "MT" to enable searching a gmock build tree
+#                       (defaults: "MD")
+#
+#-----------------------
+# Example Usage:
+#
+#    find_package(GMock REQUIRED)
+#    include_directories(${GMOCK_INCLUDE_DIRS})
+#
+#    add_executable(foo foo.cc)
+#    target_link_libraries(foo ${GMOCK_BOTH_LIBRARIES})
+#
+#=============================================================================
+# This file is released under the MIT licence:
+#
+# Copyright (c) 2011 Matej Svec
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to
+# deal in the Software without restriction, including without limitation the
+# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+# sell copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#=============================================================================
+
+
+function(_gmock_append_debugs _endvar _library)
+  if(${_library} AND ${_library}_DEBUG)
+    set(_output optimized ${${_library}} debug ${${_library}_DEBUG})
+  else()
+    set(_output ${${_library}})
+  endif()
+  set(${_endvar} ${_output} PARENT_SCOPE)
+endfunction()
+
+function(_gmock_find_library _name)
+  find_library(${_name}
+    NAMES ${ARGN}
+    HINTS
+      $ENV{GMOCK_ROOT}
+      ${GMOCK_ROOT}
+    PATH_SUFFIXES ${_gmock_libpath_suffixes}
+  )
+  mark_as_advanced(${_name})
+endfunction()
+
+
+if(NOT DEFINED GMOCK_MSVC_SEARCH)
+  set(GMOCK_MSVC_SEARCH MD)
+endif()
+
+set(_gmock_libpath_suffixes lib)
+if(MSVC)
+  if(GMOCK_MSVC_SEARCH STREQUAL "MD")
+    list(APPEND _gmock_libpath_suffixes
+      msvc/gmock-md/Debug
+      msvc/gmock-md/Release)
+  elseif(GMOCK_MSVC_SEARCH STREQUAL "MT")
+    list(APPEND _gmock_libpath_suffixes
+      msvc/gmock/Debug
+      msvc/gmock/Release)
+  endif()
+endif()
+
+find_path(GMOCK_INCLUDE_DIR gmock/gmock.h
+  HINTS
+    $ENV{GMOCK_ROOT}/include
+    ${GMOCK_ROOT}/include
+)
+mark_as_advanced(GMOCK_INCLUDE_DIR)
+
+if(MSVC AND GMOCK_MSVC_SEARCH STREQUAL "MD")
+  # The provided /MD project files for Google Mock add -md suffixes to the
+  # library names.
+  _gmock_find_library(GMOCK_LIBRARY            gmock-md  gmock)
+  _gmock_find_library(GMOCK_LIBRARY_DEBUG      gmock-mdd gmockd)
+  _gmock_find_library(GMOCK_MAIN_LIBRARY       gmock_main-md  gmock_main)
+  _gmock_find_library(GMOCK_MAIN_LIBRARY_DEBUG gmock_main-mdd gmock_maind)
+else()
+  _gmock_find_library(GMOCK_LIBRARY            gmock)
+  _gmock_find_library(GMOCK_LIBRARY_DEBUG      gmockd)
+  _gmock_find_library(GMOCK_MAIN_LIBRARY       gmock_main)
+  _gmock_find_library(GMOCK_MAIN_LIBRARY_DEBUG gmock_maind)
+endif()
+
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMock DEFAULT_MSG GMOCK_LIBRARY GMOCK_INCLUDE_DIR GMOCK_MAIN_LIBRARY)
+
+if(GMOCK_FOUND)
+  set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIR})
+  _gmock_append_debugs(GMOCK_LIBRARIES      GMOCK_LIBRARY)
+  _gmock_append_debugs(GMOCK_MAIN_LIBRARIES GMOCK_MAIN_LIBRARY)
+  set(GMOCK_BOTH_LIBRARIES ${GMOCK_LIBRARIES} ${GMOCK_MAIN_LIBRARIES})
+endif()
+

+ 64 - 0
.cmake_modules/FindJsoncpp.cmake

@@ -0,0 +1,64 @@
+# Find jsoncpp
+#
+# Find the jsoncpp includes and library
+#
+# if you nee to add a custom library search path, do it via via CMAKE_PREFIX_PATH
+#
+# This module defines
+#  JSONCPP_INCLUDE_DIRS, where to find header, etc.
+#  JSONCPP_LIBRARIES, the libraries needed to use jsoncpp.
+#  JSONCPP_FOUND, If false, do not try to use jsoncpp.
+#  JSONCPP_INCLUDE_PREFIX, include prefix for jsoncpp
+
+# only look in default directories
+find_path(
+	JSONCPP_INCLUDE_DIR
+	NAMES jsoncpp/json/json.h json/json.h
+	DOC "jsoncpp include dir"
+)
+
+find_library(
+	JSONCPP_LIBRARY
+	NAMES jsoncpp
+	DOC "jsoncpp library"
+)
+
+set(JSONCPP_INCLUDE_DIRS ${JSONCPP_INCLUDE_DIR})
+set(JSONCPP_LIBRARIES ${JSONCPP_LIBRARY})
+
+# debug library on windows
+# same naming convention as in qt (appending debug library with d)
+# boost is using the same "hack" as us with "optimized" and "debug"
+if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
+	find_library(
+		JSONCPP_LIBRARY_DEBUG
+		NAMES jsoncppd
+		DOC "jsoncpp debug library"
+	)
+
+	set(JSONCPP_LIBRARIES optimized ${JSONCPP_LIBRARIES} debug ${JSONCPP_LIBRARY_DEBUG})
+
+endif()
+
+# find JSONCPP_INCLUDE_PREFIX
+find_path(
+	JSONCPP_INCLUDE_PREFIX
+	NAMES json.h
+	PATH_SUFFIXES jsoncpp/json json
+)
+
+if (${JSONCPP_INCLUDE_PREFIX} MATCHES "jsoncpp")
+	set(JSONCPP_INCLUDE_PREFIX "jsoncpp")
+	set(JSONCPP_INCLUDE_DIRS "${JSONCPP_INCLUDE_DIRS}/jsoncpp")
+else()
+	set(JSONCPP_INCLUDE_PREFIX "")
+endif()
+
+
+# handle the QUIETLY and REQUIRED arguments and set JSONCPP_FOUND to TRUE
+# if all listed variables are TRUE, hide their existence from configuration view
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(jsoncpp DEFAULT_MSG
+	JSONCPP_INCLUDE_DIR JSONCPP_LIBRARY)
+mark_as_advanced (JSONCPP_INCLUDE_DIR JSONCPP_LIBRARY)
+

+ 7 - 0
daemon/CMakeLists.txt

@@ -1,9 +1,16 @@
 cmake_minimum_required(VERSION 2.8)
 
 set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../.cmake_modules/")
+message("${CMAKE_MODULE_PATH}")
 
 project(ccats)
 
+# dependencies used by server and tests
+find_package(Threads REQUIRED)
+find_package(Boost 1.67 REQUIRED COMPONENTS system filesystem)
+find_package(Jsoncpp REQUIRED)
+
 include(src/CMakeLists.txt)
 
 if(ENABLE_TESTS)

+ 4 - 11
daemon/src/CMakeLists.txt

@@ -4,15 +4,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 
 add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp src/JsonCommander.cpp src/FileManager.cpp src/UserManager.cpp src/Config.cpp)
 
-# use pkg-config to fix building on debian unstable
-find_package(PkgConfig REQUIRED)
-pkg_check_modules(TINS REQUIRED libtins>=4.2 libpcap)
-pkg_check_modules(JSONCPP REQUIRED jsoncpp)
+# dependencies used by server only
+find_package(libtins 4.2 REQUIRED)
 
-
-find_package(Threads REQUIRED)
-find_package(Boost 1.67 REQUIRED COMPONENTS system filesystem)
-# find_package(libtins 4.2 REQUIRED)
-
-include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR})
-target_link_libraries(ccats PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${TINS_LIBRARIES} ${PCAP_LIBRARIES} ${JSONCPP_LIBRARIES})
+include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDE_DIRS})
+target_link_libraries(ccats PRIVATE ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${LIBTINS_LIBRARIES} ${JSONCPP_LIBRARIES})

+ 4 - 12
daemon/test/CMakeLists.txt

@@ -2,26 +2,18 @@ cmake_minimum_required(VERSION 2.8)
 
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/test)
 
-# use pkg-config to fix building on debian unstable
-find_package(PkgConfig REQUIRED)
-pkg_check_modules(JSONCPP REQUIRED jsoncpp)
-
-
-find_package(Threads REQUIRED)
-find_package(Boost 1.67 REQUIRED COMPONENTS system filesystem)
-
 # Setup testing
 enable_testing()
 
-pkg_check_modules(GMOCK REQUIRED gmock)
-
+# dependencies used by tests only
 find_package(GTest REQUIRED)
+find_package(GMock REQUIRED)
 
-include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR})
+include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS})
 
 # Add test cpp file
 add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp test/ConfigMock.cpp)
-target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
+target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GTEST_BOTH_LIBRARIES} ${GMOCK_BOTH_LIBRARIES})
 
 add_test(
   NAME jsonCommanderTest