1
0

BundleUtilities.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. # BundleUtilities.cmake
  2. #
  3. # A collection of CMake utility functions useful for dealing with .app bundles
  4. # on the Mac and bundle-like directories on any OS.
  5. #
  6. # The following functions are provided by this script:
  7. # get_bundle_main_executable
  8. # get_dotapp_dir
  9. # get_bundle_and_executable
  10. # get_bundle_all_executables
  11. # get_item_key
  12. # clear_bundle_keys
  13. # set_bundle_key_values
  14. # get_bundle_keys
  15. # copy_resolved_item_into_bundle
  16. # fixup_bundle_item
  17. # fixup_bundle
  18. # copy_and_fixup_bundle
  19. # verify_bundle_prerequisites
  20. # verify_bundle_symlinks
  21. # verify_app
  22. #
  23. # Requires CMake 2.6 or greater because it uses function, break and
  24. # PARENT_SCOPE. Also depends on GetPrerequisites.cmake.
  25. #=============================================================================
  26. # Copyright 2008-2009 Kitware, Inc.
  27. #
  28. # Distributed under the OSI-approved BSD License (the "License");
  29. # see accompanying file Copyright.txt for details.
  30. #
  31. # This software is distributed WITHOUT ANY WARRANTY; without even the
  32. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. # See the License for more information.
  34. #=============================================================================
  35. # (To distributed this file outside of CMake, substitute the full
  36. # License text for the above reference.)
  37. # The functions defined in this file depend on the get_prerequisites function
  38. # (and possibly others) found in:
  39. #
  40. get_filename_component(BundleUtilities_cmake_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
  41. include("${BundleUtilities_cmake_dir}/GetPrerequisites.cmake")
  42. # get_bundle_main_executable
  43. #
  44. # The result will be the full path name of the bundle's main executable file
  45. # or an "error:" prefixed string if it could not be determined.
  46. #
  47. function(get_bundle_main_executable bundle result_var)
  48. set(result "error: '${bundle}/Contents/Info.plist' file does not exist")
  49. if(EXISTS "${bundle}/Contents/Info.plist")
  50. set(result "error: no CFBundleExecutable in '${bundle}/Contents/Info.plist' file")
  51. set(line_is_main_executable 0)
  52. set(bundle_executable "")
  53. # Read Info.plist as a list of lines:
  54. #
  55. set(eol_char "E")
  56. file(READ "${bundle}/Contents/Info.plist" info_plist)
  57. string(REGEX REPLACE ";" "\\\\;" info_plist "${info_plist}")
  58. string(REGEX REPLACE "\n" "${eol_char};" info_plist "${info_plist}")
  59. # Scan the lines for "<key>CFBundleExecutable</key>" - the line after that
  60. # is the name of the main executable.
  61. #
  62. foreach(line ${info_plist})
  63. if(line_is_main_executable)
  64. string(REGEX REPLACE "^.*<string>(.*)</string>.*$" "\\1" bundle_executable "${line}")
  65. break()
  66. endif(line_is_main_executable)
  67. if(line MATCHES "^.*<key>CFBundleExecutable</key>.*$")
  68. set(line_is_main_executable 1)
  69. endif(line MATCHES "^.*<key>CFBundleExecutable</key>.*$")
  70. endforeach(line)
  71. if(NOT "${bundle_executable}" STREQUAL "")
  72. if(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
  73. set(result "${bundle}/Contents/MacOS/${bundle_executable}")
  74. else(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
  75. # Ultimate goal:
  76. # If not in "Contents/MacOS" then scan the bundle for matching files. If
  77. # there is only one executable file that matches, then use it, otherwise
  78. # it's an error...
  79. #
  80. #file(GLOB_RECURSE file_list "${bundle}/${bundle_executable}")
  81. # But for now, pragmatically, it's an error. Expect the main executable
  82. # for the bundle to be in Contents/MacOS, it's an error if it's not:
  83. #
  84. set(result "error: '${bundle}/Contents/MacOS/${bundle_executable}' does not exist")
  85. endif(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
  86. endif(NOT "${bundle_executable}" STREQUAL "")
  87. else(EXISTS "${bundle}/Contents/Info.plist")
  88. #
  89. # More inclusive technique... (This one would work on Windows and Linux
  90. # too, if a developer followed the typical Mac bundle naming convention...)
  91. #
  92. # If there is no Info.plist file, try to find an executable with the same
  93. # base name as the .app directory:
  94. #
  95. endif(EXISTS "${bundle}/Contents/Info.plist")
  96. set(${result_var} "${result}" PARENT_SCOPE)
  97. endfunction(get_bundle_main_executable)
  98. # get_dotapp_dir
  99. #
  100. # Returns the nearest parent dir whose name ends with ".app" given the full path
  101. # to an executable. If there is no such parent dir, then return a dir at the same
  102. # level as the executable, named with the executable's base name and ending with
  103. # ".app"
  104. #
  105. # The returned directory may or may not exist.
  106. #
  107. function(get_dotapp_dir exe dotapp_dir_var)
  108. set(s "${exe}")
  109. set(has_dotapp_parent 0)
  110. if(s MATCHES "^.*/.*\\.app/.*$")
  111. set(has_dotapp_parent 1)
  112. endif(s MATCHES "^.*/.*\\.app/.*$")
  113. set(done 0)
  114. while(NOT ${done})
  115. get_filename_component(snamewe "${s}" NAME_WE)
  116. get_filename_component(sname "${s}" NAME)
  117. get_filename_component(sdir "${s}" PATH)
  118. if(has_dotapp_parent)
  119. # If there is a ".app" parent directory,
  120. # ascend until we hit it:
  121. # (typical of a Mac bundle executable)
  122. #
  123. set(s "${sdir}")
  124. if(sname MATCHES "\\.app$")
  125. set(done 1)
  126. set(dotapp_dir "${sdir}/${sname}")
  127. endif(sname MATCHES "\\.app$")
  128. else(has_dotapp_parent)
  129. # Otherwise use a directory named the same
  130. # as the exe, but with a ".app" extension:
  131. # (typical of a non-bundle executable on Mac, Windows or Linux)
  132. #
  133. set(done 1)
  134. set(dotapp_dir "${sdir}/${snamewe}.app")
  135. endif(has_dotapp_parent)
  136. endwhile(NOT ${done})
  137. set(${dotapp_dir_var} "${dotapp_dir}" PARENT_SCOPE)
  138. endfunction(get_dotapp_dir)
  139. # get_bundle_and_executable
  140. #
  141. # Takes either a ".app" directory name or the name of an executable
  142. # nested inside a ".app" directory and returns the path to the ".app"
  143. # directory in ${bundle_var} and the path to its main executable in
  144. # ${executable_var}
  145. #
  146. function(get_bundle_and_executable app bundle_var executable_var valid_var)
  147. set(valid 0)
  148. if(EXISTS "${app}")
  149. # Is it a directory ending in .app?
  150. if(IS_DIRECTORY "${app}")
  151. if(app MATCHES "\\.app$")
  152. get_bundle_main_executable("${app}" executable)
  153. if(EXISTS "${app}" AND EXISTS "${executable}")
  154. set(${bundle_var} "${app}" PARENT_SCOPE)
  155. set(${executable_var} "${executable}" PARENT_SCOPE)
  156. set(valid 1)
  157. #message(STATUS "info: handled .app directory case...")
  158. else(EXISTS "${app}" AND EXISTS "${executable}")
  159. message(STATUS "warning: *NOT* handled - .app directory case...")
  160. endif(EXISTS "${app}" AND EXISTS "${executable}")
  161. else(app MATCHES "\\.app$")
  162. message(STATUS "warning: *NOT* handled - directory but not .app case...")
  163. endif(app MATCHES "\\.app$")
  164. else(IS_DIRECTORY "${app}")
  165. # Is it an executable file?
  166. is_file_executable("${app}" is_executable)
  167. if(is_executable)
  168. get_dotapp_dir("${app}" dotapp_dir)
  169. if(EXISTS "${dotapp_dir}")
  170. set(${bundle_var} "${dotapp_dir}" PARENT_SCOPE)
  171. set(${executable_var} "${app}" PARENT_SCOPE)
  172. set(valid 1)
  173. #message(STATUS "info: handled executable file in .app dir case...")
  174. else()
  175. get_filename_component(app_dir "${app}" PATH)
  176. set(${bundle_var} "${app_dir}" PARENT_SCOPE)
  177. set(${executable_var} "${app}" PARENT_SCOPE)
  178. set(valid 1)
  179. #message(STATUS "info: handled executable file in any dir case...")
  180. endif()
  181. else(is_executable)
  182. message(STATUS "warning: *NOT* handled - not .app dir, not executable file...")
  183. endif(is_executable)
  184. endif(IS_DIRECTORY "${app}")
  185. else(EXISTS "${app}")
  186. message(STATUS "warning: *NOT* handled - directory/file does not exist...")
  187. endif(EXISTS "${app}")
  188. if(NOT valid)
  189. set(${bundle_var} "error: not a bundle" PARENT_SCOPE)
  190. set(${executable_var} "error: not a bundle" PARENT_SCOPE)
  191. endif(NOT valid)
  192. set(${valid_var} ${valid} PARENT_SCOPE)
  193. endfunction(get_bundle_and_executable)
  194. # get_bundle_all_executables
  195. #
  196. # Scans the given bundle recursively for all executable files and accumulates
  197. # them into a variable.
  198. #
  199. function(get_bundle_all_executables bundle exes_var)
  200. set(exes "")
  201. file(GLOB_RECURSE file_list "${bundle}/*")
  202. foreach(f ${file_list})
  203. is_file_executable("${f}" is_executable)
  204. if(is_executable)
  205. set(exes ${exes} "${f}")
  206. endif(is_executable)
  207. endforeach(f)
  208. set(${exes_var} "${exes}" PARENT_SCOPE)
  209. endfunction(get_bundle_all_executables)
  210. # get_item_key
  211. #
  212. # Given a file (item) name, generate a key that should be unique considering the set of
  213. # libraries that need copying or fixing up to make a bundle standalone. This is
  214. # essentially the file name including extension with "." replaced by "_"
  215. #
  216. # This key is used as a prefix for CMake variables so that we can associate a set
  217. # of variables with a given item based on its key.
  218. #
  219. function(get_item_key item key_var)
  220. get_filename_component(item_name "${item}" NAME)
  221. if(WIN32)
  222. string(TOLOWER "${item_name}" item_name)
  223. endif()
  224. string(REGEX REPLACE "\\." "_" ${key_var} "${item_name}")
  225. set(${key_var} ${${key_var}} PARENT_SCOPE)
  226. endfunction(get_item_key)
  227. # clear_bundle_keys
  228. #
  229. # Loop over the list of keys, clearing all the variables associated with each
  230. # key. After the loop, clear the list of keys itself.
  231. #
  232. # Caller of get_bundle_keys should call clear_bundle_keys when done with list
  233. # of keys.
  234. #
  235. function(clear_bundle_keys keys_var)
  236. foreach(key ${${keys_var}})
  237. set(${key}_ITEM PARENT_SCOPE)
  238. set(${key}_RESOLVED_ITEM PARENT_SCOPE)
  239. set(${key}_DEFAULT_EMBEDDED_PATH PARENT_SCOPE)
  240. set(${key}_EMBEDDED_ITEM PARENT_SCOPE)
  241. set(${key}_RESOLVED_EMBEDDED_ITEM PARENT_SCOPE)
  242. set(${key}_COPYFLAG PARENT_SCOPE)
  243. endforeach(key)
  244. set(${keys_var} PARENT_SCOPE)
  245. endfunction(clear_bundle_keys)
  246. # set_bundle_key_values
  247. #
  248. # Add a key to the list (if necessary) for the given item. If added,
  249. # also set all the variables associated with that key.
  250. #
  251. function(set_bundle_key_values keys_var context item exepath dirs copyflag)
  252. get_filename_component(item_name "${item}" NAME)
  253. get_item_key("${item}" key)
  254. list(LENGTH ${keys_var} length_before)
  255. gp_append_unique(${keys_var} "${key}")
  256. list(LENGTH ${keys_var} length_after)
  257. if(NOT length_before EQUAL length_after)
  258. gp_resolve_item("${context}" "${item}" "${exepath}" "${dirs}" resolved_item)
  259. gp_item_default_embedded_path("${item}" default_embedded_path)
  260. if(item MATCHES "[^/]+\\.framework/")
  261. # For frameworks, construct the name under the embedded path from the
  262. # opening "${item_name}.framework/" to the closing "/${item_name}":
  263. #
  264. string(REGEX REPLACE "^.*(${item_name}.framework/.*/${item_name}).*$" "${default_embedded_path}/\\1" embedded_item "${item}")
  265. else(item MATCHES "[^/]+\\.framework/")
  266. # For other items, just use the same name as the original, but in the
  267. # embedded path:
  268. #
  269. set(embedded_item "${default_embedded_path}/${item_name}")
  270. endif(item MATCHES "[^/]+\\.framework/")
  271. # Replace @executable_path and resolve ".." references:
  272. #
  273. string(REPLACE "@executable_path" "${exepath}" resolved_embedded_item "${embedded_item}")
  274. get_filename_component(resolved_embedded_item "${resolved_embedded_item}" ABSOLUTE)
  275. # *But* -- if we are not copying, then force resolved_embedded_item to be
  276. # the same as resolved_item. In the case of multiple executables in the
  277. # original bundle, using the default_embedded_path results in looking for
  278. # the resolved executable next to the main bundle executable. This is here
  279. # so that exes in the other sibling directories (like "bin") get fixed up
  280. # properly...
  281. #
  282. if(NOT copyflag)
  283. set(resolved_embedded_item "${resolved_item}")
  284. endif(NOT copyflag)
  285. set(${keys_var} ${${keys_var}} PARENT_SCOPE)
  286. set(${key}_ITEM "${item}" PARENT_SCOPE)
  287. set(${key}_RESOLVED_ITEM "${resolved_item}" PARENT_SCOPE)
  288. set(${key}_DEFAULT_EMBEDDED_PATH "${default_embedded_path}" PARENT_SCOPE)
  289. set(${key}_EMBEDDED_ITEM "${embedded_item}" PARENT_SCOPE)
  290. set(${key}_RESOLVED_EMBEDDED_ITEM "${resolved_embedded_item}" PARENT_SCOPE)
  291. set(${key}_COPYFLAG "${copyflag}" PARENT_SCOPE)
  292. else(NOT length_before EQUAL length_after)
  293. #message("warning: item key '${key}' already in the list, subsequent references assumed identical to first")
  294. endif(NOT length_before EQUAL length_after)
  295. endfunction(set_bundle_key_values)
  296. # get_bundle_keys
  297. #
  298. # Loop over all the executable and library files within the bundle (and given as
  299. # extra "${libs}") and accumulate a list of keys representing them. Set values
  300. # associated with each key such that we can loop over all of them and copy
  301. # prerequisite libs into the bundle and then do appropriate install_name_tool
  302. # fixups.
  303. #
  304. function(get_bundle_keys app libs dirs keys_var)
  305. set(${keys_var} PARENT_SCOPE)
  306. get_bundle_and_executable("${app}" bundle executable valid)
  307. if(valid)
  308. # Always use the exepath of the main bundle executable for @executable_path
  309. # replacements:
  310. #
  311. get_filename_component(exepath "${executable}" PATH)
  312. # But do fixups on all executables in the bundle:
  313. #
  314. get_bundle_all_executables("${bundle}" exes)
  315. # For each extra lib, accumulate a key as well and then also accumulate
  316. # any of its prerequisites. (Extra libs are typically dynamically loaded
  317. # plugins: libraries that are prerequisites for full runtime functionality
  318. # but that do not show up in otool -L output...)
  319. #
  320. foreach(lib ${libs})
  321. set_bundle_key_values(${keys_var} "${lib}" "${lib}" "${exepath}" "${dirs}" 1)
  322. set(prereqs "")
  323. get_prerequisites("${lib}" prereqs 1 1 "${exepath}" "${dirs}")
  324. foreach(pr ${prereqs})
  325. set_bundle_key_values(${keys_var} "${lib}" "${pr}" "${exepath}" "${dirs}" 1)
  326. endforeach(pr)
  327. endforeach(lib)
  328. # For each executable found in the bundle, accumulate keys as we go.
  329. # The list of keys should be complete when all prerequisites of all
  330. # binaries in the bundle have been analyzed.
  331. #
  332. foreach(exe ${exes})
  333. # Add the exe itself to the keys:
  334. #
  335. set_bundle_key_values(${keys_var} "${exe}" "${exe}" "${exepath}" "${dirs}" 0)
  336. # Add each prerequisite to the keys:
  337. #
  338. set(prereqs "")
  339. get_prerequisites("${exe}" prereqs 1 1 "${exepath}" "${dirs}")
  340. foreach(pr ${prereqs})
  341. set_bundle_key_values(${keys_var} "${exe}" "${pr}" "${exepath}" "${dirs}" 1)
  342. endforeach(pr)
  343. endforeach(exe)
  344. # Propagate values to caller's scope:
  345. #
  346. set(${keys_var} ${${keys_var}} PARENT_SCOPE)
  347. foreach(key ${${keys_var}})
  348. set(${key}_ITEM "${${key}_ITEM}" PARENT_SCOPE)
  349. set(${key}_RESOLVED_ITEM "${${key}_RESOLVED_ITEM}" PARENT_SCOPE)
  350. set(${key}_DEFAULT_EMBEDDED_PATH "${${key}_DEFAULT_EMBEDDED_PATH}" PARENT_SCOPE)
  351. set(${key}_EMBEDDED_ITEM "${${key}_EMBEDDED_ITEM}" PARENT_SCOPE)
  352. set(${key}_RESOLVED_EMBEDDED_ITEM "${${key}_RESOLVED_EMBEDDED_ITEM}" PARENT_SCOPE)
  353. set(${key}_COPYFLAG "${${key}_COPYFLAG}" PARENT_SCOPE)
  354. endforeach(key)
  355. endif(valid)
  356. endfunction(get_bundle_keys)
  357. # copy_resolved_item_into_bundle
  358. #
  359. # Copy a resolved item into the bundle if necessary. Copy is not necessary if
  360. # the resolved_item is "the same as" the resolved_embedded_item.
  361. #
  362. function(copy_resolved_item_into_bundle resolved_item resolved_embedded_item)
  363. if(WIN32)
  364. # ignore case on Windows
  365. string(TOLOWER "${resolved_item}" resolved_item_compare)
  366. string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
  367. else()
  368. set(resolved_item_compare "${resolved_item}")
  369. set(resolved_embedded_item_compare "${resolved_embedded_item}")
  370. endif()
  371. if("${resolved_item_compare}" STREQUAL "${resolved_embedded_item_compare}")
  372. message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
  373. else()
  374. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy ${resolved_item} ${resolved_embedded_item}")
  375. execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
  376. endif()
  377. if(UNIX AND NOT APPLE)
  378. file(RPATH_REMOVE FILE "${resolved_embedded_item}")
  379. endif(UNIX AND NOT APPLE)
  380. endfunction(copy_resolved_item_into_bundle)
  381. # fixup_bundle_item
  382. #
  383. # Get the direct/non-system prerequisites of the resolved embedded item. For each
  384. # prerequisite, change the way it is referenced to the value of the _EMBEDDED_ITEM
  385. # keyed variable for that prerequisite. (Most likely changing to an "@executable_path"
  386. # style reference.)
  387. #
  388. # Also, change the id of the item being fixed up to its own _EMBEDDED_ITEM value.
  389. #
  390. # Accumulate changes in a local variable and make *one* call to install_name_tool
  391. # at the end of the function with all the changes at once.
  392. #
  393. function(fixup_bundle_item resolved_embedded_item exepath dirs)
  394. # This item's key is "ikey":
  395. #
  396. get_item_key("${resolved_embedded_item}" ikey)
  397. set(prereqs "")
  398. get_prerequisites("${resolved_embedded_item}" prereqs 1 0 "${exepath}" "${dirs}")
  399. set(changes "")
  400. foreach(pr ${prereqs})
  401. # Each referenced item's key is "rkey" in the loop:
  402. #
  403. get_item_key("${pr}" rkey)
  404. if(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
  405. set(changes ${changes} "-change" "${pr}" "${${rkey}_EMBEDDED_ITEM}")
  406. else(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
  407. message("warning: unexpected reference to '${pr}'")
  408. endif(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
  409. endforeach(pr)
  410. # Change this item's id and all of its references in one call
  411. # to install_name_tool:
  412. #
  413. execute_process(COMMAND install_name_tool
  414. ${changes} -id "${${ikey}_EMBEDDED_ITEM}" "${resolved_embedded_item}"
  415. )
  416. endfunction(fixup_bundle_item)
  417. # fixup_bundle
  418. #
  419. # Fix up a bundle in-place and make it standalone, such that it can be drag-n-drop
  420. # copied to another machine and run on that machine as long as all of the system
  421. # libraries are compatible.
  422. #
  423. # Gather all the keys for all the executables and libraries in a bundle, and then,
  424. # for each key, copy each prerequisite into the bundle. Then fix each one up according
  425. # to its own list of prerequisites.
  426. #
  427. # Then clear all the keys and call verify_app on the final bundle to ensure that
  428. # it is truly standalone.
  429. #
  430. function(fixup_bundle app libs dirs)
  431. message(STATUS "fixup_bundle")
  432. message(STATUS " app='${app}'")
  433. message(STATUS " libs='${libs}'")
  434. message(STATUS " dirs='${dirs}'")
  435. get_bundle_and_executable("${app}" bundle executable valid)
  436. if(valid)
  437. get_filename_component(exepath "${executable}" PATH)
  438. message(STATUS "fixup_bundle: preparing...")
  439. get_bundle_keys("${app}" "${libs}" "${dirs}" keys)
  440. message(STATUS "fixup_bundle: copying...")
  441. list(LENGTH keys n)
  442. math(EXPR n ${n}*2)
  443. set(i 0)
  444. foreach(key ${keys})
  445. math(EXPR i ${i}+1)
  446. if(${${key}_COPYFLAG})
  447. message(STATUS "${i}/${n}: copying '${${key}_RESOLVED_ITEM}'")
  448. else(${${key}_COPYFLAG})
  449. message(STATUS "${i}/${n}: *NOT* copying '${${key}_RESOLVED_ITEM}'")
  450. endif(${${key}_COPYFLAG})
  451. set(show_status 0)
  452. if(show_status)
  453. message(STATUS "key='${key}'")
  454. message(STATUS "item='${${key}_ITEM}'")
  455. message(STATUS "resolved_item='${${key}_RESOLVED_ITEM}'")
  456. message(STATUS "default_embedded_path='${${key}_DEFAULT_EMBEDDED_PATH}'")
  457. message(STATUS "embedded_item='${${key}_EMBEDDED_ITEM}'")
  458. message(STATUS "resolved_embedded_item='${${key}_RESOLVED_EMBEDDED_ITEM}'")
  459. message(STATUS "copyflag='${${key}_COPYFLAG}'")
  460. message(STATUS "")
  461. endif(show_status)
  462. if(${${key}_COPYFLAG})
  463. copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
  464. "${${key}_RESOLVED_EMBEDDED_ITEM}")
  465. endif(${${key}_COPYFLAG})
  466. endforeach(key)
  467. message(STATUS "fixup_bundle: fixing...")
  468. foreach(key ${keys})
  469. math(EXPR i ${i}+1)
  470. if(APPLE)
  471. message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  472. fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")
  473. else(APPLE)
  474. message(STATUS "${i}/${n}: fix-up not required on this platform '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  475. endif(APPLE)
  476. endforeach(key)
  477. message(STATUS "fixup_bundle: cleaning up...")
  478. clear_bundle_keys(keys)
  479. message(STATUS "fixup_bundle: verifying...")
  480. verify_app("${app}")
  481. else(valid)
  482. message(SEND_ERROR "error: fixup_bundle: not a valid bundle")
  483. endif(valid)
  484. message(STATUS "fixup_bundle: done")
  485. endfunction(fixup_bundle)
  486. # copy_and_fixup_bundle
  487. #
  488. # Makes a copy of the bundle "src" at location "dst" and then fixes up the
  489. # new copied bundle in-place at "dst"...
  490. #
  491. function(copy_and_fixup_bundle src dst libs dirs)
  492. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${src}" "${dst}")
  493. fixup_bundle("${dst}" "${libs}" "${dirs}")
  494. endfunction(copy_and_fixup_bundle)
  495. # verify_bundle_prerequisites
  496. #
  497. # Verifies that the sum of all prerequisites of all files inside the bundle
  498. # are contained within the bundle or are "system" libraries, presumed to exist
  499. # everywhere.
  500. #
  501. function(verify_bundle_prerequisites bundle result_var info_var)
  502. set(result 1)
  503. set(info "")
  504. set(count 0)
  505. get_bundle_main_executable("${bundle}" main_bundle_exe)
  506. file(GLOB_RECURSE file_list "${bundle}/*")
  507. foreach(f ${file_list})
  508. is_file_executable("${f}" is_executable)
  509. if(is_executable)
  510. get_filename_component(exepath "${f}" PATH)
  511. math(EXPR count "${count} + 1")
  512. message(STATUS "executable file ${count}: ${f}")
  513. set(prereqs "")
  514. get_prerequisites("${f}" prereqs 1 1 "${exepath}" "")
  515. # On the Mac,
  516. # "embedded" and "system" prerequisites are fine... anything else means
  517. # the bundle's prerequisites are not verified (i.e., the bundle is not
  518. # really "standalone")
  519. #
  520. # On Windows (and others? Linux/Unix/...?)
  521. # "local" and "system" prereqs are fine...
  522. #
  523. set(external_prereqs "")
  524. foreach(p ${prereqs})
  525. set(p_type "")
  526. gp_file_type("${f}" "${p}" p_type)
  527. if(APPLE)
  528. if(NOT "${p_type}" STREQUAL "embedded" AND NOT "${p_type}" STREQUAL "system")
  529. set(external_prereqs ${external_prereqs} "${p}")
  530. endif()
  531. else()
  532. if(NOT "${p_type}" STREQUAL "local" AND NOT "${p_type}" STREQUAL "system")
  533. set(external_prereqs ${external_prereqs} "${p}")
  534. endif()
  535. endif()
  536. endforeach(p)
  537. if(external_prereqs)
  538. # Found non-system/somehow-unacceptable prerequisites:
  539. set(result 0)
  540. set(info ${info} "external prerequisites found:\nf='${f}'\nexternal_prereqs='${external_prereqs}'\n")
  541. endif(external_prereqs)
  542. endif(is_executable)
  543. endforeach(f)
  544. if(result)
  545. set(info "Verified ${count} executable files in '${bundle}'")
  546. endif(result)
  547. set(${result_var} "${result}" PARENT_SCOPE)
  548. set(${info_var} "${info}" PARENT_SCOPE)
  549. endfunction(verify_bundle_prerequisites)
  550. # verify_bundle_symlinks
  551. #
  552. # Verifies that any symlinks found in the bundle point to other files that are
  553. # already also in the bundle... Anything that points to an external file causes
  554. # this function to fail the verification.
  555. #
  556. function(verify_bundle_symlinks bundle result_var info_var)
  557. set(result 1)
  558. set(info "")
  559. set(count 0)
  560. # TODO: implement this function for real...
  561. # Right now, it is just a stub that verifies unconditionally...
  562. set(${result_var} "${result}" PARENT_SCOPE)
  563. set(${info_var} "${info}" PARENT_SCOPE)
  564. endfunction(verify_bundle_symlinks)
  565. # verify_app
  566. #
  567. # Verifies that an application appears valid based on running analysis tools on it.
  568. # Calls message/FATAL_ERROR if the application is not verified.
  569. #
  570. function(verify_app app)
  571. set(verified 0)
  572. set(info "")
  573. get_bundle_and_executable("${app}" bundle executable valid)
  574. message(STATUS "===========================================================================")
  575. message(STATUS "Analyzing app='${app}'")
  576. message(STATUS "bundle='${bundle}'")
  577. message(STATUS "executable='${executable}'")
  578. message(STATUS "valid='${valid}'")
  579. # Verify that the bundle does not have any "external" prerequisites:
  580. #
  581. verify_bundle_prerequisites("${bundle}" verified info)
  582. message(STATUS "verified='${verified}'")
  583. message(STATUS "info='${info}'")
  584. message(STATUS "")
  585. if(verified)
  586. # Verify that the bundle does not have any symlinks to external files:
  587. #
  588. verify_bundle_symlinks("${bundle}" verified info)
  589. message(STATUS "verified='${verified}'")
  590. message(STATUS "info='${info}'")
  591. message(STATUS "")
  592. endif(verified)
  593. if(NOT verified)
  594. message(FATAL_ERROR "error: verify_app failed")
  595. endif(NOT verified)
  596. endfunction(verify_app)