FindCxxTest.cmake 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. # Valid only for CxxTest version 3.
  13. #
  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.
  19. #
  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.
  24. #
  25. # OUTPUT Variables
  26. #
  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
  40. #
  41. # MACROS for optional use by CMake users:
  42. #
  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:
  55. #
  56. # find_package(CxxTest)
  57. # if(CXXTEST_FOUND)
  58. # include_directories(${CXXTEST_INCLUDE_DIR})
  59. # enable_testing()
  60. #
  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()
  65. #
  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:
  73. #
  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. # };
  85. #
  86. #=============================================================================
  87. # Copyright 2008-2010 Kitware, Inc.
  88. # Copyright 2008-2010 Philip Lowman <[email protected]>
  89. #
  90. # Distributed under the OSI-approved BSD License (the "License");
  91. # see accompanying file Copyright.txt for details.
  92. #
  93. # This software is distributed WITHOUT ANY WARRANTY; without even the
  94. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  95. # See the License for more information.
  96. #=============================================================================
  97. # (To distribute this file outside of CMake, substitute the full
  98. # License text for the above reference.)
  99. # Version 1.4 (11/18/10) (CMake 2.8.4)
  100. # Issue 11384: Added support to the CXX_ADD_TEST macro so header
  101. # files (containing the tests themselves) show up in
  102. # Visual Studio and other IDEs.
  103. #
  104. # Version 1.3 (8/19/10) (CMake 2.8.3)
  105. # Included patch by Simone Rossetto to check if either Python or Perl
  106. # are present in the system. Whichever intepreter that is detected
  107. # is now used to run the test generator program. If both interpreters
  108. # are detected, the CXXTEST_USE_PYTHON variable is obeyed.
  109. #
  110. # Also added support for CXXTEST_TESTGEN_ARGS, for manually specifying
  111. # options to the CxxTest code generator.
  112. # Version 1.2 (3/2/08)
  113. # Included patch from Tyler Roscoe to have the perl & python binaries
  114. # detected based on CXXTEST_INCLUDE_DIR
  115. # Version 1.1 (2/9/08)
  116. # Clarified example to illustrate need to call target_link_libraries()
  117. # Changed commands to lowercase
  118. # Added licensing info
  119. # Version 1.0 (1/8/08)
  120. # Fixed CXXTEST_INCLUDE_DIRS so it will work properly
  121. # Eliminated superfluous CXXTEST_FOUND assignment
  122. # Cleaned up and added more documentation
  123. #=============================================================
  124. # CXXTEST_ADD_TEST (public macro)
  125. #=============================================================
  126. macro(CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname)
  127. set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
  128. add_custom_command(
  129. OUTPUT ${_cxxtest_real_outfname}
  130. DEPENDS ${ARGN}
  131. COMMAND ${CXXTEST_TESTGEN_INTERPRETER}
  132. ${CXXTEST_TESTGEN_EXECUTABLE} ${CXXTEST_TESTGEN_ARGS} -o ${_cxxtest_real_outfname} ${ARGN}
  133. )
  134. set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
  135. add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname} ${ARGN})
  136. if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  137. add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
  138. elseif(EXECUTABLE_OUTPUT_PATH)
  139. add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
  140. else()
  141. add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
  142. endif()
  143. endmacro()
  144. #=============================================================
  145. # main()
  146. #=============================================================
  147. if(NOT DEFINED CXXTEST_TESTGEN_ARGS)
  148. set(CXXTEST_TESTGEN_ARGS --error-printer)
  149. endif()
  150. find_package(PythonInterp QUIET)
  151. find_package(Perl QUIET)
  152. find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
  153. find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE
  154. NAMES cxxtestgen cxxtestgen.py
  155. PATHS ${CXXTEST_INCLUDE_DIR})
  156. find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
  157. PATHS ${CXXTEST_INCLUDE_DIR})
  158. if(PYTHONINTERP_FOUND OR PERL_FOUND)
  159. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  160. if(PYTHONINTERP_FOUND AND (CXXTEST_USE_PYTHON OR NOT PERL_FOUND OR NOT DEFINED CXXTEST_USE_PYTHON))
  161. set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
  162. set(CXXTEST_TESTGEN_INTERPRETER ${PYTHON_EXECUTABLE})
  163. FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
  164. CXXTEST_INCLUDE_DIR CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
  165. elseif(PERL_FOUND)
  166. set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
  167. set(CXXTEST_TESTGEN_INTERPRETER ${PERL_EXECUTABLE})
  168. FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
  169. CXXTEST_INCLUDE_DIR CXXTEST_PERL_TESTGEN_EXECUTABLE)
  170. endif()
  171. if(CXXTEST_FOUND)
  172. set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})
  173. endif()
  174. else()
  175. set(CXXTEST_FOUND false)
  176. if(NOT CxxTest_FIND_QUIETLY)
  177. if(CxxTest_FIND_REQUIRED)
  178. message(FATAL_ERROR "Neither Python nor Perl found, cannot use CxxTest, aborting!")
  179. else()
  180. message(STATUS "Neither Python nor Perl found, CxxTest will not be used.")
  181. endif()
  182. endif()
  183. endif()