FindCxxTest.cmake 7.6 KB

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