-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathITKSphinxExamplesMacros.cmake
More file actions
207 lines (185 loc) · 7.78 KB
/
Copy pathITKSphinxExamplesMacros.cmake
File metadata and controls
207 lines (185 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
include(CMakeParseArguments)
# A high level target that the individual targets depend on.
#add_custom_target( CreateDownloadableArchives ALL
# ${CMAKE_COMMAND} -E echo "Done creating individual example downloadable archives..."
#)
# Macro for adding an example.
#
# Most of the CMake logic for the example should live in the examples
# CMakeLists.txt. That file should be standalone for building the example.
# This macro is intended to be instantiated in the CMakeLists.txt one directory
# above where the example exists. It does things like creating the example
# archives's in the example output directory, and custom targets for building
# the Sphinx documentation for an individual example. The Sphinx target will be
# called ${example_name}Doc.
macro(add_example example_name)
# Process the example's CMakeLists.txt
add_subdirectory(${example_name})
endmacro()
# Macro for adding module archives
macro(add_module_archives _module_name)
file(GLOB
${_module_name}_EXAMPLE_DIRS
CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/${_module_name}/*/
)
if(BUILD_DOCUMENTATION AND SPHINX_HTML_OUTPUT)
foreach(_example_path ${${_module_name}_EXAMPLE_DIRS})
if(IS_DIRECTORY ${_example_path})
get_filename_component(_example_name ${_example_path} NAME)
get_filename_component(_example_parent_path ${_example_path} DIRECTORY)
get_filename_component(_example_parent_name ${_example_parent_path} NAME)
set(_archive_target ${_example_parent_name}_${_example_name}_DownloadableArchive)
add_custom_target(${_archive_target}
COMMAND ${Python3_EXECUTABLE} ${ITKSphinxExamples_SOURCE_DIR}/Utilities/CreateDownloadableArchive.py
${_example_name} ${SPHINX_DESTINATION}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_example_parent_name}
COMMENT "Creating downloadable archive for ${_example_name}"
DEPENDS copy_sources
)
add_dependencies(CreateDownloadableArchives ${_archive_target})
endif()
endforeach()
endif()
endmacro()
# Macro if the corresponding module is enabled when ITK was built
# Pass in the module directory path. Also pass the module name as the second
# argument if it is different than ITK${_dir}.
macro(add_subdirectory_if_module_enabled _dir)
set(_module_name ITK${_dir})
if("${ARGV1}" STRGREATER "")
set(_module_name ${ARGV1})
endif()
list(FIND ITK_MODULES_ENABLED ${_module_name} _have)
if(NOT ${_have} EQUAL "-1")
add_subdirectory(${_dir})
endif()
endmacro()
macro(add_subdirectories_if_module_enabled _dirs_to_ignore)
file(GLOB
_current_subdirectories
CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/*/
)
message(STATUS "Got subdirectories ${_current_subdirectories}")
foreach(_subdir ${_current_subdirectories})
get_filename_component(_subdir_name ${_subdir} NAME)
if(${_subdir_name} NOT IN ${_dirs_to_ignore})
add_subdirectory_if_module_enabled(${_subdir_name})
endif()
endforeach()
endmacro()
# Creates a test that compares the output image of an example to its baseline
# image.
#
# Usage:
# compare_to_baseline(
# [TEST_NAME myAwesomeExampleBaselineComparison1] // This argument is optional and meant
# // to be used when there would be several
# // baseline comparison tests based on one given
# // example. By default TEST_NAME is
# // ${EXAMPLE_NAME}TestBaselineComparison.
#
# EXAMPLE_NAME myAwesomeExampleName
#
# BASELINE_PREFIX myBaselineImagePrefix
#
# [TEST_IMAGE myAwesomeExampleOutputImagePrefix.png] // This argument is optional and meant
# // to be used when there would be several
# // baseline comparison tests based on one given
# // example.
#
# [OPTIONS myListOfOptions] // Options to pass to ImageCompareCommand.
#
# [NO_PYTHON_COMPARISON] // Skip the auto-added Python baseline comparison
# // when the Python output is not pixel-comparable
# // to the C++ baseline (e.g. a Matplotlib figure).
#
# [DEPENDS myListOfDependencies] // Other tests that the comparison test will
# // depend on. By default, the dependency is assumed
# // to be ${EXAMPLE_NAME}Test. For Python, the comparison
# // test is added automatically. It is expected that
# // the default corresponding Python test is named
# // ${EXAMPLE_NAME}TestPython.
# )
function(compare_to_baseline)
set(options
PYTHON_ONLY
NO_PYTHON_COMPARISON
)
set(oneValueArgs
EXAMPLE_NAME
TEST_IMAGE
BASELINE_PREFIX
TEST_NAME
)
set(multiValueArgs
DEPENDS
OPTIONS
)
cmake_parse_arguments(LOCAL_COMPARISON
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(NOT LOCAL_COMPARISON_EXAMPLE_NAME)
message(FATAL_ERROR "Syntax error in use of compare_to_baseline: EXAMPLE_NAME is required")
endif()
if(NOT LOCAL_COMPARISON_BASELINE_PREFIX)
message(FATAL_ERROR "Syntax error in use of compare_to_baseline: BASELINE_PREFIX is required")
endif()
set(depends ${LOCAL_COMPARISON_EXAMPLE_NAME}Test)
if(LOCAL_COMPARISON_DEPENDS)
set(depends ${LOCAL_COMPARISON_DEPENDS})
endif()
file(GLOB baseline_image ${CMAKE_CURRENT_SOURCE_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/${LOCAL_COMPARISON_BASELINE_PREFIX}.*)
string(REPLACE .cid "" baseline_image "${baseline_image}")
get_filename_component(baseline_image_file "${baseline_image}" NAME)
set(baseline_image "${CMAKE_CURRENT_BINARY_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/${baseline_image_file}")
if(NOT LOCAL_COMPARISON_TEST_IMAGE)
string(REPLACE Baseline "" test_image "${baseline_image}")
else()
set(test_image ${CMAKE_CURRENT_BINARY_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/${LOCAL_COMPARISON_TEST_IMAGE})
endif()
set(test_name)
if(NOT LOCAL_COMPARISON_TEST_NAME)
set(test_name ${LOCAL_COMPARISON_EXAMPLE_NAME}TestBaselineComparison)
else()
set(test_name ${LOCAL_COMPARISON_TEST_NAME})
endif()
if(NOT ${PYTHON_ONLY})
add_test(NAME ${test_name}
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ImageCompareCommand"
--test-image "${test_image}"
--baseline-image "${baseline_image}"
${LOCAL_COMPARISON_OPTIONS}
)
set_tests_properties(${test_name}
PROPERTIES DEPENDS ${depends}
)
endif()
if(ITK_WRAP_PYTHON AND NOT LOCAL_COMPARISON_NO_PYTHON_COMPARISON AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${LOCAL_COMPARISON_EXAMPLE_NAME}/Code.py")
set(python_test_name ${test_name}Python)
get_filename_component(test_image_we "${test_image}" NAME_WE)
get_filename_component(test_image_dir "${test_image}" DIRECTORY)
get_filename_component(test_image_ext "${test_image}" EXT)
set(test_image "${test_image_dir}/${test_image_we}Python${test_image_ext}")
# Add the comparison for the Python test output
# Use the same baseline image as for the c++ output
add_test(NAME ${python_test_name}
COMMAND "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ImageCompareCommand"
--test-image "${test_image}"
--baseline-image "${baseline_image}"
${LOCAL_COMPARISON_OPTIONS}
)
# Depend on the Python test.
set(python_depends )
foreach(dependency ${depends})
list(APPEND python_depends ${dependency}Python)
endforeach()
set_tests_properties(${python_test_name}
PROPERTIES DEPENDS ${python_depends}
)
endif()
endfunction()