Salome HOME
Fix for '23364: [CEA 1966] Problem with FindCppUnit.cmake' issue.
[tools/configuration.git] / cmake / FindCppUnit.cmake
1 # - Find CppUnit
2 # Sets the following variables:
3 #   CPPUNIT_INCLUDE_DIRS - path to the CppUnit include directory
4 #   CPPUNIT_LIBRARIES    - path to the CppUnit libraries to be linked against
5 #   CPPUNIT_DEFINITIONS  - specific CppUnit definitions to be added
6 #
7 #  The header cppunit/extensions/HelperMacros.h is looked for.
8 #  The following libraries are searched  
9 #        cppunit_dll, or cppunitd_dll (Windows) 
10 #        cppunit (Linux)
11 #
12
13 #########################################################################
14 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
15 #
16 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
17 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
18 #
19 # This library is free software; you can redistribute it and/or
20 # modify it under the terms of the GNU Lesser General Public
21 # License as published by the Free Software Foundation; either
22 # version 2.1 of the License, or (at your option) any later version.
23 #
24 # This library is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27 # Lesser General Public License for more details.
28 #
29 # You should have received a copy of the GNU Lesser General Public
30 # License along with this library; if not, write to the Free Software
31 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
32 #
33 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
34 #
35
36 IF(NOT CppUnit_FIND_QUIETLY)
37     MESSAGE(STATUS "Looking for CppUnit ...")
38 ENDIF()
39
40 # Headers
41 SET(CPPUNIT_ROOT_DIR $ENV{CPPUNIT_ROOT_DIR} CACHE PATH "Path to the CPPUNIT.")
42 IF(CPPUNIT_ROOT_DIR)
43   LIST(APPEND CMAKE_INCLUDE_PATH "${CPPUNIT_ROOT_DIR}/include")
44   LIST(APPEND CMAKE_PROGRAM_PATH "${CPPUNIT_ROOT_DIR}/bin")
45 ENDIF(CPPUNIT_ROOT_DIR)
46
47 SET(CPPUNIT_INCLUDE_TO_FIND cppunit/extensions/HelperMacros.h)
48 FIND_PATH(CPPUNIT_INCLUDE_DIRS ${CPPUNIT_INCLUDE_TO_FIND})
49
50 # Libraries
51 IF(WIN32)
52   IF(CMAKE_BUILD_TYPE STREQUAL Debug)
53     FIND_LIBRARY(CPPUNIT_LIBRARIES cppunitd_dll)
54   ELSE(CMAKE_BUILD_TYPE STREQUAL Debug)
55     FIND_LIBRARY(CPPUNIT_LIBRARIES cppunit_dll)
56   ENDIF(CMAKE_BUILD_TYPE STREQUAL Debug)
57 ELSE(WIN32)
58
59   # RNV: Some new Linux distributions don't contain cppunit-config executable,
60   #      so use pkg-config to detect cppunit first.
61   FIND_PACKAGE(PkgConfig QUIET)
62
63   IF(PKG_CONFIG_FOUND)
64     PKG_SEARCH_MODULE(CPPUNIT_TOOL cppunit)  
65     IF(CPPUNIT_TOOL_FOUND)
66       SET(CPPUNIT_LDFLAGS ${CPPUNIT_TOOL_LIBRARIES})
67     ENDIF()
68   ENDIF()
69   
70   IF(NOT CPPUNIT_TOOL_FOUND)
71     MESSAGE("CPPUNIT is not found via pgk-config, try to find cppunit-config executable ...")
72     FIND_PROGRAM(CPPUNIT_CONFIG_BIN cppunit-config)
73     IF(NOT CPPUNIT_CONFIG_BIN)
74       MESSAGE(FATAL_ERROR "Error in CPPUNIT detection ! cppunit-config executable not found !")     
75     ENDIF(NOT CPPUNIT_CONFIG_BIN)
76     EXECUTE_PROCESS(COMMAND ${CPPUNIT_CONFIG_BIN} --libs OUTPUT_VARIABLE CPPUNIT_LDFLAGS)
77   ENDIF()
78   
79   STRING(STRIP ${CPPUNIT_LDFLAGS} CPPUNIT_LDFLAGS)
80   STRING(REPLACE " " ";" LDFLAGS_LIST ${CPPUNIT_LDFLAGS})
81   FOREACH(LDFLAG ${LDFLAGS_LIST})
82     STRING(REGEX MATCH "^-L.*" LIBDIR "${LDFLAG}")
83     STRING(REGEX MATCH "^-l.*" LIB "${LDFLAG}")
84     IF(LIBDIR)
85       STRING(REGEX REPLACE "^-L" "" LIBDIR ${LIBDIR})
86       LIST(APPEND CMAKE_LIBRARY_PATH ${LIBDIR})
87     ELSEIF(LIB)
88       STRING(REGEX REPLACE "^-l" "" LIB ${LIB})
89       LIST(APPEND LIBS ${LIB})
90     ELSE()
91       IF(CPPUNIT_CONFIG_BIN)
92         MESSAGE(FATAL_ERROR "Unrecognized token \"${LDFLAG}\" in the output of cppunit-config --libs")
93       ELSE()
94         LIST(APPEND LIBS ${LDFLAG})
95       ENDIF()
96     ENDIF()
97   ENDFOREACH(LDFLAG ${LDFLAGS_LIST})
98   FOREACH(LIB ${LIBS})
99     FIND_LIBRARY(CPPUNIT_SUBLIB_${LIB} ${LIB})
100     IF(NOT CPPUNIT_SUBLIB_${LIB})
101       MESSAGE(FATAL_ERROR "Error in CPPUNIT detection! Fail to locate the needed library ${LIB}!")
102     ENDIF(NOT CPPUNIT_SUBLIB_${LIB})
103     LIST(APPEND CPPUNIT_LIBRARIES ${CPPUNIT_SUBLIB_${LIB}})
104   ENDFOREACH(LIB ${LIBS})
105 #  MESSAGE("**** ${CPPUNIT_LIBRARIES}")
106 ENDIF(WIN32)
107
108 # Global variables
109 SET(CPPUNIT_DEFINITIONS)
110 IF(WIN32)
111   SET(CPPUNIT_DEFINITIONS -DCPPUNIT_DLL)
112 ENDIF(WIN32)
113
114 INCLUDE(FindPackageHandleStandardArgs)
115 FIND_PACKAGE_HANDLE_STANDARD_ARGS(CppUnit REQUIRED_VARS CPPUNIT_INCLUDE_DIRS CPPUNIT_LIBRARIES)