FindCxxTest.cmake 7.7 KB

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