BundleUtilities.cmake 27 KB

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