1 # BundleUtilities.cmake
3 # A collection of CMake utility functions useful for dealing with .app bundles
4 # on the Mac and bundle-like directories on any OS.
6 # The following functions are provided by this script:
7 # get_bundle_main_executable
9 # get_bundle_and_executable
10 # get_bundle_all_executables
13 # set_bundle_key_values
15 # copy_resolved_item_into_bundle
18 # copy_and_fixup_bundle
19 # verify_bundle_prerequisites
20 # verify_bundle_symlinks
23 # Requires CMake 2.6 or greater because it uses function, break and
24 # PARENT_SCOPE. Also depends on GetPrerequisites.cmake.
27 # The functions defined in this file depend on the get_prerequisites function
28 # (and possibly others) found in:
30 get_filename_component(BundleUtilities_cmake_dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
31 include("${BundleUtilities_cmake_dir}/GetPrerequisites.cmake")
34 # get_bundle_main_executable
36 # The result will be the full path name of the bundle's main executable file
37 # or an "error:" prefixed string if it could not be determined.
39 function(get_bundle_main_executable bundle result_var)
40 set(result "error: '${bundle}/Contents/Info.plist' file does not exist")
42 if(EXISTS "${bundle}/Contents/Info.plist")
43 set(result "error: no CFBundleExecutable in '${bundle}/Contents/Info.plist' file")
44 set(line_is_main_executable 0)
45 set(bundle_executable "")
47 # Read Info.plist as a list of lines:
50 file(READ "${bundle}/Contents/Info.plist" info_plist)
51 string(REGEX REPLACE ";" "\\\\;" info_plist "${info_plist}")
52 string(REGEX REPLACE "\n" "${eol_char};" info_plist "${info_plist}")
54 # Scan the lines for "<key>CFBundleExecutable</key>" - the line after that
55 # is the name of the main executable.
57 foreach(line ${info_plist})
58 if(line_is_main_executable)
59 string(REGEX REPLACE "^.*<string>(.*)</string>.*$" "\\1" bundle_executable "${line}")
61 endif(line_is_main_executable)
63 if(line MATCHES "^.*<key>CFBundleExecutable</key>.*$")
64 set(line_is_main_executable 1)
65 endif(line MATCHES "^.*<key>CFBundleExecutable</key>.*$")
68 if(NOT "${bundle_executable}" STREQUAL "")
69 if(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
70 set(result "${bundle}/Contents/MacOS/${bundle_executable}")
71 else(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
74 # If not in "Contents/MacOS" then scan the bundle for matching files. If
75 # there is only one executable file that matches, then use it, otherwise
78 #file(GLOB_RECURSE file_list "${bundle}/${bundle_executable}")
80 # But for now, pragmatically, it's an error. Expect the main executable
81 # for the bundle to be in Contents/MacOS, it's an error if it's not:
83 set(result "error: '${bundle}/Contents/MacOS/${bundle_executable}' does not exist")
84 endif(EXISTS "${bundle}/Contents/MacOS/${bundle_executable}")
85 endif(NOT "${bundle_executable}" STREQUAL "")
86 else(EXISTS "${bundle}/Contents/Info.plist")
88 # More inclusive technique... (This one would work on Windows and Linux
89 # too, if a developer followed the typical Mac bundle naming convention...)
91 # If there is no Info.plist file, try to find an executable with the same
92 # base name as the .app directory:
94 endif(EXISTS "${bundle}/Contents/Info.plist")
96 set(${result_var} "${result}" PARENT_SCOPE)
97 endfunction(get_bundle_main_executable)
102 # Returns the nearest parent dir whose name ends with ".app" given the full path
103 # to an executable. If there is no such parent dir, then return a dir at the same
104 # level as the executable, named with the executable's base name and ending with
107 # The returned directory may or may not exist.
109 function(get_dotapp_dir exe dotapp_dir_var)
112 set(has_dotapp_parent 0)
113 if(s MATCHES "^.*/.*\\.app/.*$")
114 set(has_dotapp_parent 1)
115 endif(s MATCHES "^.*/.*\\.app/.*$")
119 get_filename_component(snamewe "${s}" NAME_WE)
120 get_filename_component(sname "${s}" NAME)
121 get_filename_component(sdir "${s}" PATH)
122 if(has_dotapp_parent)
123 # If there is a ".app" parent directory,
124 # ascend until we hit it:
125 # (typical of a Mac bundle executable)
128 if(sname MATCHES "\\.app$")
130 set(dotapp_dir "${sdir}/${sname}")
131 endif(sname MATCHES "\\.app$")
132 else(has_dotapp_parent)
133 # Otherwise use a directory named the same
134 # as the exe, but with a ".app" extension:
135 # (typical of a non-bundle executable on Mac, Windows or Linux)
138 set(dotapp_dir "${sdir}/${snamewe}.app")
139 endif(has_dotapp_parent)
140 endwhile(NOT ${done})
142 set(${dotapp_dir_var} "${dotapp_dir}" PARENT_SCOPE)
143 endfunction(get_dotapp_dir)
146 # get_bundle_and_executable
148 # Takes either a ".app" directory name or the name of an executable
149 # nested inside a ".app" directory and returns the path to the ".app"
150 # directory in ${bundle_var} and the path to its main executable in
153 function(get_bundle_and_executable app bundle_var executable_var valid_var)
157 # Is it a directory ending in .app?
158 if(IS_DIRECTORY "${app}")
159 if(app MATCHES "\\.app$")
160 get_bundle_main_executable("${app}" executable)
161 if(EXISTS "${app}" AND EXISTS "${executable}")
162 set(${bundle_var} "${app}" PARENT_SCOPE)
163 set(${executable_var} "${executable}" PARENT_SCOPE)
165 #message(STATUS "info: handled .app directory case...")
166 else(EXISTS "${app}" AND EXISTS "${executable}")
167 message(STATUS "warning: *NOT* handled - .app directory case...")
168 endif(EXISTS "${app}" AND EXISTS "${executable}")
169 else(app MATCHES "\\.app$")
170 message(STATUS "warning: *NOT* handled - directory but not .app case...")
171 endif(app MATCHES "\\.app$")
172 else(IS_DIRECTORY "${app}")
173 # Is it an executable file?
174 is_file_executable("${app}" is_executable)
176 get_dotapp_dir("${app}" dotapp_dir)
177 if(EXISTS "${dotapp_dir}")
178 set(${bundle_var} "${dotapp_dir}" PARENT_SCOPE)
179 set(${executable_var} "${app}" PARENT_SCOPE)
181 #message(STATUS "info: handled executable file in .app dir case...")
183 get_filename_component(app_dir "${app}" PATH)
184 set(${bundle_var} "${app_dir}" PARENT_SCOPE)
185 set(${executable_var} "${app}" PARENT_SCOPE)
187 #message(STATUS "info: handled executable file in any dir case...")
190 message(STATUS "warning: *NOT* handled - not .app dir, not executable file...")
192 endif(IS_DIRECTORY "${app}")
193 else(EXISTS "${app}")
194 message(STATUS "warning: *NOT* handled - directory/file does not exist...")
195 endif(EXISTS "${app}")
198 set(${bundle_var} "error: not a bundle" PARENT_SCOPE)
199 set(${executable_var} "error: not a bundle" PARENT_SCOPE)
202 set(${valid_var} ${valid} PARENT_SCOPE)
203 endfunction(get_bundle_and_executable)
206 # get_bundle_all_executables
208 # Scans the given bundle recursively for all executable files and accumulates
209 # them into a variable.
211 function(get_bundle_all_executables bundle exes_var)
214 file(GLOB_RECURSE file_list "${bundle}/*")
215 foreach(f ${file_list})
216 is_file_executable("${f}" is_executable)
218 set(exes ${exes} "${f}")
222 set(${exes_var} "${exes}" PARENT_SCOPE)
223 endfunction(get_bundle_all_executables)
228 # Given a file (item) name, generate a key that should be unique considering the set of
229 # libraries that need copying or fixing up to make a bundle standalone. This is
230 # essentially the file name including extension with "." replaced by "_"
232 # This key is used as a prefix for CMake variables so that we can associate a set
233 # of variables with a given item based on its key.
235 function(get_item_key item key_var)
236 get_filename_component(item_name "${item}" NAME)
238 string(TOLOWER "${item_name}" item_name)
240 string(REGEX REPLACE "\\." "_" ${key_var} "${item_name}")
241 set(${key_var} ${${key_var}} PARENT_SCOPE)
242 endfunction(get_item_key)
247 # Loop over the list of keys, clearing all the variables associated with each
248 # key. After the loop, clear the list of keys itself.
250 # Caller of get_bundle_keys should call clear_bundle_keys when done with list
253 function(clear_bundle_keys keys_var)
254 foreach(key ${${keys_var}})
255 set(${key}_ITEM PARENT_SCOPE)
256 set(${key}_RESOLVED_ITEM PARENT_SCOPE)
257 set(${key}_DEFAULT_EMBEDDED_PATH PARENT_SCOPE)
258 set(${key}_EMBEDDED_ITEM PARENT_SCOPE)
259 set(${key}_RESOLVED_EMBEDDED_ITEM PARENT_SCOPE)
260 set(${key}_COPYFLAG PARENT_SCOPE)
262 set(${keys_var} PARENT_SCOPE)
263 endfunction(clear_bundle_keys)
266 # set_bundle_key_values
268 # Add a key to the list (if necessary) for the given item. If added,
269 # also set all the variables associated with that key.
271 function(set_bundle_key_values keys_var context item exepath dirs copyflag)
272 get_filename_component(item_name "${item}" NAME)
274 get_item_key("${item}" key)
276 list(LENGTH ${keys_var} length_before)
277 gp_append_unique(${keys_var} "${key}")
278 list(LENGTH ${keys_var} length_after)
280 if(NOT length_before EQUAL length_after)
281 gp_resolve_item("${context}" "${item}" "${exepath}" "${dirs}" resolved_item)
283 gp_item_default_embedded_path("${item}" default_embedded_path)
285 if(item MATCHES "[^/]+\\.framework/")
286 # For frameworks, construct the name under the embedded path from the
287 # opening "${item_name}.framework/" to the closing "/${item_name}":
289 string(REGEX REPLACE "^.*(${item_name}.framework/.*/${item_name}).*$" "${default_embedded_path}/\\1" embedded_item "${item}")
290 else(item MATCHES "[^/]+\\.framework/")
291 # For other items, just use the same name as the original, but in the
294 set(embedded_item "${default_embedded_path}/${item_name}")
295 endif(item MATCHES "[^/]+\\.framework/")
297 # Replace @executable_path and resolve ".." references:
299 string(REPLACE "@executable_path" "${exepath}" resolved_embedded_item "${embedded_item}")
300 get_filename_component(resolved_embedded_item "${resolved_embedded_item}" ABSOLUTE)
302 # *But* -- if we are not copying, then force resolved_embedded_item to be
303 # the same as resolved_item. In the case of multiple executables in the
304 # original bundle, using the default_embedded_path results in looking for
305 # the resolved executable next to the main bundle executable. This is here
306 # so that exes in the other sibling directories (like "bin") get fixed up
310 set(resolved_embedded_item "${resolved_item}")
313 set(${keys_var} ${${keys_var}} PARENT_SCOPE)
314 set(${key}_ITEM "${item}" PARENT_SCOPE)
315 set(${key}_RESOLVED_ITEM "${resolved_item}" PARENT_SCOPE)
316 set(${key}_DEFAULT_EMBEDDED_PATH "${default_embedded_path}" PARENT_SCOPE)
317 set(${key}_EMBEDDED_ITEM "${embedded_item}" PARENT_SCOPE)
318 set(${key}_RESOLVED_EMBEDDED_ITEM "${resolved_embedded_item}" PARENT_SCOPE)
319 set(${key}_COPYFLAG "${copyflag}" PARENT_SCOPE)
320 else(NOT length_before EQUAL length_after)
321 #message("warning: item key '${key}' already in the list, subsequent references assumed identical to first")
322 endif(NOT length_before EQUAL length_after)
323 endfunction(set_bundle_key_values)
328 # Loop over all the executable and library files within the bundle (and given as
329 # extra "${libs}") and accumulate a list of keys representing them. Set values
330 # associated with each key such that we can loop over all of them and copy
331 # prerequisite libs into the bundle and then do appropriate install_name_tool
334 function(get_bundle_keys app libs dirs keys_var)
335 set(${keys_var} PARENT_SCOPE)
337 get_bundle_and_executable("${app}" bundle executable valid)
339 # Always use the exepath of the main bundle executable for @executable_path
342 get_filename_component(exepath "${executable}" PATH)
344 # But do fixups on all executables in the bundle:
346 get_bundle_all_executables("${bundle}" exes)
348 # For each extra lib, accumulate a key as well and then also accumulate
349 # any of its prerequisites. (Extra libs are typically dynamically loaded
350 # plugins: libraries that are prerequisites for full runtime functionality
351 # but that do not show up in otool -L output...)
354 set_bundle_key_values(${keys_var} "${lib}" "${lib}" "${exepath}" "${dirs}" 1)
357 get_prerequisites("${lib}" prereqs 1 1 "${exepath}" "${dirs}")
358 foreach(pr ${prereqs})
359 set_bundle_key_values(${keys_var} "${lib}" "${pr}" "${exepath}" "${dirs}" 1)
363 # For each executable found in the bundle, accumulate keys as we go.
364 # The list of keys should be complete when all prerequisites of all
365 # binaries in the bundle have been analyzed.
368 # Add the exe itself to the keys:
370 set_bundle_key_values(${keys_var} "${exe}" "${exe}" "${exepath}" "${dirs}" 0)
372 # Add each prerequisite to the keys:
375 get_prerequisites("${exe}" prereqs 1 1 "${exepath}" "${dirs}")
376 foreach(pr ${prereqs})
377 set_bundle_key_values(${keys_var} "${exe}" "${pr}" "${exepath}" "${dirs}" 1)
381 # Propagate values to caller's scope:
383 set(${keys_var} ${${keys_var}} PARENT_SCOPE)
384 foreach(key ${${keys_var}})
385 set(${key}_ITEM "${${key}_ITEM}" PARENT_SCOPE)
386 set(${key}_RESOLVED_ITEM "${${key}_RESOLVED_ITEM}" PARENT_SCOPE)
387 set(${key}_DEFAULT_EMBEDDED_PATH "${${key}_DEFAULT_EMBEDDED_PATH}" PARENT_SCOPE)
388 set(${key}_EMBEDDED_ITEM "${${key}_EMBEDDED_ITEM}" PARENT_SCOPE)
389 set(${key}_RESOLVED_EMBEDDED_ITEM "${${key}_RESOLVED_EMBEDDED_ITEM}" PARENT_SCOPE)
390 set(${key}_COPYFLAG "${${key}_COPYFLAG}" PARENT_SCOPE)
393 endfunction(get_bundle_keys)
396 # copy_resolved_item_into_bundle
398 # Copy a resolved item into the bundle if necessary. Copy is not necessary if
399 # the resolved_item is "the same as" the resolved_embedded_item.
401 function(copy_resolved_item_into_bundle resolved_item resolved_embedded_item)
403 # ignore case on Windows
404 string(TOLOWER "${resolved_item}" resolved_item_compare)
405 string(TOLOWER "${resolved_embedded_item}" resolved_embedded_item_compare)
407 set(resolved_item_compare "${resolved_item}")
408 set(resolved_embedded_item_compare "${resolved_embedded_item}")
411 if("${resolved_item_compare}" STREQUAL "${resolved_embedded_item_compare}")
412 message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
414 #message(STATUS "copying COMMAND ${CMAKE_COMMAND} -E copy ${resolved_item} ${resolved_embedded_item}")
415 execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
417 endfunction(copy_resolved_item_into_bundle)
422 # Get the direct/non-system prerequisites of the resolved embedded item. For each
423 # prerequisite, change the way it is referenced to the value of the _EMBEDDED_ITEM
424 # keyed variable for that prerequisite. (Most likely changing to an "@executable_path"
427 # Also, change the id of the item being fixed up to its own _EMBEDDED_ITEM value.
429 # Accumulate changes in a local variable and make *one* call to install_name_tool
430 # at the end of the function with all the changes at once.
432 function(fixup_bundle_item resolved_embedded_item exepath dirs)
433 # This item's key is "ikey":
435 get_item_key("${resolved_embedded_item}" ikey)
438 get_prerequisites("${resolved_embedded_item}" prereqs 1 0 "${exepath}" "${dirs}")
442 foreach(pr ${prereqs})
443 # Each referenced item's key is "rkey" in the loop:
445 get_item_key("${pr}" rkey)
447 if(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
448 set(changes ${changes} "-change" "${pr}" "${${rkey}_EMBEDDED_ITEM}")
449 else(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
450 message("warning: unexpected reference to '${pr}'")
451 endif(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
454 # Change this item's id and all of its references in one call
455 # to install_name_tool:
457 execute_process(COMMAND install_name_tool
458 ${changes} -id "${${ikey}_EMBEDDED_ITEM}" "${resolved_embedded_item}"
460 endfunction(fixup_bundle_item)
465 # Fix up a bundle in-place and make it standalone, such that it can be drag-n-drop
466 # copied to another machine and run on that machine as long as all of the system
467 # libraries are compatible.
469 # Gather all the keys for all the executables and libraries in a bundle, and then,
470 # for each key, copy each prerequisite into the bundle. Then fix each one up according
471 # to its own list of prerequisites.
473 # Then clear all the keys and call verify_app on the final bundle to ensure that
474 # it is truly standalone.
476 function(fixup_bundle app libs dirs)
477 message(STATUS "fixup_bundle")
478 message(STATUS " app='${app}'")
479 message(STATUS " libs='${libs}'")
480 message(STATUS " dirs='${dirs}'")
482 get_bundle_and_executable("${app}" bundle executable valid)
484 get_filename_component(exepath "${executable}" PATH)
486 message(STATUS "fixup_bundle: preparing...")
487 get_bundle_keys("${app}" "${libs}" "${dirs}" keys)
489 message(STATUS "fixup_bundle: copying...")
496 if(${${key}_COPYFLAG})
497 message(STATUS "${i}/${n}: copying '${${key}_RESOLVED_ITEM}'")
498 else(${${key}_COPYFLAG})
499 message(STATUS "${i}/${n}: *NOT* copying '${${key}_RESOLVED_ITEM}'")
500 endif(${${key}_COPYFLAG})
504 message(STATUS "key='${key}'")
505 message(STATUS "item='${${key}_ITEM}'")
506 message(STATUS "resolved_item='${${key}_RESOLVED_ITEM}'")
507 message(STATUS "default_embedded_path='${${key}_DEFAULT_EMBEDDED_PATH}'")
508 message(STATUS "embedded_item='${${key}_EMBEDDED_ITEM}'")
509 message(STATUS "resolved_embedded_item='${${key}_RESOLVED_EMBEDDED_ITEM}'")
510 message(STATUS "copyflag='${${key}_COPYFLAG}'")
514 if(${${key}_COPYFLAG})
515 copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
516 "${${key}_RESOLVED_EMBEDDED_ITEM}")
517 endif(${${key}_COPYFLAG})
520 message(STATUS "fixup_bundle: fixing...")
524 message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
525 fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")
527 message(STATUS "${i}/${n}: fix-up not required on this platform '${${key}_RESOLVED_EMBEDDED_ITEM}'")
531 message(STATUS "fixup_bundle: cleaning up...")
532 clear_bundle_keys(keys)
534 message(STATUS "fixup_bundle: verifying...")
537 message(SEND_ERROR "error: fixup_bundle: not a valid bundle")
540 message(STATUS "fixup_bundle: done")
541 endfunction(fixup_bundle)
544 # copy_and_fixup_bundle
546 # Makes a copy of the bundle "src" at location "dst" and then fixes up the
547 # new copied bundle in-place at "dst"...
549 function(copy_and_fixup_bundle src dst libs dirs)
550 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${src}" "${dst}")
551 fixup_bundle("${dst}" "${libs}" "${dirs}")
552 endfunction(copy_and_fixup_bundle)
555 # verify_bundle_prerequisites
557 # Verifies that the sum of all prerequisites of all files inside the bundle
558 # are contained within the bundle or are "system" libraries, presumed to exist
561 function(verify_bundle_prerequisites bundle result_var info_var)
566 get_bundle_main_executable("${bundle}" main_bundle_exe)
568 file(GLOB_RECURSE file_list "${bundle}/*")
569 foreach(f ${file_list})
570 is_file_executable("${f}" is_executable)
572 get_filename_component(exepath "${f}" PATH)
573 math(EXPR count "${count} + 1")
575 message(STATUS "executable file ${count}: ${f}")
578 get_prerequisites("${f}" prereqs 1 1 "${exepath}" "")
581 # "embedded" and "system" prerequisites are fine... anything else means
582 # the bundle's prerequisites are not verified (i.e., the bundle is not
583 # really "standalone")
585 # On Windows (and others? Linux/Unix/...?)
586 # "local" and "system" prereqs are fine...
588 set(external_prereqs "")
590 foreach(p ${prereqs})
592 gp_file_type("${f}" "${p}" p_type)
595 if(NOT "${p_type}" STREQUAL "embedded" AND NOT "${p_type}" STREQUAL "system")
596 set(external_prereqs ${external_prereqs} "${p}")
599 if(NOT "${p_type}" STREQUAL "local" AND NOT "${p_type}" STREQUAL "system")
600 set(external_prereqs ${external_prereqs} "${p}")
606 # Found non-system/somehow-unacceptable prerequisites:
608 set(info ${info} "external prerequisites found:\nf='${f}'\nexternal_prereqs='${external_prereqs}'\n")
609 endif(external_prereqs)
614 set(info "Verified ${count} executable files in '${bundle}'")
617 set(${result_var} "${result}" PARENT_SCOPE)
618 set(${info_var} "${info}" PARENT_SCOPE)
619 endfunction(verify_bundle_prerequisites)
622 # verify_bundle_symlinks
624 # Verifies that any symlinks found in the bundle point to other files that are
625 # already also in the bundle... Anything that points to an external file causes
626 # this function to fail the verification.
628 function(verify_bundle_symlinks bundle result_var info_var)
633 # TODO: implement this function for real...
634 # Right now, it is just a stub that verifies unconditionally...
636 set(${result_var} "${result}" PARENT_SCOPE)
637 set(${info_var} "${info}" PARENT_SCOPE)
638 endfunction(verify_bundle_symlinks)
643 # Verifies that an application appears valid based on running analysis tools on it.
644 # Calls message/FATAL_ERROR if the application is not verified.
646 function(verify_app app)
650 get_bundle_and_executable("${app}" bundle executable valid)
652 message(STATUS "===========================================================================")
653 message(STATUS "Analyzing app='${app}'")
654 message(STATUS "bundle='${bundle}'")
655 message(STATUS "executable='${executable}'")
656 message(STATUS "valid='${valid}'")
658 # Verify that the bundle does not have any "external" prerequisites:
660 verify_bundle_prerequisites("${bundle}" verified info)
661 message(STATUS "verified='${verified}'")
662 message(STATUS "info='${info}'")
666 # Verify that the bundle does not have any symlinks to external files:
668 verify_bundle_symlinks("${bundle}" verified info)
669 message(STATUS "verified='${verified}'")
670 message(STATUS "info='${info}'")
675 message(FATAL_ERROR "error: verify_app failed")
677 endfunction(verify_app)