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}" AND EXISTS "${app}")
178 set(${bundle_var} "${dotapp_dir}" PARENT_SCOPE)
179 set(${executable_var} "${app}" PARENT_SCOPE)
181 #message(STATUS "info: handled executable file case...")
182 else(EXISTS "${dotapp_dir}" AND EXISTS "${app}")
183 message(STATUS "warning: *NOT* handled - executable file case...")
184 endif(EXISTS "${dotapp_dir}" AND EXISTS "${app}")
186 message(STATUS "warning: *NOT* handled - not .app dir, not executable file...")
188 endif(IS_DIRECTORY "${app}")
189 else(EXISTS "${app}")
190 message(STATUS "warning: *NOT* handled - directory/file does not exist...")
191 endif(EXISTS "${app}")
194 set(${bundle_var} "error: not a bundle" PARENT_SCOPE)
195 set(${executable_var} "error: not a bundle" PARENT_SCOPE)
198 set(${valid_var} ${valid} PARENT_SCOPE)
199 endfunction(get_bundle_and_executable)
202 # get_bundle_all_executables
204 # Scans the given bundle recursively for all executable files and accumulates
205 # them into a variable.
207 function(get_bundle_all_executables bundle exes_var)
210 file(GLOB_RECURSE file_list "${bundle}/*")
211 foreach(f ${file_list})
212 is_file_executable("${f}" is_executable)
214 set(exes ${exes} "${f}")
218 set(${exes_var} "${exes}" PARENT_SCOPE)
219 endfunction(get_bundle_all_executables)
224 # Given a file (item) name, generate a key that should be unique considering the set of
225 # libraries that need copying or fixing up to make a bundle standalone. This is
226 # essentially the file name including extension with "." replaced by "_"
228 # This key is used as a prefix for CMake variables so that we can associate a set
229 # of variables with a given item based on its key.
231 function(get_item_key item key_var)
232 get_filename_component(item_name "${item}" NAME)
233 string(REGEX REPLACE "\\." "_" ${key_var} "${item_name}")
234 set(${key_var} ${${key_var}} PARENT_SCOPE)
235 endfunction(get_item_key)
240 # Loop over the list of keys, clearing all the variables associated with each
241 # key. After the loop, clear the list of keys itself.
243 # Caller of get_bundle_keys should call clear_bundle_keys when done with list
246 function(clear_bundle_keys keys_var)
247 foreach(key ${${keys_var}})
248 set(${key}_ITEM PARENT_SCOPE)
249 set(${key}_RESOLVED_ITEM PARENT_SCOPE)
250 set(${key}_DEFAULT_EMBEDDED_PATH PARENT_SCOPE)
251 set(${key}_EMBEDDED_ITEM PARENT_SCOPE)
252 set(${key}_RESOLVED_EMBEDDED_ITEM PARENT_SCOPE)
253 set(${key}_COPYFLAG PARENT_SCOPE)
255 set(${keys_var} PARENT_SCOPE)
256 endfunction(clear_bundle_keys)
259 # set_bundle_key_values
261 # Add a key to the list (if necessary) for the given item. If added,
262 # also set all the variables associated with that key.
264 function(set_bundle_key_values keys_var context item exepath dirs copyflag)
265 get_filename_component(item_name "${item}" NAME)
267 get_item_key("${item}" key)
269 list(LENGTH ${keys_var} length_before)
270 gp_append_unique(${keys_var} "${key}")
271 list(LENGTH ${keys_var} length_after)
273 if(NOT length_before EQUAL length_after)
274 gp_resolve_item("${context}" "${item}" "${exepath}" "${dirs}" resolved_item)
276 gp_item_default_embedded_path("${item}" default_embedded_path)
278 if(item MATCHES "[^/]+\\.framework/")
279 # For frameworks, construct the name under the embedded path from the
280 # opening "${item_name}.framework/" to the closing "/${item_name}":
282 string(REGEX REPLACE "^.*(${item_name}.framework/.*/${item_name}).*$" "${default_embedded_path}/\\1" embedded_item "${item}")
283 else(item MATCHES "[^/]+\\.framework/")
284 # For other items, just use the same name as the original, but in the
287 set(embedded_item "${default_embedded_path}/${item_name}")
288 endif(item MATCHES "[^/]+\\.framework/")
290 # Replace @executable_path and resolve ".." references:
292 string(REPLACE "@executable_path" "${exepath}" resolved_embedded_item "${embedded_item}")
293 get_filename_component(resolved_embedded_item "${resolved_embedded_item}" ABSOLUTE)
295 # *But* -- if we are not copying, then force resolved_embedded_item to be
296 # the same as resolved_item. In the case of multiple executables in the
297 # original bundle, using the default_embedded_path results in looking for
298 # the resolved executable next to the main bundle executable. This is here
299 # so that exes in the other sibling directories (like "bin") get fixed up
303 set(resolved_embedded_item "${resolved_item}")
306 set(${keys_var} ${${keys_var}} PARENT_SCOPE)
307 set(${key}_ITEM "${item}" PARENT_SCOPE)
308 set(${key}_RESOLVED_ITEM "${resolved_item}" PARENT_SCOPE)
309 set(${key}_DEFAULT_EMBEDDED_PATH "${default_embedded_path}" PARENT_SCOPE)
310 set(${key}_EMBEDDED_ITEM "${embedded_item}" PARENT_SCOPE)
311 set(${key}_RESOLVED_EMBEDDED_ITEM "${resolved_embedded_item}" PARENT_SCOPE)
312 set(${key}_COPYFLAG "${copyflag}" PARENT_SCOPE)
313 else(NOT length_before EQUAL length_after)
314 #message("warning: item key '${key}' already in the list, subsequent references assumed identical to first")
315 endif(NOT length_before EQUAL length_after)
316 endfunction(set_bundle_key_values)
321 # Loop over all the executable and library files within the bundle (and given as
322 # extra "${libs}") and accumulate a list of keys representing them. Set values
323 # associated with each key such that we can loop over all of them and copy
324 # prerequisite libs into the bundle and then do appropriate install_name_tool
327 function(get_bundle_keys app libs dirs keys_var)
328 set(${keys_var} PARENT_SCOPE)
330 get_bundle_and_executable("${app}" bundle executable valid)
332 # Always use the exepath of the main bundle executable for @executable_path
335 get_filename_component(exepath "${executable}" PATH)
337 # But do fixups on all executables in the bundle:
339 get_bundle_all_executables("${bundle}" exes)
341 # For each extra lib, accumulate a key as well and then also accumulate
342 # any of its prerequisites. (Extra libs are typically dynamically loaded
343 # plugins: libraries that are prerequisites for full runtime functionality
344 # but that do not show up in otool -L output...)
347 set_bundle_key_values(${keys_var} "${lib}" "${lib}" "${exepath}" "${dirs}" 1)
350 get_prerequisites("${lib}" prereqs 1 1 "${exepath}" "${dirs}")
351 foreach(pr ${prereqs})
352 set_bundle_key_values(${keys_var} "${lib}" "${pr}" "${exepath}" "${dirs}" 1)
356 # For each executable found in the bundle, accumulate keys as we go.
357 # The list of keys should be complete when all prerequisites of all
358 # binaries in the bundle have been analyzed.
361 # Add the exe itself to the keys:
363 set_bundle_key_values(${keys_var} "${exe}" "${exe}" "${exepath}" "${dirs}" 0)
365 # Add each prerequisite to the keys:
368 get_prerequisites("${exe}" prereqs 1 1 "${exepath}" "${dirs}")
369 foreach(pr ${prereqs})
370 set_bundle_key_values(${keys_var} "${exe}" "${pr}" "${exepath}" "${dirs}" 1)
374 # Propagate values to caller's scope:
376 set(${keys_var} ${${keys_var}} PARENT_SCOPE)
377 foreach(key ${${keys_var}})
378 set(${key}_ITEM "${${key}_ITEM}" PARENT_SCOPE)
379 set(${key}_RESOLVED_ITEM "${${key}_RESOLVED_ITEM}" PARENT_SCOPE)
380 set(${key}_DEFAULT_EMBEDDED_PATH "${${key}_DEFAULT_EMBEDDED_PATH}" PARENT_SCOPE)
381 set(${key}_EMBEDDED_ITEM "${${key}_EMBEDDED_ITEM}" PARENT_SCOPE)
382 set(${key}_RESOLVED_EMBEDDED_ITEM "${${key}_RESOLVED_EMBEDDED_ITEM}" PARENT_SCOPE)
383 set(${key}_COPYFLAG "${${key}_COPYFLAG}" PARENT_SCOPE)
386 endfunction(get_bundle_keys)
389 # copy_resolved_item_into_bundle
391 # Copy a resolved item into the bundle if necessary. Copy is not necessary if the resolved_item
392 # is the same as the resolved_embedded_item.
394 function(copy_resolved_item_into_bundle resolved_item resolved_embedded_item)
395 if("${resolved_item}" STREQUAL "${resolved_embedded_item}")
396 message(STATUS "warning: resolved_item == resolved_embedded_item - not copying...")
397 else("${resolved_item}" STREQUAL "${resolved_embedded_item}")
398 execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${resolved_item}" "${resolved_embedded_item}")
399 endif("${resolved_item}" STREQUAL "${resolved_embedded_item}")
400 endfunction(copy_resolved_item_into_bundle)
405 # Get the direct/non-system prerequisites of the resolved embedded item. For each
406 # prerequisite, change the way it is referenced to the value of the _EMBEDDED_ITEM
407 # keyed variable for that prerequisite. (Most likely changing to an "@executable_path"
410 # Also, change the id of the item being fixed up to its own _EMBEDDED_ITEM value.
412 # Accumulate changes in a local variable and make *one* call to install_name_tool
413 # at the end of the function with all the changes at once.
415 function(fixup_bundle_item resolved_embedded_item exepath dirs)
416 # This item's key is "ikey":
418 get_item_key("${resolved_embedded_item}" ikey)
421 get_prerequisites("${resolved_embedded_item}" prereqs 1 0 "${exepath}" "${dirs}")
425 foreach(pr ${prereqs})
426 # Each referenced item's key is "rkey" in the loop:
428 get_item_key("${pr}" rkey)
430 if(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
431 set(changes ${changes} "-change" "${pr}" "${${rkey}_EMBEDDED_ITEM}")
432 else(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
433 message("warning: unexpected reference to '${pr}'")
434 endif(NOT "${${rkey}_EMBEDDED_ITEM}" STREQUAL "")
437 # Change this item's id and all of its references in one call
438 # to install_name_tool:
440 execute_process(COMMAND install_name_tool
441 ${changes} -id "${${ikey}_EMBEDDED_ITEM}" "${resolved_embedded_item}"
443 endfunction(fixup_bundle_item)
448 # Fix up a bundle in-place and make it standalone, such that it can be drag-n-drop
449 # copied to another machine and run on that machine as long as all of the system
450 # libraries are compatible.
452 # Gather all the keys for all the executables and libraries in a bundle, and then,
453 # for each key, copy each prerequisite into the bundle. Then fix each one up according
454 # to its own list of prerequisites.
456 # Then clear all the keys and call verify_app on the final bundle to ensure that
457 # it is truly standalone.
459 function(fixup_bundle app libs dirs)
460 message(STATUS "fixup_bundle")
461 message(STATUS " app='${app}'")
462 message(STATUS " libs='${libs}'")
463 message(STATUS " dirs='${dirs}'")
465 get_bundle_and_executable("${app}" bundle executable valid)
467 get_filename_component(exepath "${executable}" PATH)
469 message(STATUS "fixup_bundle: preparing...")
470 get_bundle_keys("${app}" "${libs}" "${dirs}" keys)
472 message(STATUS "fixup_bundle: copying...")
479 if(${${key}_COPYFLAG})
480 message(STATUS "${i}/${n}: copying '${${key}_RESOLVED_ITEM}'")
481 else(${${key}_COPYFLAG})
482 message(STATUS "${i}/${n}: *NOT* copying '${${key}_RESOLVED_ITEM}'")
483 endif(${${key}_COPYFLAG})
487 message(STATUS "key='${key}'")
488 message(STATUS "item='${${key}_ITEM}'")
489 message(STATUS "resolved_item='${${key}_RESOLVED_ITEM}'")
490 message(STATUS "default_embedded_path='${${key}_DEFAULT_EMBEDDED_PATH}'")
491 message(STATUS "embedded_item='${${key}_EMBEDDED_ITEM}'")
492 message(STATUS "resolved_embedded_item='${${key}_RESOLVED_EMBEDDED_ITEM}'")
493 message(STATUS "copyflag='${${key}_COPYFLAG}'")
497 if(${${key}_COPYFLAG})
498 copy_resolved_item_into_bundle("${${key}_RESOLVED_ITEM}"
499 "${${key}_RESOLVED_EMBEDDED_ITEM}")
500 endif(${${key}_COPYFLAG})
503 message(STATUS "fixup_bundle: fixing...")
506 message(STATUS "${i}/${n}: fixing up '${${key}_RESOLVED_EMBEDDED_ITEM}'")
507 #message(STATUS " exepath='${exepath}'")
508 fixup_bundle_item("${${key}_RESOLVED_EMBEDDED_ITEM}" "${exepath}" "${dirs}")
511 message(STATUS "fixup_bundle: cleaning up...")
512 clear_bundle_keys(keys)
514 message(STATUS "fixup_bundle: verifying...")
517 message(STATUS "error: fixup_bundle: not a valid bundle")
520 message(STATUS "fixup_bundle: done")
521 endfunction(fixup_bundle)
524 # copy_and_fixup_bundle
526 # Makes a copy of the bundle "src" at location "dst" and then fixes up the
527 # new copied bundle in-place at "dst"...
529 function(copy_and_fixup_bundle src dst libs dirs)
530 execute_process(COMMAND ${CMAKE_COMMAND} -E copy_directory "${src}" "${dst}")
531 fixup_bundle("${dst}" "${libs}" "${dirs}")
532 endfunction(copy_and_fixup_bundle)
535 # verify_bundle_prerequisites
537 # Verifies that the sum of all prerequisites of all files inside the bundle
538 # are contained within the bundle or are "system" libraries, presumed to exist
541 function(verify_bundle_prerequisites bundle result_var info_var)
546 get_bundle_main_executable("${bundle}" main_bundle_exe)
548 file(GLOB_RECURSE file_list "${bundle}/*")
549 foreach(f ${file_list})
550 is_file_executable("${f}" is_executable)
552 get_filename_component(exepath "${f}" PATH)
553 message(STATUS "executable file: ${f}")
555 math(EXPR count "${count} + 1")
558 get_prerequisites("${f}" prereqs 1 1 "${exepath}" "")
560 # "embedded" and "system" prerequisites are fine... anything else means
561 # the bundle's prerequisites are not verified (i.e., the bundle is not
562 # really "standalone")
564 set(external_prereqs "")
565 foreach(p ${prereqs})
567 gp_file_type("${f}" "${p}" p_type)
568 if (NOT "${p_type}" STREQUAL "embedded" AND NOT "${p_type}" STREQUAL "system")
569 set(external_prereqs ${external_prereqs} "${p}")
570 endif (NOT "${p_type}" STREQUAL "embedded" AND NOT "${p_type}" STREQUAL "system")
574 # Found non-system/non-embedded prerequisites:
576 set(info ${info} "non-system/non-embedded prerequisites found:\nf='${f}'\nexternal_prereqs='${external_prereqs}'\n")
577 endif(external_prereqs)
582 set(info "Verified ${count} executable files in '${bundle}'")
585 set(${result_var} "${result}" PARENT_SCOPE)
586 set(${info_var} "${info}" PARENT_SCOPE)
587 endfunction(verify_bundle_prerequisites)
590 # verify_bundle_symlinks
592 # Verifies that any symlinks found in the bundle point to other files that are
593 # already also in the bundle... Anything that points to an external file causes
594 # this function to fail the verification.
596 function(verify_bundle_symlinks bundle result_var info_var)
601 # TODO: implement this function for real...
602 # Right now, it is just a stub that verifies unconditionally...
604 set(${result_var} "${result}" PARENT_SCOPE)
605 set(${info_var} "${info}" PARENT_SCOPE)
606 endfunction(verify_bundle_symlinks)
611 # Verifies that an application appears valid based on running analysis tools on it.
612 # Calls message/FATAL_ERROR if the application is not verified.
614 function(verify_app app)
618 get_bundle_and_executable("${app}" bundle executable valid)
620 message(STATUS "===========================================================================")
621 message(STATUS "Analyzing app='${app}'")
622 message(STATUS "bundle='${bundle}'")
623 message(STATUS "executable='${executable}'")
624 message(STATUS "valid='${valid}'")
626 # Verify that the bundle does not have any "external" prerequisites:
628 verify_bundle_prerequisites("${bundle}" verified info)
629 message(STATUS "verified='${verified}'")
630 message(STATUS "info='${info}'")
634 # Verify that the bundle does not have any symlinks to external files:
636 verify_bundle_symlinks("${bundle}" verified info)
637 message(STATUS "verified='${verified}'")
638 message(STATUS "info='${info}'")
643 message(FATAL_ERROR "error: verify_app failed")
645 endfunction(verify_app)