BundleUtilities.cmake 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. string(REPLACE "\r" "${eol_char};" info_plist "${info_plist}")
  235. # Scan the lines for "<key>CFBundleExecutable</key>" - the line after that
  236. # is the name of the main executable.
  237. #
  238. foreach(line ${info_plist})
  239. if(line_is_main_executable)
  240. string(REGEX REPLACE "^.*<string>(.*)</string>.*$" "\\1" bundle_executable "${line}")
  241. break()
  242. endif()
  243. if(line MATCHES "<key>CFBundleExecutable</key>")
  244. set(line_is_main_executable 1)
  245. endif()
  246. endforeach()
  247. if(NOT "${bundle_executable}" STREQUAL "")
  248. if(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
  249. set(result "${bundle}/Contents/MacOS/${bundle_executable}")
  250. else()
  251. # Ultimate goal:
  252. # If not in "Contents/MacOS" then scan the bundle for matching files. If
  253. # there is only one executable file that matches, then use it, otherwise
  254. # it's an error...
  255. #
  256. #file(GLOB_RECURSE file_list "${bundle}/${bundle_executable}")
  257. # But for now, pragmatically, it's an error. Expect the main executable
  258. # for the bundle to be in Contents/MacOS, it's an error if it's not:
  259. #
  260. set(result "error: '${bundle}/Contents/MacOS/${bundle_executable}' does not exist")
  261. endif()
  262. endif()
  263. else()
  264. #
  265. # More inclusive technique... (This one would work on Windows and Linux
  266. # too, if a developer followed the typical Mac bundle naming convention...)
  267. #
  268. # If there is no Info.plist file, try to find an executable with the same
  269. # base name as the .app directory:
  270. #
  271. endif()
  272. set(${result_var} "${result}" PARENT_SCOPE)
  273. endfunction()
  274. function(get_dotapp_dir exe dotapp_dir_var)
  275. set(s "${exe}")
  276. if(s MATCHES "/.*\\.app/")
  277. # If there is a ".app" parent directory,
  278. # ascend until we hit it:
  279. # (typical of a Mac bundle executable)
  280. #
  281. set(done 0)
  282. while(NOT ${done})
  283. get_filename_component(snamewe "${s}" NAME_WE)
  284. get_filename_component(sname "${s}" NAME)
  285. get_filename_component(sdir "${s}" PATH)
  286. set(s "${sdir}")
  287. if(sname MATCHES "\\.app$")
  288. set(done 1)
  289. set(dotapp_dir "${sdir}/${sname}")
  290. endif()
  291. endwhile()
  292. else()
  293. # Otherwise use a directory containing the exe
  294. # (typical of a non-bundle executable on Mac, Windows or Linux)
  295. #
  296. is_file_executable("${s}" is_executable)
  297. if(is_executable)
  298. get_filename_component(sdir "${s}" PATH)
  299. set(dotapp_dir "${sdir}")
  300. else()
  301. set(dotapp_dir "${s}")
  302. endif()
  303. endif()
  304. set(${dotapp_dir_var} "${dotapp_dir}" PARENT_SCOPE)
  305. endfunction()
  306. function(get_bundle_and_executable app bundle_var executable_var valid_var)
  307. set(valid 0)
  308. if(EXISTS "${app}")
  309. # Is it a directory ending in .app?
  310. if(IS_DIRECTORY "${app}")
  311. if(app MATCHES "\\.app$")
  312. get_bundle_main_executable("${app}" executable)
  313. if(EXISTS "${app}" AND EXISTS "${executable}")
  314. set(${bundle_var} "${app}" PARENT_SCOPE)
  315. set(${executable_var} "${executable}" PARENT_SCOPE)
  316. set(valid 1)
  317. #message(STATUS "info: handled .app directory case...")
  318. else()
  319. message(STATUS "warning: *NOT* handled - .app directory case...")
  320. endif()
  321. else()
  322. message(STATUS "warning: *NOT* handled - directory but not .app case...")
  323. endif()
  324. else()
  325. # Is it an executable file?
  326. is_file_executable("${app}" is_executable)
  327. if(is_executable)
  328. get_dotapp_dir("${app}" dotapp_dir)
  329. if(EXISTS "${dotapp_dir}")
  330. set(${bundle_var} "${dotapp_dir}" PARENT_SCOPE)
  331. set(${executable_var} "${app}" PARENT_SCOPE)
  332. set(valid 1)
  333. #message(STATUS "info: handled executable file in .app dir case...")
  334. else()
  335. get_filename_component(app_dir "${app}" PATH)
  336. set(${bundle_var} "${app_dir}" PARENT_SCOPE)
  337. set(${executable_var} "${app}" PARENT_SCOPE)
  338. set(valid 1)
  339. #message(STATUS "info: handled executable file in any dir case...")
  340. endif()
  341. else()
  342. message(STATUS "warning: *NOT* handled - not .app dir, not executable file...")
  343. endif()
  344. endif()
  345. else()
  346. message(STATUS "warning: *NOT* handled - directory/file does not exist...")
  347. endif()
  348. if(NOT valid)
  349. set(${bundle_var} "error: not a bundle" PARENT_SCOPE)
  350. set(${executable_var} "error: not a bundle" PARENT_SCOPE)
  351. endif()
  352. set(${valid_var} ${valid} PARENT_SCOPE)
  353. endfunction()
  354. function(get_bundle_all_executables bundle exes_var)
  355. set(exes "")
  356. file(GLOB_RECURSE file_list "${bundle}/*")
  357. foreach(f ${file_list})
  358. is_file_executable("${f}" is_executable)
  359. if(is_executable)
  360. set(exes ${exes} "${f}")
  361. endif()
  362. endforeach()
  363. set(${exes_var} "${exes}" PARENT_SCOPE)
  364. endfunction()
  365. function(get_item_key item key_var)
  366. get_filename_component(item_name "${item}" NAME)
  367. if(WIN32)
  368. string(TOLOWER "${item_name}" item_name)
  369. endif()
  370. string(REPLACE "." "_" ${key_var} "${item_name}")
  371. set(${key_var} ${${key_var}} PARENT_SCOPE)
  372. endfunction()
  373. function(clear_bundle_keys keys_var)
  374. foreach(key ${${keys_var}})
  375. set(${key}_ITEM PARENT_SCOPE)
  376. set(${key}_RESOLVED_ITEM PARENT_SCOPE)
  377. set(${key}_DEFAULT_EMBEDDED_PATH PARENT_SCOPE)
  378. set(${key}_EMBEDDED_ITEM PARENT_SCOPE)
  379. set(${key}_RESOLVED_EMBEDDED_ITEM PARENT_SCOPE)
  380. set(${key}_COPYFLAG PARENT_SCOPE)
  381. endforeach()
  382. set(${keys_var} PARENT_SCOPE)
  383. endfunction()
  384. function(set_bundle_key_values keys_var context item exepath dirs copyflag)
  385. get_filename_component(item_name "${item}" NAME)
  386. get_item_key("${item}" key)
  387. list(LENGTH ${keys_var} length_before)
  388. gp_append_unique(${keys_var} "${key}")
  389. list(LENGTH ${keys_var} length_after)
  390. if(NOT length_before EQUAL length_after)
  391. gp_resolve_item("${context}" "${item}" "${exepath}" "${dirs}" resolved_item)
  392. gp_item_default_embedded_path("${item}" default_embedded_path)
  393. if(item MATCHES "[^/]+\\.framework/")
  394. # For frameworks, construct the name under the embedded path from the
  395. # opening "${item_name}.framework/" to the closing "/${item_name}":
  396. #
  397. string(REGEX REPLACE "^.*(${item_name}.framework/.*/?${item_name}).*$" "${default_embedded_path}/\\1" embedded_item "${item}")
  398. else()
  399. # For other items, just use the same name as the original, but in the
  400. # embedded path:
  401. #
  402. set(embedded_item "${default_embedded_path}/${item_name}")
  403. endif()
  404. # Replace @executable_path and resolve ".." references:
  405. #
  406. string(REPLACE "@executable_path" "${exepath}" resolved_embedded_item "${embedded_item}")
  407. get_filename_component(resolved_embedded_item "${resolved_embedded_item}" ABSOLUTE)
  408. # *But* -- if we are not copying, then force resolved_embedded_item to be
  409. # the same as resolved_item. In the case of multiple executables in the
  410. # original bundle, using the default_embedded_path results in looking for
  411. # the resolved executable next to the main bundle executable. This is here
  412. # so that exes in the other sibling directories (like "bin") get fixed up
  413. # properly...
  414. #
  415. if(NOT copyflag)
  416. set(resolved_embedded_item "${resolved_item}")
  417. endif()
  418. set(${keys_var} ${${keys_var}} PARENT_SCOPE)
  419. set(${key}_ITEM "${item}" PARENT_SCOPE)
  420. set(${key}_RESOLVED_ITEM "${resolved_item}" PARENT_SCOPE)
  421. set(${key}_DEFAULT_EMBEDDED_PATH "${default_embedded_path}" PARENT_SCOPE)
  422. set(${key}_EMBEDDED_ITEM "${embedded_item}" PARENT_SCOPE)
  423. set(${key}_RESOLVED_EMBEDDED_ITEM "${resolved_embedded_item}" PARENT_SCOPE)
  424. set(${key}_COPYFLAG "${copyflag}" PARENT_SCOPE)
  425. else()
  426. #message("warning: item key '${key}' already in the list, subsequent references assumed identical to first")
  427. endif()
  428. endfunction()
  429. function(get_bundle_keys app libs dirs keys_var)
  430. set(${keys_var} PARENT_SCOPE)
  431. get_bundle_and_executable("${app}" bundle executable valid)
  432. if(valid)
  433. # Always use the exepath of the main bundle executable for @executable_path
  434. # replacements:
  435. #
  436. get_filename_component(exepath "${executable}" PATH)
  437. # But do fixups on all executables in the bundle:
  438. #
  439. get_bundle_all_executables("${bundle}" exes)
  440. # For each extra lib, accumulate a key as well and then also accumulate
  441. # any of its prerequisites. (Extra libs are typically dynamically loaded
  442. # plugins: libraries that are prerequisites for full runtime functionality
  443. # but that do not show up in otool -L output...)
  444. #
  445. foreach(lib ${libs})
  446. set_bundle_key_values(${keys_var} "${lib}" "${lib}" "${exepath}" "${dirs}" 0)
  447. set(prereqs "")
  448. get_prerequisites("${lib}" prereqs 1 1 "${exepath}" "${dirs}")
  449. foreach(pr ${prereqs})
  450. set_bundle_key_values(${keys_var} "${lib}" "${pr}" "${exepath}" "${dirs}" 1)
  451. endforeach()
  452. endforeach()
  453. # For each executable found in the bundle, accumulate keys as we go.
  454. # The list of keys should be complete when all prerequisites of all
  455. # binaries in the bundle have been analyzed.
  456. #
  457. foreach(exe ${exes})
  458. # Add the exe itself to the keys:
  459. #
  460. set_bundle_key_values(${keys_var} "${exe}" "${exe}" "${exepath}" "${dirs}" 0)
  461. # Add each prerequisite to the keys:
  462. #
  463. set(prereqs "")
  464. get_prerequisites("${exe}" prereqs 1 1 "${exepath}" "${dirs}")
  465. foreach(pr ${prereqs})
  466. set_bundle_key_values(${keys_var} "${exe}" "${pr}" "${exepath}" "${dirs}" 1)
  467. endforeach()
  468. endforeach()
  469. # Propagate values to caller's scope:
  470. #
  471. set(${keys_var} ${${keys_var}} PARENT_SCOPE)
  472. foreach(key ${${keys_var}})
  473. set(${key}_ITEM "${${key}_ITEM}" PARENT_SCOPE)
  474. set(${key}_RESOLVED_ITEM "${${key}_RESOLVED_ITEM}" PARENT_SCOPE)
  475. set(${key}_DEFAULT_EMBEDDED_PATH "${${key}_DEFAULT_EMBEDDED_PATH}" PARENT_SCOPE)
  476. set(${key}_EMBEDDED_ITEM "${${key}_EMBEDDED_ITEM}" PARENT_SCOPE)
  477. set(${key}_RESOLVED_EMBEDDED_ITEM "${${key}_RESOLVED_EMBEDDED_ITEM}" PARENT_SCOPE)
  478. set(${key}_COPYFLAG "${${key}_COPYFLAG}" PARENT_SCOPE)
  479. endforeach()
  480. endif()
  481. endfunction()
  482. function(copy_resolved_item_into_bundle resolved_item resolved_embedded_item)
  483. if(WIN32)
  484. # ignore case on Windows
  485. string(TOLOWER "${resolved_item}" resolved_item_compare)
  486. string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
  487. else()
  488. set(resolved_item_compare "${resolved_item}")
  489. set(resolved_embedded_item_compare "${resolved_embedded_item}")
  490. endif()
  491. if("${resolved_item_compare}" STREQUAL "${resolved_embedded_item_compare}")
  492. message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
  493. else()
  494. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy ${resolved_item} ${resolved_embedded_item}")
  495. execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
  496. if(UNIX AND NOT APPLE)
  497. file(RPATH_REMOVE FILE "${resolved_embedded_item}")
  498. endif()
  499. endif()
  500. endfunction()
  501. function(copy_resolved_framework_into_bundle resolved_item resolved_embedded_item)
  502. if(WIN32)
  503. # ignore case on Windows
  504. string(TOLOWER "${resolved_item}" resolved_item_compare)
  505. string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
  506. else()
  507. set(resolved_item_compare "${resolved_item}")
  508. set(resolved_embedded_item_compare "${resolved_embedded_item}")
  509. endif()
  510. if("${resolved_item_compare}" STREQUAL "${resolved_embedded_item_compare}")
  511. message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
  512. else()
  513. if(BU_COPY_FULL_FRAMEWORK_CONTENTS)
  514. # Full Framework (everything):
  515. get_filename_component(resolved_dir "${resolved_item}" PATH)
  516. get_filename_component(resolved_dir "${resolved_dir}/../.." ABSOLUTE)
  517. get_filename_component(resolved_embedded_dir "${resolved_embedded_item}" PATH)
  518. get_filename_component(resolved_embedded_dir "${resolved_embedded_dir}/../.." ABSOLUTE)
  519. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy_directory '${resolved_dir}' '${resolved_embedded_dir}'")
  520. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${resolved_dir}" "${resolved_embedded_dir}")
  521. else()
  522. # Framework lib itself:
  523. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy ${resolved_item} ${resolved_embedded_item}")
  524. execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
  525. # Plus Resources, if they exist:
  526. string(REGEX REPLACE "^(.*)/[^/]+/[^/]+/[^/]+$" "\\1/Resources" resolved_resources "${resolved_item}")
  527. string(REGEX REPLACE "^(.*)/[^/]+/[^/]+/[^/]+$" "\\1/Resources" resolved_embedded_resources "${resolved_embedded_item}")
  528. if(EXISTS "${resolved_resources}")
  529. #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy_directory '${resolved_resources}' '${resolved_embedded_resources}'")
  530. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${resolved_resources}" "${resolved_embedded_resources}")
  531. endif()
  532. endif()
  533. if(UNIX AND NOT APPLE)
  534. file(RPATH_REMOVE FILE "${resolved_embedded_item}")
  535. endif()
  536. endif()
  537. endfunction()
  538. function(fixup_bundle_item resolved_embedded_item exepath dirs)
  539. # This item's key is "ikey":
  540. #
  541. get_item_key("${resolved_embedded_item}" ikey)
  542. # Ensure the item is "inside the .app bundle" -- it should not be fixed up if
  543. # it is not in the .app bundle... Otherwise, we'll modify files in the build
  544. # tree, or in other varied locations around the file system, with our call to
  545. # install_name_tool. Make sure that doesn't happen here:
  546. #
  547. get_dotapp_dir("${exepath}" exe_dotapp_dir)
  548. string(LENGTH "${exe_dotapp_dir}/" exe_dotapp_dir_length)
  549. string(LENGTH "${resolved_embedded_item}" resolved_embedded_item_length)
  550. set(path_too_short 0)
  551. set(is_embedded 0)
  552. if(${resolved_embedded_item_length} LESS ${exe_dotapp_dir_length})
  553. set(path_too_short 1)
  554. endif()
  555. if(NOT path_too_short)
  556. string(SUBSTRING "${resolved_embedded_item}" 0 ${exe_dotapp_dir_length} item_substring)
  557. if("${exe_dotapp_dir}/" STREQUAL "${item_substring}")
  558. set(is_embedded 1)
  559. endif()
  560. endif()
  561. if(NOT is_embedded)
  562. message(" exe_dotapp_dir/='${exe_dotapp_dir}/'")
  563. message(" item_substring='${item_substring}'")
  564. message(" resolved_embedded_item='${resolved_embedded_item}'")
  565. message("")
  566. message("Install or copy the item into the bundle before calling fixup_bundle.")
  567. message("Or maybe there's a typo or incorrect path in one of the args to fixup_bundle?")
  568. message("")
  569. message(FATAL_ERROR "cannot fixup an item that is not in the bundle...")
  570. endif()
  571. set(prereqs "")
  572. get_prerequisites("${resolved_embedded_item}" prereqs 1 0 "${exepath}" "${dirs}")
  573. set(changes "")
  574. foreach(pr ${prereqs})
  575. # Each referenced item's key is "rkey" in the loop:
  576. #
  577. get_item_key("${pr}" rkey)
  578. if(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
  579. set(changes ${changes} "-change" "${pr}" "${${rkey}_EMBEDDED_ITEM}")
  580. else()
  581. message("warning: unexpected reference to '${pr}'")
  582. endif()
  583. endforeach()
  584. if(BU_CHMOD_BUNDLE_ITEMS)
  585. execute_process(COMMAND chmod u+w "${resolved_embedded_item}")
  586. endif()
  587. # Change this item's id and all of its references in one call
  588. # to install_name_tool:
  589. #
  590. execute_process(COMMAND install_name_tool
  591. ${changes} -id "${${ikey}_EMBEDDED_ITEM}" "${resolved_embedded_item}"
  592. )
  593. endfunction()
  594. function(fixup_bundle app libs dirs)
  595. message(STATUS "fixup_bundle")
  596. message(STATUS " app='${app}'")
  597. message(STATUS " libs='${libs}'")
  598. message(STATUS " dirs='${dirs}'")
  599. get_bundle_and_executable("${app}" bundle executable valid)
  600. if(valid)
  601. get_filename_component(exepath "${executable}" PATH)
  602. message(STATUS "fixup_bundle: preparing...")
  603. get_bundle_keys("${app}" "${libs}" "${dirs}" keys)
  604. message(STATUS "fixup_bundle: copying...")
  605. list(LENGTH keys n)
  606. math(EXPR n ${n}*2)
  607. set(i 0)
  608. foreach(key ${keys})
  609. math(EXPR i ${i}+1)
  610. if(${${key}_COPYFLAG})
  611. message(STATUS "${i}/${n}: copying '${${key}_RESOLVED_ITEM}'")
  612. else()
  613. message(STATUS "${i}/${n}: *NOT* copying '${${key}_RESOLVED_ITEM}'")
  614. endif()
  615. set(show_status 0)
  616. if(show_status)
  617. message(STATUS "key='${key}'")
  618. message(STATUS "item='${${key}_ITEM}'")
  619. message(STATUS "resolved_item='${${key}_RESOLVED_ITEM}'")
  620. message(STATUS "default_embedded_path='${${key}_DEFAULT_EMBEDDED_PATH}'")
  621. message(STATUS "embedded_item='${${key}_EMBEDDED_ITEM}'")
  622. message(STATUS "resolved_embedded_item='${${key}_RESOLVED_EMBEDDED_ITEM}'")
  623. message(STATUS "copyflag='${${key}_COPYFLAG}'")
  624. message(STATUS "")
  625. endif()
  626. if(${${key}_COPYFLAG})
  627. set(item "${${key}_ITEM}")
  628. if(item MATCHES "[^/]+\\.framework/")
  629. copy_resolved_framework_into_bundle("${${key}_RESOLVED_ITEM}"
  630. "${${key}_RESOLVED_EMBEDDED_ITEM}")
  631. else()
  632. copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
  633. "${${key}_RESOLVED_EMBEDDED_ITEM}")
  634. endif()
  635. endif()
  636. endforeach()
  637. message(STATUS "fixup_bundle: fixing...")
  638. foreach(key ${keys})
  639. math(EXPR i ${i}+1)
  640. if(APPLE)
  641. message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  642. fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")
  643. else()
  644. message(STATUS "${i}/${n}: fix-up not required on this platform '${${key}_RESOLVED_EMBEDDED_ITEM}'")
  645. endif()
  646. endforeach()
  647. message(STATUS "fixup_bundle: cleaning up...")
  648. clear_bundle_keys(keys)
  649. message(STATUS "fixup_bundle: verifying...")
  650. verify_app("${app}")
  651. else()
  652. message(SEND_ERROR "error: fixup_bundle: not a valid bundle")
  653. endif()
  654. message(STATUS "fixup_bundle: done")
  655. endfunction()
  656. function(copy_and_fixup_bundle src dst libs dirs)
  657. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${src}" "${dst}")
  658. fixup_bundle("${dst}" "${libs}" "${dirs}")
  659. endfunction()
  660. function(verify_bundle_prerequisites bundle result_var info_var)
  661. set(result 1)
  662. set(info "")
  663. set(count 0)
  664. get_bundle_main_executable("${bundle}" main_bundle_exe)
  665. file(GLOB_RECURSE file_list "${bundle}/*")
  666. foreach(f ${file_list})
  667. is_file_executable("${f}" is_executable)
  668. if(is_executable)
  669. get_filename_component(exepath "${f}" PATH)
  670. math(EXPR count "${count} + 1")
  671. message(STATUS "executable file ${count}: ${f}")
  672. set(prereqs "")
  673. get_prerequisites("${f}" prereqs 1 1 "${exepath}" "")
  674. # On the Mac,
  675. # "embedded" and "system" prerequisites are fine... anything else means
  676. # the bundle's prerequisites are not verified (i.e., the bundle is not
  677. # really "standalone")
  678. #
  679. # On Windows (and others? Linux/Unix/...?)
  680. # "local" and "system" prereqs are fine...
  681. #
  682. set(external_prereqs "")
  683. foreach(p ${prereqs})
  684. set(p_type "")
  685. gp_file_type("${f}" "${p}" p_type)
  686. if(APPLE)
  687. if(NOT "${p_type}" STREQUAL "embedded" AND NOT "${p_type}" STREQUAL "system")
  688. set(external_prereqs ${external_prereqs} "${p}")
  689. endif()
  690. else()
  691. if(NOT "${p_type}" STREQUAL "local" AND NOT "${p_type}" STREQUAL "system")
  692. set(external_prereqs ${external_prereqs} "${p}")
  693. endif()
  694. endif()
  695. endforeach()
  696. if(external_prereqs)
  697. # Found non-system/somehow-unacceptable prerequisites:
  698. set(result 0)
  699. set(info ${info} "external prerequisites found:\nf='${f}'\nexternal_prereqs='${external_prereqs}'\n")
  700. endif()
  701. endif()
  702. endforeach()
  703. if(result)
  704. set(info "Verified ${count} executable files in '${bundle}'")
  705. endif()
  706. set(${result_var} "${result}" PARENT_SCOPE)
  707. set(${info_var} "${info}" PARENT_SCOPE)
  708. endfunction()
  709. function(verify_bundle_symlinks bundle result_var info_var)
  710. set(result 1)
  711. set(info "")
  712. set(count 0)
  713. # TODO: implement this function for real...
  714. # Right now, it is just a stub that verifies unconditionally...
  715. set(${result_var} "${result}" PARENT_SCOPE)
  716. set(${info_var} "${info}" PARENT_SCOPE)
  717. endfunction()
  718. function(verify_app app)
  719. set(verified 0)
  720. set(info "")
  721. get_bundle_and_executable("${app}" bundle executable valid)
  722. message(STATUS "===========================================================================")
  723. message(STATUS "Analyzing app='${app}'")
  724. message(STATUS "bundle='${bundle}'")
  725. message(STATUS "executable='${executable}'")
  726. message(STATUS "valid='${valid}'")
  727. # Verify that the bundle does not have any "external" prerequisites:
  728. #
  729. verify_bundle_prerequisites("${bundle}" verified info)
  730. message(STATUS "verified='${verified}'")
  731. message(STATUS "info='${info}'")
  732. message(STATUS "")
  733. if(verified)
  734. # Verify that the bundle does not have any symlinks to external files:
  735. #
  736. verify_bundle_symlinks("${bundle}" verified info)
  737. message(STATUS "verified='${verified}'")
  738. message(STATUS "info='${info}'")
  739. message(STATUS "")
  740. endif()
  741. if(NOT verified)
  742. message(FATAL_ERROR "error: verify_app failed")
  743. endif()
  744. endfunction()