BundleUtilities.cmake 40 KB

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