BundleUtilities.cmake 29 KB

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