Add Github Actions to PRs
[zynaddsubfx-code.git] / cmake / FindCxxTest.cmake
blob1b9f52cc781e2c605017dd2146f2839b89c14ebd
1 # - Find CxxTest
2 # Find the CxxTest suite and declare a helper macro for creating unit tests
3 # and integrating them with CTest.
4 # For more details on CxxTest see http://cxxtest.tigris.org
6 # INPUT Variables
8 #   CXXTEST_USE_PYTHON [deprecated since 1.3]
9 #       Only used in the case both Python & Perl
10 #       are detected on the system to control
11 #       which CxxTest code generator is used.
12 #       Valid only for CxxTest version 3.
14 #       NOTE: In older versions of this Find Module,
15 #       this variable controlled if the Python test
16 #       generator was used instead of the Perl one,
17 #       regardless of which scripting language the
18 #       user had installed.
20 #   CXXTEST_TESTGEN_ARGS (since CMake 2.8.3)
21 #       Specify a list of options to pass to the CxxTest code
22 #       generator.  If not defined, --error-printer is
23 #       passed.
25 # OUTPUT Variables
27 #   CXXTEST_FOUND
28 #       True if the CxxTest framework was found
29 #   CXXTEST_INCLUDE_DIRS
30 #       Where to find the CxxTest include directory
31 #   CXXTEST_PERL_TESTGEN_EXECUTABLE
32 #       The perl-based test generator
33 #   CXXTEST_PYTHON_TESTGEN_EXECUTABLE
34 #       The python-based test generator
35 #   CXXTEST_TESTGEN_EXECUTABLE (since CMake 2.8.3)
36 #       The test generator that is actually used (chosen using user preferences
37 #       and interpreters found in the system)
38 #   CXXTEST_TESTGEN_INTERPRETER (since CMake 2.8.3)
39 #       The full path to the Perl or Python executable on the system
41 # MACROS for optional use by CMake users:
43 #    CXXTEST_ADD_TEST(<test_name> <gen_source_file> <input_files_to_testgen...>)
44 #       Creates a CxxTest runner and adds it to the CTest testing suite
45 #       Parameters:
46 #           test_name               The name of the test
47 #           gen_source_file         The generated source filename to be
48 #                                   generated by CxxTest
49 #           input_files_to_testgen  The list of header files containing the
50 #                                   CxxTest::TestSuite's to be included in
51 #                                   this runner
52 #           
53 #       #==============
54 #       Example Usage:
56 #           find_package(CxxTest)
57 #           if(CXXTEST_FOUND)
58 #               include_directories(${CXXTEST_INCLUDE_DIR})
59 #               enable_testing()
61 #               CXXTEST_ADD_TEST(unittest_foo foo_test.cc
62 #                                 ${CMAKE_CURRENT_SOURCE_DIR}/foo_test.h)
63 #               target_link_libraries(unittest_foo foo) # as needed
64 #           endif()
66 #              This will (if CxxTest is found):
67 #              1. Invoke the testgen executable to autogenerate foo_test.cc in the
68 #                 binary tree from "foo_test.h" in the current source directory.
69 #              2. Create an executable and test called unittest_foo.
70 #               
71 #      #=============
72 #      Example foo_test.h:
74 #          #include <cxxtest/TestSuite.h>
75 #          
76 #          class MyTestSuite : public CxxTest::TestSuite 
77 #          {
78 #          public:
79 #             void testAddition( void )
80 #             {
81 #                TS_ASSERT( 1 + 1 > 1 );
82 #                TS_ASSERT_EQUALS( 1 + 1, 2 );
83 #             }
84 #          };
87 #=============================================================================
88 # Copyright 2008-2010 Kitware, Inc.
89 # Copyright 2008-2010 Philip Lowman <philip@yhbt.com>
91 # Distributed under the OSI-approved BSD License (the "License");
92 # see accompanying file Copyright.txt for details.
94 # This software is distributed WITHOUT ANY WARRANTY; without even the
95 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
96 # See the License for more information.
97 #=============================================================================
98 # (To distribute this file outside of CMake, substitute the full
99 #  License text for the above reference.)
101 # Version 1.4 (11/18/10) (CMake 2.8.4)
102 #     Issue 11384: Added support to the CXX_ADD_TEST macro so header
103 #                  files (containing the tests themselves) show up in
104 #                  Visual Studio and other IDEs.
106 # Version 1.3 (8/19/10) (CMake 2.8.3)
107 #     Included patch by Simone Rossetto to check if either Python or Perl
108 #     are present in the system.  Whichever interpreter that is detected
109 #     is now used to run the test generator program.  If both interpreters
110 #     are detected, the CXXTEST_USE_PYTHON variable is obeyed.
112 #     Also added support for CXXTEST_TESTGEN_ARGS, for manually specifying
113 #     options to the CxxTest code generator.
114 # Version 1.2 (3/2/08)
115 #     Included patch from Tyler Roscoe to have the perl & python binaries
116 #     detected based on CXXTEST_INCLUDE_DIR
117 # Version 1.1 (2/9/08)
118 #     Clarified example to illustrate need to call target_link_libraries()
119 #     Changed commands to lowercase
120 #     Added licensing info
121 # Version 1.0 (1/8/08)
122 #     Fixed CXXTEST_INCLUDE_DIRS so it will work properly
123 #     Eliminated superfluous CXXTEST_FOUND assignment
124 #     Cleaned up and added more documentation
126 #=============================================================
127 # CXXTEST_ADD_TEST (public macro)
128 #=============================================================
129 macro(CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname)
130     set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
132     add_custom_command(
133         OUTPUT  ${_cxxtest_real_outfname}
134         DEPENDS ${ARGN}
135         COMMAND ${CXXTEST_TESTGEN_INTERPRETER}
136         ${CXXTEST_TESTGEN_EXECUTABLE} ${CXXTEST_TESTGEN_ARGS} -o ${_cxxtest_real_outfname} ${ARGN}
137     )
139     set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
140     add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname} ${ARGN})
142     if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
143         add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
144     elseif(EXECUTABLE_OUTPUT_PATH)
145         add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
146     else()
147         add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
148     endif()
150 endmacro(CXXTEST_ADD_TEST)
152 #=============================================================
153 # main()
154 #=============================================================
155 if(NOT DEFINED CXXTEST_TESTGEN_ARGS)
156    set(CXXTEST_TESTGEN_ARGS --error-printer)
157 endif()
159 find_package(PythonInterp QUIET)
160 find_package(Perl QUIET)
162 find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
163 find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE
164          NAMES cxxtestgen cxxtestgen.py
165          PATHS ${CXXTEST_INCLUDE_DIR})
166 find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
167          PATHS ${CXXTEST_INCLUDE_DIR})
169 if(PYTHONINTERP_FOUND OR PERL_FOUND)
170    include(FindPackageHandleStandardArgs)
172    if(PYTHONINTERP_FOUND AND (CXXTEST_USE_PYTHON OR NOT PERL_FOUND OR NOT DEFINED CXXTEST_USE_PYTHON))
173       set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
174       set(CXXTEST_TESTGEN_INTERPRETER ${PYTHON_EXECUTABLE})
175       FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
176           CXXTEST_INCLUDE_DIR CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
178    elseif(PERL_FOUND)
179       set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
180       set(CXXTEST_TESTGEN_INTERPRETER ${PERL_EXECUTABLE})
181       FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
182           CXXTEST_INCLUDE_DIR CXXTEST_PERL_TESTGEN_EXECUTABLE)
183    endif()
185    if(CXXTEST_FOUND)
186       set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})
187    endif()
189 else()
191    set(CXXTEST_FOUND false)
192    if(NOT CxxTest_FIND_QUIETLY)
193       if(CxxTest_FIND_REQUIRED)
194          message(FATAL_ERROR "Neither Python nor Perl found, cannot use CxxTest, aborting!")
195       else()
196          message(STATUS "Neither Python nor Perl found, CxxTest will not be used.")
197       endif()
198    endif()
200 endif()