BundleUtilities.cmake 30 KB

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