BundleUtilities.cmake 24 KB

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