BundleUtilities.cmake 41 KB

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