FindCxxTest.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.3 (8/19/10) (CMake 2.8.3)
  99. # Included patch by Simone Rossetto to check if either Python or Perl
  100. # are present in the system. Whichever intepreter that is detected
  101. # is now used to run the test generator program. If both interpreters
  102. # are detected, the CXXTEST_USE_PYTHON variable is obeyed.
  103. #
  104. # Also added support for CXXTEST_TESTGEN_ARGS, for manually specifying
  105. # options to the CxxTest code generator.
  106. # Version 1.2 (3/2/08)
  107. # Included patch from Tyler Roscoe to have the perl & python binaries
  108. # detected based on CXXTEST_INCLUDE_DIR
  109. # Version 1.1 (2/9/08)
  110. # Clarified example to illustrate need to call target_link_libraries()
  111. # Changed commands to lowercase
  112. # Added licensing info
  113. # Version 1.0 (1/8/08)
  114. # Fixed CXXTEST_INCLUDE_DIRS so it will work properly
  115. # Eliminated superfluous CXXTEST_FOUND assignment
  116. # Cleaned up and added more documentation
  117. #=============================================================
  118. # CXXTEST_ADD_TEST (public macro)
  119. #=============================================================
  120. macro(CXXTEST_ADD_TEST _cxxtest_testname _cxxtest_outfname)
  121. set(_cxxtest_real_outfname ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_outfname})
  122. add_custom_command(
  123. OUTPUT ${_cxxtest_real_outfname}
  124. DEPENDS ${ARGN}
  125. COMMAND ${CXXTEST_TESTGEN_INTERPRETER}
  126. ${CXXTEST_TESTGEN_EXECUTABLE} ${CXXTEST_TESTGEN_ARGS} -o ${_cxxtest_real_outfname} ${ARGN}
  127. )
  128. set_source_files_properties(${_cxxtest_real_outfname} PROPERTIES GENERATED true)
  129. add_executable(${_cxxtest_testname} ${_cxxtest_real_outfname})
  130. if(CMAKE_RUNTIME_OUTPUT_DIRECTORY)
  131. add_test(${_cxxtest_testname} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${_cxxtest_testname})
  132. elseif(EXECUTABLE_OUTPUT_PATH)
  133. add_test(${_cxxtest_testname} ${EXECUTABLE_OUTPUT_PATH}/${_cxxtest_testname})
  134. else()
  135. add_test(${_cxxtest_testname} ${CMAKE_CURRENT_BINARY_DIR}/${_cxxtest_testname})
  136. endif()
  137. endmacro(CXXTEST_ADD_TEST)
  138. #=============================================================
  139. # main()
  140. #=============================================================
  141. if(NOT DEFINED CXXTEST_TESTGEN_ARGS)
  142. set(CXXTEST_TESTGEN_ARGS --error-printer)
  143. endif()
  144. find_package(PythonInterp QUIET)
  145. find_package(Perl QUIET)
  146. find_path(CXXTEST_INCLUDE_DIR cxxtest/TestSuite.h)
  147. find_program(CXXTEST_PYTHON_TESTGEN_EXECUTABLE cxxtestgen.py
  148. PATHS ${CXXTEST_INCLUDE_DIR})
  149. find_program(CXXTEST_PERL_TESTGEN_EXECUTABLE cxxtestgen.pl
  150. PATHS ${CXXTEST_INCLUDE_DIR})
  151. if(PYTHONINTERP_FOUND OR PERL_FOUND)
  152. include(FindPackageHandleStandardArgs)
  153. if(PYTHONINTERP_FOUND AND (CXXTEST_USE_PYTHON OR NOT PERL_FOUND))
  154. set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PYTHON_TESTGEN_EXECUTABLE})
  155. set(CXXTEST_TESTGEN_INTERPRETER ${PYTHON_EXECUTABLE})
  156. FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
  157. CXXTEST_INCLUDE_DIR CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
  158. elseif(PERL_FOUND)
  159. set(CXXTEST_TESTGEN_EXECUTABLE ${CXXTEST_PERL_TESTGEN_EXECUTABLE})
  160. set(CXXTEST_TESTGEN_INTERPRETER ${PERL_EXECUTABLE})
  161. FIND_PACKAGE_HANDLE_STANDARD_ARGS(CxxTest DEFAULT_MSG
  162. CXXTEST_INCLUDE_DIR CXXTEST_PERL_TESTGEN_EXECUTABLE)
  163. endif()
  164. if(CXXTEST_FOUND)
  165. set(CXXTEST_INCLUDE_DIRS ${CXXTEST_INCLUDE_DIR})
  166. endif()
  167. else()
  168. set(CXXTEST_FOUND false)
  169. if(NOT CxxTest_FIND_QUIETLY)
  170. if(CxxTest_FIND_REQUIRED)
  171. message(FATAL_ERROR "Neither Python nor Perl found, cannot use CxxTest, aborting!")
  172. else()
  173. message(STATUS "Neither Python nor Perl found, CxxTest will not be used.")
  174. endif()
  175. endif()
  176. endif()