1
0

BundleUtilities.cmake 28 KB

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