FindCxxTest.cmake 7.6 KB

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