1# This checks HeaderTest's in each module.  A HeaderTest can be found in the
2# module 'test' directory in a file itk<module_name>HeaderTest.cxx.  This
3# contains a null main(), but includes all the classes in the module.  The
4# primary purpose of this test is to make sure there are not missing module
5# dependencies.
6
7# This does not force the developer to install python to be able to build ITK.
8# The tests will simply not be run if python is unavailable.
9find_package(PythonInterp)
10
11# Improve performance of MSVC GUI, by reducing number of header tests.
12set( MAXIMUM_NUMBER_OF_HEADERS_default 35 )
13if( MSVC )
14  set( MAXIMUM_NUMBER_OF_HEADERS_default 9999 )
15endif()
16
17# The maximum number of headers in a test.  This helps limit memory issues,
18# and the cppcheck tests.  However, if this is not unity, there is a slight
19# chance that problems may be hidden.  For a complete header check, set to "1".
20set( MAXIMUM_NUMBER_OF_HEADERS ${MAXIMUM_NUMBER_OF_HEADERS_default} CACHE STRING "The number of headers in a HeaderTest code." )
21mark_as_advanced( MAXIMUM_NUMBER_OF_HEADERS )
22
23if(NOT TARGET ITKHeaderTests)
24  add_custom_target( ITKHeaderTests
25    ${CMAKE_COMMAND} --build ${ITK_BINARY_DIR}
26    COMMENT "Regenerating and building the header tests." )
27endif()
28
29macro( itk_module_headertest _name )
30  if( NOT ${_name}_THIRD_PARTY
31      AND EXISTS ${${_name}_SOURCE_DIR}/include
32      AND PYTHON_EXECUTABLE
33      AND NOT (PYTHON_VERSION_STRING VERSION_LESS 2.6)
34      AND NOT (${_name} STREQUAL ITKTestKernel)
35      AND NOT (CMAKE_GENERATOR MATCHES "^Visual Studio 10.*"))
36
37    # Count how many tests we are going to get, and put the source files in
38    # the list _outputs.
39    # WARNING: This code is highly coupled with the BuildHeaderTest.py file
40    # below.  Before making any logic changes here, make sure that script is not
41    # effected.
42    set( _include ${${_name}_SOURCE_DIR}/include )
43    file( GLOB _h_files ${_include}/*.h )
44    file( GLOB _hxx_files ${_include}/*.hxx )
45    set( _header_files ${_h_files} ${_hxx_files} )
46    list( LENGTH _h_files _num_headers )
47    set( _outputs ${${_name}_BINARY_DIR}/test/${_name}HeaderTest1.cxx )
48    set( _test_num 1 )
49    set( _available_headers "${MAXIMUM_NUMBER_OF_HEADERS}" )
50    while( ${_num_headers} GREATER ${_available_headers} )
51      math( EXPR _test_num "${_test_num} + 1" )
52      math( EXPR _available_headers "${_available_headers} + ${MAXIMUM_NUMBER_OF_HEADERS}" )
53      list( APPEND _outputs
54        ${${_name}_BINARY_DIR}/test/${_name}HeaderTest${_test_num}.cxx )
55    endwhile()
56
57    add_custom_target( ${_name}HeaderTestClean
58      ${CMAKE_COMMAND} -E remove ${_outputs} )
59    add_dependencies( ITKHeaderTests ${_name}HeaderTestClean )
60
61    # We check to see if the headers are changed.  If so, remove the header test
62    # source files so they are regenerated.
63    set( _headers_list_md5 "${${_name}_BINARY_DIR}/test/CMakeFiles/HeadersList.md5" )
64    list( SORT _header_files )
65    string( MD5 _new_md5 "${_header_files}" )
66    set( _regenerate_sources FALSE )
67    if( NOT EXISTS "${_headers_list_md5}" )
68      set( _regenerate_sources TRUE )
69    else()
70      file( READ "${_headers_list_md5}" _old_md5 )
71      if( NOT ("${_old_md5}" STREQUAL "${_new_md5}"))
72        set( _regenerate_sources TRUE )
73      endif()
74    endif()
75    file( WRITE "${_headers_list_md5}" "${_new_md5}" )
76    if( ${_regenerate_sources} )
77      file( REMOVE ${_outputs} )
78    endif()
79
80    set( _test_num 1 )
81    foreach( _header_test_src ${_outputs} )
82      get_filename_component( _test_name ${_header_test_src} NAME_WE )
83      add_custom_command(
84        OUTPUT ${_header_test_src}
85        COMMAND ${PYTHON_EXECUTABLE} ${ITK_CMAKE_DIR}/../Utilities/Maintenance/BuildHeaderTest.py
86        ${_name}
87        ${${_name}_SOURCE_DIR}
88        ${${_name}_BINARY_DIR}
89        ${MAXIMUM_NUMBER_OF_HEADERS}
90        ${_test_num}
91        )
92      add_executable( ${_test_name} ${_header_test_src} )
93      target_link_libraries( ${_test_name} ${${_name}_LIBRARIES} itksys )
94
95      add_dependencies(${_name}-all ${_test_name})
96      math( EXPR _test_num "${_test_num} + 1" )
97    endforeach()
98  endif()
99endmacro()
100