BundleUtilities.cmake 28 KB

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