File: ITKModuleRemote.cmake

package info (click to toggle)
insighttoolkit5 5.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,404 kB
  • sloc: cpp: 783,697; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (288 lines) | stat: -rw-r--r-- 10,263 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Function to fetch remote modules.

# Helper to perform the initial git clone and checkout.
function(
  _git_clone
  git_executable
  git_repository
  git_tag
  module_dir)
  execute_process(
    COMMAND "${git_executable}" clone "${git_repository}" "${module_dir}"
    RESULT_VARIABLE error_code
    OUTPUT_QUIET ERROR_QUIET)
  if(error_code)
    message(FATAL_ERROR "Failed to clone repository: '${git_repository}'")
  endif()

  execute_process(
    COMMAND "${git_executable}" checkout ${git_tag}
    WORKING_DIRECTORY "${module_dir}"
    RESULT_VARIABLE error_code
    OUTPUT_QUIET ERROR_QUIET)
  if(error_code)
    message(FATAL_ERROR "Failed to checkout tag: '${git_tag}'")
  endif()

  execute_process(
    COMMAND "${git_executable}" submodule init
    WORKING_DIRECTORY "${module_dir}"
    RESULT_VARIABLE error_code)
  if(error_code)
    message(FATAL_ERROR "Failed to init submodules in: '${module_dir}'")
  endif()

  execute_process(
    COMMAND "${git_executable}" submodule update --recursive
    WORKING_DIRECTORY "${module_dir}"
    RESULT_VARIABLE error_code)
  if(error_code)
    message(FATAL_ERROR "Failed to update submodules in: '${module_dir}'")
  endif()
endfunction()

# Helper to perform a git update.  Checks the current Git revision against the
# desired revision and only performs a fetch and checkout if needed.
function(
  _git_update
  git_executable
  git_repository
  git_tag
  module_dir)
  # Verify that remote url are the same
  execute_process(
    COMMAND "${git_executable}" config --get remote.origin.url
    WORKING_DIRECTORY "${module_dir}"
    RESULT_VARIABLE error_code
    OUTPUT_VARIABLE remote_origin_url
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  if(NOT
     "${remote_origin_url}"
     STREQUAL
     "${git_repository}")
    message(WARNING "Remote URL changed from ${git_repository} to ${remote_origin_url}")
    execute_process(
      COMMAND "${git_executable}" remote rename origin old_origin
      WORKING_DIRECTORY "${module_dir}"
      RESULT_VARIABLE error_code
      OUTPUT_VARIABLE ignored
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(
      COMMAND "${git_executable}" remote add origin "${git_repository}"
      WORKING_DIRECTORY "${module_dir}"
      RESULT_VARIABLE error_code
      OUTPUT_VARIABLE ignored
      OUTPUT_STRIP_TRAILING_WHITESPACE)
  endif()
  execute_process(
    COMMAND "${git_executable}" rev-parse --verify "${git_tag}"
    WORKING_DIRECTORY "${module_dir}"
    RESULT_VARIABLE error_code
    OUTPUT_VARIABLE tag_hash
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  if(error_code)
    message(FATAL_ERROR "Failed to get the hash for tag '${module_dir}'")
  endif()
  execute_process(
    COMMAND "${git_executable}" rev-parse --verify HEAD
    WORKING_DIRECTORY "${module_dir}"
    RESULT_VARIABLE error_code
    OUTPUT_VARIABLE head_hash
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  if(error_code)
    message(FATAL_ERROR "Failed to get the hash for ${git_repository} HEAD")
  endif()

  # Is the hash checkout out that we want?
  if(NOT ("${tag_hash}" STREQUAL "${head_hash}"))
    execute_process(
      COMMAND "${git_executable}" fetch "${git_repository}"
      WORKING_DIRECTORY "${module_dir}"
      RESULT_VARIABLE error_code)
    if(error_code)
      message(FATAL_ERROR "Failed to fetch repository '${git_repository}'")
    endif()

    execute_process(
      COMMAND "${git_executable}" checkout ${git_tag}
      WORKING_DIRECTORY "${module_dir}"
      RESULT_VARIABLE error_code)
    if(error_code)
      message(FATAL_ERROR "Failed to checkout tag: '${git_tag}'")
    endif()

    execute_process(
      COMMAND "${git_executable}" submodule update --recursive
      WORKING_DIRECTORY "${module_dir}"
      RESULT_VARIABLE error_code)
    if(error_code)
      message(FATAL_ERROR "Failed to update submodules in: '${module_dir}'")
    endif()
  endif()
endfunction()

# Helper function to fetch a module stored in a Git repository.
# Git fetches are only performed when required.
function(
  _fetch_with_git
  git_executable
  git_repository
  git_tag
  module_dir)
  if("${git_tag}" STREQUAL "" OR "${git_repository}" STREQUAL "")
    message(FATAL_ERROR "Tag or repository for git checkout should not be empty.")
  endif()

  # If we don't have a clone yet.
  if(NOT EXISTS "${module_dir}")
    _git_clone(
      "${git_executable}"
      "${git_repository}"
      "${git_tag}"
      "${module_dir}")
    message(STATUS " The remote module: ${git_repository} is cloned into the directory ${module_dir}")
  else() # We already have a clone, but we need to check that it has the right revision.
    _git_update(
      "${git_executable}"
      "${git_repository}"
      "${git_tag}"
      "${module_dir}")
  endif()
endfunction()

# Download and turn on a remote module.
#
# The module CMake option is created: Module_${module_name}, which defaults to OFF.
# Once set to ON, the module is downloaded into the Remote module group.
#
# A module name and description are required.  The description will show up in
# the CMake user interface.
#
# The following options are currently supported:
#    [MODULE_COMPLIANCE_LEVEL [5|4|3|2|1|0] ] # The compliance level of the module, used for filtering
#            Compliance level 5 star (AKA ITK main modules, or remote modules that could become core modules)
#            Compliance Level 4 star (Very high-quality code, perhaps small community dependance)
#            Compliance Level 3 star (Quality beta code)
#            Compliance Level 2 star (Alpha code feature API development or niche community/execution environment dependance)
#            Compliance Level 1 star (Pre-alpha features under development and code of unknown quality)
#            Compliance Level 0 star ( Code/Feature of known poor-quality or deprecated status)
#    [GIT_REPOSITORY url]            # URL of git repo
#    [GIT_TAG tag]                   # Git branch name, commit id or tag
#
# A CMake variable Module_${name}_GIT_TAG can be set
# in to override the value in the remote module configuration file.
# The intent of the Module_${name}_GIT_TAG variable override is to
# facilitate testing of remote module branch behaviors without
# requiring changes to the ITK code base. If Module_${name}_GIT_TAG is
# "" then no git fetch or update will be performed.
function(itk_fetch_module _name _description)
  include(CMakeParseArguments)
  cmake_parse_arguments(
    _fetch_options
    ""
    "MODULE_COMPLIANCE_LEVEL;GIT_REPOSITORY;GIT_TAG"
    ""
    ${ARGN})
  set(MODULE_COMPLIANCE_LEVEL "${_fetch_options_MODULE_COMPLIANCE_LEVEL}")
  if(NOT MODULE_COMPLIANCE_LEVEL)
    set(DEFAULT_MODULE_COMPLIANCE_LEVEL 1)
    message(
      STATUS
        "Implicitly setting unspecified compliance level for module Module_${_name} to ${DEFAULT_MODULE_COMPLIANCE_LEVEL}"
    )
    set(MODULE_COMPLIANCE_LEVEL ${DEFAULT_MODULE_COMPLIANCE_LEVEL})
  endif()

  set(Module_${_name}_REMOTE_COMPLIANCE_LEVEL
      ${MODULE_COMPLIANCE_LEVEL}
      CACHE INTERNAL "Variable to indicate the Module_${_name} compliance level")

  if(NOT DEFINED Module_${_name})
    option(Module_${_name} "(Remote-${MODULE_COMPLIANCE_LEVEL}) ${_description}" OFF)
    mark_as_advanced(Module_${_name})
  else()
    # If Module_${_name} is set manually, put its value in the CACHE
    option(Module_${_name} "(Remote-${MODULE_COMPLIANCE_LEVEL}) ${_description}" ${Module_${_name}})
  endif()

  if(${MODULE_COMPLIANCE_LEVEL} GREATER_EQUAL ${ITK_MINIMUM_COMPLIANCE_LEVEL})
    set(Module_${_name}_VALID ON)
  else()
    set(Module_${_name}_VALID OFF)
  endif()
  # message(INFO " MODULE_VALID Module_${_name}:${Module_${_name}_VALID}:${MODULE_COMPLIANCE_LEVEL}>=${ITK_MINIMUM_COMPLIANCE_LEVEL}")

  # Fetch_$_remote_module} is deprecated. To maintain backward compatibility:
  if(Fetch_${_name})
    message(
      WARNING "Fetch_${_name} is deprecated, please use Module_${_name} to download and enable the remote module.")
    set(Module_${_name}
        ON
        CACHE FORCE "(Remote-${MODULE_COMPLIANCE_LEVEL}) ${_description}")
  endif()

  if(Module_${_name})
    if(ITK_FORBID_DOWNLOADS)
      return()
    endif()
    itk_download_attempt_check(Module_${_name})
    find_package(Git)
    if(NOT GIT_EXECUTABLE)
      message(FATAL_ERROR "error: could not find git for clone of ${_name}")
    endif()
    execute_process(
      COMMAND "${GIT_EXECUTABLE}" --version
      OUTPUT_VARIABLE ov
      OUTPUT_STRIP_TRAILING_WHITESPACE)
    string(
      REGEX
      REPLACE "^git version (.+)$"
              "\\1"
              _version
              "${ov}")
    if("${_version}" VERSION_LESS 1.6.6)
      message(FATAL_ERROR "Git version 1.6.6 or later is required.")
    endif()

    set(REMOTE_GIT_TAG "${_fetch_options_GIT_TAG}")

    if(DEFINED Module_${_name}_GIT_TAG
       AND NOT
           "${Module_${_name}_GIT_TAG}"
           STREQUAL
           "${_fetch_options_GIT_TAG}")
      set(REMOTE_GIT_TAG "${Module_${_name}_GIT_TAG}")
      message(STATUS "NOTE: Using override 'Module_${_name}_GIT_TAG=${REMOTE_GIT_TAG}'\n"
                     "      instead of value 'GIT_TAG=${_fetch_options_GIT_TAG}'\n"
                     "      specified in file ${ITK_SOURCE_DIR}/Modules/Remote/${_name}.remote.cmake'")
    endif()
    set(Module_${_name}_GIT_TAG
        "${REMOTE_GIT_TAG}"
        CACHE STRING "Override default GIT_TAG value for remote module ${_name}")
    mark_as_advanced(Module_${_name}_GIT_TAG)
    # Show remote module options if building.
    set_property(CACHE Module_${_name}_GIT_TAG PROPERTY TYPE STRING)
    if(DEFINED Module_${_name}_BUILD_EXAMPLES)
      set_property(CACHE Module_${_name}_BUILD_EXAMPLES PROPERTY TYPE BOOL)
    endif()

    if(NOT
       REMOTE_GIT_TAG
       STREQUAL
       "")
      _fetch_with_git(
        "${GIT_EXECUTABLE}"
        "${_fetch_options_GIT_REPOSITORY}"
        "${REMOTE_GIT_TAG}"
        "${ITK_SOURCE_DIR}/Modules/Remote/${_name}")
    endif()
  else()
    # Hide remote module options if not building.
    if(DEFINED Module_${_name}_GIT_TAG)
      set_property(CACHE Module_${_name}_GIT_TAG PROPERTY TYPE INTERNAL)
    endif()
    if(DEFINED Module_${_name}_BUILD_EXAMPLES)
      set_property(CACHE Module_${_name}_BUILD_EXAMPLES PROPERTY TYPE INTERNAL)
    endif()
  endif()
endfunction()