ExternalProject.cmake 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367
  1. #[=======================================================================[.rst:
  2. ExternalProject
  3. ---------------
  4. Create custom targets to build projects in external trees
  5. .. command:: ExternalProject_Add
  6. The ``ExternalProject_Add`` function creates a custom target to drive
  7. download, update/patch, configure, build, install and test steps of an
  8. external project::
  9. ExternalProject_Add(<name> [<option>...])
  10. General options are:
  11. ``DEPENDS <projects>...``
  12. Targets on which the project depends
  13. ``PREFIX <dir>``
  14. Root dir for entire project
  15. ``LIST_SEPARATOR <sep>``
  16. Sep to be replaced by ; in cmd lines
  17. ``TMP_DIR <dir>``
  18. Directory to store temporary files
  19. ``STAMP_DIR <dir>``
  20. Directory to store step timestamps
  21. ``EXCLUDE_FROM_ALL 1``
  22. The "all" target does not depend on this
  23. Download step options are:
  24. ``DOWNLOAD_NAME <fname>``
  25. File name to store (if not end of URL)
  26. ``DOWNLOAD_DIR <dir>``
  27. Directory to store downloaded files
  28. ``DOWNLOAD_COMMAND <cmd>...``
  29. Command to download source tree
  30. ``DOWNLOAD_NO_PROGRESS 1``
  31. Disable download progress reports
  32. ``CVS_REPOSITORY <cvsroot>``
  33. CVSROOT of CVS repository
  34. ``CVS_MODULE <mod>``
  35. Module to checkout from CVS repo
  36. ``CVS_TAG <tag>``
  37. Tag to checkout from CVS repo
  38. ``SVN_REPOSITORY <url>``
  39. URL of Subversion repo
  40. ``SVN_REVISION -r<rev>``
  41. Revision to checkout from Subversion repo
  42. ``SVN_USERNAME <username>``
  43. Username for Subversion checkout and update
  44. ``SVN_PASSWORD <password>``
  45. Password for Subversion checkout and update
  46. ``SVN_TRUST_CERT 1``
  47. Trust the Subversion server site certificate
  48. ``GIT_REPOSITORY <url>``
  49. URL of git repo
  50. ``GIT_TAG <tag>``
  51. Git branch name, commit id or tag
  52. ``GIT_SUBMODULES <module>...``
  53. Git submodules that shall be updated, all if empty
  54. ``HG_REPOSITORY <url>``
  55. URL of mercurial repo
  56. ``HG_TAG <tag>``
  57. Mercurial branch name, commit id or tag
  58. ``URL /.../src.tgz``
  59. Full path or URL of source
  60. ``URL_HASH ALGO=value``
  61. Hash of file at URL
  62. ``URL_MD5 md5``
  63. Equivalent to URL_HASH MD5=md5
  64. ``TLS_VERIFY <bool>``
  65. Should certificate for https be checked
  66. ``TLS_CAINFO <file>``
  67. Path to a certificate authority file
  68. ``TIMEOUT <seconds>``
  69. Time allowed for file download operations
  70. Update/Patch step options are:
  71. ``UPDATE_COMMAND <cmd>...``
  72. Source work-tree update command
  73. ``UPDATE_DISCONNECTED 1``
  74. Never update automatically from the remote repository
  75. ``PATCH_COMMAND <cmd>...``
  76. Command to patch downloaded source
  77. Configure step options are:
  78. ``SOURCE_DIR <dir>``
  79. Source dir to be used for build
  80. ``CONFIGURE_COMMAND <cmd>...``
  81. Build tree configuration command
  82. ``CMAKE_COMMAND /.../cmake``
  83. Specify alternative cmake executable
  84. ``CMAKE_GENERATOR <gen>``
  85. Specify generator for native build
  86. ``CMAKE_GENERATOR_PLATFORM <platform>``
  87. Generator-specific platform name
  88. ``CMAKE_GENERATOR_TOOLSET <toolset>``
  89. Generator-specific toolset name
  90. ``CMAKE_ARGS <arg>...``
  91. Arguments to CMake command line.
  92. These arguments are passed to CMake command line, and can contain
  93. arguments other than cache values, see also
  94. :manual:`CMake Options <cmake(1)>`. Arguments in the form
  95. ``-Dvar:string=on`` are always passed to the command line, and
  96. therefore cannot be changed by the user.
  97. Arguments may use
  98. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  99. ``CMAKE_CACHE_ARGS <arg>...``
  100. Initial cache arguments, of the form ``-Dvar:string=on``.
  101. These arguments are written in a pre-load a script that populates
  102. CMake cache, see also :manual:`cmake -C <cmake(1)>`. This allows to
  103. overcome command line length limits.
  104. These arguments are :command:`set` using the ``FORCE`` argument,
  105. and therefore cannot be changed by the user.
  106. Arguments may use
  107. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  108. ``CMAKE_CACHE_DEFAULT_ARGS <arg>...``
  109. Initial default cache arguments, of the form ``-Dvar:string=on``.
  110. These arguments are written in a pre-load a script that populates
  111. CMake cache, see also :manual:`cmake -C <cmake(1)>`. This allows to
  112. overcome command line length limits.
  113. These arguments can be used as default value that will be set if no
  114. previous value is found in the cache, and that the user can change
  115. later.
  116. Arguments may use
  117. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  118. Build step options are:
  119. ``BINARY_DIR <dir>``
  120. Specify build dir location
  121. ``BUILD_COMMAND <cmd>...``
  122. Command to drive the native build
  123. ``BUILD_IN_SOURCE 1``
  124. Use source dir for build dir
  125. ``BUILD_ALWAYS 1``
  126. No stamp file, build step always runs
  127. ``BUILD_BYPRODUCTS <file>...``
  128. Files that will be generated by the build command but may or may
  129. not have their modification time updated by subsequent builds.
  130. Install step options are:
  131. ``INSTALL_DIR <dir>``
  132. Installation prefix
  133. ``INSTALL_COMMAND <cmd>...``
  134. Command to drive install after build
  135. Test step options are:
  136. ``TEST_BEFORE_INSTALL 1``
  137. Add test step executed before install step
  138. ``TEST_AFTER_INSTALL 1``
  139. Add test step executed after install step
  140. ``TEST_EXCLUDE_FROM_MAIN 1``
  141. Main target does not depend on the test step
  142. ``TEST_COMMAND <cmd>...``
  143. Command to drive test
  144. Output logging options are:
  145. ``LOG_DOWNLOAD 1``
  146. Wrap download in script to log output
  147. ``LOG_UPDATE 1``
  148. Wrap update in script to log output
  149. ``LOG_CONFIGURE 1``
  150. Wrap configure in script to log output
  151. ``LOG_BUILD 1``
  152. Wrap build in script to log output
  153. ``LOG_TEST 1``
  154. Wrap test in script to log output
  155. ``LOG_INSTALL 1``
  156. Wrap install in script to log output
  157. Other options are:
  158. ``STEP_TARGETS <step-target>...``
  159. Generate custom targets for these steps
  160. ``INDEPENDENT_STEP_TARGETS <step-target>...``
  161. Generate custom targets for these steps that do not depend on other
  162. external projects even if a dependency is set
  163. The ``*_DIR`` options specify directories for the project, with default
  164. directories computed as follows. If the ``PREFIX`` option is given to
  165. ``ExternalProject_Add()`` or the ``EP_PREFIX`` directory property is set,
  166. then an external project is built and installed under the specified prefix::
  167. TMP_DIR = <prefix>/tmp
  168. STAMP_DIR = <prefix>/src/<name>-stamp
  169. DOWNLOAD_DIR = <prefix>/src
  170. SOURCE_DIR = <prefix>/src/<name>
  171. BINARY_DIR = <prefix>/src/<name>-build
  172. INSTALL_DIR = <prefix>
  173. Otherwise, if the ``EP_BASE`` directory property is set then components
  174. of an external project are stored under the specified base::
  175. TMP_DIR = <base>/tmp/<name>
  176. STAMP_DIR = <base>/Stamp/<name>
  177. DOWNLOAD_DIR = <base>/Download/<name>
  178. SOURCE_DIR = <base>/Source/<name>
  179. BINARY_DIR = <base>/Build/<name>
  180. INSTALL_DIR = <base>/Install/<name>
  181. If no ``PREFIX``, ``EP_PREFIX``, or ``EP_BASE`` is specified then the
  182. default is to set ``PREFIX`` to ``<name>-prefix``. Relative paths are
  183. interpreted with respect to the build directory corresponding to the
  184. source directory in which ``ExternalProject_Add`` is invoked.
  185. If ``SOURCE_DIR`` is explicitly set to an existing directory the project
  186. will be built from it. Otherwise a download step must be specified
  187. using one of the ``DOWNLOAD_COMMAND``, ``CVS_*``, ``SVN_*``, or ``URL``
  188. options. The ``URL`` option may refer locally to a directory or source
  189. tarball, or refer to a remote tarball (e.g. ``http://.../src.tgz``).
  190. If ``UPDATE_DISCONNECTED`` is set, the update step is not executed
  191. automatically when building the main target. The update step can still
  192. be added as a step target and called manually. This is useful if you
  193. want to allow to build the project when you are disconnected from the
  194. network (you might still need the network for the download step).
  195. This is disabled by default.
  196. The directory property ``EP_UPDATE_DISCONNECTED`` can be used to change
  197. the default value for all the external projects in the current
  198. directory and its subdirectories.
  199. .. command:: ExternalProject_Add_Step
  200. The ``ExternalProject_Add_Step`` function adds a custom step to an
  201. external project::
  202. ExternalProject_Add_Step(<name> <step> [<option>...])
  203. Options are:
  204. ``COMMAND <cmd>...``
  205. Command line invoked by this step
  206. ``COMMENT "<text>..."``
  207. Text printed when step executes
  208. ``DEPENDEES <step>...``
  209. Steps on which this step depends
  210. ``DEPENDERS <step>...``
  211. Steps that depend on this step
  212. ``DEPENDS <file>...``
  213. Files on which this step depends
  214. ``BYPRODUCTS <file>...``
  215. Files that will be generated by this step but may or may not
  216. have their modification time updated by subsequent builds.
  217. ``ALWAYS 1``
  218. No stamp file, step always runs
  219. ``EXCLUDE_FROM_MAIN 1``
  220. Main target does not depend on this step
  221. ``WORKING_DIRECTORY <dir>``
  222. Working directory for command
  223. ``LOG 1``
  224. Wrap step in script to log output
  225. The command line, comment, working directory, and byproducts of every
  226. standard and custom step are processed to replace tokens ``<SOURCE_DIR>``,
  227. ``<BINARY_DIR>``, ``<INSTALL_DIR>``, and ``<TMP_DIR>`` with
  228. corresponding property values.
  229. Any builtin step that specifies a ``<step>_COMMAND cmd...`` or custom
  230. step that specifies a ``COMMAND cmd...`` may specify additional command
  231. lines using the form ``COMMAND cmd...``. At build time the commands
  232. will be executed in order and aborted if any one fails. For example::
  233. ... BUILD_COMMAND make COMMAND echo done ...
  234. specifies to run ``make`` and then ``echo done`` during the build step.
  235. Whether the current working directory is preserved between commands is
  236. not defined. Behavior of shell operators like ``&&`` is not defined.
  237. Arguments to ``<step>_COMMAND`` or ``COMMAND`` options may use
  238. :manual:`generator expressions <cmake-generator-expressions(7)>`.
  239. .. command:: ExternalProject_Get_Property
  240. The ``ExternalProject_Get_Property`` function retrieves external project
  241. target properties::
  242. ExternalProject_Get_Property(<name> [prop1 [prop2 [...]]])
  243. It stores property values in variables of the same name. Property
  244. names correspond to the keyword argument names of
  245. ``ExternalProject_Add``.
  246. .. command:: ExternalProject_Add_StepTargets
  247. The ``ExternalProject_Add_StepTargets`` function generates custom
  248. targets for the steps listed::
  249. ExternalProject_Add_StepTargets(<name> [NO_DEPENDS] [step1 [step2 [...]]])
  250. If ``NO_DEPENDS`` is set, the target will not depend on the
  251. dependencies of the complete project. This is usually safe to use for
  252. the download, update, and patch steps that do not require that all the
  253. dependencies are updated and built. Using ``NO_DEPENDS`` for other
  254. of the default steps might break parallel builds, so you should avoid,
  255. it. For custom steps, you should consider whether or not the custom
  256. commands requires that the dependencies are configured, built and
  257. installed.
  258. If ``STEP_TARGETS`` or ``INDEPENDENT_STEP_TARGETS`` is set then
  259. ``ExternalProject_Add_StepTargets`` is automatically called at the end
  260. of matching calls to ``ExternalProject_Add_Step``. Pass
  261. ``STEP_TARGETS`` or ``INDEPENDENT_STEP_TARGETS`` explicitly to
  262. individual ``ExternalProject_Add`` calls, or implicitly to all
  263. ``ExternalProject_Add`` calls by setting the directory properties
  264. ``EP_STEP_TARGETS`` and ``EP_INDEPENDENT_STEP_TARGETS``. The
  265. ``INDEPENDENT`` version of the argument and of the property will call
  266. ``ExternalProject_Add_StepTargets`` with the ``NO_DEPENDS`` argument.
  267. If ``STEP_TARGETS`` and ``INDEPENDENT_STEP_TARGETS`` are not set,
  268. clients may still manually call ``ExternalProject_Add_StepTargets``
  269. after calling ``ExternalProject_Add`` or ``ExternalProject_Add_Step``.
  270. This functionality is provided to make it easy to drive the steps
  271. independently of each other by specifying targets on build command
  272. lines. For example, you may be submitting to a sub-project based
  273. dashboard, where you want to drive the configure portion of the build,
  274. then submit to the dashboard, followed by the build portion, followed
  275. by tests. If you invoke a custom target that depends on a step
  276. halfway through the step dependency chain, then all the previous steps
  277. will also run to ensure everything is up to date.
  278. For example, to drive configure, build and test steps independently
  279. for each ``ExternalProject_Add`` call in your project, write the following
  280. line prior to any ``ExternalProject_Add`` calls in your ``CMakeLists.txt``
  281. file::
  282. set_property(DIRECTORY PROPERTY EP_STEP_TARGETS configure build test)
  283. .. command:: ExternalProject_Add_StepDependencies
  284. The ``ExternalProject_Add_StepDependencies`` function add some
  285. dependencies for some external project step::
  286. ExternalProject_Add_StepDependencies(<name> <step> [target1 [target2 [...]]])
  287. This function takes care to set both target and file level
  288. dependencies, and will ensure that parallel builds will not break.
  289. It should be used instead of :command:`add_dependencies()` when adding
  290. a dependency for some of the step targets generated by
  291. ``ExternalProject``.
  292. #]=======================================================================]
  293. #=============================================================================
  294. # Copyright 2008-2013 Kitware, Inc.
  295. #
  296. # Distributed under the OSI-approved BSD License (the "License");
  297. # see accompanying file Copyright.txt for details.
  298. #
  299. # This software is distributed WITHOUT ANY WARRANTY; without even the
  300. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  301. # See the License for more information.
  302. #=============================================================================
  303. # (To distribute this file outside of CMake, substitute the full
  304. # License text for the above reference.)
  305. # Pre-compute a regex to match documented keywords for each command.
  306. math(EXPR _ep_documentation_line_count "${CMAKE_CURRENT_LIST_LINE} - 16")
  307. file(STRINGS "${CMAKE_CURRENT_LIST_FILE}" lines
  308. LIMIT_COUNT ${_ep_documentation_line_count}
  309. REGEX "^\\.\\. command:: [A-Za-z0-9_]+|^ ``[A-Z0-9_]+ .*``$")
  310. foreach(line IN LISTS lines)
  311. if("${line}" MATCHES "^\\.\\. command:: ([A-Za-z0-9_]+)")
  312. if(_ep_func)
  313. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  314. endif()
  315. set(_ep_func "${CMAKE_MATCH_1}")
  316. #message("function [${_ep_func}]")
  317. set(_ep_keywords_${_ep_func} "^(")
  318. set(_ep_keyword_sep)
  319. elseif("${line}" MATCHES "^ ``([A-Z0-9_]+) .*``$")
  320. set(_ep_key "${CMAKE_MATCH_1}")
  321. #message(" keyword [${_ep_key}]")
  322. set(_ep_keywords_${_ep_func}
  323. "${_ep_keywords_${_ep_func}}${_ep_keyword_sep}${_ep_key}")
  324. set(_ep_keyword_sep "|")
  325. endif()
  326. endforeach()
  327. if(_ep_func)
  328. set(_ep_keywords_${_ep_func} "${_ep_keywords_${_ep_func}})$")
  329. endif()
  330. # Save regex matching supported hash algorithm names.
  331. set(_ep_hash_algos "MD5|SHA1|SHA224|SHA256|SHA384|SHA512")
  332. set(_ep_hash_regex "^(${_ep_hash_algos})=([0-9A-Fa-f]+)$")
  333. function(_ep_parse_arguments f name ns args)
  334. # Transfer the arguments to this function into target properties for the
  335. # new custom target we just added so that we can set up all the build steps
  336. # correctly based on target properties.
  337. #
  338. # We loop through ARGN and consider the namespace starting with an
  339. # upper-case letter followed by at least two more upper-case letters,
  340. # numbers or underscores to be keywords.
  341. set(key)
  342. foreach(arg IN LISTS args)
  343. set(is_value 1)
  344. if(arg MATCHES "^[A-Z][A-Z0-9_][A-Z0-9_]+$" AND
  345. NOT (("x${arg}x" STREQUAL "x${key}x") AND ("x${key}x" STREQUAL "xCOMMANDx")) AND
  346. NOT arg MATCHES "^(TRUE|FALSE)$")
  347. if(_ep_keywords_${f} AND arg MATCHES "${_ep_keywords_${f}}")
  348. set(is_value 0)
  349. endif()
  350. endif()
  351. if(is_value)
  352. if(key)
  353. # Value
  354. if(NOT arg STREQUAL "")
  355. set_property(TARGET ${name} APPEND PROPERTY ${ns}${key} "${arg}")
  356. else()
  357. get_property(have_key TARGET ${name} PROPERTY ${ns}${key} SET)
  358. if(have_key)
  359. get_property(value TARGET ${name} PROPERTY ${ns}${key})
  360. set_property(TARGET ${name} PROPERTY ${ns}${key} "${value};${arg}")
  361. else()
  362. set_property(TARGET ${name} PROPERTY ${ns}${key} "${arg}")
  363. endif()
  364. endif()
  365. else()
  366. # Missing Keyword
  367. message(AUTHOR_WARNING "value '${arg}' with no previous keyword in ${f}")
  368. endif()
  369. else()
  370. set(key "${arg}")
  371. endif()
  372. endforeach()
  373. endfunction()
  374. define_property(DIRECTORY PROPERTY "EP_BASE" INHERITED
  375. BRIEF_DOCS "Base directory for External Project storage."
  376. FULL_DOCS
  377. "See documentation of the ExternalProject_Add() function in the "
  378. "ExternalProject module."
  379. )
  380. define_property(DIRECTORY PROPERTY "EP_PREFIX" INHERITED
  381. BRIEF_DOCS "Top prefix for External Project storage."
  382. FULL_DOCS
  383. "See documentation of the ExternalProject_Add() function in the "
  384. "ExternalProject module."
  385. )
  386. define_property(DIRECTORY PROPERTY "EP_STEP_TARGETS" INHERITED
  387. BRIEF_DOCS
  388. "List of ExternalProject steps that automatically get corresponding targets"
  389. FULL_DOCS
  390. "These targets will be dependent on the main target dependencies"
  391. "See documentation of the ExternalProject_Add_StepTargets() function in the "
  392. "ExternalProject module."
  393. )
  394. define_property(DIRECTORY PROPERTY "EP_INDEPENDENT_STEP_TARGETS" INHERITED
  395. BRIEF_DOCS
  396. "List of ExternalProject steps that automatically get corresponding targets"
  397. FULL_DOCS
  398. "These targets will not be dependent on the main target dependencies"
  399. "See documentation of the ExternalProject_Add_StepTargets() function in the "
  400. "ExternalProject module."
  401. )
  402. define_property(DIRECTORY PROPERTY "EP_UPDATE_DISCONNECTED" INHERITED
  403. BRIEF_DOCS "Never update automatically from the remote repo."
  404. FULL_DOCS
  405. "See documentation of the ExternalProject_Add() function in the "
  406. "ExternalProject module."
  407. )
  408. function(_ep_write_gitclone_script script_filename source_dir git_EXECUTABLE git_repository git_tag git_submodules src_name work_dir gitclone_infofile gitclone_stampfile)
  409. file(WRITE ${script_filename}
  410. "if(\"${git_tag}\" STREQUAL \"\")
  411. message(FATAL_ERROR \"Tag for git checkout should not be empty.\")
  412. endif()
  413. set(run 0)
  414. if(\"${gitclone_infofile}\" IS_NEWER_THAN \"${gitclone_stampfile}\")
  415. set(run 1)
  416. endif()
  417. if(NOT run)
  418. message(STATUS \"Avoiding repeated git clone, stamp file is up to date: '${gitclone_stampfile}'\")
  419. return()
  420. endif()
  421. execute_process(
  422. COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
  423. RESULT_VARIABLE error_code
  424. )
  425. if(error_code)
  426. message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
  427. endif()
  428. # try the clone 3 times incase there is an odd git clone issue
  429. set(error_code 1)
  430. set(number_of_tries 0)
  431. while(error_code AND number_of_tries LESS 3)
  432. execute_process(
  433. COMMAND \"${git_EXECUTABLE}\" clone \"${git_repository}\" \"${src_name}\"
  434. WORKING_DIRECTORY \"${work_dir}\"
  435. RESULT_VARIABLE error_code
  436. )
  437. math(EXPR number_of_tries \"\${number_of_tries} + 1\")
  438. endwhile()
  439. if(number_of_tries GREATER 1)
  440. message(STATUS \"Had to git clone more than once:
  441. \${number_of_tries} times.\")
  442. endif()
  443. if(error_code)
  444. message(FATAL_ERROR \"Failed to clone repository: '${git_repository}'\")
  445. endif()
  446. execute_process(
  447. COMMAND \"${git_EXECUTABLE}\" checkout ${git_tag}
  448. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  449. RESULT_VARIABLE error_code
  450. )
  451. if(error_code)
  452. message(FATAL_ERROR \"Failed to checkout tag: '${git_tag}'\")
  453. endif()
  454. execute_process(
  455. COMMAND \"${git_EXECUTABLE}\" submodule init
  456. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  457. RESULT_VARIABLE error_code
  458. )
  459. if(error_code)
  460. message(FATAL_ERROR \"Failed to init submodules in: '${work_dir}/${src_name}'\")
  461. endif()
  462. execute_process(
  463. COMMAND \"${git_EXECUTABLE}\" submodule update --recursive ${git_submodules}
  464. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  465. RESULT_VARIABLE error_code
  466. )
  467. if(error_code)
  468. message(FATAL_ERROR \"Failed to update submodules in: '${work_dir}/${src_name}'\")
  469. endif()
  470. # Complete success, update the script-last-run stamp file:
  471. #
  472. execute_process(
  473. COMMAND \${CMAKE_COMMAND} -E copy
  474. \"${gitclone_infofile}\"
  475. \"${gitclone_stampfile}\"
  476. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  477. RESULT_VARIABLE error_code
  478. )
  479. if(error_code)
  480. message(FATAL_ERROR \"Failed to copy script-last-run stamp file: '${gitclone_stampfile}'\")
  481. endif()
  482. "
  483. )
  484. endfunction()
  485. function(_ep_write_hgclone_script script_filename source_dir hg_EXECUTABLE hg_repository hg_tag src_name work_dir hgclone_infofile hgclone_stampfile)
  486. file(WRITE ${script_filename}
  487. "if(\"${hg_tag}\" STREQUAL \"\")
  488. message(FATAL_ERROR \"Tag for hg checkout should not be empty.\")
  489. endif()
  490. set(run 0)
  491. if(\"${hgclone_infofile}\" IS_NEWER_THAN \"${hgclone_stampfile}\")
  492. set(run 1)
  493. endif()
  494. if(NOT run)
  495. message(STATUS \"Avoiding repeated hg clone, stamp file is up to date: '${hgclone_stampfile}'\")
  496. return()
  497. endif()
  498. execute_process(
  499. COMMAND \${CMAKE_COMMAND} -E remove_directory \"${source_dir}\"
  500. RESULT_VARIABLE error_code
  501. )
  502. if(error_code)
  503. message(FATAL_ERROR \"Failed to remove directory: '${source_dir}'\")
  504. endif()
  505. execute_process(
  506. COMMAND \"${hg_EXECUTABLE}\" clone \"${hg_repository}\" \"${src_name}\"
  507. WORKING_DIRECTORY \"${work_dir}\"
  508. RESULT_VARIABLE error_code
  509. )
  510. if(error_code)
  511. message(FATAL_ERROR \"Failed to clone repository: '${hg_repository}'\")
  512. endif()
  513. execute_process(
  514. COMMAND \"${hg_EXECUTABLE}\" update ${hg_tag}
  515. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  516. RESULT_VARIABLE error_code
  517. )
  518. if(error_code)
  519. message(FATAL_ERROR \"Failed to checkout tag: '${hg_tag}'\")
  520. endif()
  521. # Complete success, update the script-last-run stamp file:
  522. #
  523. execute_process(
  524. COMMAND \${CMAKE_COMMAND} -E copy
  525. \"${hgclone_infofile}\"
  526. \"${hgclone_stampfile}\"
  527. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  528. RESULT_VARIABLE error_code
  529. )
  530. if(error_code)
  531. message(FATAL_ERROR \"Failed to copy script-last-run stamp file: '${hgclone_stampfile}'\")
  532. endif()
  533. "
  534. )
  535. endfunction()
  536. function(_ep_write_gitupdate_script script_filename git_EXECUTABLE git_tag git_submodules git_repository work_dir)
  537. file(WRITE ${script_filename}
  538. "if(\"${git_tag}\" STREQUAL \"\")
  539. message(FATAL_ERROR \"Tag for git checkout should not be empty.\")
  540. endif()
  541. execute_process(
  542. COMMAND \"${git_EXECUTABLE}\" rev-list --max-count=1 HEAD
  543. WORKING_DIRECTORY \"${work_dir}\"
  544. RESULT_VARIABLE error_code
  545. OUTPUT_VARIABLE head_sha
  546. OUTPUT_STRIP_TRAILING_WHITESPACE
  547. )
  548. if(error_code)
  549. message(FATAL_ERROR \"Failed to get the hash for HEAD\")
  550. endif()
  551. execute_process(
  552. COMMAND \"${git_EXECUTABLE}\" show-ref ${git_tag}
  553. WORKING_DIRECTORY \"${work_dir}\"
  554. OUTPUT_VARIABLE show_ref_output
  555. )
  556. # If a remote ref is asked for, which can possibly move around,
  557. # we must always do a fetch and checkout.
  558. if(\"\${show_ref_output}\" MATCHES \"remotes\")
  559. set(is_remote_ref 1)
  560. else()
  561. set(is_remote_ref 0)
  562. endif()
  563. # Tag is in the form <remote>/<tag> (i.e. origin/master) we must strip
  564. # the remote from the tag.
  565. if(\"\${show_ref_output}\" MATCHES \"refs/remotes/${git_tag}\")
  566. string(REGEX MATCH \"^([^/]+)/(.+)$\" _unused \"${git_tag}\")
  567. set(git_remote \"\${CMAKE_MATCH_1}\")
  568. set(git_tag \"\${CMAKE_MATCH_2}\")
  569. else()
  570. set(git_remote \"origin\")
  571. set(git_tag \"${git_tag}\")
  572. endif()
  573. # This will fail if the tag does not exist (it probably has not been fetched
  574. # yet).
  575. execute_process(
  576. COMMAND \"${git_EXECUTABLE}\" rev-list --max-count=1 ${git_tag}
  577. WORKING_DIRECTORY \"${work_dir}\"
  578. RESULT_VARIABLE error_code
  579. OUTPUT_VARIABLE tag_sha
  580. OUTPUT_STRIP_TRAILING_WHITESPACE
  581. )
  582. # Is the hash checkout out that we want?
  583. if(error_code OR is_remote_ref OR NOT (\"\${tag_sha}\" STREQUAL \"\${head_sha}\"))
  584. execute_process(
  585. COMMAND \"${git_EXECUTABLE}\" fetch
  586. WORKING_DIRECTORY \"${work_dir}\"
  587. RESULT_VARIABLE error_code
  588. )
  589. if(error_code)
  590. message(FATAL_ERROR \"Failed to fetch repository '${git_repository}'\")
  591. endif()
  592. if(is_remote_ref)
  593. # Check if stash is needed
  594. execute_process(
  595. COMMAND \"${git_EXECUTABLE}\" status --porcelain
  596. WORKING_DIRECTORY \"${work_dir}\"
  597. RESULT_VARIABLE error_code
  598. OUTPUT_VARIABLE repo_status
  599. )
  600. if(error_code)
  601. message(FATAL_ERROR \"Failed to get the status\")
  602. endif()
  603. string(LENGTH \"\${repo_status}\" need_stash)
  604. # If not in clean state, stash changes in order to be able to be able to
  605. # perform git pull --rebase
  606. if(need_stash)
  607. execute_process(
  608. COMMAND \"${git_EXECUTABLE}\" stash save --all --quiet
  609. WORKING_DIRECTORY \"${work_dir}\"
  610. RESULT_VARIABLE error_code
  611. )
  612. if(error_code)
  613. message(FATAL_ERROR \"Failed to stash changes\")
  614. endif()
  615. endif()
  616. # Pull changes from the remote branch
  617. execute_process(
  618. COMMAND \"${git_EXECUTABLE}\" rebase \${git_remote}/\${git_tag}
  619. WORKING_DIRECTORY \"${work_dir}\"
  620. RESULT_VARIABLE error_code
  621. )
  622. if(error_code)
  623. # Rebase failed: Restore previous state.
  624. execute_process(
  625. COMMAND \"${git_EXECUTABLE}\" rebase --abort
  626. WORKING_DIRECTORY \"${work_dir}\"
  627. )
  628. if(need_stash)
  629. execute_process(
  630. COMMAND \"${git_EXECUTABLE}\" stash pop --index --quiet
  631. WORKING_DIRECTORY \"${work_dir}\"
  632. )
  633. endif()
  634. message(FATAL_ERROR \"\\nFailed to rebase in: '${work_dir}/${src_name}'.\\nYou will have to resolve the conflicts manually\")
  635. endif()
  636. if(need_stash)
  637. execute_process(
  638. COMMAND \"${git_EXECUTABLE}\" stash pop --index --quiet
  639. WORKING_DIRECTORY \"${work_dir}\"
  640. RESULT_VARIABLE error_code
  641. )
  642. if(error_code)
  643. # Stash pop --index failed: Try again dropping the index
  644. execute_process(
  645. COMMAND \"${git_EXECUTABLE}\" reset --hard --quiet
  646. WORKING_DIRECTORY \"${work_dir}\"
  647. RESULT_VARIABLE error_code
  648. )
  649. execute_process(
  650. COMMAND \"${git_EXECUTABLE}\" stash pop --quiet
  651. WORKING_DIRECTORY \"${work_dir}\"
  652. RESULT_VARIABLE error_code
  653. )
  654. if(error_code)
  655. # Stash pop failed: Restore previous state.
  656. execute_process(
  657. COMMAND \"${git_EXECUTABLE}\" reset --hard --quiet \${head_sha}
  658. WORKING_DIRECTORY \"${work_dir}\"
  659. )
  660. execute_process(
  661. COMMAND \"${git_EXECUTABLE}\" stash pop --index --quiet
  662. WORKING_DIRECTORY \"${work_dir}\"
  663. )
  664. message(FATAL_ERROR \"\\nFailed to unstash changes in: '${work_dir}/${src_name}'.\\nYou will have to resolve the conflicts manually\")
  665. endif()
  666. endif()
  667. endif()
  668. else()
  669. execute_process(
  670. COMMAND \"${git_EXECUTABLE}\" checkout ${git_tag}
  671. WORKING_DIRECTORY \"${work_dir}\"
  672. RESULT_VARIABLE error_code
  673. )
  674. if(error_code)
  675. message(FATAL_ERROR \"Failed to checkout tag: '${git_tag}'\")
  676. endif()
  677. endif()
  678. execute_process(
  679. COMMAND \"${git_EXECUTABLE}\" submodule update --recursive ${git_submodules}
  680. WORKING_DIRECTORY \"${work_dir}/${src_name}\"
  681. RESULT_VARIABLE error_code
  682. )
  683. if(error_code)
  684. message(FATAL_ERROR \"Failed to update submodules in: '${work_dir}/${src_name}'\")
  685. endif()
  686. endif()
  687. "
  688. )
  689. endfunction(_ep_write_gitupdate_script)
  690. function(_ep_write_downloadfile_script script_filename remote local timeout no_progress hash tls_verify tls_cainfo)
  691. if(timeout)
  692. set(timeout_args TIMEOUT ${timeout})
  693. set(timeout_msg "${timeout} seconds")
  694. else()
  695. set(timeout_args "# no TIMEOUT")
  696. set(timeout_msg "none")
  697. endif()
  698. if(no_progress)
  699. set(show_progress "")
  700. else()
  701. set(show_progress "SHOW_PROGRESS")
  702. endif()
  703. if("${hash}" MATCHES "${_ep_hash_regex}")
  704. string(CONCAT hash_check
  705. "if(EXISTS \"${local}\")\n"
  706. " file(\"${CMAKE_MATCH_1}\" \"${local}\" hash_value)\n"
  707. " if(\"x\${hash_value}\" STREQUAL \"x${CMAKE_MATCH_2}\")\n"
  708. " return()\n"
  709. " endif()\n"
  710. "endif()\n"
  711. )
  712. else()
  713. set(hash_check "")
  714. endif()
  715. # check for curl globals in the project
  716. if(DEFINED CMAKE_TLS_VERIFY)
  717. set(tls_verify "set(CMAKE_TLS_VERIFY ${CMAKE_TLS_VERIFY})")
  718. endif()
  719. if(DEFINED CMAKE_TLS_CAINFO)
  720. set(tls_cainfo "set(CMAKE_TLS_CAINFO \"${CMAKE_TLS_CAINFO}\")")
  721. endif()
  722. # now check for curl locals so that the local values
  723. # will override the globals
  724. # check for tls_verify argument
  725. string(LENGTH "${tls_verify}" tls_verify_len)
  726. if(tls_verify_len GREATER 0)
  727. set(tls_verify "set(CMAKE_TLS_VERIFY ${tls_verify})")
  728. endif()
  729. # check for tls_cainfo argument
  730. string(LENGTH "${tls_cainfo}" tls_cainfo_len)
  731. if(tls_cainfo_len GREATER 0)
  732. set(tls_cainfo "set(CMAKE_TLS_CAINFO \"${tls_cainfo}\")")
  733. endif()
  734. file(WRITE ${script_filename}
  735. "${hash_check}message(STATUS \"downloading...
  736. src='${remote}'
  737. dst='${local}'
  738. timeout='${timeout_msg}'\")
  739. ${tls_verify}
  740. ${tls_cainfo}
  741. file(DOWNLOAD
  742. \"${remote}\"
  743. \"${local}\"
  744. ${show_progress}
  745. ${timeout_args}
  746. STATUS status
  747. LOG log)
  748. list(GET status 0 status_code)
  749. list(GET status 1 status_string)
  750. if(NOT status_code EQUAL 0)
  751. message(FATAL_ERROR \"error: downloading '${remote}' failed
  752. status_code: \${status_code}
  753. status_string: \${status_string}
  754. log: \${log}
  755. \")
  756. endif()
  757. message(STATUS \"downloading... done\")
  758. "
  759. )
  760. endfunction()
  761. function(_ep_write_verifyfile_script script_filename local hash retries download_script)
  762. if("${hash}" MATCHES "${_ep_hash_regex}")
  763. set(algo "${CMAKE_MATCH_1}")
  764. string(TOLOWER "${CMAKE_MATCH_2}" expect_value)
  765. set(script_content "set(expect_value \"${expect_value}\")
  766. set(attempt 0)
  767. set(succeeded 0)
  768. while(\${attempt} LESS ${retries} OR \${attempt} EQUAL ${retries} AND NOT \${succeeded})
  769. file(${algo} \"\${file}\" actual_value)
  770. if(\"\${actual_value}\" STREQUAL \"\${expect_value}\")
  771. set(succeeded 1)
  772. elseif(\${attempt} LESS ${retries})
  773. message(STATUS \"${algo} hash of \${file}
  774. does not match expected value
  775. expected: \${expect_value}
  776. actual: \${actual_value}
  777. Retrying download.
  778. \")
  779. file(REMOVE \"\${file}\")
  780. execute_process(COMMAND \${CMAKE_COMMAND} -P \"${download_script}\")
  781. endif()
  782. math(EXPR attempt \"\${attempt} + 1\")
  783. endwhile()
  784. if(\${succeeded})
  785. message(STATUS \"verifying file... done\")
  786. else()
  787. message(FATAL_ERROR \"error: ${algo} hash of
  788. \${file}
  789. does not match expected value
  790. expected: \${expect_value}
  791. actual: \${actual_value}
  792. \")
  793. endif()")
  794. else()
  795. set(script_content "message(STATUS \"verifying file... warning: did not verify file - no URL_HASH specified?\")")
  796. endif()
  797. file(WRITE ${script_filename} "set(file \"${local}\")
  798. message(STATUS \"verifying file...
  799. file='\${file}'\")
  800. ${script_content}
  801. ")
  802. endfunction()
  803. function(_ep_write_extractfile_script script_filename name filename directory)
  804. set(args "")
  805. if(filename MATCHES "(\\.|=)(7z|tar\\.bz2|tar\\.gz|tar\\.xz|tbz2|tgz|txz|zip)$")
  806. set(args xfz)
  807. endif()
  808. if(filename MATCHES "(\\.|=)tar$")
  809. set(args xf)
  810. endif()
  811. if(args STREQUAL "")
  812. message(SEND_ERROR "error: do not know how to extract '${filename}' -- known types are .7z, .tar, .tar.bz2, .tar.gz, .tar.xz, .tbz2, .tgz, .txz and .zip")
  813. return()
  814. endif()
  815. file(WRITE ${script_filename}
  816. "# Make file names absolute:
  817. #
  818. get_filename_component(filename \"${filename}\" ABSOLUTE)
  819. get_filename_component(directory \"${directory}\" ABSOLUTE)
  820. message(STATUS \"extracting...
  821. src='\${filename}'
  822. dst='\${directory}'\")
  823. if(NOT EXISTS \"\${filename}\")
  824. message(FATAL_ERROR \"error: file to extract does not exist: '\${filename}'\")
  825. endif()
  826. # Prepare a space for extracting:
  827. #
  828. set(i 1234)
  829. while(EXISTS \"\${directory}/../ex-${name}\${i}\")
  830. math(EXPR i \"\${i} + 1\")
  831. endwhile()
  832. set(ut_dir \"\${directory}/../ex-${name}\${i}\")
  833. file(MAKE_DIRECTORY \"\${ut_dir}\")
  834. # Extract it:
  835. #
  836. message(STATUS \"extracting... [tar ${args}]\")
  837. execute_process(COMMAND \${CMAKE_COMMAND} -E tar ${args} \${filename}
  838. WORKING_DIRECTORY \${ut_dir}
  839. RESULT_VARIABLE rv)
  840. if(NOT rv EQUAL 0)
  841. message(STATUS \"extracting... [error clean up]\")
  842. file(REMOVE_RECURSE \"\${ut_dir}\")
  843. message(FATAL_ERROR \"error: extract of '\${filename}' failed\")
  844. endif()
  845. # Analyze what came out of the tar file:
  846. #
  847. message(STATUS \"extracting... [analysis]\")
  848. file(GLOB contents \"\${ut_dir}/*\")
  849. list(LENGTH contents n)
  850. if(NOT n EQUAL 1 OR NOT IS_DIRECTORY \"\${contents}\")
  851. set(contents \"\${ut_dir}\")
  852. endif()
  853. # Move \"the one\" directory to the final directory:
  854. #
  855. message(STATUS \"extracting... [rename]\")
  856. file(REMOVE_RECURSE \${directory})
  857. get_filename_component(contents \${contents} ABSOLUTE)
  858. file(RENAME \${contents} \${directory})
  859. # Clean up:
  860. #
  861. message(STATUS \"extracting... [clean up]\")
  862. file(REMOVE_RECURSE \"\${ut_dir}\")
  863. message(STATUS \"extracting... done\")
  864. "
  865. )
  866. endfunction()
  867. function(_ep_set_directories name)
  868. get_property(prefix TARGET ${name} PROPERTY _EP_PREFIX)
  869. if(NOT prefix)
  870. get_property(prefix DIRECTORY PROPERTY EP_PREFIX)
  871. if(NOT prefix)
  872. get_property(base DIRECTORY PROPERTY EP_BASE)
  873. if(NOT base)
  874. set(prefix "${name}-prefix")
  875. endif()
  876. endif()
  877. endif()
  878. if(prefix)
  879. set(tmp_default "${prefix}/tmp")
  880. set(download_default "${prefix}/src")
  881. set(source_default "${prefix}/src/${name}")
  882. set(binary_default "${prefix}/src/${name}-build")
  883. set(stamp_default "${prefix}/src/${name}-stamp")
  884. set(install_default "${prefix}")
  885. else()
  886. set(tmp_default "${base}/tmp/${name}")
  887. set(download_default "${base}/Download/${name}")
  888. set(source_default "${base}/Source/${name}")
  889. set(binary_default "${base}/Build/${name}")
  890. set(stamp_default "${base}/Stamp/${name}")
  891. set(install_default "${base}/Install/${name}")
  892. endif()
  893. get_property(build_in_source TARGET ${name} PROPERTY _EP_BUILD_IN_SOURCE)
  894. if(build_in_source)
  895. get_property(have_binary_dir TARGET ${name} PROPERTY _EP_BINARY_DIR SET)
  896. if(have_binary_dir)
  897. message(FATAL_ERROR
  898. "External project ${name} has both BINARY_DIR and BUILD_IN_SOURCE!")
  899. endif()
  900. endif()
  901. set(top "${CMAKE_CURRENT_BINARY_DIR}")
  902. set(places stamp download source binary install tmp)
  903. foreach(var ${places})
  904. string(TOUPPER "${var}" VAR)
  905. get_property(${var}_dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  906. if(NOT ${var}_dir)
  907. set(${var}_dir "${${var}_default}")
  908. endif()
  909. if(NOT IS_ABSOLUTE "${${var}_dir}")
  910. get_filename_component(${var}_dir "${top}/${${var}_dir}" ABSOLUTE)
  911. endif()
  912. set_property(TARGET ${name} PROPERTY _EP_${VAR}_DIR "${${var}_dir}")
  913. endforeach()
  914. if(build_in_source)
  915. get_property(source_dir TARGET ${name} PROPERTY _EP_SOURCE_DIR)
  916. set_property(TARGET ${name} PROPERTY _EP_BINARY_DIR "${source_dir}")
  917. endif()
  918. # Make the directories at CMake configure time *and* add a custom command
  919. # to make them at build time. They need to exist at makefile generation
  920. # time for Borland make and wmake so that CMake may generate makefiles
  921. # with "cd C:\short\paths\with\no\spaces" commands in them.
  922. #
  923. # Additionally, the add_custom_command is still used in case somebody
  924. # removes one of the necessary directories and tries to rebuild without
  925. # re-running cmake.
  926. foreach(var ${places})
  927. string(TOUPPER "${var}" VAR)
  928. get_property(dir TARGET ${name} PROPERTY _EP_${VAR}_DIR)
  929. file(MAKE_DIRECTORY "${dir}")
  930. if(NOT EXISTS "${dir}")
  931. message(FATAL_ERROR "dir '${dir}' does not exist after file(MAKE_DIRECTORY)")
  932. endif()
  933. endforeach()
  934. endfunction()
  935. # IMPORTANT: this MUST be a macro and not a function because of the
  936. # in-place replacements that occur in each ${var}
  937. #
  938. macro(_ep_replace_location_tags target_name)
  939. set(vars ${ARGN})
  940. foreach(var ${vars})
  941. if(${var})
  942. foreach(dir SOURCE_DIR BINARY_DIR INSTALL_DIR TMP_DIR)
  943. get_property(val TARGET ${target_name} PROPERTY _EP_${dir})
  944. string(REPLACE "<${dir}>" "${val}" ${var} "${${var}}")
  945. endforeach()
  946. endif()
  947. endforeach()
  948. endmacro()
  949. function(_ep_command_line_to_initial_cache var args force)
  950. set(script_initial_cache "")
  951. set(regex "^([^:]+):([^=]+)=(.*)$")
  952. set(setArg "")
  953. set(forceArg "")
  954. if(force)
  955. set(forceArg "FORCE")
  956. endif()
  957. foreach(line ${args})
  958. if("${line}" MATCHES "^-D(.*)")
  959. set(line "${CMAKE_MATCH_1}")
  960. if(setArg)
  961. # This is required to build up lists in variables, or complete an entry
  962. set(setArg "${setArg}${accumulator}\" CACHE ${type} \"Initial cache\" ${forceArg})")
  963. set(script_initial_cache "${script_initial_cache}\n${setArg}")
  964. set(accumulator "")
  965. set(setArg "")
  966. endif()
  967. if("${line}" MATCHES "${regex}")
  968. set(name "${CMAKE_MATCH_1}")
  969. set(type "${CMAKE_MATCH_2}")
  970. set(value "${CMAKE_MATCH_3}")
  971. set(setArg "set(${name} \"${value}")
  972. else()
  973. message(WARNING "Line '${line}' does not match regex. Ignoring.")
  974. endif()
  975. else()
  976. # Assume this is a list to append to the last var
  977. set(accumulator "${accumulator};${line}")
  978. endif()
  979. endforeach()
  980. # Catch the final line of the args
  981. if(setArg)
  982. set(setArg "${setArg}${accumulator}\" CACHE ${type} \"Initial cache\" ${forceArg})")
  983. set(script_initial_cache "${script_initial_cache}\n${setArg}")
  984. endif()
  985. set(${var} ${script_initial_cache} PARENT_SCOPE)
  986. endfunction()
  987. function(_ep_write_initial_cache target_name script_filename script_initial_cache)
  988. # Write out values into an initial cache, that will be passed to CMake with -C
  989. # Replace location tags.
  990. _ep_replace_location_tags(${target_name} script_initial_cache)
  991. # Write out the initial cache file to the location specified.
  992. file(GENERATE OUTPUT "${script_filename}" CONTENT "${script_initial_cache}")
  993. endfunction()
  994. function(ExternalProject_Get_Property name)
  995. foreach(var ${ARGN})
  996. string(TOUPPER "${var}" VAR)
  997. get_property(${var} TARGET ${name} PROPERTY _EP_${VAR})
  998. if(NOT ${var})
  999. message(FATAL_ERROR "External project \"${name}\" has no ${var}")
  1000. endif()
  1001. set(${var} "${${var}}" PARENT_SCOPE)
  1002. endforeach()
  1003. endfunction()
  1004. function(_ep_get_configure_command_id name cfg_cmd_id_var)
  1005. get_target_property(cmd ${name} _EP_CONFIGURE_COMMAND)
  1006. if(cmd STREQUAL "")
  1007. # Explicit empty string means no configure step for this project
  1008. set(${cfg_cmd_id_var} "none" PARENT_SCOPE)
  1009. else()
  1010. if(NOT cmd)
  1011. # Default is "use cmake":
  1012. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  1013. else()
  1014. # Otherwise we have to analyze the value:
  1015. if(cmd MATCHES "^[^;]*/configure")
  1016. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  1017. elseif(cmd MATCHES "^[^;]*/cmake" AND NOT cmd MATCHES ";-[PE];")
  1018. set(${cfg_cmd_id_var} "cmake" PARENT_SCOPE)
  1019. elseif(cmd MATCHES "config")
  1020. set(${cfg_cmd_id_var} "configure" PARENT_SCOPE)
  1021. else()
  1022. set(${cfg_cmd_id_var} "unknown:${cmd}" PARENT_SCOPE)
  1023. endif()
  1024. endif()
  1025. endif()
  1026. endfunction()
  1027. function(_ep_get_build_command name step cmd_var)
  1028. set(cmd "${${cmd_var}}")
  1029. if(NOT cmd)
  1030. set(args)
  1031. _ep_get_configure_command_id(${name} cfg_cmd_id)
  1032. if(cfg_cmd_id STREQUAL "cmake")
  1033. # CMake project. Select build command based on generator.
  1034. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  1035. if("${CMAKE_GENERATOR}" MATCHES "Make" AND
  1036. ("${cmake_generator}" MATCHES "Make" OR NOT cmake_generator))
  1037. # The project uses the same Makefile generator. Use recursive make.
  1038. set(cmd "$(MAKE)")
  1039. if(step STREQUAL "INSTALL")
  1040. set(args install)
  1041. endif()
  1042. if(step STREQUAL "TEST")
  1043. set(args test)
  1044. endif()
  1045. else()
  1046. # Drive the project with "cmake --build".
  1047. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  1048. if(cmake_command)
  1049. set(cmd "${cmake_command}")
  1050. else()
  1051. set(cmd "${CMAKE_COMMAND}")
  1052. endif()
  1053. set(args --build ".")
  1054. if (CMAKE_CFG_INTDIR AND NOT CMAKE_CFG_INTDIR STREQUAL ".")
  1055. list(APPEND args --config "${CMAKE_CFG_INTDIR}")
  1056. endif ()
  1057. if(step STREQUAL "INSTALL")
  1058. list(APPEND args --target install)
  1059. endif()
  1060. # But for "TEST" drive the project with corresponding "ctest".
  1061. if(step STREQUAL "TEST")
  1062. string(REGEX REPLACE "^(.*/)cmake([^/]*)$" "\\1ctest\\2" cmd "${cmd}")
  1063. set(args "")
  1064. endif()
  1065. endif()
  1066. else()
  1067. # Non-CMake project. Guess "make" and "make install" and "make test".
  1068. if("${CMAKE_GENERATOR}" MATCHES "Makefiles")
  1069. # Try to get the parallel arguments
  1070. set(cmd "$(MAKE)")
  1071. else()
  1072. set(cmd "make")
  1073. endif()
  1074. if(step STREQUAL "INSTALL")
  1075. set(args install)
  1076. endif()
  1077. if(step STREQUAL "TEST")
  1078. set(args test)
  1079. endif()
  1080. endif()
  1081. # Use user-specified arguments instead of default arguments, if any.
  1082. get_property(have_args TARGET ${name} PROPERTY _EP_${step}_ARGS SET)
  1083. if(have_args)
  1084. get_target_property(args ${name} _EP_${step}_ARGS)
  1085. endif()
  1086. list(APPEND cmd ${args})
  1087. endif()
  1088. set(${cmd_var} "${cmd}" PARENT_SCOPE)
  1089. endfunction()
  1090. function(_ep_write_log_script name step cmd_var)
  1091. ExternalProject_Get_Property(${name} stamp_dir)
  1092. set(command "${${cmd_var}}")
  1093. set(make "")
  1094. set(code_cygpath_make "")
  1095. if(command MATCHES "^\\$\\(MAKE\\)")
  1096. # GNU make recognizes the string "$(MAKE)" as recursive make, so
  1097. # ensure that it appears directly in the makefile.
  1098. string(REGEX REPLACE "^\\$\\(MAKE\\)" "\${make}" command "${command}")
  1099. set(make "-Dmake=$(MAKE)")
  1100. if(WIN32 AND NOT CYGWIN)
  1101. set(code_cygpath_make "
  1102. if(\${make} MATCHES \"^/\")
  1103. execute_process(
  1104. COMMAND cygpath -w \${make}
  1105. OUTPUT_VARIABLE cygpath_make
  1106. ERROR_VARIABLE cygpath_make
  1107. RESULT_VARIABLE cygpath_error
  1108. OUTPUT_STRIP_TRAILING_WHITESPACE
  1109. )
  1110. if(NOT cygpath_error)
  1111. set(make \${cygpath_make})
  1112. endif()
  1113. endif()
  1114. ")
  1115. endif()
  1116. endif()
  1117. set(config "")
  1118. if("${CMAKE_CFG_INTDIR}" MATCHES "^\\$")
  1119. string(REPLACE "${CMAKE_CFG_INTDIR}" "\${config}" command "${command}")
  1120. set(config "-Dconfig=${CMAKE_CFG_INTDIR}")
  1121. endif()
  1122. # Wrap multiple 'COMMAND' lines up into a second-level wrapper
  1123. # script so all output can be sent to one log file.
  1124. if(command MATCHES "(^|;)COMMAND;")
  1125. set(code_execute_process "
  1126. ${code_cygpath_make}
  1127. execute_process(COMMAND \${command} RESULT_VARIABLE result)
  1128. if(result)
  1129. set(msg \"Command failed (\${result}):\\n\")
  1130. foreach(arg IN LISTS command)
  1131. set(msg \"\${msg} '\${arg}'\")
  1132. endforeach()
  1133. message(FATAL_ERROR \"\${msg}\")
  1134. endif()
  1135. ")
  1136. set(code "")
  1137. set(cmd "")
  1138. set(sep "")
  1139. foreach(arg IN LISTS command)
  1140. if("x${arg}" STREQUAL "xCOMMAND")
  1141. if(NOT "x${cmd}" STREQUAL "x")
  1142. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  1143. endif()
  1144. set(cmd "")
  1145. set(sep "")
  1146. else()
  1147. set(cmd "${cmd}${sep}${arg}")
  1148. set(sep ";")
  1149. endif()
  1150. endforeach()
  1151. set(code "${code}set(command \"${cmd}\")${code_execute_process}")
  1152. file(GENERATE OUTPUT "${stamp_dir}/${name}-${step}-$<CONFIG>-impl.cmake" CONTENT "${code}")
  1153. set(command ${CMAKE_COMMAND} "-Dmake=\${make}" "-Dconfig=\${config}" -P ${stamp_dir}/${name}-${step}-$<CONFIG>-impl.cmake)
  1154. endif()
  1155. # Wrap the command in a script to log output to files.
  1156. set(script ${stamp_dir}/${name}-${step}-$<CONFIG>.cmake)
  1157. set(logbase ${stamp_dir}/${name}-${step})
  1158. set(code "
  1159. ${code_cygpath_make}
  1160. set(command \"${command}\")
  1161. execute_process(
  1162. COMMAND \${command}
  1163. RESULT_VARIABLE result
  1164. OUTPUT_FILE \"${logbase}-out.log\"
  1165. ERROR_FILE \"${logbase}-err.log\"
  1166. )
  1167. if(result)
  1168. set(msg \"Command failed: \${result}\\n\")
  1169. foreach(arg IN LISTS command)
  1170. set(msg \"\${msg} '\${arg}'\")
  1171. endforeach()
  1172. set(msg \"\${msg}\\nSee also\\n ${logbase}-*.log\")
  1173. message(FATAL_ERROR \"\${msg}\")
  1174. else()
  1175. set(msg \"${name} ${step} command succeeded. See also ${logbase}-*.log\")
  1176. message(STATUS \"\${msg}\")
  1177. endif()
  1178. ")
  1179. file(GENERATE OUTPUT "${script}" CONTENT "${code}")
  1180. set(command ${CMAKE_COMMAND} ${make} ${config} -P ${script})
  1181. set(${cmd_var} "${command}" PARENT_SCOPE)
  1182. endfunction()
  1183. # This module used to use "/${CMAKE_CFG_INTDIR}" directly and produced
  1184. # makefiles with "/./" in paths for custom command dependencies. Which
  1185. # resulted in problems with parallel make -j invocations.
  1186. #
  1187. # This function was added so that the suffix (search below for ${cfgdir}) is
  1188. # only set to "/${CMAKE_CFG_INTDIR}" when ${CMAKE_CFG_INTDIR} is not going to
  1189. # be "." (multi-configuration build systems like Visual Studio and Xcode...)
  1190. #
  1191. function(_ep_get_configuration_subdir_suffix suffix_var)
  1192. set(suffix "")
  1193. if(CMAKE_CONFIGURATION_TYPES)
  1194. set(suffix "/${CMAKE_CFG_INTDIR}")
  1195. endif()
  1196. set(${suffix_var} "${suffix}" PARENT_SCOPE)
  1197. endfunction()
  1198. function(_ep_get_step_stampfile name step stampfile_var)
  1199. ExternalProject_Get_Property(${name} stamp_dir)
  1200. _ep_get_configuration_subdir_suffix(cfgdir)
  1201. set(stampfile "${stamp_dir}${cfgdir}/${name}-${step}")
  1202. set(${stampfile_var} "${stampfile}" PARENT_SCOPE)
  1203. endfunction()
  1204. function(ExternalProject_Add_StepTargets name)
  1205. set(steps ${ARGN})
  1206. if(ARGC GREATER 1 AND "${ARGV1}" STREQUAL "NO_DEPENDS")
  1207. set(no_deps 1)
  1208. list(REMOVE_AT steps 0)
  1209. endif()
  1210. foreach(step ${steps})
  1211. if(no_deps AND "${step}" MATCHES "^(configure|build|install|test)$")
  1212. message(AUTHOR_WARNING "Using NO_DEPENDS for \"${step}\" step might break parallel builds")
  1213. endif()
  1214. _ep_get_step_stampfile(${name} ${step} stamp_file)
  1215. add_custom_target(${name}-${step}
  1216. DEPENDS ${stamp_file})
  1217. set_property(TARGET ${name}-${step} PROPERTY _EP_IS_EXTERNAL_PROJECT_STEP 1)
  1218. set_property(TARGET ${name}-${step} PROPERTY LABELS ${name})
  1219. set_property(TARGET ${name}-${step} PROPERTY FOLDER "ExternalProjectTargets/${name}")
  1220. # Depend on other external projects (target-level).
  1221. if(NOT no_deps)
  1222. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1223. foreach(arg IN LISTS deps)
  1224. add_dependencies(${name}-${step} ${arg})
  1225. endforeach()
  1226. endif()
  1227. endforeach()
  1228. endfunction()
  1229. function(ExternalProject_Add_Step name step)
  1230. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  1231. _ep_get_configuration_subdir_suffix(cfgdir)
  1232. set(complete_stamp_file "${cmf_dir}${cfgdir}/${name}-complete")
  1233. _ep_get_step_stampfile(${name} ${step} stamp_file)
  1234. _ep_parse_arguments(ExternalProject_Add_Step
  1235. ${name} _EP_${step}_ "${ARGN}")
  1236. get_property(exclude_from_main TARGET ${name} PROPERTY _EP_${step}_EXCLUDE_FROM_MAIN)
  1237. if(NOT exclude_from_main)
  1238. add_custom_command(APPEND
  1239. OUTPUT ${complete_stamp_file}
  1240. DEPENDS ${stamp_file}
  1241. )
  1242. endif()
  1243. # Steps depending on this step.
  1244. get_property(dependers TARGET ${name} PROPERTY _EP_${step}_DEPENDERS)
  1245. foreach(depender IN LISTS dependers)
  1246. _ep_get_step_stampfile(${name} ${depender} depender_stamp_file)
  1247. add_custom_command(APPEND
  1248. OUTPUT ${depender_stamp_file}
  1249. DEPENDS ${stamp_file}
  1250. )
  1251. endforeach()
  1252. # Dependencies on files.
  1253. get_property(depends TARGET ${name} PROPERTY _EP_${step}_DEPENDS)
  1254. # Byproducts of the step.
  1255. get_property(byproducts TARGET ${name} PROPERTY _EP_${step}_BYPRODUCTS)
  1256. # Dependencies on steps.
  1257. get_property(dependees TARGET ${name} PROPERTY _EP_${step}_DEPENDEES)
  1258. foreach(dependee IN LISTS dependees)
  1259. _ep_get_step_stampfile(${name} ${dependee} dependee_stamp_file)
  1260. list(APPEND depends ${dependee_stamp_file})
  1261. endforeach()
  1262. # The command to run.
  1263. get_property(command TARGET ${name} PROPERTY _EP_${step}_COMMAND)
  1264. if(command)
  1265. set(comment "Performing ${step} step for '${name}'")
  1266. else()
  1267. set(comment "No ${step} step for '${name}'")
  1268. endif()
  1269. get_property(work_dir TARGET ${name} PROPERTY _EP_${step}_WORKING_DIRECTORY)
  1270. # Replace list separators.
  1271. get_property(sep TARGET ${name} PROPERTY _EP_LIST_SEPARATOR)
  1272. if(sep AND command)
  1273. string(REPLACE "${sep}" "\\;" command "${command}")
  1274. endif()
  1275. # Replace location tags.
  1276. _ep_replace_location_tags(${name} comment command work_dir byproducts)
  1277. # Custom comment?
  1278. get_property(comment_set TARGET ${name} PROPERTY _EP_${step}_COMMENT SET)
  1279. if(comment_set)
  1280. get_property(comment TARGET ${name} PROPERTY _EP_${step}_COMMENT)
  1281. endif()
  1282. # Run every time?
  1283. get_property(always TARGET ${name} PROPERTY _EP_${step}_ALWAYS)
  1284. if(always)
  1285. set_property(SOURCE ${stamp_file} PROPERTY SYMBOLIC 1)
  1286. set(touch)
  1287. # Remove any existing stamp in case the option changed in an existing tree.
  1288. if(CMAKE_CONFIGURATION_TYPES)
  1289. foreach(cfg ${CMAKE_CONFIGURATION_TYPES})
  1290. string(REPLACE "/${CMAKE_CFG_INTDIR}" "/${cfg}" stamp_file_config "${stamp_file}")
  1291. file(REMOVE ${stamp_file_config})
  1292. endforeach()
  1293. else()
  1294. file(REMOVE ${stamp_file})
  1295. endif()
  1296. else()
  1297. set(touch ${CMAKE_COMMAND} -E touch ${stamp_file})
  1298. endif()
  1299. # Wrap with log script?
  1300. get_property(log TARGET ${name} PROPERTY _EP_${step}_LOG)
  1301. if(command AND log)
  1302. _ep_write_log_script(${name} ${step} command)
  1303. endif()
  1304. if("${command}" STREQUAL "")
  1305. # Some generators (i.e. Xcode) will not generate a file level target
  1306. # if no command is set, and therefore the dependencies on this
  1307. # target will be broken.
  1308. # The empty command is replaced by an echo command here in order to
  1309. # avoid this issue.
  1310. set(command ${CMAKE_COMMAND} -E echo_append)
  1311. endif()
  1312. add_custom_command(
  1313. OUTPUT ${stamp_file}
  1314. BYPRODUCTS ${byproducts}
  1315. COMMENT ${comment}
  1316. COMMAND ${command}
  1317. COMMAND ${touch}
  1318. DEPENDS ${depends}
  1319. WORKING_DIRECTORY ${work_dir}
  1320. VERBATIM
  1321. )
  1322. set_property(TARGET ${name} APPEND PROPERTY _EP_STEPS ${step})
  1323. # Add custom "step target"?
  1324. get_property(step_targets TARGET ${name} PROPERTY _EP_STEP_TARGETS)
  1325. if(NOT step_targets)
  1326. get_property(step_targets DIRECTORY PROPERTY EP_STEP_TARGETS)
  1327. endif()
  1328. foreach(st ${step_targets})
  1329. if("${st}" STREQUAL "${step}")
  1330. ExternalProject_Add_StepTargets(${name} ${step})
  1331. break()
  1332. endif()
  1333. endforeach()
  1334. get_property(independent_step_targets TARGET ${name} PROPERTY _EP_INDEPENDENT_STEP_TARGETS)
  1335. if(NOT independent_step_targets)
  1336. get_property(independent_step_targets DIRECTORY PROPERTY EP_INDEPENDENT_STEP_TARGETS)
  1337. endif()
  1338. foreach(st ${independent_step_targets})
  1339. if("${st}" STREQUAL "${step}")
  1340. ExternalProject_Add_StepTargets(${name} NO_DEPENDS ${step})
  1341. break()
  1342. endif()
  1343. endforeach()
  1344. endfunction()
  1345. function(ExternalProject_Add_StepDependencies name step)
  1346. set(dependencies ${ARGN})
  1347. # Sanity checks on "name" and "step".
  1348. if(NOT TARGET ${name})
  1349. message(FATAL_ERROR "Cannot find target \"${name}\". Perhaps it has not yet been created using ExternalProject_Add.")
  1350. endif()
  1351. get_property(type TARGET ${name} PROPERTY TYPE)
  1352. if(NOT type STREQUAL "UTILITY")
  1353. message(FATAL_ERROR "Target \"${name}\" was not generated by ExternalProject_Add.")
  1354. endif()
  1355. get_property(is_ep TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT)
  1356. if(NOT is_ep)
  1357. message(FATAL_ERROR "Target \"${name}\" was not generated by ExternalProject_Add.")
  1358. endif()
  1359. get_property(steps TARGET ${name} PROPERTY _EP_STEPS)
  1360. list(FIND steps ${step} is_step)
  1361. if(NOT is_step)
  1362. message(FATAL_ERROR "External project \"${name}\" does not have a step \"${step}\".")
  1363. endif()
  1364. if(TARGET ${name}-${step})
  1365. get_property(type TARGET ${name}-${step} PROPERTY TYPE)
  1366. if(NOT type STREQUAL "UTILITY")
  1367. message(FATAL_ERROR "Target \"${name}-${step}\" was not generated by ExternalProject_Add_StepTargets.")
  1368. endif()
  1369. get_property(is_ep_step TARGET ${name}-${step} PROPERTY _EP_IS_EXTERNAL_PROJECT_STEP)
  1370. if(NOT is_ep_step)
  1371. message(FATAL_ERROR "Target \"${name}-${step}\" was not generated by ExternalProject_Add_StepTargets.")
  1372. endif()
  1373. endif()
  1374. # Always add file-level dependency, but add target-level dependency
  1375. # only if the target exists for that step.
  1376. _ep_get_step_stampfile(${name} ${step} stamp_file)
  1377. foreach(dep ${dependencies})
  1378. add_custom_command(APPEND
  1379. OUTPUT ${stamp_file}
  1380. DEPENDS ${dep})
  1381. if(TARGET ${name}-${step})
  1382. foreach(dep ${dependencies})
  1383. add_dependencies(${name}-${step} ${dep})
  1384. endforeach()
  1385. endif()
  1386. endforeach()
  1387. endfunction()
  1388. function(_ep_add_mkdir_command name)
  1389. ExternalProject_Get_Property(${name}
  1390. source_dir binary_dir install_dir stamp_dir download_dir tmp_dir)
  1391. _ep_get_configuration_subdir_suffix(cfgdir)
  1392. ExternalProject_Add_Step(${name} mkdir
  1393. COMMENT "Creating directories for '${name}'"
  1394. COMMAND ${CMAKE_COMMAND} -E make_directory ${source_dir}
  1395. COMMAND ${CMAKE_COMMAND} -E make_directory ${binary_dir}
  1396. COMMAND ${CMAKE_COMMAND} -E make_directory ${install_dir}
  1397. COMMAND ${CMAKE_COMMAND} -E make_directory ${tmp_dir}
  1398. COMMAND ${CMAKE_COMMAND} -E make_directory ${stamp_dir}${cfgdir}
  1399. COMMAND ${CMAKE_COMMAND} -E make_directory ${download_dir}
  1400. )
  1401. endfunction()
  1402. function(_ep_get_git_version git_EXECUTABLE git_version_var)
  1403. if(git_EXECUTABLE)
  1404. execute_process(
  1405. COMMAND "${git_EXECUTABLE}" --version
  1406. OUTPUT_VARIABLE ov
  1407. ERROR_VARIABLE ev
  1408. OUTPUT_STRIP_TRAILING_WHITESPACE
  1409. )
  1410. string(REGEX REPLACE "^git version (.+)$" "\\1" version "${ov}")
  1411. set(${git_version_var} "${version}" PARENT_SCOPE)
  1412. endif()
  1413. endfunction()
  1414. function(_ep_is_dir_empty dir empty_var)
  1415. file(GLOB gr "${dir}/*")
  1416. if("${gr}" STREQUAL "")
  1417. set(${empty_var} 1 PARENT_SCOPE)
  1418. else()
  1419. set(${empty_var} 0 PARENT_SCOPE)
  1420. endif()
  1421. endfunction()
  1422. function(_ep_add_download_command name)
  1423. ExternalProject_Get_Property(${name} source_dir stamp_dir download_dir tmp_dir)
  1424. get_property(cmd_set TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND SET)
  1425. get_property(cmd TARGET ${name} PROPERTY _EP_DOWNLOAD_COMMAND)
  1426. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  1427. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  1428. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  1429. get_property(hg_repository TARGET ${name} PROPERTY _EP_HG_REPOSITORY )
  1430. get_property(url TARGET ${name} PROPERTY _EP_URL)
  1431. get_property(fname TARGET ${name} PROPERTY _EP_DOWNLOAD_NAME)
  1432. # TODO: Perhaps file:// should be copied to download dir before extraction.
  1433. string(REGEX REPLACE "^file://" "" url "${url}")
  1434. set(depends)
  1435. set(comment)
  1436. set(work_dir)
  1437. if(cmd_set)
  1438. set(work_dir ${download_dir})
  1439. elseif(cvs_repository)
  1440. find_package(CVS QUIET)
  1441. if(NOT CVS_EXECUTABLE)
  1442. message(FATAL_ERROR "error: could not find cvs for checkout of ${name}")
  1443. endif()
  1444. get_target_property(cvs_module ${name} _EP_CVS_MODULE)
  1445. if(NOT cvs_module)
  1446. message(FATAL_ERROR "error: no CVS_MODULE")
  1447. endif()
  1448. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  1449. set(repository ${cvs_repository})
  1450. set(module ${cvs_module})
  1451. set(tag ${cvs_tag})
  1452. configure_file(
  1453. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1454. "${stamp_dir}/${name}-cvsinfo.txt"
  1455. @ONLY
  1456. )
  1457. get_filename_component(src_name "${source_dir}" NAME)
  1458. get_filename_component(work_dir "${source_dir}" PATH)
  1459. set(comment "Performing download step (CVS checkout) for '${name}'")
  1460. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q co ${cvs_tag} -d ${src_name} ${cvs_module})
  1461. list(APPEND depends ${stamp_dir}/${name}-cvsinfo.txt)
  1462. elseif(svn_repository)
  1463. find_package(Subversion QUIET)
  1464. if(NOT Subversion_SVN_EXECUTABLE)
  1465. message(FATAL_ERROR "error: could not find svn for checkout of ${name}")
  1466. endif()
  1467. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  1468. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  1469. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  1470. get_property(svn_trust_cert TARGET ${name} PROPERTY _EP_SVN_TRUST_CERT)
  1471. set(repository "${svn_repository} user=${svn_username} password=${svn_password}")
  1472. set(module)
  1473. set(tag ${svn_revision})
  1474. configure_file(
  1475. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1476. "${stamp_dir}/${name}-svninfo.txt"
  1477. @ONLY
  1478. )
  1479. get_filename_component(src_name "${source_dir}" NAME)
  1480. get_filename_component(work_dir "${source_dir}" PATH)
  1481. set(comment "Performing download step (SVN checkout) for '${name}'")
  1482. set(svn_user_pw_args "")
  1483. if(DEFINED svn_username)
  1484. set(svn_user_pw_args ${svn_user_pw_args} "--username=${svn_username}")
  1485. endif()
  1486. if(DEFINED svn_password)
  1487. set(svn_user_pw_args ${svn_user_pw_args} "--password=${svn_password}")
  1488. endif()
  1489. if(svn_trust_cert)
  1490. set(svn_trust_cert_args --trust-server-cert)
  1491. endif()
  1492. set(cmd ${Subversion_SVN_EXECUTABLE} co ${svn_repository} ${svn_revision}
  1493. --non-interactive ${svn_trust_cert_args} ${svn_user_pw_args} ${src_name})
  1494. list(APPEND depends ${stamp_dir}/${name}-svninfo.txt)
  1495. elseif(git_repository)
  1496. find_package(Git QUIET)
  1497. if(NOT GIT_EXECUTABLE)
  1498. message(FATAL_ERROR "error: could not find git for clone of ${name}")
  1499. endif()
  1500. # The git submodule update '--recursive' flag requires git >= v1.6.5
  1501. #
  1502. _ep_get_git_version("${GIT_EXECUTABLE}" git_version)
  1503. if(git_version VERSION_LESS 1.6.5)
  1504. message(FATAL_ERROR "error: git version 1.6.5 or later required for 'git submodule update --recursive': git_version='${git_version}'")
  1505. endif()
  1506. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  1507. if(NOT git_tag)
  1508. set(git_tag "master")
  1509. endif()
  1510. get_property(git_submodules TARGET ${name} PROPERTY _EP_GIT_SUBMODULES)
  1511. # For the download step, and the git clone operation, only the repository
  1512. # should be recorded in a configured RepositoryInfo file. If the repo
  1513. # changes, the clone script should be run again. But if only the tag
  1514. # changes, avoid running the clone script again. Let the 'always' running
  1515. # update step checkout the new tag.
  1516. #
  1517. set(repository ${git_repository})
  1518. set(module)
  1519. set(tag)
  1520. configure_file(
  1521. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1522. "${stamp_dir}/${name}-gitinfo.txt"
  1523. @ONLY
  1524. )
  1525. get_filename_component(src_name "${source_dir}" NAME)
  1526. get_filename_component(work_dir "${source_dir}" PATH)
  1527. # Since git clone doesn't succeed if the non-empty source_dir exists,
  1528. # create a cmake script to invoke as download command.
  1529. # The script will delete the source directory and then call git clone.
  1530. #
  1531. _ep_write_gitclone_script(${tmp_dir}/${name}-gitclone.cmake ${source_dir}
  1532. ${GIT_EXECUTABLE} ${git_repository} ${git_tag} "${git_submodules}" ${src_name} ${work_dir}
  1533. ${stamp_dir}/${name}-gitinfo.txt ${stamp_dir}/${name}-gitclone-lastrun.txt
  1534. )
  1535. set(comment "Performing download step (git clone) for '${name}'")
  1536. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitclone.cmake)
  1537. list(APPEND depends ${stamp_dir}/${name}-gitinfo.txt)
  1538. elseif(hg_repository)
  1539. find_package(Hg QUIET)
  1540. if(NOT HG_EXECUTABLE)
  1541. message(FATAL_ERROR "error: could not find hg for clone of ${name}")
  1542. endif()
  1543. get_property(hg_tag TARGET ${name} PROPERTY _EP_HG_TAG)
  1544. if(NOT hg_tag)
  1545. set(hg_tag "tip")
  1546. endif()
  1547. # For the download step, and the hg clone operation, only the repository
  1548. # should be recorded in a configured RepositoryInfo file. If the repo
  1549. # changes, the clone script should be run again. But if only the tag
  1550. # changes, avoid running the clone script again. Let the 'always' running
  1551. # update step checkout the new tag.
  1552. #
  1553. set(repository ${hg_repository})
  1554. set(module)
  1555. set(tag)
  1556. configure_file(
  1557. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1558. "${stamp_dir}/${name}-hginfo.txt"
  1559. @ONLY
  1560. )
  1561. get_filename_component(src_name "${source_dir}" NAME)
  1562. get_filename_component(work_dir "${source_dir}" PATH)
  1563. # Since hg clone doesn't succeed if the non-empty source_dir exists,
  1564. # create a cmake script to invoke as download command.
  1565. # The script will delete the source directory and then call hg clone.
  1566. #
  1567. _ep_write_hgclone_script(${tmp_dir}/${name}-hgclone.cmake ${source_dir}
  1568. ${HG_EXECUTABLE} ${hg_repository} ${hg_tag} ${src_name} ${work_dir}
  1569. ${stamp_dir}/${name}-hginfo.txt ${stamp_dir}/${name}-hgclone-lastrun.txt
  1570. )
  1571. set(comment "Performing download step (hg clone) for '${name}'")
  1572. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-hgclone.cmake)
  1573. list(APPEND depends ${stamp_dir}/${name}-hginfo.txt)
  1574. elseif(url)
  1575. get_filename_component(work_dir "${source_dir}" PATH)
  1576. get_property(hash TARGET ${name} PROPERTY _EP_URL_HASH)
  1577. if(hash AND NOT "${hash}" MATCHES "${_ep_hash_regex}")
  1578. message(FATAL_ERROR "URL_HASH is set to\n ${hash}\n"
  1579. "but must be ALGO=value where ALGO is\n ${_ep_hash_algos}\n"
  1580. "and value is a hex string.")
  1581. endif()
  1582. get_property(md5 TARGET ${name} PROPERTY _EP_URL_MD5)
  1583. if(md5 AND NOT "MD5=${md5}" MATCHES "${_ep_hash_regex}")
  1584. message(FATAL_ERROR "URL_MD5 is set to\n ${md5}\nbut must be a hex string.")
  1585. endif()
  1586. if(md5 AND NOT hash)
  1587. set(hash "MD5=${md5}")
  1588. endif()
  1589. set(repository "external project URL")
  1590. set(module "${url}")
  1591. set(tag "${hash}")
  1592. set(retries 0)
  1593. set(download_script "")
  1594. configure_file(
  1595. "${CMAKE_ROOT}/Modules/RepositoryInfo.txt.in"
  1596. "${stamp_dir}/${name}-urlinfo.txt"
  1597. @ONLY
  1598. )
  1599. list(APPEND depends ${stamp_dir}/${name}-urlinfo.txt)
  1600. if(IS_DIRECTORY "${url}")
  1601. get_filename_component(abs_dir "${url}" ABSOLUTE)
  1602. set(comment "Performing download step (DIR copy) for '${name}'")
  1603. set(cmd ${CMAKE_COMMAND} -E remove_directory ${source_dir}
  1604. COMMAND ${CMAKE_COMMAND} -E copy_directory ${abs_dir} ${source_dir})
  1605. else()
  1606. if("${url}" MATCHES "^[a-z]+://")
  1607. # TODO: Should download and extraction be different steps?
  1608. if("x${fname}" STREQUAL "x")
  1609. string(REGEX MATCH "[^/\\?]*$" fname "${url}")
  1610. endif()
  1611. if(NOT "${fname}" MATCHES "(\\.|=)(7z|tar|tar\\.bz2|tar\\.gz|tar\\.xz|tbz2|tgz|txz|zip)$")
  1612. string(REGEX MATCH "([^/\\?]+(\\.|=)(7z|tar|tar\\.bz2|tar\\.gz|tar\\.xz|tbz2|tgz|txz|zip))/.*$" match_result "${url}")
  1613. set(fname "${CMAKE_MATCH_1}")
  1614. endif()
  1615. if(NOT "${fname}" MATCHES "(\\.|=)(7z|tar|tar\\.bz2|tar\\.gz|tar\\.xz|tbz2|tgz|txz|zip)$")
  1616. message(FATAL_ERROR "Could not extract tarball filename from url:\n ${url}")
  1617. endif()
  1618. string(REPLACE ";" "-" fname "${fname}")
  1619. set(file ${download_dir}/${fname})
  1620. get_property(timeout TARGET ${name} PROPERTY _EP_TIMEOUT)
  1621. get_property(no_progress TARGET ${name} PROPERTY _EP_DOWNLOAD_NO_PROGRESS)
  1622. get_property(tls_verify TARGET ${name} PROPERTY _EP_TLS_VERIFY)
  1623. get_property(tls_cainfo TARGET ${name} PROPERTY _EP_TLS_CAINFO)
  1624. set(download_script "${stamp_dir}/download-${name}.cmake")
  1625. _ep_write_downloadfile_script("${download_script}" "${url}" "${file}" "${timeout}" "${no_progress}" "${hash}" "${tls_verify}" "${tls_cainfo}")
  1626. set(cmd ${CMAKE_COMMAND} -P "${download_script}"
  1627. COMMAND)
  1628. set(retries 3)
  1629. set(comment "Performing download step (download, verify and extract) for '${name}'")
  1630. else()
  1631. set(file "${url}")
  1632. set(comment "Performing download step (verify and extract) for '${name}'")
  1633. endif()
  1634. _ep_write_verifyfile_script("${stamp_dir}/verify-${name}.cmake" "${file}" "${hash}" "${retries}" "${download_script}")
  1635. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/verify-${name}.cmake
  1636. COMMAND)
  1637. _ep_write_extractfile_script("${stamp_dir}/extract-${name}.cmake" "${name}" "${file}" "${source_dir}")
  1638. list(APPEND cmd ${CMAKE_COMMAND} -P ${stamp_dir}/extract-${name}.cmake)
  1639. endif()
  1640. else()
  1641. _ep_is_dir_empty("${source_dir}" empty)
  1642. if(${empty})
  1643. message(SEND_ERROR
  1644. "No download info given for '${name}' and its source directory:\n"
  1645. " ${source_dir}\n"
  1646. "is not an existing non-empty directory. Please specify one of:\n"
  1647. " * SOURCE_DIR with an existing non-empty directory\n"
  1648. " * URL\n"
  1649. " * GIT_REPOSITORY\n"
  1650. " * HG_REPOSITORY\n"
  1651. " * CVS_REPOSITORY and CVS_MODULE\n"
  1652. " * SVN_REVISION\n"
  1653. " * DOWNLOAD_COMMAND"
  1654. )
  1655. endif()
  1656. endif()
  1657. get_property(log TARGET ${name} PROPERTY _EP_LOG_DOWNLOAD)
  1658. if(log)
  1659. set(log LOG 1)
  1660. else()
  1661. set(log "")
  1662. endif()
  1663. ExternalProject_Add_Step(${name} download
  1664. COMMENT ${comment}
  1665. COMMAND ${cmd}
  1666. WORKING_DIRECTORY ${work_dir}
  1667. DEPENDS ${depends}
  1668. DEPENDEES mkdir
  1669. ${log}
  1670. )
  1671. endfunction()
  1672. function(_ep_add_update_command name)
  1673. ExternalProject_Get_Property(${name} source_dir tmp_dir)
  1674. get_property(cmd_set TARGET ${name} PROPERTY _EP_UPDATE_COMMAND SET)
  1675. get_property(cmd TARGET ${name} PROPERTY _EP_UPDATE_COMMAND)
  1676. get_property(cvs_repository TARGET ${name} PROPERTY _EP_CVS_REPOSITORY)
  1677. get_property(svn_repository TARGET ${name} PROPERTY _EP_SVN_REPOSITORY)
  1678. get_property(git_repository TARGET ${name} PROPERTY _EP_GIT_REPOSITORY)
  1679. get_property(hg_repository TARGET ${name} PROPERTY _EP_HG_REPOSITORY )
  1680. get_property(update_disconnected_set TARGET ${name} PROPERTY _EP_UPDATE_DISCONNECTED SET)
  1681. if(update_disconnected_set)
  1682. get_property(update_disconnected TARGET ${name} PROPERTY _EP_UPDATE_DISCONNECTED)
  1683. else()
  1684. get_property(update_disconnected DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED)
  1685. endif()
  1686. set(work_dir)
  1687. set(comment)
  1688. set(always)
  1689. if(cmd_set)
  1690. set(work_dir ${source_dir})
  1691. elseif(cvs_repository)
  1692. if(NOT CVS_EXECUTABLE)
  1693. message(FATAL_ERROR "error: could not find cvs for update of ${name}")
  1694. endif()
  1695. set(work_dir ${source_dir})
  1696. set(comment "Performing update step (CVS update) for '${name}'")
  1697. get_property(cvs_tag TARGET ${name} PROPERTY _EP_CVS_TAG)
  1698. set(cmd ${CVS_EXECUTABLE} -d ${cvs_repository} -q up -dP ${cvs_tag})
  1699. set(always 1)
  1700. elseif(svn_repository)
  1701. if(NOT Subversion_SVN_EXECUTABLE)
  1702. message(FATAL_ERROR "error: could not find svn for update of ${name}")
  1703. endif()
  1704. set(work_dir ${source_dir})
  1705. set(comment "Performing update step (SVN update) for '${name}'")
  1706. get_property(svn_revision TARGET ${name} PROPERTY _EP_SVN_REVISION)
  1707. get_property(svn_username TARGET ${name} PROPERTY _EP_SVN_USERNAME)
  1708. get_property(svn_password TARGET ${name} PROPERTY _EP_SVN_PASSWORD)
  1709. get_property(svn_trust_cert TARGET ${name} PROPERTY _EP_SVN_TRUST_CERT)
  1710. set(svn_user_pw_args "")
  1711. if(DEFINED svn_username)
  1712. set(svn_user_pw_args ${svn_user_pw_args} "--username=${svn_username}")
  1713. endif()
  1714. if(DEFINED svn_password)
  1715. set(svn_user_pw_args ${svn_user_pw_args} "--password=${svn_password}")
  1716. endif()
  1717. if(svn_trust_cert)
  1718. set(svn_trust_cert_args --trust-server-cert)
  1719. endif()
  1720. set(cmd ${Subversion_SVN_EXECUTABLE} up ${svn_revision}
  1721. --non-interactive ${svn_trust_cert_args} ${svn_user_pw_args})
  1722. set(always 1)
  1723. elseif(git_repository)
  1724. if(NOT GIT_EXECUTABLE)
  1725. message(FATAL_ERROR "error: could not find git for fetch of ${name}")
  1726. endif()
  1727. set(work_dir ${source_dir})
  1728. set(comment "Performing update step for '${name}'")
  1729. get_property(git_tag TARGET ${name} PROPERTY _EP_GIT_TAG)
  1730. if(NOT git_tag)
  1731. set(git_tag "master")
  1732. endif()
  1733. get_property(git_submodules TARGET ${name} PROPERTY _EP_GIT_SUBMODULES)
  1734. _ep_write_gitupdate_script(${tmp_dir}/${name}-gitupdate.cmake
  1735. ${GIT_EXECUTABLE} ${git_tag} "${git_submodules}" ${git_repository} ${work_dir}
  1736. )
  1737. set(cmd ${CMAKE_COMMAND} -P ${tmp_dir}/${name}-gitupdate.cmake)
  1738. set(always 1)
  1739. elseif(hg_repository)
  1740. if(NOT HG_EXECUTABLE)
  1741. message(FATAL_ERROR "error: could not find hg for pull of ${name}")
  1742. endif()
  1743. set(work_dir ${source_dir})
  1744. set(comment "Performing update step (hg pull) for '${name}'")
  1745. get_property(hg_tag TARGET ${name} PROPERTY _EP_HG_TAG)
  1746. if(NOT hg_tag)
  1747. set(hg_tag "tip")
  1748. endif()
  1749. if("${HG_VERSION_STRING}" STREQUAL "2.1")
  1750. message(WARNING "Mercurial 2.1 does not distinguish an empty pull from a failed pull:
  1751. http://mercurial.selenic.com/wiki/UpgradeNotes#A2.1.1:_revert_pull_return_code_change.2C_compile_issue_on_OS_X
  1752. http://thread.gmane.org/gmane.comp.version-control.mercurial.devel/47656
  1753. Update to Mercurial >= 2.1.1.
  1754. ")
  1755. endif()
  1756. set(cmd ${HG_EXECUTABLE} pull
  1757. COMMAND ${HG_EXECUTABLE} update ${hg_tag}
  1758. )
  1759. set(always 1)
  1760. endif()
  1761. get_property(log TARGET ${name} PROPERTY _EP_LOG_UPDATE)
  1762. if(log)
  1763. set(log LOG 1)
  1764. else()
  1765. set(log "")
  1766. endif()
  1767. ExternalProject_Add_Step(${name} update
  1768. COMMENT ${comment}
  1769. COMMAND ${cmd}
  1770. ALWAYS ${always}
  1771. EXCLUDE_FROM_MAIN ${update_disconnected}
  1772. WORKING_DIRECTORY ${work_dir}
  1773. DEPENDEES download
  1774. ${log}
  1775. )
  1776. if(always AND update_disconnected)
  1777. _ep_get_step_stampfile(${name} skip-update skip-update_stamp_file)
  1778. string(REPLACE "Performing" "Skipping" comment "${comment}")
  1779. ExternalProject_Add_Step(${name} skip-update
  1780. COMMENT ${comment}
  1781. ALWAYS 1
  1782. EXCLUDE_FROM_MAIN 1
  1783. WORKING_DIRECTORY ${work_dir}
  1784. DEPENDEES download
  1785. ${log}
  1786. )
  1787. set_property(SOURCE ${skip-update_stamp_file} PROPERTY SYMBOLIC 1)
  1788. endif()
  1789. endfunction()
  1790. function(_ep_add_patch_command name)
  1791. ExternalProject_Get_Property(${name} source_dir)
  1792. get_property(cmd_set TARGET ${name} PROPERTY _EP_PATCH_COMMAND SET)
  1793. get_property(cmd TARGET ${name} PROPERTY _EP_PATCH_COMMAND)
  1794. set(work_dir)
  1795. if(cmd_set)
  1796. set(work_dir ${source_dir})
  1797. endif()
  1798. ExternalProject_Add_Step(${name} patch
  1799. COMMAND ${cmd}
  1800. WORKING_DIRECTORY ${work_dir}
  1801. DEPENDEES download
  1802. )
  1803. endfunction()
  1804. # TODO: Make sure external projects use the proper compiler
  1805. function(_ep_add_configure_command name)
  1806. ExternalProject_Get_Property(${name} source_dir binary_dir tmp_dir)
  1807. # Depend on other external projects (file-level).
  1808. set(file_deps)
  1809. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  1810. foreach(dep IN LISTS deps)
  1811. get_property(dep_type TARGET ${dep} PROPERTY TYPE)
  1812. if(dep_type STREQUAL "UTILITY")
  1813. get_property(is_ep TARGET ${dep} PROPERTY _EP_IS_EXTERNAL_PROJECT)
  1814. if(is_ep)
  1815. _ep_get_step_stampfile(${dep} "done" done_stamp_file)
  1816. list(APPEND file_deps ${done_stamp_file})
  1817. endif()
  1818. endif()
  1819. endforeach()
  1820. get_property(cmd_set TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND SET)
  1821. if(cmd_set)
  1822. get_property(cmd TARGET ${name} PROPERTY _EP_CONFIGURE_COMMAND)
  1823. else()
  1824. get_target_property(cmake_command ${name} _EP_CMAKE_COMMAND)
  1825. if(cmake_command)
  1826. set(cmd "${cmake_command}")
  1827. else()
  1828. set(cmd "${CMAKE_COMMAND}")
  1829. endif()
  1830. get_property(cmake_args TARGET ${name} PROPERTY _EP_CMAKE_ARGS)
  1831. list(APPEND cmd ${cmake_args})
  1832. # If there are any CMAKE_CACHE_ARGS or CMAKE_CACHE_DEFAULT_ARGS,
  1833. # write an initial cache and use it
  1834. get_property(cmake_cache_args TARGET ${name} PROPERTY _EP_CMAKE_CACHE_ARGS)
  1835. get_property(cmake_cache_default_args TARGET ${name} PROPERTY _EP_CMAKE_CACHE_DEFAULT_ARGS)
  1836. if(cmake_cache_args OR cmake_cache_default_args)
  1837. set(_ep_cache_args_script "${tmp_dir}/${name}-cache-$<CONFIG>.cmake")
  1838. if(cmake_cache_args)
  1839. _ep_command_line_to_initial_cache(script_initial_cache_force "${cmake_cache_args}" 1)
  1840. endif()
  1841. if(cmake_cache_default_args)
  1842. _ep_command_line_to_initial_cache(script_initial_cache_default "${cmake_cache_default_args}" 0)
  1843. endif()
  1844. _ep_write_initial_cache(${name} "${_ep_cache_args_script}" "${script_initial_cache_force}${script_initial_cache_default}")
  1845. list(APPEND cmd "-C${_ep_cache_args_script}")
  1846. endif()
  1847. get_target_property(cmake_generator ${name} _EP_CMAKE_GENERATOR)
  1848. get_target_property(cmake_generator_platform ${name} _EP_CMAKE_GENERATOR_PLATFORM)
  1849. get_target_property(cmake_generator_toolset ${name} _EP_CMAKE_GENERATOR_TOOLSET)
  1850. if(cmake_generator)
  1851. list(APPEND cmd "-G${cmake_generator}")
  1852. if(cmake_generator_platform)
  1853. list(APPEND cmd "-A${cmake_generator_platform}")
  1854. endif()
  1855. if(cmake_generator_toolset)
  1856. list(APPEND cmd "-T${cmake_generator_toolset}")
  1857. endif()
  1858. else()
  1859. if(CMAKE_EXTRA_GENERATOR)
  1860. list(APPEND cmd "-G${CMAKE_EXTRA_GENERATOR} - ${CMAKE_GENERATOR}")
  1861. else()
  1862. list(APPEND cmd "-G${CMAKE_GENERATOR}")
  1863. endif()
  1864. if(cmake_generator_platform)
  1865. message(FATAL_ERROR "Option CMAKE_GENERATOR_PLATFORM not allowed without CMAKE_GENERATOR.")
  1866. endif()
  1867. if(CMAKE_GENERATOR_PLATFORM)
  1868. list(APPEND cmd "-A${CMAKE_GENERATOR_PLATFORM}")
  1869. endif()
  1870. if(cmake_generator_toolset)
  1871. message(FATAL_ERROR "Option CMAKE_GENERATOR_TOOLSET not allowed without CMAKE_GENERATOR.")
  1872. endif()
  1873. if(CMAKE_GENERATOR_TOOLSET)
  1874. list(APPEND cmd "-T${CMAKE_GENERATOR_TOOLSET}")
  1875. endif()
  1876. endif()
  1877. list(APPEND cmd "${source_dir}")
  1878. endif()
  1879. # If anything about the configure command changes, (command itself, cmake
  1880. # used, cmake args or cmake generator) then re-run the configure step.
  1881. # Fixes issue http://public.kitware.com/Bug/view.php?id=10258
  1882. #
  1883. if(NOT EXISTS ${tmp_dir}/${name}-cfgcmd.txt.in)
  1884. file(WRITE ${tmp_dir}/${name}-cfgcmd.txt.in "cmd='\@cmd\@'\n")
  1885. endif()
  1886. configure_file(${tmp_dir}/${name}-cfgcmd.txt.in ${tmp_dir}/${name}-cfgcmd.txt)
  1887. list(APPEND file_deps ${tmp_dir}/${name}-cfgcmd.txt)
  1888. list(APPEND file_deps ${_ep_cache_args_script})
  1889. get_property(log TARGET ${name} PROPERTY _EP_LOG_CONFIGURE)
  1890. if(log)
  1891. set(log LOG 1)
  1892. else()
  1893. set(log "")
  1894. endif()
  1895. get_property(update_disconnected_set TARGET ${name} PROPERTY _EP_UPDATE_DISCONNECTED SET)
  1896. if(update_disconnected_set)
  1897. get_property(update_disconnected TARGET ${name} PROPERTY _EP_UPDATE_DISCONNECTED)
  1898. else()
  1899. get_property(update_disconnected DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED)
  1900. endif()
  1901. if(update_disconnected)
  1902. set(update_dep skip-update)
  1903. else()
  1904. set(update_dep update)
  1905. endif()
  1906. ExternalProject_Add_Step(${name} configure
  1907. COMMAND ${cmd}
  1908. WORKING_DIRECTORY ${binary_dir}
  1909. DEPENDEES ${update_dep} patch
  1910. DEPENDS ${file_deps}
  1911. ${log}
  1912. )
  1913. endfunction()
  1914. function(_ep_add_build_command name)
  1915. ExternalProject_Get_Property(${name} binary_dir)
  1916. get_property(cmd_set TARGET ${name} PROPERTY _EP_BUILD_COMMAND SET)
  1917. if(cmd_set)
  1918. get_property(cmd TARGET ${name} PROPERTY _EP_BUILD_COMMAND)
  1919. else()
  1920. _ep_get_build_command(${name} BUILD cmd)
  1921. endif()
  1922. get_property(log TARGET ${name} PROPERTY _EP_LOG_BUILD)
  1923. if(log)
  1924. set(log LOG 1)
  1925. else()
  1926. set(log "")
  1927. endif()
  1928. get_property(build_always TARGET ${name} PROPERTY _EP_BUILD_ALWAYS)
  1929. if(build_always)
  1930. set(always 1)
  1931. else()
  1932. set(always 0)
  1933. endif()
  1934. get_property(build_byproducts TARGET ${name} PROPERTY _EP_BUILD_BYPRODUCTS)
  1935. ExternalProject_Add_Step(${name} build
  1936. COMMAND ${cmd}
  1937. BYPRODUCTS ${build_byproducts}
  1938. WORKING_DIRECTORY ${binary_dir}
  1939. DEPENDEES configure
  1940. ALWAYS ${always}
  1941. ${log}
  1942. )
  1943. endfunction()
  1944. function(_ep_add_install_command name)
  1945. ExternalProject_Get_Property(${name} binary_dir)
  1946. get_property(cmd_set TARGET ${name} PROPERTY _EP_INSTALL_COMMAND SET)
  1947. if(cmd_set)
  1948. get_property(cmd TARGET ${name} PROPERTY _EP_INSTALL_COMMAND)
  1949. else()
  1950. _ep_get_build_command(${name} INSTALL cmd)
  1951. endif()
  1952. get_property(log TARGET ${name} PROPERTY _EP_LOG_INSTALL)
  1953. if(log)
  1954. set(log LOG 1)
  1955. else()
  1956. set(log "")
  1957. endif()
  1958. ExternalProject_Add_Step(${name} install
  1959. COMMAND ${cmd}
  1960. WORKING_DIRECTORY ${binary_dir}
  1961. DEPENDEES build
  1962. ${log}
  1963. )
  1964. endfunction()
  1965. function(_ep_add_test_command name)
  1966. ExternalProject_Get_Property(${name} binary_dir)
  1967. get_property(before TARGET ${name} PROPERTY _EP_TEST_BEFORE_INSTALL)
  1968. get_property(after TARGET ${name} PROPERTY _EP_TEST_AFTER_INSTALL)
  1969. get_property(exclude TARGET ${name} PROPERTY _EP_TEST_EXCLUDE_FROM_MAIN)
  1970. get_property(cmd_set TARGET ${name} PROPERTY _EP_TEST_COMMAND SET)
  1971. # Only actually add the test step if one of the test related properties is
  1972. # explicitly set. (i.e. the test step is omitted unless requested...)
  1973. #
  1974. if(cmd_set OR before OR after OR exclude)
  1975. if(cmd_set)
  1976. get_property(cmd TARGET ${name} PROPERTY _EP_TEST_COMMAND)
  1977. else()
  1978. _ep_get_build_command(${name} TEST cmd)
  1979. endif()
  1980. if(before)
  1981. set(dependees_args DEPENDEES build)
  1982. else()
  1983. set(dependees_args DEPENDEES install)
  1984. endif()
  1985. if(exclude)
  1986. set(dependers_args "")
  1987. set(exclude_args EXCLUDE_FROM_MAIN 1)
  1988. else()
  1989. if(before)
  1990. set(dependers_args DEPENDERS install)
  1991. else()
  1992. set(dependers_args "")
  1993. endif()
  1994. set(exclude_args "")
  1995. endif()
  1996. get_property(log TARGET ${name} PROPERTY _EP_LOG_TEST)
  1997. if(log)
  1998. set(log LOG 1)
  1999. else()
  2000. set(log "")
  2001. endif()
  2002. ExternalProject_Add_Step(${name} test
  2003. COMMAND ${cmd}
  2004. WORKING_DIRECTORY ${binary_dir}
  2005. ${dependees_args}
  2006. ${dependers_args}
  2007. ${exclude_args}
  2008. ${log}
  2009. )
  2010. endif()
  2011. endfunction()
  2012. function(ExternalProject_Add name)
  2013. _ep_get_configuration_subdir_suffix(cfgdir)
  2014. # Add a custom target for the external project.
  2015. set(cmf_dir ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles)
  2016. set(complete_stamp_file "${cmf_dir}${cfgdir}/${name}-complete")
  2017. # The "ALL" option to add_custom_target just tells it to not set the
  2018. # EXCLUDE_FROM_ALL target property. Later, if the EXCLUDE_FROM_ALL
  2019. # argument was passed, we explicitly set it for the target.
  2020. add_custom_target(${name} ALL DEPENDS ${complete_stamp_file})
  2021. set_property(TARGET ${name} PROPERTY _EP_IS_EXTERNAL_PROJECT 1)
  2022. set_property(TARGET ${name} PROPERTY LABELS ${name})
  2023. set_property(TARGET ${name} PROPERTY FOLDER "ExternalProjectTargets/${name}")
  2024. _ep_parse_arguments(ExternalProject_Add ${name} _EP_ "${ARGN}")
  2025. _ep_set_directories(${name})
  2026. _ep_get_step_stampfile(${name} "done" done_stamp_file)
  2027. _ep_get_step_stampfile(${name} "install" install_stamp_file)
  2028. # Set the EXCLUDE_FROM_ALL target property if required.
  2029. get_property(exclude_from_all TARGET ${name} PROPERTY _EP_EXCLUDE_FROM_ALL)
  2030. if(exclude_from_all)
  2031. set_property(TARGET ${name} PROPERTY EXCLUDE_FROM_ALL TRUE)
  2032. endif()
  2033. # The 'complete' step depends on all other steps and creates a
  2034. # 'done' mark. A dependent external project's 'configure' step
  2035. # depends on the 'done' mark so that it rebuilds when this project
  2036. # rebuilds. It is important that 'done' is not the output of any
  2037. # custom command so that CMake does not propagate build rules to
  2038. # other external project targets, which may cause problems during
  2039. # parallel builds. However, the Ninja generator needs to see the entire
  2040. # dependency graph, and can cope with custom commands belonging to
  2041. # multiple targets, so we add the 'done' mark as an output for Ninja only.
  2042. set(complete_outputs ${complete_stamp_file})
  2043. if(${CMAKE_GENERATOR} MATCHES "Ninja")
  2044. set(complete_outputs ${complete_outputs} ${done_stamp_file})
  2045. endif()
  2046. add_custom_command(
  2047. OUTPUT ${complete_outputs}
  2048. COMMENT "Completed '${name}'"
  2049. COMMAND ${CMAKE_COMMAND} -E make_directory ${cmf_dir}${cfgdir}
  2050. COMMAND ${CMAKE_COMMAND} -E touch ${complete_stamp_file}
  2051. COMMAND ${CMAKE_COMMAND} -E touch ${done_stamp_file}
  2052. DEPENDS ${install_stamp_file}
  2053. VERBATIM
  2054. )
  2055. # Depend on other external projects (target-level).
  2056. get_property(deps TARGET ${name} PROPERTY _EP_DEPENDS)
  2057. foreach(arg IN LISTS deps)
  2058. add_dependencies(${name} ${arg})
  2059. endforeach()
  2060. # Set up custom build steps based on the target properties.
  2061. # Each step depends on the previous one.
  2062. #
  2063. # The target depends on the output of the final step.
  2064. # (Already set up above in the DEPENDS of the add_custom_target command.)
  2065. #
  2066. _ep_add_mkdir_command(${name})
  2067. _ep_add_download_command(${name})
  2068. _ep_add_update_command(${name})
  2069. _ep_add_patch_command(${name})
  2070. _ep_add_configure_command(${name})
  2071. _ep_add_build_command(${name})
  2072. _ep_add_install_command(${name})
  2073. # Test is special in that it might depend on build, or it might depend
  2074. # on install.
  2075. #
  2076. _ep_add_test_command(${name})
  2077. endfunction()