ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/InstallRules/BundleUtilities.cmake
Revision: 1.1
Committed: Sat Oct 22 22:38:10 2011 UTC (12 years, 6 months ago) by greg
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2P1, rad5R1, rad4R2, rad4R1, rad5R0
Log Message:
Added qt interface for rvu and cmake files from Bill Hoffman

File Contents

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