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