BundleUtilities.cmake 30 KB

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