BundleUtilities.cmake 34 KB

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