BundleUtilities.cmake 41 KB

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