BundleUtilities.cmake 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. BundleUtilities
  5. ---------------
  6. This module provides utility commands for assembling standalone,
  7. bundle-style applications with CMake, such as macOS ``.app`` bundles or
  8. similar directory-based application bundles on other operating systems.
  9. Load this module in a CMake installation with:
  10. .. code-block:: cmake
  11. include(BundleUtilities)
  12. .. note::
  13. Do not use this module at configure time (from ``CMakeLists.txt``).
  14. Instead, include it and invoke its commands from an :command:`install(CODE)`
  15. or :command:`install(SCRIPT)`.
  16. Commands
  17. ^^^^^^^^
  18. This module provides the following commands:
  19. * :command:`fixup_bundle`
  20. * :command:`copy_and_fixup_bundle`
  21. * :command:`verify_app`
  22. * :command:`get_bundle_main_executable`
  23. * :command:`get_dotapp_dir`
  24. * :command:`get_bundle_and_executable`
  25. * :command:`get_bundle_all_executables`
  26. * :command:`get_item_key`
  27. * :command:`get_item_rpaths`
  28. * :command:`clear_bundle_keys`
  29. * :command:`set_bundle_key_values`
  30. * :command:`get_bundle_keys`
  31. * :command:`copy_resolved_item_into_bundle`
  32. * :command:`copy_resolved_framework_into_bundle`
  33. * :command:`fixup_bundle_item`
  34. * :command:`verify_bundle_prerequisites`
  35. * :command:`verify_bundle_symlinks`
  36. .. command:: fixup_bundle
  37. Prepares a bundle for distribution by fixing up its internal dependencies:
  38. .. code-block:: cmake
  39. fixup_bundle(<app> <libs> <dirs> [IGNORE_ITEM <files>...])
  40. This command modifies the ``<app>`` bundle in-place to make it
  41. self-contained and portable, so that it can be drag-n-drop copied to
  42. another machine and run there, assuming all of the system libraries are
  43. compatible.
  44. This command collects all dependencies (keys) for the executables and
  45. libraries in the bundle. For each dependency, it copies the required
  46. files into the bundle and adjusts them according to their own
  47. prerequisites. Once complete, it clears the collected keys and invokes
  48. the :command:`verify_app` command to ensure the final bundle is truly
  49. standalone.
  50. The arguments are:
  51. ``<app>``
  52. The path to the bundle to fix. This can be an ``.app`` directory or
  53. direct path to an executable.
  54. ``<libs>``
  55. A list of libraries that must be fixed up, but that cannot be
  56. automatically determined by the ``otool`` output analysis (i.e.
  57. ``plugins``). If plugins are passed to this command as this parameter,
  58. they should be installed or copied into the bundle before calling this
  59. command.
  60. ``<dirs>``
  61. A list of paths where libraries might be found. These paths are searched
  62. first when a target without any path info is given. Then standard system
  63. locations are also searched: ``PATH``, Framework locations, ``/usr/lib``,
  64. etc.
  65. ``IGNORE_ITEM <files>...``
  66. .. versionadded:: 3.6
  67. Optional list of file names to ignore
  68. (e.g. ``IGNORE_ITEM "vcredist_x86.exe;vcredist_x64.exe"``).
  69. .. command:: copy_and_fixup_bundle
  70. Copies the bundle and fixes up the new copied bundle in-place:
  71. .. code-block:: cmake
  72. copy_and_fixup_bundle(<src> <dst> <libs> <dirs>)
  73. This command makes a copy of the bundle ``<src>`` at location ``<dst>``
  74. and then fixes up the new copied bundle in-place at ``<dst>``.
  75. The arguments are:
  76. ``<src>``
  77. The directory of the bundle being copied.
  78. ``<dst>``
  79. The destination directory of the bundle copy.
  80. ``<libs>``
  81. A list of libraries that must be fixed up, but that cannot be
  82. automatically determined by the ``otool`` output analysis (i.e.
  83. ``plugins``). If plugins are passed to this command as this parameter,
  84. they should be installed or copied into the bundle before calling this
  85. command.
  86. ``<dirs>``
  87. A list of paths where libraries might be found. These paths are searched
  88. first when a target without any path info is given. Then standard system
  89. locations are also searched: ``PATH``, Framework locations, ``/usr/lib``,
  90. etc.
  91. .. command:: verify_app
  92. Verifies that an application bundle appears valid based on running analysis
  93. tools on it:
  94. .. code-block:: cmake
  95. verify_app(<app> [IGNORE_ITEM <files>...])
  96. If the application fails verification, a :command:`message(FATAL_ERROR)`
  97. is issued, halting the installation process.
  98. The arguments are:
  99. ``<app>``
  100. The path to the application to verify. This can be a ``.app`` directory
  101. or a standalone executable.
  102. ``IGNORE_ITEM <files>...``
  103. .. versionadded:: 3.6
  104. Optional list of file names to ignore
  105. (e.g. ``IGNORE_ITEM "vcredist_x86.exe;vcredist_x64.exe"``).
  106. .. command:: get_bundle_main_executable
  107. Retrieves the main executable within a given application bundle:
  108. .. code-block:: cmake
  109. get_bundle_main_executable(<bundle> <result-var>)
  110. The result is stored in a ``<result-var>`` variable and will contain a
  111. full path name of the bundle's main executable file, or an ``error:``
  112. prefixed string if it could not be determined.
  113. .. command:: get_dotapp_dir
  114. Locates the enclosing ``.app`` directory for the given executable:
  115. .. code-block:: cmake
  116. get_dotapp_dir(<exe> <dotapp-dir-var>)
  117. This command retrieves the nearest parent dir whose name ends with ``.app``
  118. given the full path to an executable and stores it to the
  119. ``<dotapp-dir-var>`` variable. If there is no such parent dir, then it
  120. simply retrieves the directory containing the executable.
  121. The retrieved directory may or may not exist.
  122. .. command:: get_bundle_and_executable
  123. Takes either a ``.app`` directory name or the name of an executable
  124. nested inside a ``.app`` directory and retrieves the path to the ``.app``
  125. directory and the path to its main executable:
  126. .. code-block:: cmake
  127. get_bundle_and_executable(<app> <bundle-var> <executable-var> <valid-var>)
  128. The arguments are:
  129. ``<app>``
  130. The name of the application being processed.
  131. ``<bundle-var>``
  132. Variable name in which to store the resulting path to the ``.app``
  133. directory. In case of any error, this variable will contain an error
  134. message prefixed with string ``error:``.
  135. ``<executable-var>``
  136. Variable name in which to store the resulting main executable. In case
  137. of any error, this variable will contain an error message prefixed with
  138. string ``error:``.
  139. ``<valid-var>``
  140. Variable name in which the boolean result is stored whether this command
  141. was successful or not.
  142. .. command:: get_bundle_all_executables
  143. Gets all executables of a given bundle:
  144. .. code-block:: cmake
  145. get_bundle_all_executables(<bundle> <exes-var>)
  146. This command scans ``<bundle>`` bundle recursively for all executable
  147. files and stores them into a variable ``<exes-var>``.
  148. .. command:: get_item_key
  149. Generates a unique key for the given item:
  150. .. code-block:: cmake
  151. get_item_key(<item> <key-var>)
  152. Given ``<item>`` file name, this command generates ``<key-var>`` key that
  153. should be unique considering the set of libraries that need copying or
  154. fixing up to make a bundle standalone. This is essentially the file name
  155. including extension with ``.`` replaced by ``_``.
  156. This key is used as a prefix for CMake variables so that a set of
  157. variables can be associated with a given item based on its key.
  158. .. command:: get_item_rpaths
  159. Gets RPATHS (run-time search paths) for the given item:
  160. .. code-block:: cmake
  161. get_item_rpaths(<item> <rpaths-var>)
  162. This command gets RPATHS of the ``<item>`` file name and stores them to
  163. the variable with provided name ``<rpaths-var>``.
  164. .. command:: clear_bundle_keys
  165. Clears all variables associated with keys:
  166. .. code-block:: cmake
  167. clear_bundle_keys(<keys-var>)
  168. This command loops over the ``<keys-var>`` list of keys, clearing all the
  169. variables associated with each key. After the loop, it clears the list of
  170. keys itself. This command should be called after the
  171. :command:`get_bundle_keys` command, when done working with a list of keys.
  172. .. command:: set_bundle_key_values
  173. Adds a key to the list of keys:
  174. .. code-block:: cmake
  175. set_bundle_key_values(
  176. <keys-var>
  177. <context>
  178. <item>
  179. <exepath>
  180. <dirs>
  181. <copyflag>
  182. [<rpaths>]
  183. )
  184. This command adds the ``<keys-var>`` key to the list (if necessary) for
  185. the given item. If added, also set all the variables associated with
  186. that key.
  187. The arguments are:
  188. ``<keys-var>``
  189. Variable name holding the name of the key to be added to the list for
  190. the given item.
  191. ``<context>``
  192. The path to the top level loading path used for ``@loader_path``
  193. replacement on Apple operating systems. When resolving item,
  194. ``@loader_path`` references will be resolved relative to the directory
  195. of the given context value (presumably another library).
  196. ``<item>``
  197. The item for which to add the key.
  198. ``<exepath>``
  199. The path to the top level executable used for ``@executable_path``
  200. replacement on Apple operating systems.
  201. ``<dirs>``
  202. A list of paths where libraries might be found. These paths are searched
  203. first when a target without any path info is given. Then standard system
  204. locations are also searched: ``PATH``, Framework locations, ``/usr/lib``,
  205. etc.
  206. ``<copyflag>``
  207. If set to ``1`` library symlink structure will be preserved.
  208. ``<rpaths>``
  209. Optional run-time search paths for an executable file or library to help
  210. find files.
  211. .. command:: get_bundle_keys
  212. Gets bundle keys:
  213. .. code-block:: cmake
  214. get_bundle_keys(<app> <libs> <dirs> <keys-var> [IGNORE_ITEM <files>...])
  215. This command loops over all the executable and library files within
  216. ``<app>`` bundle (and given as extra ``<libs>``) and accumulate a list of
  217. keys representing them. It sets values associated with each key such
  218. that they can be looped over all of them and copies prerequisite libs into
  219. the bundle and then does appropriate ``install_name_tool`` fixups.
  220. The arguments are:
  221. ``<app>``
  222. The path to the bundle to fix. This can be an ``.app`` directory or
  223. direct path to an executable.
  224. ``<libs>``
  225. A list of libraries that must be fixed up, but that cannot be
  226. automatically determined by the ``otool`` output analysis (i.e.
  227. ``plugins``). If plugins are passed to this command as this parameter,
  228. they should be installed or copied into the bundle before calling this
  229. command.
  230. ``<dirs>``
  231. A list of paths where libraries might be found. These paths are searched
  232. first when a target without any path info is given. Then standard system
  233. locations are also searched: ``PATH``, Framework locations, ``/usr/lib``,
  234. etc.
  235. ``<keys-var>``
  236. Variable name holding a list of keys that represent all executable and
  237. library files within the bundle.
  238. ``IGNORE_ITEM <files>...``
  239. .. versionadded:: 3.6
  240. Optional list of file names to ignore
  241. (e.g. ``IGNORE_ITEM "vcredist_x86.exe;vcredist_x64.exe"``).
  242. .. command:: copy_resolved_item_into_bundle
  243. Copies a resolved item into the bundle if necessary:
  244. .. code-block:: cmake
  245. copy_resolved_item_into_bundle(<resolved-item> <resolved-embedded-item>)
  246. Copy is not necessary, if the ``<resolved-item>`` is "the same as" the
  247. ``<resolved-embedded-item>``.
  248. .. command:: copy_resolved_framework_into_bundle
  249. Copies a resolved framework into the bundle if necessary:
  250. .. code-block:: cmake
  251. copy_resolved_framework_into_bundle(<resolved-item> <resolved-embedded-item>)
  252. Copy is not necessary, if the ``<resolved-item>`` is "the same as" the
  253. ``<resolved-embedded-item>``.
  254. The following variables can be set before invoking this command:
  255. ``BU_COPY_FULL_FRAMEWORK_CONTENTS``
  256. By default, this variable is not set. If full frameworks should be
  257. embedded in the bundles, set this variable to boolean true before calling
  258. the :command:`fixup_bundle` command. By default, this command copies
  259. the framework dylib itself plus the framework ``Resources`` directory.
  260. .. command:: fixup_bundle_item
  261. Fixes up bundle item:
  262. .. code-block:: cmake
  263. fixup_bundle_item(<resolved-embedded-item> <exepath> <dirs>)
  264. This command gets the direct/non-system prerequisites of the
  265. ``<resolved-embedded-item>`` and for each prerequisite, it changes the
  266. way it is referenced to the value of the ``_EMBEDDED_ITEM`` keyed variable
  267. for that prerequisite. Most likely changing to an ``@executable_path``
  268. style reference.
  269. This command requires that the ``<resolved-embedded-item>`` be ``inside``
  270. the bundle already. In other words, if plugins are passed to
  271. :command:`fixup_bundle` command as its ``<libs>`` parameter, they should
  272. be installed or copied into the bundle before calling the
  273. :command:`fixup_bundle` command.
  274. Also, it changes the id of the item being fixed up to its own
  275. ``_EMBEDDED_ITEM`` value.
  276. Changes are accumulated in a local variable and *one* call is made to
  277. ``install_name_tool`` command-line tool at the end of this command with
  278. all the changes at once.
  279. The arguments are:
  280. ``<resolved-embedded-item>``
  281. The bundle item to be fixed up.
  282. ``<exepath>``
  283. The path to the top level executable used for ``@executable_path``
  284. replacement on Apple operating systems.
  285. ``<dirs>``
  286. A list of paths where libraries might be found. These paths are searched
  287. first when a target without any path info is given. Then standard system
  288. locations are also searched: ``PATH``, Framework locations, ``/usr/lib``,
  289. etc.
  290. The following variables can be set before invoking this command:
  291. ``BU_CHMOD_BUNDLE_ITEMS``
  292. If this variable is set to boolean true value then bundle items will be
  293. marked writable before ``install_name_tool`` tool tries to change them.
  294. .. command:: verify_bundle_prerequisites
  295. Verifies that the sum of all prerequisites of all files inside the
  296. bundle are contained within the bundle or are ``system`` libraries,
  297. presumed to exist everywhere:
  298. .. code-block:: cmake
  299. verify_bundle_prerequisites(
  300. <bundle>
  301. <result-var>
  302. <info-var>
  303. [IGNORE_ITEM <files>...]
  304. )
  305. The arguments are:
  306. ``<bundle>``
  307. Name of the bundle being verified.
  308. ``<result-var>``
  309. Name of the variable in which to store a boolean result of whether a
  310. verification was successful.
  311. ``<info-var>``
  312. Name of the variable holding any informational messages produced by the
  313. verification.
  314. ``IGNORE_ITEM <files>...``
  315. .. versionadded:: 3.6
  316. Optional list of file names to ignore
  317. (e.g. ``IGNORE_ITEM "vcredist_x86.exe;vcredist_x64.exe"``).
  318. .. command:: verify_bundle_symlinks
  319. Verifies that any symlinks found in the specified bundle point to other
  320. files that are already also in the bundle:
  321. .. code-block:: cmake
  322. verify_bundle_symlinks(<bundle> <result-var> <info-var>)
  323. Anything that points to an external file causes this command to fail the
  324. verification.
  325. The arguments are:
  326. ``<bundle>``
  327. Name of the bundle being verified.
  328. ``<result-var>``
  329. Name of the variable in which to store a boolean result of whether a
  330. verification was successful.
  331. ``<info-var>``
  332. Name of the variable holding any informational messages produced by the
  333. verification.
  334. Examples
  335. ^^^^^^^^
  336. Using this module inside the installation code that is executed at the
  337. installation phase:
  338. .. code-block:: cmake
  339. :caption: ``CMakeLists.txt``
  340. # ...
  341. install(CODE "
  342. include(BundleUtilities)
  343. set(BU_CHMOD_BUNDLE_ITEMS TRUE)
  344. fixup_bundle(
  345. \"${fixup_exe}\"
  346. \"${plugins}\"
  347. \"${bin_dir};${library_dir};${binary_dir}\"
  348. )
  349. ")
  350. #]=======================================================================]
  351. function(_warn_cmp0080)
  352. cmake_policy(GET_WARNING CMP0080 _cmp0080_warning)
  353. message(AUTHOR_WARNING "${_cmp0080_warning}\n")
  354. endfunction()
  355. # Do not include this module at configure time!
  356. if(DEFINED CMAKE_GENERATOR)
  357. cmake_policy(GET CMP0080 _BundleUtilities_CMP0080)
  358. if(_BundleUtilities_CMP0080 STREQUAL "NEW")
  359. message(FATAL_ERROR "BundleUtilities cannot be included at configure time!")
  360. elseif(NOT _BundleUtilities_CMP0080 STREQUAL "OLD" AND NOT _CMP0080_SUPPRESS_WARNING)
  361. _warn_cmp0080()
  362. endif()
  363. endif()
  364. # The functions defined in this file depend on the get_prerequisites function
  365. # (and possibly others) found in:
  366. #
  367. include("${CMAKE_CURRENT_LIST_DIR}/GetPrerequisites.cmake")
  368. function(get_bundle_main_executable bundle result_var)
  369. set(result "error: '${bundle}/Contents/Info.plist' file does not exist")
  370. if(EXISTS "${bundle}/Contents/Info.plist")
  371. set(result "error: no CFBundleExecutable in '${bundle}/Contents/Info.plist' file")
  372. set(line_is_main_executable 0)
  373. set(bundle_executable "")
  374. # Read Info.plist as a list of lines:
  375. #
  376. set(eol_char "E")
  377. file(READ "${bundle}/Contents/Info.plist" info_plist)
  378. string(REPLACE ";" "\\;" info_plist "${info_plist}")
  379. string(REPLACE "\n" "${eol_char};" info_plist "${info_plist}")
  380. string(REPLACE "\r" "${eol_char};" info_plist "${info_plist}")
  381. # Scan the lines for "<key>CFBundleExecutable</key>" - the line after that
  382. # is the name of the main executable.
  383. #
  384. foreach(line ${info_plist})
  385. if(line_is_main_executable)
  386. string(REGEX REPLACE "^.*<string>(.*)</string>.*$" "\\1" bundle_executable "${line}")
  387. break()
  388. endif()
  389. if(line MATCHES "<key>CFBundleExecutable</key>")
  390. set(line_is_main_executable 1)
  391. endif()
  392. endforeach()
  393. if(NOT bundle_executable STREQUAL "")
  394. if(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
  395. set(result "${bundle}/Contents/MacOS/${bundle_executable}")
  396. else()
  397. # Ultimate goal:
  398. # If not in "Contents/MacOS" then scan the bundle for matching files. If
  399. # there is only one executable file that matches, then use it, otherwise
  400. # it's an error...
  401. #
  402. #file(GLOB_RECURSE file_list "${bundle}/${bundle_executable}")
  403. # But for now, pragmatically, it's an error. Expect the main executable
  404. # for the bundle to be in Contents/MacOS, it's an error if it's not:
  405. #
  406. set(result "error: '${bundle}/Contents/MacOS/${bundle_executable}' does not exist")
  407. endif()
  408. endif()
  409. else()
  410. #
  411. # More inclusive technique... (This one would work on Windows and Linux
  412. # too, if a developer followed the typical Mac bundle naming convention...)
  413. #
  414. # If there is no Info.plist file, try to find an executable with the same
  415. # base name as the .app directory:
  416. #
  417. endif()
  418. set(${result_var} "${result}" PARENT_SCOPE)
  419. endfunction()
  420. function(get_dotapp_dir exe dotapp_dir_var)
  421. set(s "${exe}")
  422. if(s MATCHES "/.*\\.app/")
  423. # If there is a ".app" parent directory,
  424. # ascend until we hit it:
  425. # (typical of a Mac bundle executable)
  426. #
  427. set(done 0)
  428. while(NOT ${done})
  429. get_filename_component(snamewe "${s}" NAME_WE)
  430. get_filename_component(sname "${s}" NAME)
  431. get_filename_component(sdir "${s}" PATH)
  432. set(s "${sdir}")
  433. if(sname MATCHES "\\.app$")
  434. set(done 1)
  435. set(dotapp_dir "${sdir}/${sname}")
  436. endif()
  437. endwhile()
  438. else()
  439. # Otherwise use a directory containing the exe
  440. # (typical of a non-bundle executable on Mac, Windows or Linux)
  441. #
  442. is_file_executable("${s}" is_executable)
  443. if(is_executable)
  444. get_filename_component(sdir "${s}" PATH)
  445. set(dotapp_dir "${sdir}")
  446. else()
  447. set(dotapp_dir "${s}")
  448. endif()
  449. endif()
  450. set(${dotapp_dir_var} "${dotapp_dir}" PARENT_SCOPE)
  451. endfunction()
  452. function(get_bundle_and_executable app bundle_var executable_var valid_var)
  453. set(valid 0)
  454. if(EXISTS "${app}")
  455. # Is it a directory ending in .app?
  456. if(IS_DIRECTORY "${app}")
  457. if(app MATCHES "\\.app$")
  458. get_bundle_main_executable("${app}" executable)
  459. if(EXISTS "${app}" AND EXISTS "${executable}")
  460. set(${bundle_var} "${app}" PARENT_SCOPE)
  461. set(${executable_var} "${executable}" PARENT_SCOPE)
  462. set(valid 1)
  463. #message(STATUS "info: handled .app directory case...")
  464. else()
  465. message(STATUS "warning: *NOT* handled - .app directory case...")
  466. endif()
  467. else()
  468. message(STATUS "warning: *NOT* handled - directory but not .app case...")
  469. endif()
  470. else()
  471. # Is it an executable file?
  472. is_file_executable("${app}" is_executable)
  473. if(is_executable)
  474. get_dotapp_dir("${app}" dotapp_dir)
  475. if(EXISTS "${dotapp_dir}")
  476. set(${bundle_var} "${dotapp_dir}" PARENT_SCOPE)
  477. set(${executable_var} "${app}" PARENT_SCOPE)
  478. set(valid 1)
  479. #message(STATUS "info: handled executable file in .app dir case...")
  480. else()
  481. get_filename_component(app_dir "${app}" PATH)
  482. set(${bundle_var} "${app_dir}" PARENT_SCOPE)
  483. set(${executable_var} "${app}" PARENT_SCOPE)
  484. set(valid 1)
  485. #message(STATUS "info: handled executable file in any dir case...")
  486. endif()
  487. else()
  488. message(STATUS "warning: *NOT* handled - not .app dir, not executable file...")
  489. endif()
  490. endif()
  491. else()
  492. message(STATUS "warning: *NOT* handled - directory/file does not exist...")
  493. endif()
  494. if(NOT valid)
  495. set(${bundle_var} "error: not a bundle" PARENT_SCOPE)
  496. set(${executable_var} "error: not a bundle" PARENT_SCOPE)
  497. endif()
  498. set(${valid_var} ${valid} PARENT_SCOPE)
  499. endfunction()
  500. function(get_bundle_all_executables bundle exes_var)
  501. set(exes "")
  502. if(UNIX)
  503. find_program(find_cmd "find")
  504. mark_as_advanced(find_cmd)
  505. endif()
  506. # find command is much quicker than checking every file one by one on Unix
  507. # which can take long time for large bundles, and since anyway we expect
  508. # executable to have execute flag set we can narrow the list much quicker.
  509. if(find_cmd)
  510. execute_process(COMMAND "${find_cmd}" "${bundle}"
  511. -type f \( -perm -0100 -o -perm -0010 -o -perm -0001 \)
  512. OUTPUT_VARIABLE file_list
  513. OUTPUT_STRIP_TRAILING_WHITESPACE
  514. )
  515. string(REPLACE "\n" ";" file_list "${file_list}")
  516. else()
  517. file(GLOB_RECURSE file_list "${bundle}/*")
  518. endif()
  519. foreach(f ${file_list})
  520. is_file_executable("${f}" is_executable)
  521. if(is_executable)
  522. set(exes ${exes} "${f}")
  523. endif()
  524. endforeach()
  525. set(${exes_var} "${exes}" PARENT_SCOPE)
  526. endfunction()
  527. function(get_item_rpaths item rpaths_var)
  528. if(APPLE)
  529. find_program(otool_cmd "otool")
  530. mark_as_advanced(otool_cmd)
  531. endif()
  532. if(otool_cmd)
  533. execute_process(
  534. COMMAND "${otool_cmd}" -l "${item}"
  535. OUTPUT_VARIABLE load_cmds_ov
  536. RESULT_VARIABLE otool_rv
  537. ERROR_VARIABLE otool_ev
  538. )
  539. if(NOT otool_rv STREQUAL "0")
  540. message(FATAL_ERROR "otool -l failed: ${otool_rv}\n${otool_ev}")
  541. endif()
  542. string(REGEX REPLACE "[^\n]+cmd LC_RPATH\n[^\n]+\n[^\n]+path ([^\n]+) \\(offset[^\n]+\n" "rpath \\1\n" load_cmds_ov "${load_cmds_ov}")
  543. string(REGEX MATCHALL "rpath [^\n]+" load_cmds_ov "${load_cmds_ov}")
  544. string(REGEX REPLACE "rpath " "" load_cmds_ov "${load_cmds_ov}")
  545. if(load_cmds_ov)
  546. foreach(rpath ${load_cmds_ov})
  547. if(NOT rpath IN_LIST ${rpaths_var})
  548. list(APPEND ${rpaths_var} "${rpath}")
  549. endif()
  550. endforeach()
  551. endif()
  552. endif()
  553. if(UNIX AND NOT APPLE)
  554. file(READ_ELF ${item} RPATH rpath_var RUNPATH runpath_var CAPTURE_ERROR error_var)
  555. get_filename_component(item_dir ${item} DIRECTORY)
  556. foreach(rpath ${rpath_var} ${runpath_var})
  557. # Substitute $ORIGIN with the exepath and add to the found rpaths
  558. string(REPLACE "$ORIGIN" "${item_dir}" rpath "${rpath}")
  559. if(NOT rpath IN_LIST ${rpaths_var})
  560. list(APPEND ${rpaths_var} "${rpath}")
  561. endif()
  562. endforeach()
  563. endif()
  564. set(${rpaths_var} ${${rpaths_var}} PARENT_SCOPE)
  565. endfunction()
  566. function(get_item_key item key_var)
  567. get_filename_component(item_name "${item}" NAME)
  568. if(WIN32)
  569. string(TOLOWER "${item_name}" item_name)
  570. endif()
  571. string(REPLACE "." "_" ${key_var} "${item_name}")
  572. set(${key_var} ${${key_var}} PARENT_SCOPE)
  573. endfunction()
  574. function(clear_bundle_keys keys_var)
  575. foreach(key ${${keys_var}})
  576. set(${key}_ITEM PARENT_SCOPE)
  577. set(${key}_RESOLVED_ITEM PARENT_SCOPE)
  578. set(${key}_DEFAULT_EMBEDDED_PATH PARENT_SCOPE)
  579. set(${key}_EMBEDDED_ITEM PARENT_SCOPE)
  580. set(${key}_RESOLVED_EMBEDDED_ITEM PARENT_SCOPE)
  581. set(${key}_COPYFLAG PARENT_SCOPE)
  582. set(${key}_RPATHS PARENT_SCOPE)
  583. endforeach()
  584. set(${keys_var} PARENT_SCOPE)
  585. endfunction()
  586. function(set_bundle_key_values keys_var context item exepath dirs copyflag)
  587. if(ARGC GREATER 6)
  588. set(rpaths "${ARGV6}")
  589. else()
  590. set(rpaths "")
  591. endif()
  592. get_filename_component(item_name "${item}" NAME)
  593. get_item_key("${item}" key)
  594. list(LENGTH ${keys_var} length_before)
  595. if(NOT key IN_LIST ${keys_var})
  596. list(APPEND ${keys_var} "${key}")
  597. endif()
  598. list(LENGTH ${keys_var} length_after)
  599. if(NOT length_before EQUAL length_after)
  600. gp_resolve_item("${context}" "${item}" "${exepath}" "${dirs}" resolved_item "${rpaths}")
  601. gp_item_default_embedded_path("${item}" default_embedded_path)
  602. get_item_rpaths("${resolved_item}" item_rpaths)
  603. if((NOT item MATCHES "\\.dylib$") AND (item MATCHES "[^/]+\\.framework/"))
  604. # For frameworks, construct the name under the embedded path from the
  605. # opening "${item_name}.framework/" to the closing "/${item_name}":
  606. #
  607. string(REGEX REPLACE "^.*(${item_name}.framework/.*/?${item_name}).*$" "${default_embedded_path}/\\1" embedded_item "${item}")
  608. else()
  609. # For other items, just use the same name as the original, but in the
  610. # embedded path:
  611. #
  612. set(embedded_item "${default_embedded_path}/${item_name}")
  613. endif()
  614. # Replace @executable_path and resolve ".." references:
  615. #
  616. string(REPLACE "@executable_path" "${exepath}" resolved_embedded_item "${embedded_item}")
  617. get_filename_component(resolved_embedded_item "${resolved_embedded_item}" ABSOLUTE)
  618. # *But* -- if we are not copying, then force resolved_embedded_item to be
  619. # the same as resolved_item. In the case of multiple executables in the
  620. # original bundle, using the default_embedded_path results in looking for
  621. # the resolved executable next to the main bundle executable. This is here
  622. # so that exes in the other sibling directories (like "bin") get fixed up
  623. # properly...
  624. #
  625. if(NOT copyflag)
  626. set(resolved_embedded_item "${resolved_item}")
  627. endif()
  628. set(${keys_var} ${${keys_var}} PARENT_SCOPE)
  629. set(${key}_ITEM "${item}" PARENT_SCOPE)
  630. set(${key}_RESOLVED_ITEM "${resolved_item}" PARENT_SCOPE)
  631. set(${key}_DEFAULT_EMBEDDED_PATH "${default_embedded_path}" PARENT_SCOPE)
  632. set(${key}_EMBEDDED_ITEM "${embedded_item}" PARENT_SCOPE)
  633. set(${key}_RESOLVED_EMBEDDED_ITEM "${resolved_embedded_item}" PARENT_SCOPE)
  634. set(${key}_COPYFLAG "${copyflag}" PARENT_SCOPE)
  635. set(${key}_RPATHS "${item_rpaths}" PARENT_SCOPE)
  636. set(${key}_RDEP_RPATHS "${rpaths}" PARENT_SCOPE)
  637. else()
  638. #message("warning: item key '${key}' already in the list, subsequent references assumed identical to first")
  639. endif()
  640. endfunction()
  641. function(get_bundle_keys app libs dirs keys_var)
  642. set(${keys_var} PARENT_SCOPE)
  643. set(options)
  644. set(oneValueArgs)
  645. set(multiValueArgs IGNORE_ITEM)
  646. cmake_parse_arguments(CFG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  647. get_bundle_and_executable("${app}" bundle executable valid)
  648. if(valid)
  649. # Always use the exepath of the main bundle executable for @executable_path
  650. # replacements:
  651. #
  652. get_filename_component(exepath "${executable}" PATH)
  653. # But do fixups on all executables in the bundle:
  654. #
  655. get_bundle_all_executables("${bundle}" exes)
  656. # Set keys for main executable first:
  657. #
  658. set_bundle_key_values(${keys_var} "${executable}" "${executable}" "${exepath}" "${dirs}" 0)
  659. # Get rpaths specified by main executable:
  660. #
  661. get_item_key("${executable}" executable_key)
  662. set(main_rpaths "${${executable_key}_RPATHS}")
  663. # For each extra lib, accumulate a key as well and then also accumulate
  664. # any of its prerequisites. (Extra libs are typically dynamically loaded
  665. # plugins: libraries that are prerequisites for full runtime functionality
  666. # but that do not show up in otool -L output...)
  667. #
  668. foreach(lib ${libs})
  669. set_bundle_key_values(${keys_var} "${lib}" "${lib}" "${exepath}" "${dirs}" 0 "${main_rpaths}")
  670. set(prereqs "")
  671. get_filename_component(prereq_filename ${lib} NAME)
  672. if(NOT prereq_filename IN_LIST CFG_IGNORE_ITEM)
  673. get_prerequisites("${lib}" prereqs 1 1 "${exepath}" "${dirs}" "${main_rpaths}")
  674. foreach(pr ${prereqs})
  675. set_bundle_key_values(${keys_var} "${lib}" "${pr}" "${exepath}" "${dirs}" 1 "${main_rpaths}")
  676. endforeach()
  677. else()
  678. message(STATUS "Ignoring file: ${prereq_filename}")
  679. endif()
  680. endforeach()
  681. # For each executable found in the bundle, accumulate keys as we go.
  682. # The list of keys should be complete when all prerequisites of all
  683. # binaries in the bundle have been analyzed.
  684. #
  685. foreach(exe ${exes})
  686. # Main executable is scanned first above:
  687. #
  688. if(NOT exe STREQUAL executable)
  689. # Add the exe itself to the keys:
  690. #
  691. set_bundle_key_values(${keys_var} "${exe}" "${exe}" "${exepath}" "${dirs}" 0 "${main_rpaths}")
  692. # Get rpaths specified by executable:
  693. #
  694. get_item_key("${exe}" exe_key)
  695. set(exe_rpaths "${main_rpaths}" "${${exe_key}_RPATHS}")
  696. else()
  697. set(exe_rpaths "${main_rpaths}")
  698. endif()
  699. # Add each prerequisite to the keys:
  700. #
  701. set(prereqs "")
  702. get_filename_component(prereq_filename ${exe} NAME)
  703. if(NOT prereq_filename IN_LIST CFG_IGNORE_ITEM)
  704. get_prerequisites("${exe}" prereqs 1 1 "${exepath}" "${dirs}" "${exe_rpaths}")
  705. foreach(pr ${prereqs})
  706. set_bundle_key_values(${keys_var} "${exe}" "${pr}" "${exepath}" "${dirs}" 1 "${exe_rpaths}")
  707. endforeach()
  708. else()
  709. message(STATUS "Ignoring file: ${prereq_filename}")
  710. endif()
  711. endforeach()
  712. # preserve library symlink structure
  713. foreach(key ${${keys_var}})
  714. if("${${key}_COPYFLAG}" STREQUAL "1")
  715. if(IS_SYMLINK "${${key}_RESOLVED_ITEM}")
  716. get_filename_component(target "${${key}_RESOLVED_ITEM}" REALPATH)
  717. set_bundle_key_values(${keys_var} "${exe}" "${target}" "${exepath}" "${dirs}" 1 "${exe_rpaths}")
  718. get_item_key("${target}" targetkey)
  719. if(WIN32)
  720. # ignore case on Windows
  721. string(TOLOWER "${${key}_RESOLVED_ITEM}" resolved_item_compare)
  722. string(TOLOWER "${${targetkey}_RESOLVED_EMBEDDED_ITEM}" resolved_embedded_item_compare)
  723. else()
  724. set(resolved_item_compare "${${key}_RESOLVED_ITEM}")
  725. set(resolved_embedded_item_compare "${${targetkey}_RESOLVED_EMBEDDED_ITEM}")
  726. endif()
  727. get_filename_component(resolved_item_compare "${resolved_item_compare}" NAME)
  728. get_filename_component(resolved_embedded_item_compare "${resolved_embedded_item_compare}" NAME)
  729. if(NOT resolved_item_compare STREQUAL resolved_embedded_item_compare)
  730. set(${key}_COPYFLAG "2")
  731. set(${key}_RESOLVED_ITEM "${${targetkey}_RESOLVED_EMBEDDED_ITEM}")
  732. endif()
  733. endif()
  734. endif()
  735. endforeach()
  736. # Propagate values to caller's scope:
  737. #
  738. set(${keys_var} ${${keys_var}} PARENT_SCOPE)
  739. foreach(key ${${keys_var}})
  740. set(${key}_ITEM "${${key}_ITEM}" PARENT_SCOPE)
  741. set(${key}_RESOLVED_ITEM "${${key}_RESOLVED_ITEM}" PARENT_SCOPE)
  742. set(${key}_DEFAULT_EMBEDDED_PATH "${${key}_DEFAULT_EMBEDDED_PATH}" PARENT_SCOPE)
  743. set(${key}_EMBEDDED_ITEM "${${key}_EMBEDDED_ITEM}" PARENT_SCOPE)
  744. set(${key}_RESOLVED_EMBEDDED_ITEM "${${key}_RESOLVED_EMBEDDED_ITEM}" PARENT_SCOPE)
  745. set(${key}_COPYFLAG "${${key}_COPYFLAG}" PARENT_SCOPE)
  746. set(${key}_RPATHS "${${key}_RPATHS}" PARENT_SCOPE)
  747. set(${key}_RDEP_RPATHS "${${key}_RDEP_RPATHS}" PARENT_SCOPE)
  748. endforeach()
  749. endif()
  750. endfunction()
  751. function(link_resolved_item_into_bundle resolved_item resolved_embedded_item)
  752. if(WIN32)
  753. # ignore case on Windows
  754. string(TOLOWER "${resolved_item}" resolved_item_compare)
  755. string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
  756. else()
  757. set(resolved_item_compare "${resolved_item}")
  758. set(resolved_embedded_item_compare "${resolved_embedded_item}")
  759. endif()
  760. if(resolved_item_compare STREQUAL resolved_embedded_item_compare)
  761. message(STATUS "warning: resolved_item == resolved_embedded_item - not linking...")
  762. else()
  763. get_filename_component(target_dir "${resolved_embedded_item}" DIRECTORY)
  764. file(RELATIVE_PATH symlink_target "${target_dir}" "${resolved_item}")
  765. if (NOT EXISTS "${target_dir}")
  766. file(MAKE_DIRECTORY "${target_dir}")
  767. endif()
  768. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${symlink_target}" "${resolved_embedded_item}")
  769. endif()
  770. endfunction()
  771. function(copy_resolved_item_into_bundle resolved_item resolved_embedded_item)
  772. if(WIN32)
  773. # ignore case on Windows
  774. string(TOLOWER "${resolved_item}" resolved_item_compare)
  775. string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
  776. else()
  777. set(resolved_item_compare "${resolved_item}")
  778. set(resolved_embedded_item_compare "${resolved_embedded_item}")
  779. endif()
  780. if(resolved_item_compare STREQUAL resolved_embedded_item_compare)
  781. message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
  782. else()
  783. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy ${resolved_item} ${resolved_embedded_item}")
  784. execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
  785. if(UNIX AND NOT APPLE)
  786. file(RPATH_REMOVE FILE "${resolved_embedded_item}")
  787. endif()
  788. endif()
  789. endfunction()
  790. function(copy_resolved_framework_into_bundle resolved_item resolved_embedded_item)
  791. if(WIN32)
  792. # ignore case on Windows
  793. string(TOLOWER "${resolved_item}" resolved_item_compare)
  794. string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
  795. else()
  796. set(resolved_item_compare "${resolved_item}")
  797. set(resolved_embedded_item_compare "${resolved_embedded_item}")
  798. endif()
  799. if(resolved_item_compare STREQUAL resolved_embedded_item_compare)
  800. message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
  801. else()
  802. if(BU_COPY_FULL_FRAMEWORK_CONTENTS)
  803. # Full Framework (everything):
  804. get_filename_component(resolved_dir "${resolved_item}" PATH)
  805. get_filename_component(resolved_dir "${resolved_dir}/../.." ABSOLUTE)
  806. get_filename_component(resolved_embedded_dir "${resolved_embedded_item}" PATH)
  807. get_filename_component(resolved_embedded_dir "${resolved_embedded_dir}/../.." ABSOLUTE)
  808. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy_directory '${resolved_dir}' '${resolved_embedded_dir}'")
  809. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${resolved_dir}" "${resolved_embedded_dir}")
  810. else()
  811. # Framework lib itself:
  812. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy ${resolved_item} ${resolved_embedded_item}")
  813. execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
  814. # Plus Resources, if they exist:
  815. string(REGEX REPLACE "^(.*)/[^/]+$" "\\1/Resources" resolved_resources "${resolved_item}")
  816. string(REGEX REPLACE "^(.*)/[^/]+$" "\\1/Resources" resolved_embedded_resources "${resolved_embedded_item}")
  817. if(EXISTS "${resolved_resources}")
  818. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy_directory '${resolved_resources}' '${resolved_embedded_resources}'")
  819. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${resolved_resources}" "${resolved_embedded_resources}")
  820. endif()
  821. # Some frameworks e.g. Qt put Info.plist in wrong place, so when it is
  822. # missing in resources, copy it from other well known incorrect locations:
  823. if(NOT EXISTS "${resolved_resources}/Info.plist")
  824. # Check for Contents/Info.plist in framework root (older Qt SDK):
  825. string(REGEX REPLACE "^(.*)/[^/]+/[^/]+/[^/]+$" "\\1/Contents/Info.plist" resolved_info_plist "${resolved_item}")
  826. string(REGEX REPLACE "^(.*)/[^/]+$" "\\1/Resources/Info.plist" resolved_embedded_info_plist "${resolved_embedded_item}")
  827. if(EXISTS "${resolved_info_plist}")
  828. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy_directory '${resolved_info_plist}' '${resolved_embedded_info_plist}'")
  829. execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_info_plist}" "${resolved_embedded_info_plist}")
  830. endif()
  831. endif()
  832. # Check if framework is versioned and fix it layout
  833. string(REGEX REPLACE "^.*/([^/]+)/[^/]+$" "\\1" resolved_embedded_version "${resolved_embedded_item}")
  834. string(REGEX REPLACE "^(.*)/[^/]+/[^/]+$" "\\1" resolved_embedded_versions "${resolved_embedded_item}")
  835. string(REGEX REPLACE "^.*/([^/]+)/[^/]+/[^/]+$" "\\1" resolved_embedded_versions_basename "${resolved_embedded_item}")
  836. if(resolved_embedded_versions_basename STREQUAL "Versions")
  837. # Ensure Current symlink points to the framework version
  838. if(NOT EXISTS "${resolved_embedded_versions}/Current")
  839. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${resolved_embedded_version}" "${resolved_embedded_versions}/Current")
  840. endif()
  841. # Restore symlinks in framework root pointing to current framework
  842. # binary and resources:
  843. string(REGEX REPLACE "^(.*)/[^/]+/[^/]+/[^/]+$" "\\1" resolved_embedded_root "${resolved_embedded_item}")
  844. string(REGEX REPLACE "^.*/([^/]+)$" "\\1" resolved_embedded_item_basename "${resolved_embedded_item}")
  845. if(NOT EXISTS "${resolved_embedded_root}/${resolved_embedded_item_basename}")
  846. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "Versions/Current/${resolved_embedded_item_basename}" "${resolved_embedded_root}/${resolved_embedded_item_basename}")
  847. endif()
  848. if(NOT EXISTS "${resolved_embedded_root}/Resources")
  849. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "Versions/Current/Resources" "${resolved_embedded_root}/Resources")
  850. endif()
  851. endif()
  852. endif()
  853. if(UNIX AND NOT APPLE)
  854. file(RPATH_REMOVE FILE "${resolved_embedded_item}")
  855. endif()
  856. endif()
  857. endfunction()
  858. function(fixup_bundle_item resolved_embedded_item exepath dirs)
  859. # This item's key is "ikey":
  860. #
  861. get_item_key("${resolved_embedded_item}" ikey)
  862. # Ensure the item is "inside the .app bundle" -- it should not be fixed up if
  863. # it is not in the .app bundle... Otherwise, we'll modify files in the build
  864. # tree, or in other varied locations around the file system, with our call to
  865. # install_name_tool. Make sure that doesn't happen here:
  866. #
  867. get_dotapp_dir("${exepath}" exe_dotapp_dir)
  868. string(LENGTH "${exe_dotapp_dir}/" exe_dotapp_dir_length)
  869. string(LENGTH "${resolved_embedded_item}" resolved_embedded_item_length)
  870. set(path_too_short 0)
  871. set(is_embedded 0)
  872. if(resolved_embedded_item_length LESS exe_dotapp_dir_length)
  873. set(path_too_short 1)
  874. endif()
  875. if(NOT path_too_short)
  876. string(SUBSTRING "${resolved_embedded_item}" 0 ${exe_dotapp_dir_length} item_substring)
  877. if("${exe_dotapp_dir}/" STREQUAL item_substring)
  878. set(is_embedded 1)
  879. endif()
  880. endif()
  881. if(NOT is_embedded)
  882. message(" exe_dotapp_dir/='${exe_dotapp_dir}/'")
  883. message(" item_substring='${item_substring}'")
  884. message(" resolved_embedded_item='${resolved_embedded_item}'")
  885. message("")
  886. message("Install or copy the item into the bundle before calling fixup_bundle.")
  887. message("Or maybe there's a typo or incorrect path in one of the args to fixup_bundle?")
  888. message("")
  889. message(FATAL_ERROR "cannot fixup an item that is not in the bundle...")
  890. endif()
  891. set(rpaths "${${ikey}_RPATHS}" "${${ikey}_RDEP_RPATHS}")
  892. set(prereqs "")
  893. get_prerequisites("${resolved_embedded_item}" prereqs 1 0 "${exepath}" "${dirs}" "${rpaths}")
  894. set(changes "")
  895. foreach(pr ${prereqs})
  896. # Each referenced item's key is "rkey" in the loop:
  897. #
  898. get_item_key("${pr}" rkey)
  899. if(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
  900. set(changes ${changes} "-change" "${pr}" "${${rkey}_EMBEDDED_ITEM}")
  901. else()
  902. message("warning: unexpected reference to '${pr}'")
  903. endif()
  904. endforeach()
  905. # CMAKE_INSTALL_NAME_TOOL may not be set if executed in script mode
  906. # Duplicated from CMakeFindBinUtils.cmake
  907. find_program(CMAKE_INSTALL_NAME_TOOL NAMES install_name_tool HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
  908. # Only if install_name_tool supports -delete_rpath:
  909. #
  910. execute_process(COMMAND ${CMAKE_INSTALL_NAME_TOOL}
  911. OUTPUT_VARIABLE install_name_tool_usage
  912. ERROR_VARIABLE install_name_tool_usage
  913. )
  914. if(install_name_tool_usage MATCHES ".*-delete_rpath.*")
  915. foreach(rpath ${${ikey}_RPATHS})
  916. set(changes ${changes} -delete_rpath "${rpath}")
  917. endforeach()
  918. endif()
  919. if(${ikey}_EMBEDDED_ITEM)
  920. set(changes ${changes} -id "${${ikey}_EMBEDDED_ITEM}")
  921. endif()
  922. # Change this item's id and all of its references in one call
  923. # to install_name_tool:
  924. #
  925. if(changes)
  926. # Check for a script by extension (.bat,.sh,...) or if the file starts with "#!" (shebang)
  927. file(READ ${resolved_embedded_item} file_contents LIMIT 5)
  928. if(NOT "${resolved_embedded_item}" MATCHES "\\.(bat|c?sh|bash|ksh|cmd)$" AND
  929. NOT file_contents MATCHES "^#!")
  930. set(cmd ${CMAKE_INSTALL_NAME_TOOL} ${changes} "${resolved_embedded_item}")
  931. if(BU_CHMOD_BUNDLE_ITEMS)
  932. execute_process(COMMAND chmod u+w "${resolved_embedded_item}")
  933. endif()
  934. execute_process(COMMAND ${cmd} RESULT_VARIABLE install_name_tool_result)
  935. if(NOT install_name_tool_result EQUAL 0)
  936. string(REPLACE ";" "' '" msg "'${cmd}'")
  937. message(FATAL_ERROR "Command failed:\n ${msg}")
  938. endif()
  939. endif()
  940. endif()
  941. endfunction()
  942. function(fixup_bundle app libs dirs)
  943. message(STATUS "fixup_bundle")
  944. message(STATUS " app='${app}'")
  945. message(STATUS " libs='${libs}'")
  946. message(STATUS " dirs='${dirs}'")
  947. set(options)
  948. set(oneValueArgs)
  949. set(multiValueArgs IGNORE_ITEM)
  950. cmake_parse_arguments(CFG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  951. message(STATUS " ignoreItems='${CFG_IGNORE_ITEM}'")
  952. get_bundle_and_executable("${app}" bundle executable valid)
  953. if(valid)
  954. get_filename_component(exepath "${executable}" PATH)
  955. message(STATUS "fixup_bundle: preparing...")
  956. get_bundle_keys("${app}" "${libs}" "${dirs}" keys IGNORE_ITEM "${CFG_IGNORE_ITEM}")
  957. message(STATUS "fixup_bundle: copying...")
  958. list(LENGTH keys n)
  959. math(EXPR n ${n}*2)
  960. set(i 0)
  961. foreach(key ${keys})
  962. math(EXPR i ${i}+1)
  963. if("${${key}_COPYFLAG}" STREQUAL "2")
  964. message(STATUS "${i}/${n}: linking '${${key}_RESOLVED_ITEM}' -> '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  965. elseif(${${key}_COPYFLAG})
  966. message(STATUS "${i}/${n}: copying '${${key}_RESOLVED_ITEM}'")
  967. else()
  968. message(STATUS "${i}/${n}: *NOT* copying '${${key}_RESOLVED_ITEM}'")
  969. endif()
  970. set(show_status 0)
  971. if(show_status)
  972. message(STATUS "key='${key}'")
  973. message(STATUS "item='${${key}_ITEM}'")
  974. message(STATUS "resolved_item='${${key}_RESOLVED_ITEM}'")
  975. message(STATUS "default_embedded_path='${${key}_DEFAULT_EMBEDDED_PATH}'")
  976. message(STATUS "embedded_item='${${key}_EMBEDDED_ITEM}'")
  977. message(STATUS "resolved_embedded_item='${${key}_RESOLVED_EMBEDDED_ITEM}'")
  978. message(STATUS "copyflag='${${key}_COPYFLAG}'")
  979. message(STATUS "")
  980. endif()
  981. if("${${key}_COPYFLAG}" STREQUAL "2")
  982. link_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
  983. "${${key}_RESOLVED_EMBEDDED_ITEM}")
  984. elseif(${${key}_COPYFLAG})
  985. set(item "${${key}_ITEM}")
  986. if(item MATCHES "[^/]+\\.framework/")
  987. copy_resolved_framework_into_bundle("${${key}_RESOLVED_ITEM}"
  988. "${${key}_RESOLVED_EMBEDDED_ITEM}")
  989. else()
  990. copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
  991. "${${key}_RESOLVED_EMBEDDED_ITEM}")
  992. endif()
  993. endif()
  994. endforeach()
  995. message(STATUS "fixup_bundle: fixing...")
  996. foreach(key ${keys})
  997. math(EXPR i ${i}+1)
  998. if(APPLE)
  999. message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  1000. if(NOT "${${key}_COPYFLAG}" STREQUAL "2")
  1001. fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")
  1002. endif()
  1003. else()
  1004. message(STATUS "${i}/${n}: fix-up not required on this platform '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  1005. endif()
  1006. endforeach()
  1007. message(STATUS "fixup_bundle: cleaning up...")
  1008. clear_bundle_keys(keys)
  1009. message(STATUS "fixup_bundle: verifying...")
  1010. verify_app("${app}" IGNORE_ITEM "${CFG_IGNORE_ITEM}")
  1011. else()
  1012. message(SEND_ERROR "error: fixup_bundle: not a valid bundle")
  1013. endif()
  1014. message(STATUS "fixup_bundle: done")
  1015. endfunction()
  1016. function(copy_and_fixup_bundle src dst libs dirs)
  1017. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${src}" "${dst}")
  1018. fixup_bundle("${dst}" "${libs}" "${dirs}")
  1019. endfunction()
  1020. function(verify_bundle_prerequisites bundle result_var info_var)
  1021. set(result 1)
  1022. set(info "")
  1023. set(count 0)
  1024. set(options)
  1025. set(oneValueArgs)
  1026. set(multiValueArgs IGNORE_ITEM)
  1027. cmake_parse_arguments(CFG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  1028. get_bundle_main_executable("${bundle}" main_bundle_exe)
  1029. get_bundle_all_executables("${bundle}" file_list)
  1030. foreach(f ${file_list})
  1031. get_filename_component(exepath "${f}" PATH)
  1032. math(EXPR count "${count} + 1")
  1033. message(STATUS "executable file ${count}: ${f}")
  1034. set(prereqs "")
  1035. get_filename_component(prereq_filename ${f} NAME)
  1036. if(NOT prereq_filename IN_LIST CFG_IGNORE_ITEM)
  1037. get_item_rpaths(${f} _main_exe_rpaths)
  1038. get_prerequisites("${f}" prereqs 1 1 "${exepath}" "${_main_exe_rpaths}")
  1039. # On the Mac,
  1040. # "embedded" and "system" prerequisites are fine... anything else means
  1041. # the bundle's prerequisites are not verified (i.e., the bundle is not
  1042. # really "standalone")
  1043. #
  1044. # On Windows (and others? Linux/Unix/...?)
  1045. # "local" and "system" prereqs are fine...
  1046. #
  1047. set(external_prereqs "")
  1048. foreach(p ${prereqs})
  1049. set(p_type "")
  1050. gp_file_type("${f}" "${p}" p_type)
  1051. if(APPLE)
  1052. if(NOT p_type STREQUAL "embedded" AND NOT p_type STREQUAL "system")
  1053. set(external_prereqs ${external_prereqs} "${p}")
  1054. endif()
  1055. else()
  1056. if(NOT p_type STREQUAL "local" AND NOT p_type STREQUAL "system")
  1057. set(external_prereqs ${external_prereqs} "${p}")
  1058. endif()
  1059. endif()
  1060. endforeach()
  1061. if(external_prereqs)
  1062. # Found non-system/somehow-unacceptable prerequisites:
  1063. set(result 0)
  1064. set(info ${info} "external prerequisites found:\nf='${f}'\nexternal_prereqs='${external_prereqs}'\n")
  1065. endif()
  1066. else()
  1067. message(STATUS "Ignoring file: ${prereq_filename}")
  1068. endif()
  1069. endforeach()
  1070. if(result)
  1071. set(info "Verified ${count} executable files in '${bundle}'")
  1072. endif()
  1073. set(${result_var} "${result}" PARENT_SCOPE)
  1074. set(${info_var} "${info}" PARENT_SCOPE)
  1075. endfunction()
  1076. function(verify_bundle_symlinks bundle result_var info_var)
  1077. set(result 1)
  1078. set(info "")
  1079. set(count 0)
  1080. # TODO: implement this function for real...
  1081. # Right now, it is just a stub that verifies unconditionally...
  1082. set(${result_var} "${result}" PARENT_SCOPE)
  1083. set(${info_var} "${info}" PARENT_SCOPE)
  1084. endfunction()
  1085. function(verify_app app)
  1086. set(verified 0)
  1087. set(info "")
  1088. set(options)
  1089. set(oneValueArgs)
  1090. set(multiValueArgs IGNORE_ITEM)
  1091. cmake_parse_arguments(CFG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  1092. get_bundle_and_executable("${app}" bundle executable valid)
  1093. message(STATUS "===========================================================================")
  1094. message(STATUS "Analyzing app='${app}'")
  1095. message(STATUS "bundle='${bundle}'")
  1096. message(STATUS "executable='${executable}'")
  1097. message(STATUS "valid='${valid}'")
  1098. # Verify that the bundle does not have any "external" prerequisites:
  1099. #
  1100. verify_bundle_prerequisites("${bundle}" verified info IGNORE_ITEM "${CFG_IGNORE_ITEM}")
  1101. message(STATUS "verified='${verified}'")
  1102. message(STATUS "info='${info}'")
  1103. message(STATUS "")
  1104. if(verified)
  1105. # Verify that the bundle does not have any symlinks to external files:
  1106. #
  1107. verify_bundle_symlinks("${bundle}" verified info)
  1108. message(STATUS "verified='${verified}'")
  1109. message(STATUS "info='${info}'")
  1110. message(STATUS "")
  1111. endif()
  1112. if(NOT verified)
  1113. message(FATAL_ERROR "error: verify_app failed")
  1114. endif()
  1115. endfunction()