ctkMacroCheckExternalProjectDependency.cmake 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. #.rst:
  2. # ExternalProjectDependency
  3. # -------------------------
  4. #
  5. # .. only:: html
  6. #
  7. # .. contents::
  8. ###########################################################################
  9. #
  10. # Library: CTK
  11. #
  12. # Copyright (c) Kitware Inc.
  13. #
  14. # Licensed under the Apache License, Version 2.0 (the "License");
  15. # you may not use this file except in compliance with the License.
  16. # You may obtain a copy of the License at
  17. #
  18. # http://www.apache.org/licenses/LICENSE-2.0.txt
  19. #
  20. # Unless required by applicable law or agreed to in writing, software
  21. # distributed under the License is distributed on an "AS IS" BASIS,
  22. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. # See the License for the specific language governing permissions and
  24. # limitations under the License.
  25. #
  26. ###########################################################################
  27. include(CMakeParseArguments)
  28. #.rst:
  29. # Global Variables
  30. # ^^^^^^^^^^^^^^^^
  31. #.rst:
  32. # .. cmake:variable:: EXTERNAL_PROJECT_DIR
  33. #
  34. # This variable describes the directory in which external project files
  35. # matching ``<EXTERNAL_PROJECT_FILE_PREFIX><projectname>.cmake`` expression are globbed.
  36. #
  37. if(NOT EXISTS "${EXTERNAL_PROJECT_DIR}")
  38. set(EXTERNAL_PROJECT_DIR ${CMAKE_SOURCE_DIR}/SuperBuild)
  39. endif()
  40. #.rst:
  41. # .. cmake:variable:: EXTERNAL_PROJECT_ADDITIONAL_DIR
  42. #
  43. # If set, this variable represents an other directory in which external project files
  44. # are searched for if not already found in ``EXTERNAL_PROJECT_DIR``.
  45. #.rst:
  46. # .. cmake:variable:: EXTERNAL_PROJECT_FILE_PREFIX
  47. #
  48. # This variable describes the prefix of the external project files looked up in
  49. # ``EXTERNAL_PROJECT_DIR``. It defaults to ``External_``.
  50. #
  51. if(NOT DEFINED EXTERNAL_PROJECT_FILE_PREFIX)
  52. set(EXTERNAL_PROJECT_FILE_PREFIX "External_")
  53. endif()
  54. #.rst:
  55. # .. cmake:variable:: SUPERBUILD_TOPLEVEL_PROJECT
  56. #
  57. # This variable can be set to explicitly identify the name of the top-level project.
  58. # If not set, it default to the value of ``CMAKE_PROJECT_NAME``.
  59. if(NOT DEFINED SUPERBUILD_TOPLEVEL_PROJECT)
  60. if(NOT DEFINED CMAKE_PROJECT_NAME)
  61. message(FATAL_ERROR "Failed to initialize variable SUPERBUILD_TOPLEVEL_PROJECT. Variable CMAKE_PROJECT_NAME is not defined.")
  62. endif()
  63. set(SUPERBUILD_TOPLEVEL_PROJECT ${CMAKE_PROJECT_NAME})
  64. endif()
  65. #.rst:
  66. # .. cmake:variable:: EP_LIST_SEPARATOR
  67. #
  68. # This variable is used to separate list items when passed in various external project
  69. # ``..._COMMAND`` options.
  70. #
  71. # If defaults to ``^^``.
  72. if(NOT DEFINED EP_LIST_SEPARATOR)
  73. set(EP_LIST_SEPARATOR "^^")
  74. endif()
  75. #.rst:
  76. # .. cmake:variable:: EP_GIT_PROTOCOL
  77. #
  78. # The value of this variable is controled by the option ``<SUPERBUILD_TOPLEVEL_PROJECT>_USE_GIT_PROTOCOL``
  79. # automatically defined by including this CMake module. Setting this option allows to update the value of
  80. # ``EP_GIT_PROTOCOL`` variable.
  81. #
  82. # If enabled, the variable ``EP_GIT_PROTOCOL`` is set to ``git``. Otherwise, it is set to ``https``.
  83. # The option is enabled by default.
  84. #
  85. # The variable ``EP_GIT_PROTOCOL`` can be used when adding external project. For example:
  86. #
  87. # .. code-block:: cmake
  88. #
  89. # ExternalProject_Add(${proj}
  90. # ${${proj}_EP_ARGS}
  91. # GIT_REPOSITORY "${EP_GIT_PROTOCOL}://github.com/Foo/Foo.git"
  92. # [...]
  93. # )
  94. #
  95. option(${SUPERBUILD_TOPLEVEL_PROJECT}_USE_GIT_PROTOCOL "If behind a firewall turn this off to use https instead." ON)
  96. set(EP_GIT_PROTOCOL "git")
  97. if(NOT ${SUPERBUILD_TOPLEVEL_PROJECT}_USE_GIT_PROTOCOL)
  98. set(EP_GIT_PROTOCOL "https")
  99. endif()
  100. # Compute -G arg for configuring external projects with the same CMake generator:
  101. if(CMAKE_EXTRA_GENERATOR)
  102. set(EP_CMAKE_GENERATOR "${CMAKE_EXTRA_GENERATOR} - ${CMAKE_GENERATOR}")
  103. else()
  104. set(EP_CMAKE_GENERATOR "${CMAKE_GENERATOR}")
  105. endif()
  106. set(EP_CMAKE_GENERATOR_PLATFORM "${CMAKE_GENERATOR_PLATFORM}")
  107. set(EP_CMAKE_GENERATOR_TOOLSET "${CMAKE_GENERATOR_TOOLSET}")
  108. #.rst:
  109. # Functions
  110. # ^^^^^^^^^
  111. #.rst:
  112. # .. cmake:function:: mark_as_superbuild
  113. #
  114. # .. code-block:: cmake
  115. #
  116. # mark_as_superbuild(<varname1>[:<vartype1>] [<varname2>[:<vartype2>] [...]])
  117. #
  118. # .. code-block:: cmake
  119. #
  120. # mark_as_superbuild(
  121. # VARS <varname1>[:<vartype1>] [<varname2>[:<vartype2>] [...]]
  122. # [PROJECTS <projectname> [<projectname> [...]] | ALL_PROJECTS]
  123. # [LABELS <label1> [<label2> [...]]]
  124. # )
  125. #
  126. # .. code-block:: cmake
  127. #
  128. # PROJECTS corresponds to a list of <projectname> that will be added using 'ExternalProject_Add' function.
  129. # If not specified and called within a project file, it defaults to the value of 'SUPERBUILD_TOPLEVEL_PROJECT'.
  130. # If instead 'ALL_PROJECTS' is specified, the variables and labels will be passed to all projects.
  131. #
  132. # VARS is an expected list of variables specified as <varname>:<vartype> to pass to <projectname>
  133. #
  134. #
  135. # LABELS is an optional list of label to associate with the variable names specified using 'VARS' and passed to
  136. # the <projectname> as CMake CACHE args of the form:
  137. # -D<projectname>_EP_LABEL_<label1>=<varname1>;<varname2>[...]
  138. # -D<projectname>_EP_LABEL_<label2>=<varname1>;<varname2>[...]
  139. #
  140. function(mark_as_superbuild)
  141. set(options ALL_PROJECTS CMAKE_CMD)
  142. set(oneValueArgs)
  143. set(multiValueArgs VARS PROJECTS LABELS)
  144. cmake_parse_arguments(_sb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  145. set(_vars ${_sb_UNPARSED_ARGUMENTS})
  146. set(_named_parameters_expected 0)
  147. if(_sb_PROJECTS OR _sb_ALL_PROJECTS OR _sb_LABELS OR _sb_VARS)
  148. set(_named_parameters_expected 1)
  149. set(_vars ${_sb_VARS})
  150. endif()
  151. if(_named_parameters_expected AND _sb_UNPARSED_ARGUMENTS)
  152. message(FATAL_ERROR "Arguments '${_sb_UNPARSED_ARGUMENTS}' should be associated with VARS parameter !")
  153. endif()
  154. if(_sb_PROJECTS AND _sb_ALL_PROJECTS)
  155. message(FATAL_ERROR "Arguments 'PROJECTS' and 'ALL_PROJECTS' are mutually exclusive !")
  156. endif()
  157. foreach(var ${_vars})
  158. set(_type_specified 0)
  159. if(var MATCHES ":")
  160. set(_type_specified 1)
  161. endif()
  162. # XXX Display warning with variable type is also specified for cache variable.
  163. set(_var ${var})
  164. if(NOT _type_specified)
  165. get_property(_type_set_in_cache CACHE ${_var} PROPERTY TYPE SET)
  166. set(_var_name ${_var})
  167. set(_var_type "STRING")
  168. if(_type_set_in_cache)
  169. get_property(_var_type CACHE ${_var_name} PROPERTY TYPE)
  170. endif()
  171. set(_var ${_var_name}:${_var_type})
  172. endif()
  173. list(APPEND _vars_with_type ${_var})
  174. endforeach()
  175. if(_sb_ALL_PROJECTS)
  176. set(optional_arg_ALL_PROJECTS "ALL_PROJECTS")
  177. else()
  178. set(optional_arg_ALL_PROJECTS PROJECTS ${_sb_PROJECTS})
  179. endif()
  180. _sb_append_to_cmake_args(
  181. VARS ${_vars_with_type} LABELS ${_sb_LABELS} ${optional_arg_ALL_PROJECTS})
  182. endfunction()
  183. #
  184. # _sb_extract_varname_and_vartype(<cmake_varname_and_type> <varname_var> [<vartype_var>])
  185. #
  186. # <cmake_varname_and_type> corresponds to variable name and variable type passed as "<varname>:<vartype>"
  187. #
  188. # <varname_var> will be set to "<varname>"
  189. #
  190. # <vartype_var> is an optional variable name that will be set to "<vartype>"
  191. #
  192. function(_sb_extract_varname_and_vartype cmake_varname_and_type varname_var)
  193. set(_vartype_var "${ARGV2}")
  194. string(REPLACE ":" ";" varname_and_vartype ${cmake_varname_and_type})
  195. list(GET varname_and_vartype 0 _varname)
  196. list(GET varname_and_vartype 1 _vartype)
  197. set(${varname_var} ${_varname} PARENT_SCOPE)
  198. if(_vartype_var MATCHES ".+")
  199. set(${_vartype_var} ${_vartype} PARENT_SCOPE)
  200. endif()
  201. endfunction()
  202. function(_sb_list_to_string separator input_list output_string_var)
  203. set(_string "")
  204. # Get list length
  205. list(LENGTH input_list list_length)
  206. # If the list has 0 or 1 element, there is no need to loop over.
  207. if(list_length LESS 2)
  208. set(_string "${input_list}")
  209. else()
  210. math(EXPR last_element_index "${list_length} - 1")
  211. foreach(index RANGE ${last_element_index})
  212. # Get current item_value
  213. list(GET input_list ${index} item_value)
  214. if(NOT item_value STREQUAL "")
  215. # .. and append non-empty value to output string
  216. set(_string "${_string}${item_value}")
  217. # Append separator if current element is NOT the last one.
  218. if(NOT index EQUAL last_element_index)
  219. set(_string "${_string}${separator}")
  220. endif()
  221. endif()
  222. endforeach()
  223. endif()
  224. set(${output_string_var} ${_string} PARENT_SCOPE)
  225. endfunction()
  226. #
  227. # _sb_cmakevar_to_cmakearg(<cmake_varname_and_type> <cmake_arg_var> <has_cfg_intdir_var> [<varname_var> [<vartype_var>]])
  228. #
  229. # <cmake_varname_and_type> corresponds to variable name and variable type passed as "<varname>:<vartype>"
  230. #
  231. # <cmake_arg_var> is a variable name that will be set to "-D<varname>:<vartype>=${<varname>}"
  232. #
  233. # <has_int_dir_var> is set to either TRUE or FALSE.
  234. # FALSE means that the value does NOT reference ${CMAKE_CFG_INTDIR} and
  235. # the generated cmake argument should be passed to ExternalProject_Add as CMAKE_CACHE_ARGS.
  236. # TRUEmeans that the value does reference ${CMAKE_CFG_INTDIR} and
  237. # the generated cmake argument should be passed to ExternalProject_Add as CMAKE_ARGS.
  238. #
  239. # <varname_var> is an optional variable name that will be set to "<varname>"
  240. #
  241. # <vartype_var> is an optional variable name that will be set to "<vartype>"
  242. #
  243. function(_sb_cmakevar_to_cmakearg cmake_varname_and_type cmake_arg_var has_cfg_intdir_var)
  244. set(_varname_var "${ARGV3}")
  245. set(_vartype_var "${ARGV4}")
  246. _sb_extract_varname_and_vartype(${cmake_varname_and_type} _varname _vartype)
  247. set(_var_value "${${_varname}}")
  248. get_property(_value_set_in_cache CACHE ${_varname} PROPERTY VALUE SET)
  249. if(_value_set_in_cache)
  250. get_property(_var_value CACHE ${_varname} PROPERTY VALUE)
  251. endif()
  252. set(_has_cfg_intdir FALSE)
  253. if(CMAKE_CONFIGURATION_TYPES)
  254. string(FIND "${_var_value}" ${CMAKE_CFG_INTDIR} _index)
  255. if(NOT _index EQUAL -1)
  256. # Separate list item with <EP_LIST_SEPARATOR>
  257. _sb_list_to_string(${EP_LIST_SEPARATOR} "${_var_value}" _var_value)
  258. set(_has_cfg_intdir TRUE)
  259. endif()
  260. endif()
  261. if(NOT _has_cfg_intdir)
  262. string(REPLACE "\"" "\\\"" _var_value "${_var_value}")
  263. endif()
  264. set(${cmake_arg_var} -D${_varname}:${_vartype}=${_var_value} PARENT_SCOPE)
  265. set(${has_cfg_intdir_var} ${_has_cfg_intdir} PARENT_SCOPE)
  266. if(_varname_var MATCHES ".+")
  267. set(${_varname_var} ${_varname} PARENT_SCOPE)
  268. endif()
  269. if(_vartype_var MATCHES ".+")
  270. set(${_vartype_var} ${_vartype} PARENT_SCOPE)
  271. endif()
  272. endfunction()
  273. set(_ALL_PROJECT_IDENTIFIER "ALLALLALL")
  274. #
  275. # _sb_append_to_cmake_args(
  276. # [VARS <varname1>:<vartype1> [<varname2>:<vartype2> [...]]]
  277. # [PROJECTS <projectname> [<projectname> [...]] | ALL_PROJECTS]
  278. # [LABELS <label1> [<label2> [...]]]
  279. # )
  280. #
  281. # PROJECTS corresponds to a list of <projectname> that will be added using 'ExternalProject_Add' function.
  282. # If not specified and called within a project file, it defaults to the value of 'SUPERBUILD_TOPLEVEL_PROJECT'.
  283. # If instead 'ALL_PROJECTS' is specified, the variables and labels will be passed to all projects.
  284. #
  285. # VARS is an expected list of variables specified as <varname>:<vartype> to pass to <projectname>
  286. #
  287. #
  288. # LABELS is an optional list of label to associate with the variable names specified using 'VARS' and passed to
  289. # the <projectname> as CMake CACHE args of the form:
  290. # -D<projectname>_EP_LABEL_<label1>=<varname1>;<varname2>[...]
  291. # -D<projectname>_EP_LABEL_<label2>=<varname1>;<varname2>[...]
  292. #
  293. function(_sb_append_to_cmake_args)
  294. set(options ALL_PROJECTS)
  295. set(oneValueArgs)
  296. set(multiValueArgs VARS PROJECTS LABELS)
  297. cmake_parse_arguments(_sb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  298. if(NOT _sb_PROJECTS AND NOT _sb_ALL_PROJECTS)
  299. set(_sb_PROJECTS ${SUPERBUILD_TOPLEVEL_PROJECT})
  300. endif()
  301. if(_sb_ALL_PROJECTS)
  302. set(_sb_PROJECTS ${_ALL_PROJECT_IDENTIFIER})
  303. endif()
  304. foreach(_sb_PROJECT ${_sb_PROJECTS})
  305. set(_ep_varnames "")
  306. foreach(varname_and_vartype ${_sb_VARS})
  307. if(NOT TARGET ${_sb_PROJECT})
  308. set_property(GLOBAL APPEND PROPERTY ${_sb_PROJECT}_EP_CMAKE_ARGS ${varname_and_vartype})
  309. _sb_extract_varname_and_vartype(${varname_and_vartype} _varname)
  310. else()
  311. message(FATAL_ERROR "Function _sb_append_to_cmake_args not allowed because project '${_sb_PROJECT}' already added !")
  312. endif()
  313. list(APPEND _ep_varnames ${_varname})
  314. endforeach()
  315. if(_sb_LABELS)
  316. set_property(GLOBAL APPEND PROPERTY ${_sb_PROJECT}_EP_LABELS ${_sb_LABELS})
  317. foreach(label ${_sb_LABELS})
  318. set_property(GLOBAL APPEND PROPERTY ${_sb_PROJECT}_EP_LABEL_${label} ${_ep_varnames})
  319. endforeach()
  320. endif()
  321. endforeach()
  322. endfunction()
  323. #.rst:
  324. # .. cmake:function:: ExternalProject_DeclareLabels
  325. #
  326. # .. code-block:: cmake
  327. #
  328. # ExternalProject_DeclareLabels(
  329. # [PROJECTS <projectname> [<projectname> [...]] | ALL_PROJECTS]
  330. # LABELS <label1> [<label2> [...]]
  331. # )
  332. #
  333. # .. code-block:: cmake
  334. #
  335. # PROJECTS corresponds to a list of <projectname> that will be added using 'ExternalProject_Add' function.
  336. # If not specified and called within a project file, it defaults to the value of 'SUPERBUILD_TOPLEVEL_PROJECT'.
  337. # If instead 'ALL_PROJECTS' is specified, the variables and labels will be passed to all projects.
  338. #
  339. # LABELS is a list of label to pass to the <projectname> as CMake CACHE args of the
  340. # form -D<projectname>_EP_LABEL_<label>= unless specific variables
  341. # have been associated with the labels using mark_as_superbuild.
  342. #
  343. function(ExternalProject_DeclareLabels)
  344. set(options ALL_PROJECTS)
  345. set(oneValueArgs)
  346. set(multiValueArgs PROJECTS LABELS)
  347. cmake_parse_arguments(_sb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  348. if(_sb_PROJECTS AND _sb_ALL_PROJECTS)
  349. message(FATAL_ERROR "Arguments 'PROJECTS' and 'ALL_PROJECTS' are mutually exclusive !")
  350. endif()
  351. if(_sb_ALL_PROJECTS)
  352. set(optional_arg_ALL_PROJECTS "ALL_PROJECTS")
  353. else()
  354. set(optional_arg_ALL_PROJECTS PROJECTS ${_sb_PROJECTS})
  355. endif()
  356. _sb_append_to_cmake_args(
  357. LABELS ${_sb_LABELS} ${optional_arg_ALL_PROJECTS})
  358. endfunction()
  359. function(_sb_get_external_project_arguments proj varname)
  360. mark_as_superbuild(${SUPERBUILD_TOPLEVEL_PROJECT}_USE_SYSTEM_${proj}:BOOL)
  361. function(_sb_collect_args proj)
  362. # Set list of CMake args associated with each label
  363. get_property(_labels GLOBAL PROPERTY ${proj}_EP_LABELS)
  364. if(_labels)
  365. list(REMOVE_DUPLICATES _labels)
  366. foreach(label ${_labels})
  367. get_property(${proj}_EP_LABEL_${label} GLOBAL PROPERTY ${proj}_EP_LABEL_${label})
  368. if(${proj}_EP_LABEL_${label})
  369. list(REMOVE_DUPLICATES ${proj}_EP_LABEL_${label})
  370. endif()
  371. _sb_append_to_cmake_args(PROJECTS ${proj}
  372. VARS ${proj}_EP_LABEL_${label}:STRING)
  373. endforeach()
  374. endif()
  375. get_property(_args GLOBAL PROPERTY ${proj}_EP_CMAKE_ARGS)
  376. foreach(var ${_args})
  377. _sb_cmakevar_to_cmakearg(${var} cmake_arg _has_cfg_intdir)
  378. set(_ep_property "CMAKE_CACHE_ARGS")
  379. if(_has_cfg_intdir)
  380. set(_ep_property "CMAKE_ARGS")
  381. endif()
  382. set_property(GLOBAL APPEND PROPERTY ${proj}_EP_PROPERTY_${_ep_property} ${cmake_arg})
  383. endforeach()
  384. endfunction()
  385. _sb_collect_args(${proj})
  386. _sb_collect_args(${_ALL_PROJECT_IDENTIFIER})
  387. set(_ep_arguments "")
  388. # Automatically propagate CMake options
  389. foreach(_cmake_option IN ITEMS
  390. CMAKE_EXPORT_COMPILE_COMMANDS
  391. )
  392. if(DEFINED ${_cmake_option})
  393. list(APPEND _ep_arguments CMAKE_CACHE_ARGS
  394. -D${_cmake_option}:BOOL=${${_cmake_option}}
  395. )
  396. endif()
  397. endforeach()
  398. foreach(property CMAKE_ARGS CMAKE_CACHE_ARGS)
  399. get_property(${proj}_EP_PROPERTY_${property} GLOBAL PROPERTY ${proj}_EP_PROPERTY_${property})
  400. get_property(${_ALL_PROJECT_IDENTIFIER}_EP_PROPERTY_${property} GLOBAL PROPERTY ${_ALL_PROJECT_IDENTIFIER}_EP_PROPERTY_${property})
  401. set(_all ${${proj}_EP_PROPERTY_${property}} ${${_ALL_PROJECT_IDENTIFIER}_EP_PROPERTY_${property}})
  402. if(_all)
  403. list(APPEND _ep_arguments ${property} ${_all})
  404. endif()
  405. endforeach()
  406. list(APPEND _ep_arguments LIST_SEPARATOR ${EP_LIST_SEPARATOR})
  407. list(APPEND _ep_arguments CMAKE_GENERATOR ${_sb_CMAKE_GENERATOR})
  408. if(CMAKE_VERSION VERSION_GREATER "3.0")
  409. list(APPEND _ep_arguments CMAKE_GENERATOR_PLATFORM ${_sb_CMAKE_GENERATOR_PLATFORM})
  410. endif()
  411. list(APPEND _ep_arguments CMAKE_GENERATOR_TOOLSET ${_sb_CMAKE_GENERATOR_TOOLSET})
  412. if(CMAKE_VERSION VERSION_EQUAL "3.4" OR CMAKE_VERSION VERSION_GREATER "3.4")
  413. # USES_TERMINAL_* options were introduced in CMake 3.4
  414. foreach(step IN ITEMS DOWNLOAD UPDATE CONFIGURE BUILD TEST INSTALL)
  415. list(APPEND _ep_arguments
  416. USES_TERMINAL_${step} 1
  417. )
  418. endforeach()
  419. endif()
  420. set(${varname} ${_ep_arguments} PARENT_SCOPE)
  421. endfunction()
  422. function(_sb_update_indent proj)
  423. superbuild_stack_size(SB_PROJECT_STACK _stack_size)
  424. set(_indent "")
  425. if(_stack_size GREATER 0)
  426. foreach(not_used RANGE 1 ${_stack_size})
  427. set(_indent " ${_indent}")
  428. endforeach()
  429. endif()
  430. set_property(GLOBAL PROPERTY SUPERBUILD_${proj}_INDENT ${_indent})
  431. endfunction()
  432. #.rst:
  433. # .. cmake:function:: ExternalProject_Message
  434. #
  435. # .. code-block:: cmake
  436. #
  437. # ExternalProject_Message(<project_name> <msg> [condition])
  438. #
  439. function(ExternalProject_Message proj msg)
  440. set(_display 1)
  441. if(NOT "x${ARGV2}" STREQUAL "x")
  442. set(_display ${ARGN})
  443. endif()
  444. if(${_display})
  445. get_property(_indent GLOBAL PROPERTY SUPERBUILD_${proj}_INDENT)
  446. message(STATUS "SuperBuild - ${_indent}${msg}")
  447. endif()
  448. endfunction()
  449. #
  450. # superbuild_stack_content(<stack_name> <output_var>)
  451. #
  452. # <stack_name> corresponds to the name of stack.
  453. #
  454. # <output_var> is the name of CMake variable that will be set with the content
  455. # of the stack identified by <stack_name>.
  456. #
  457. function(superbuild_stack_content stack_name output_var)
  458. get_property(_stack GLOBAL PROPERTY ${stack_name})
  459. set(${output_var} ${_stack} PARENT_SCOPE)
  460. endfunction()
  461. #
  462. # superbuild_stack_size(<stack_name> <output_var>)
  463. #
  464. # <stack_name> corresponds to the name of stack.
  465. #
  466. # <output_var> is the name of CMake variable that will be set with the size
  467. # of the stack identified by <stack_name>.
  468. #
  469. function(superbuild_stack_size stack_name output_var)
  470. get_property(_stack GLOBAL PROPERTY ${stack_name})
  471. list(LENGTH _stack _stack_size)
  472. set(${output_var} ${_stack_size} PARENT_SCOPE)
  473. endfunction()
  474. #
  475. # superbuild_stack_push(<stack_name> <value>)
  476. #
  477. # <stack_name> corresponds to the name of stack.
  478. #
  479. # <value> is appended to the stack identified by <stack_name>.
  480. #
  481. function(superbuild_stack_push stack_name value)
  482. set_property(GLOBAL APPEND PROPERTY ${stack_name} ${value})
  483. endfunction()
  484. #
  485. # superbuild_stack_pop(<stack_name> <item_var>)
  486. #
  487. # <stack_name> corresponds to the name of stack.
  488. #
  489. # <item_var> names a CMake variable that will be set with the item
  490. # removed from the stack identified by <stack_name>
  491. #
  492. function(superbuild_stack_pop stack_name item_var)
  493. get_property(_stack GLOBAL PROPERTY ${stack_name})
  494. list(LENGTH _stack _stack_size)
  495. if(_stack_size GREATER 0)
  496. math(EXPR _index_to_remove "${_stack_size} - 1")
  497. list(GET _stack ${_index_to_remove} _item)
  498. list(REMOVE_AT _stack ${_index_to_remove})
  499. set_property(GLOBAL PROPERTY ${stack_name} ${_stack})
  500. set(${item_var} ${_item} PARENT_SCOPE)
  501. endif()
  502. endfunction()
  503. function(_sb_is_optional proj output_var)
  504. set(_include_project 1)
  505. if(COMMAND superbuild_is_external_project_includable)
  506. superbuild_is_external_project_includable("${proj}" _include_project)
  507. endif()
  508. set(optional 1)
  509. if(_include_project)
  510. set(optional 0)
  511. endif()
  512. set(${output_var} ${optional} PARENT_SCOPE)
  513. endfunction()
  514. #.rst:
  515. # .. cmake:function:: ExternalProject_Include_Dependencies
  516. #
  517. # .. code-block:: cmake
  518. #
  519. # ExternalProject_Include_Dependencies(<project_name>
  520. # [PROJECT_VAR <project_var>]
  521. # [EP_ARGS_VAR <external_project_args_var>]
  522. # [DEPENDS_VAR <depends_var>]
  523. # [USE_SYSTEM_VAR <use_system_var>]
  524. # [SUPERBUILD_VAR <superbuild_var>]
  525. # [CMAKE_GENERATOR <cmake_generator>]
  526. # [CMAKE_GENERATOR_PLATFORM <cmake_generator_platform>]
  527. # [CMAKE_GENERATOR_TOOLSET <cmake_generator_toolset>]
  528. # )
  529. #
  530. #
  531. # .. code-block:: cmake
  532. #
  533. # PROJECT_VAR Name of the variable containing the name of the included project.
  534. # By default, it is `proj` and it is set to `<project_name>`.
  535. #
  536. # EP_ARGS_VAR Name of the variable listing arguments to pass to ExternalProject.
  537. # If not specified, variable name default to `<project_name>_EP_ARGS`.
  538. #
  539. # DEPENDS_VAR Name of the variable containing the dependency of the included project.
  540. # By default, it is `<project_name>_DEPENDS`.
  541. #
  542. #
  543. # USE_SYSTEM_VAR Name of the variable indicating if the system version of <project_name>
  544. # should be looked up. Lookup of the project is left to the developer implementing
  545. # the external project file.
  546. # By default, it is `<SUPERBUILD_TOPLEVEL_PROJECT>_USE_SYSTEM_<project_name>`.
  547. #
  548. # SUPERBUILD_VAR Name of the variable indicating if the top-level or inner project is being built.
  549. # By default, it is `<SUPERBUILD_TOPLEVEL_PROJECT>_SUPERBUILD`.
  550. #
  551. #
  552. # CMAKE_GENERATOR
  553. # CMAKE_GENERATOR_PLATFORM
  554. # CMAKE_GENERATOR_TOOLSET These three options allow to overwrite the values set in the top-level project that
  555. # would otherwise automatically be propagated to dependent projects.
  556. #
  557. macro(ExternalProject_Include_Dependencies project_name)
  558. set(options)
  559. set(oneValueArgs PROJECT_VAR DEPENDS_VAR EP_ARGS_VAR USE_SYSTEM_VAR SUPERBUILD_VAR
  560. CMAKE_GENERATOR
  561. CMAKE_GENERATOR_PLATFORM
  562. CMAKE_GENERATOR_TOOLSET
  563. )
  564. set(multiValueArgs)
  565. cmake_parse_arguments(_sb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  566. # Sanity checks
  567. if(x${project_name} STREQUAL xPROJECT_VAR
  568. OR x${project_name} STREQUAL xEP_ARGS_VAR
  569. OR x${project_name} STREQUAL xDEPENDS_VAR
  570. OR x${project_name} STREQUAL xUSE_SYSTEM_VAR
  571. OR x${project_name} STREQUAL xSUPERBUILD_VAR
  572. OR x${project_name} STREQUAL xCMAKE_GENERATOR
  573. OR x${project_name} STREQUAL xCMAKE_GENERATOR_PLATFORM
  574. OR x${project_name} STREQUAL xCMAKE_GENERATOR_TOOLSET
  575. )
  576. message(FATAL_ERROR "Argument <project_name> is missing !")
  577. endif()
  578. if(_sb_UNPARSED_ARGUMENTS)
  579. message(FATAL_ERROR "Invalid arguments: ${_sb_UNPARSED_ARGUMENTS}")
  580. endif()
  581. # Set default for optional PROJECT_VAR parameter
  582. if(NOT _sb_PROJECT_VAR)
  583. set(_sb_PROJECT_VAR proj)
  584. set(${_sb_PROJECT_VAR} ${project_name})
  585. #message("[${project_name}] Setting _sb_PROJECT_VAR with default value '${_sb_PROJECT_VAR}'")
  586. endif()
  587. if(_sb_PROJECT_VAR AND NOT x${project_name} STREQUAL x${${_sb_PROJECT_VAR}})
  588. message(FATAL_ERROR
  589. "Argument <project_name>:${project_name} and PROJECT_VAR:${_sb_PROJECT_VAR}:${${_sb_PROJECT_VAR}} are different !")
  590. endif()
  591. set(_sb_proj ${project_name})
  592. # Skip if project already included
  593. get_property(_is_included GLOBAL PROPERTY SB_${_sb_proj}_FILE_INCLUDED)
  594. if(_is_included)
  595. return()
  596. endif()
  597. # Set default for optional DEPENDS_VAR and EP_ARGS parameters
  598. foreach(param DEPENDS EP_ARGS)
  599. if(NOT _sb_${param}_VAR)
  600. set(_sb_${param}_VAR ${_sb_proj}_${param})
  601. #message("[${project_name}] Setting _sb_${param}_VAR with default value '${_sb_${param}_VAR}'")
  602. endif()
  603. endforeach()
  604. # Set top level project
  605. superbuild_stack_size(SB_PROJECT_STACK _stack_size)
  606. if(_stack_size EQUAL 0)
  607. set(SUPERBUILD_TOPLEVEL_PROJECT ${_sb_proj})
  608. endif()
  609. # Set default for optional USE_SYSTEM_VAR parameter
  610. if(NOT _sb_USE_SYSTEM_VAR)
  611. set(_sb_USE_SYSTEM_VAR ${SUPERBUILD_TOPLEVEL_PROJECT}_USE_SYSTEM_${_sb_proj})
  612. #message("[${project_name}] Setting _sb_USE_SYSTEM_VAR with default value '${_sb_USE_SYSTEM_VAR}'")
  613. endif()
  614. # Set default for optional SUPERBUILD_VAR parameter
  615. if(NOT _sb_SUPERBUILD_VAR)
  616. set(_sb_SUPERBUILD_VAR ${SUPERBUILD_TOPLEVEL_PROJECT}_SUPERBUILD)
  617. #message("[${project_name}] Setting _sb_SUPERBUILD_VAR with default value '${_sb_SUPERBUILD_VAR}'")
  618. endif()
  619. # Set default for optional CMAKE_GENERATOR_* parameters
  620. foreach(varname IN ITEMS
  621. "CMAKE_GENERATOR"
  622. "CMAKE_GENERATOR_PLATFORM"
  623. "CMAKE_GENERATOR_TOOLSET"
  624. )
  625. if(NOT _sb_${varname})
  626. set(_sb_${varname} ${EP_${varname}})
  627. #message("[${project_name}] Setting _sb_${varname} with default value '${_sb_${varname}}'")
  628. else()
  629. #message("[${project_name}] Setting _sb_${varname} to value '${_sb_${varname}}'")
  630. endif()
  631. endforeach()
  632. # Keeping track of variable name independently of the recursion
  633. if(NOT DEFINED _sb_SB_VAR)
  634. set(_sb_SB_VAR ${_sb_SUPERBUILD_VAR})
  635. #message("[${project_name}] Setting _sb_SB_VAR with default value '${_sb_SB_VAR}'")
  636. endif()
  637. # Set local variables
  638. set(_sb_DEPENDS ${${_sb_DEPENDS_VAR}})
  639. set(_sb_USE_SYSTEM ${${_sb_USE_SYSTEM_VAR}})
  640. _sb_update_indent(${_sb_proj})
  641. # Keep track of the projects
  642. list(APPEND SB_${SUPERBUILD_TOPLEVEL_PROJECT}_POSSIBLE_DEPENDS ${_sb_proj})
  643. # Use system ?
  644. get_property(_use_system_set GLOBAL PROPERTY SB_${_sb_proj}_USE_SYSTEM SET)
  645. if(_use_system_set)
  646. get_property(_sb_USE_SYSTEM GLOBAL PROPERTY SB_${_sb_proj}_USE_SYSTEM)
  647. endif()
  648. # Is this the first run ?
  649. if(${_sb_proj} STREQUAL ${SUPERBUILD_TOPLEVEL_PROJECT} AND NOT DEFINED SB_FIRST_PASS)
  650. message(STATUS "SuperBuild - First pass")
  651. set(SB_FIRST_PASS TRUE)
  652. endif()
  653. set(_sb_REQUIRED_DEPENDS)
  654. foreach(dep ${_sb_DEPENDS})
  655. if(NOT ${_sb_proj} STREQUAL ${SUPERBUILD_TOPLEVEL_PROJECT})
  656. # Set "use system" variable if it has NOT already been explicitly set
  657. get_property(_sb_${dep}_USE_SYSTEM_VAR GLOBAL PROPERTY SB_${dep}_USE_SYSTEM_VAR)
  658. if(_sb_USE_SYSTEM AND NOT DEFINED ${_sb_${dep}_USE_SYSTEM_VAR})
  659. set_property(GLOBAL PROPERTY SB_${dep}_USE_SYSTEM ${_sb_USE_SYSTEM})
  660. #message(${_sb_proj} "Property SB_${dep}_USE_SYSTEM set to [${_sb_USE_SYSTEM_VAR}:${_sb_USE_SYSTEM}]")
  661. endif()
  662. endif()
  663. _sb_is_optional(${dep} _optional)
  664. set_property(GLOBAL PROPERTY SB_${dep}_OPTIONAL ${_optional})
  665. #message(${_sb_proj} "[${_sb_proj}] Property SB_${dep}_OPTIONAL set to ${_optional}")
  666. if(NOT _optional)
  667. list(APPEND _sb_REQUIRED_DEPENDS ${dep})
  668. endif()
  669. endforeach()
  670. # Display dependency of project being processed
  671. if(_sb_REQUIRED_DEPENDS AND SB_SECOND_PASS AND ${_sb_SB_VAR})
  672. set(dependency_str "")
  673. foreach(dep ${_sb_REQUIRED_DEPENDS})
  674. get_property(_is_included GLOBAL PROPERTY SB_${dep}_FILE_INCLUDED)
  675. set(_include_status "")
  676. if(_is_included)
  677. set(_include_status "[INCLUDED]")
  678. endif()
  679. set(dependency_str "${dependency_str}${dep}${_include_status}, ")
  680. endforeach()
  681. ExternalProject_Message(${_sb_proj} "${_sb_proj} => Requires ${dependency_str}")
  682. endif()
  683. # Save variables
  684. set_property(GLOBAL PROPERTY SB_${_sb_proj}_REQUIRED_DEPENDS ${_sb_REQUIRED_DEPENDS})
  685. set_property(GLOBAL PROPERTY SB_${_sb_proj}_DEPENDS ${_sb_DEPENDS})
  686. set_property(GLOBAL PROPERTY SB_${_sb_proj}_DEPENDS_VAR ${_sb_DEPENDS_VAR})
  687. set_property(GLOBAL PROPERTY SB_${_sb_proj}_EP_ARGS_VAR ${_sb_EP_ARGS_VAR})
  688. set_property(GLOBAL PROPERTY SB_${_sb_proj}_USE_SYSTEM ${_sb_USE_SYSTEM})
  689. set_property(GLOBAL PROPERTY SB_${_sb_proj}_USE_SYSTEM_VAR ${_sb_USE_SYSTEM_VAR})
  690. set_property(GLOBAL PROPERTY SB_${_sb_proj}_PROJECT_VAR ${_sb_PROJECT_VAR})
  691. foreach(varname IN ITEMS
  692. "CMAKE_GENERATOR"
  693. "CMAKE_GENERATOR_PLATFORM"
  694. "CMAKE_GENERATOR_TOOLSET"
  695. )
  696. set_property(GLOBAL PROPERTY SB_${_sb_proj}_${varname} ${_sb_${varname}})
  697. endforeach()
  698. superbuild_stack_push(SB_PROJECT_STACK ${_sb_proj})
  699. # Include dependencies
  700. foreach(dep ${_sb_DEPENDS})
  701. get_property(_included GLOBAL PROPERTY SB_${dep}_FILE_INCLUDED)
  702. if(NOT _included)
  703. # XXX - Refactor - Add a single variable named 'EXTERNAL_PROJECT_DIRS'
  704. if(EXISTS "${EXTERNAL_PROJECT_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake")
  705. include(${EXTERNAL_PROJECT_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake)
  706. elseif(EXISTS "${${dep}_FILEPATH}")
  707. include(${${dep}_FILEPATH})
  708. elseif(EXISTS "${EXTERNAL_PROJECT_ADDITIONAL_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake")
  709. include(${EXTERNAL_PROJECT_ADDITIONAL_DIR}/${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake)
  710. else()
  711. message(FATAL_ERROR "Can't find ${EXTERNAL_PROJECT_FILE_PREFIX}${dep}.cmake")
  712. endif()
  713. set_property(GLOBAL PROPERTY SB_${dep}_FILE_INCLUDED 1)
  714. endif()
  715. endforeach()
  716. # Restore variables
  717. superbuild_stack_pop(SB_PROJECT_STACK _sb_proj)
  718. foreach(varname IN ITEMS
  719. "CMAKE_GENERATOR"
  720. "CMAKE_GENERATOR_PLATFORM"
  721. "CMAKE_GENERATOR_TOOLSET"
  722. )
  723. get_property(_sb_${varname} GLOBAL PROPERTY SB_${_sb_proj}_${varname})
  724. endforeach()
  725. get_property(_sb_PROJECT_VAR GLOBAL PROPERTY SB_${_sb_proj}_PROJECT_VAR)
  726. get_property(_sb_USE_SYSTEM_VAR GLOBAL PROPERTY SB_${_sb_proj}_USE_SYSTEM_VAR)
  727. get_property(_sb_USE_SYSTEM GLOBAL PROPERTY SB_${_sb_proj}_USE_SYSTEM)
  728. get_property(_sb_EP_ARGS_VAR GLOBAL PROPERTY SB_${_sb_proj}_EP_ARGS_VAR)
  729. get_property(_sb_DEPENDS_VAR GLOBAL PROPERTY SB_${_sb_proj}_DEPENDS_VAR)
  730. get_property(_sb_DEPENDS GLOBAL PROPERTY SB_${_sb_proj}_DEPENDS)
  731. get_property(_sb_REQUIRED_DEPENDS GLOBAL PROPERTY SB_${_sb_proj}_REQUIRED_DEPENDS)
  732. # Use system ?
  733. set(_include_type "")
  734. if(_sb_USE_SYSTEM)
  735. set(_include_type " (SYSTEM)")
  736. endif()
  737. get_property(_optional GLOBAL PROPERTY SB_${_sb_proj}_OPTIONAL)
  738. ExternalProject_Message(${_sb_proj} "${_sb_proj}[OK]${_include_type}" SB_SECOND_PASS AND ${_sb_SB_VAR} AND NOT _optional)
  739. if(${_sb_proj} STREQUAL ${SUPERBUILD_TOPLEVEL_PROJECT} AND SB_FIRST_PASS)
  740. set(SB_FIRST_PASS FALSE)
  741. ExternalProject_Message(${_sb_proj} "First pass - done")
  742. if(${_sb_SB_VAR})
  743. foreach(possible_proj ${SB_${SUPERBUILD_TOPLEVEL_PROJECT}_POSSIBLE_DEPENDS})
  744. get_property(_optional GLOBAL PROPERTY SB_${possible_proj}_OPTIONAL)
  745. if(_optional)
  746. ExternalProject_Message(${_sb_proj} "${possible_proj}[OPTIONAL]")
  747. endif()
  748. set_property(GLOBAL PROPERTY SB_${possible_proj}_FILE_INCLUDED 0)
  749. endforeach()
  750. set(${_sb_PROJECT_VAR} ${_sb_proj})
  751. set(SB_SECOND_PASS TRUE)
  752. set(_ep_include_deps_EXTRA_ARGS )
  753. foreach(varname IN ITEMS
  754. "CMAKE_GENERATOR"
  755. "CMAKE_GENERATOR_PLATFORM"
  756. "CMAKE_GENERATOR_TOOLSET"
  757. )
  758. list(APPEND _ep_include_deps_EXTRA_ARGS
  759. ${varname} ${_sb_${varname}}
  760. )
  761. endforeach()
  762. ExternalProject_Include_Dependencies(${_sb_proj}
  763. PROJECT_VAR ${_sb_PROJECT_VAR}
  764. DEPENDS_VAR ${_sb_DEPENDS_VAR}
  765. EP_ARGS_VAR ${_sb_EP_ARGS_VAR}
  766. USE_SYSTEM_VAR _sb_USE_SYSTEM
  767. SUPERBUILD_VAR ${_sb_SB_VAR}
  768. ${_ep_include_deps_EXTRA_ARGS}
  769. )
  770. set(SB_SECOND_PASS FALSE)
  771. endif()
  772. endif()
  773. if(SB_FIRST_PASS OR _optional)
  774. if(NOT ${_sb_proj} STREQUAL ${SUPERBUILD_TOPLEVEL_PROJECT})
  775. return()
  776. endif()
  777. endif()
  778. if(SB_SECOND_PASS)
  779. _sb_get_external_project_arguments(${_sb_proj} ${_sb_EP_ARGS_VAR})
  780. endif()
  781. if(NOT SB_FIRST_PASS AND NOT SB_SECOND_PASS
  782. AND ${_sb_proj} STREQUAL ${SUPERBUILD_TOPLEVEL_PROJECT})
  783. #ExternalProject_Message(${_sb_proj} "Clean up")
  784. unset(_sb_SB_VAR)
  785. unset(SB_FIRST_PASS)
  786. unset(SB_SECOND_PASS)
  787. endif()
  788. # Set public variables
  789. set(${_sb_PROJECT_VAR} ${_sb_proj})
  790. set(${_sb_DEPENDS_VAR} ${_sb_REQUIRED_DEPENDS})
  791. set(${_sb_USE_SYSTEM_VAR} ${_sb_USE_SYSTEM})
  792. #message("[${_sb_proj}] #################################")
  793. #message("[${_sb_proj}] Setting ${_sb_PROJECT_VAR}:${_sb_proj}")
  794. #message("[${_sb_proj}] Setting ${_sb_EP_ARGS_VAR}:${${_sb_EP_ARGS_VAR}}")
  795. #message("[${_sb_proj}] Setting ${_sb_DEPENDS_VAR}:${${_sb_DEPENDS_VAR}}")
  796. #message("[${_sb_proj}] Setting ${_sb_USE_SYSTEM_VAR}:${_sb_USE_SYSTEM}")
  797. endmacro()
  798. #.rst:
  799. # .. cmake:function:: ExternalProject_Add_Empty
  800. #
  801. # .. code-block:: cmake
  802. #
  803. # ExternalProject_Add_Empty(<project_name>
  804. # DEPENDS <depends>
  805. # )
  806. #
  807. macro(ExternalProject_Add_Empty project_name)
  808. set(options)
  809. set(oneValueArgs)
  810. set(multiValueArgs DEPENDS)
  811. cmake_parse_arguments(_sb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  812. # Sanity checks
  813. if(x${project_name} STREQUAL xDEPENDS)
  814. message(FATAL_ERROR "Argument <project_name> is missing !")
  815. endif()
  816. if(_sb_UNPARSED_ARGUMENTS)
  817. message(FATAL_ERROR "Invalid arguments: ${_sb_UNPARSED_ARGUMENTS}")
  818. endif()
  819. ExternalProject_Add(${project_name}
  820. SOURCE_DIR ${CMAKE_BINARY_DIR}/${project_name}
  821. BINARY_DIR ${project_name}-build
  822. DOWNLOAD_COMMAND ""
  823. CONFIGURE_COMMAND ""
  824. BUILD_COMMAND ""
  825. INSTALL_COMMAND ""
  826. DEPENDS ${_sb_DEPENDS}
  827. )
  828. endmacro()
  829. #.rst:
  830. # .. cmake:function:: ExternalProject_Install_CMake
  831. #
  832. # Install an external CMake-based project as part of the ``install`` target.
  833. #
  834. # .. code-block:: cmake
  835. #
  836. # ExternalProject_Install_CMake(<project_name>)
  837. #
  838. # This causes building the main project's ``install`` target to also execute
  839. # the CMake install script for the specified external project. The project must
  840. # be previously declared with :command:`ExternalProject_Add`.
  841. #
  842. function(ExternalProject_Install_CMake project_name)
  843. ExternalProject_Get_Property(${project_name} binary_dir)
  844. install(SCRIPT ${binary_dir}/cmake_install.cmake)
  845. endfunction()
  846. #.rst:
  847. # .. cmake:function:: ExternalProject_SetIfNotDefined
  848. #
  849. # Set a variable to its default value if not already defined.
  850. #
  851. # .. code-block:: cmake
  852. #
  853. # ExternalProject_SetIfNotDefined(<var> <defaultvalue> [OBFUSCATE] [QUIET])
  854. #
  855. # The default value is set with:
  856. # (1) if set, the value environment variable <var>.
  857. # (2) if set, the value of local variable variable <var>.
  858. # (3) if none of the above, the value passed as a parameter.
  859. #
  860. # Setting the optional parameter 'OBFUSCATE' will display 'OBFUSCATED' instead of the real value.
  861. # Setting the optional parameter 'QUIET' will not display any message.
  862. macro(ExternalProject_SetIfNotDefined var defaultvalue)
  863. set(_obfuscate FALSE)
  864. set(_quiet FALSE)
  865. foreach(arg ${ARGN})
  866. if(arg STREQUAL "OBFUSCATE")
  867. set(_obfuscate TRUE)
  868. endif()
  869. if(arg STREQUAL "QUIET")
  870. set(_quiet TRUE)
  871. endif()
  872. endforeach()
  873. if(DEFINED ENV{${var}} AND NOT DEFINED ${var})
  874. set(_value "$ENV{${var}}")
  875. if(_obfuscate)
  876. set(_value "OBFUSCATED")
  877. endif()
  878. if(NOT _quiet)
  879. message(STATUS "Setting '${var}' variable with environment variable value '${_value}'")
  880. endif()
  881. set(${var} $ENV{${var}})
  882. endif()
  883. if(NOT DEFINED ${var})
  884. set(_value "${defaultvalue}")
  885. if(_obfuscate)
  886. set(_value "OBFUSCATED")
  887. endif()
  888. if(NOT _quiet)
  889. message(STATUS "Setting '${var}' variable with default value '${_value}'")
  890. endif()
  891. set(${var} "${defaultvalue}")
  892. endif()
  893. endmacro()
  894. #.rst:
  895. # .. cmake:function:: ExternalProject_AlwaysConfigure
  896. #
  897. # Add a external project step named `forceconfigure` to `project_name` ensuring
  898. # the project will always be reconfigured.
  899. #
  900. # .. code-block:: cmake
  901. #
  902. # ExternalProject_AlwaysConfigure(<project_name>)
  903. function(ExternalProject_AlwaysConfigure proj)
  904. # This custom external project step forces the configure and later
  905. # steps to run.
  906. _ep_get_step_stampfile(${proj} "configure" stampfile)
  907. ExternalProject_Add_Step(${proj} forceconfigure
  908. COMMAND ${CMAKE_COMMAND} -E remove ${stampfile}
  909. COMMENT "Forcing configure step for '${proj}'"
  910. DEPENDEES build
  911. ALWAYS 1
  912. )
  913. endfunction()