1 # GetPrerequisites.cmake
3 # This script provides functions to list the .dll, .dylib or .so files that an
4 # executable or shared library file depends on. (Its prerequisites.)
6 # It uses various tools to obtain the list of required shared library files:
11 # The following functions are provided by this script:
14 # gp_item_default_embedded_path
15 # (projects can override with gp_item_default_embedded_path_override)
17 # (projects can override with gp_resolve_item_override)
18 # gp_resolved_file_type
22 # list_prerequisites_by_glob
24 # Requires CMake 2.6 or greater because it uses function, break, return and
28 # gp_append_unique list_var value
30 # Append value to the list variable ${list_var} only if the value is not
31 # already in the list.
33 function(gp_append_unique list_var value)
36 foreach(item ${${list_var}})
37 if("${item}" STREQUAL "${value}")
40 endif("${item}" STREQUAL "${value}")
44 set(${list_var} ${${list_var}} "${value}" PARENT_SCOPE)
46 endfunction(gp_append_unique)
49 # is_file_executable file result_var
51 # Return 1 in ${result_var} if ${file} is a binary executable.
53 # Return 0 in ${result_var} otherwise.
55 function(is_file_executable file result_var)
57 # A file is not executable until proven otherwise:
59 set(${result_var} 0 PARENT_SCOPE)
61 get_filename_component(file_full "${file}" ABSOLUTE)
62 string(TOLOWER "${file_full}" file_full_lower)
64 # If file name ends in .exe or .dll on Windows, *assume* executable:
67 if("${file_full_lower}" MATCHES "\\.(exe|dll)$")
68 set(${result_var} 1 PARENT_SCOPE)
70 endif("${file_full_lower}" MATCHES "\\.(exe|dll)$")
72 # A clause could be added here that uses output or return value of dumpbin
73 # to determine ${result_var}. In 99%+? practical cases, the exe|dll name
74 # match will be sufficient...
78 # Use the information returned from the Unix shell command "file" to
79 # determine if ${file_full} should be considered an executable file...
81 # If the file command's output contains "executable" and does *not* contain
82 # "text" then it is likely an executable suitable for prerequisite analysis
83 # via the get_prerequisites macro.
87 find_program(file_cmd "file")
91 execute_process(COMMAND "${file_cmd}" "${file_full}"
92 OUTPUT_VARIABLE file_ov
93 OUTPUT_STRIP_TRAILING_WHITESPACE
96 # Replace the name of the file in the output with a placeholder token
97 # (the string " _file_full_ ") so that just in case the path name of
98 # the file contains the word "text" or "executable" we are not fooled
99 # into thinking "the wrong thing" because the file name matches the
100 # other 'file' command output we are looking for...
102 string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
103 string(TOLOWER "${file_ov}" file_ov)
105 #message(STATUS "file_ov='${file_ov}'")
106 if("${file_ov}" MATCHES "executable")
107 #message(STATUS "executable!")
108 if("${file_ov}" MATCHES "text")
109 #message(STATUS "but text, so *not* a binary executable!")
110 else("${file_ov}" MATCHES "text")
111 set(${result_var} 1 PARENT_SCOPE)
113 endif("${file_ov}" MATCHES "text")
114 endif("${file_ov}" MATCHES "executable")
116 message(STATUS "warning: No 'file' command, skipping execute_process...")
119 endfunction(is_file_executable)
122 # gp_item_default_embedded_path item default_embedded_path_var
124 # Return the path that others should refer to the item by when the item
125 # is embedded inside a bundle.
127 # Override on a per-project basis by providing a project-specific
128 # gp_item_default_embedded_path_override function.
130 function(gp_item_default_embedded_path item default_embedded_path_var)
132 # On Windows and Linux, "embed" prerequisites in the same directory
133 # as the executable by default:
135 set(path "@executable_path")
138 # On the Mac, relative to the executable depending on the type
139 # of the thing we are embedding:
143 # The assumption here is that all executables in the bundle will be
144 # in same-level-directories inside the bundle. The parent directory
145 # of an executable inside the bundle should be MacOS or a sibling of
146 # MacOS and all embedded paths returned from here will begin with
147 # "@executable_path/../" and will work from all executables in all
148 # such same-level-directories inside the bundle.
151 # By default, embed things right next to the main bundle executable:
153 set(path "@executable_path/../../Contents/MacOS")
155 # Embed .dylibs right next to the main bundle executable:
157 if(item MATCHES "\\.dylib$")
158 set(path "@executable_path/../MacOS")
160 endif(item MATCHES "\\.dylib$")
162 # Embed frameworks in the embedded "Frameworks" directory (sibling of MacOS):
165 if(item MATCHES "[^/]+\\.framework/")
166 set(path "@executable_path/../Frameworks")
168 endif(item MATCHES "[^/]+\\.framework/")
169 endif(NOT overridden)
172 # Provide a hook so that projects can override the default embedded location
173 # of any given library by whatever logic they choose:
175 if(COMMAND gp_item_default_embedded_path_override)
176 gp_item_default_embedded_path_override("${item}" path)
177 endif(COMMAND gp_item_default_embedded_path_override)
179 set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
180 endfunction(gp_item_default_embedded_path)
183 # gp_resolve_item context item exepath dirs resolved_item_var
185 # Resolve an item into an existing full path file.
187 # Override on a per-project basis by providing a project-specific
188 # gp_resolve_item_override function.
190 function(gp_resolve_item context item exepath dirs resolved_item_var)
192 set(resolved_item "${item}")
194 # Is it already resolved?
196 if(EXISTS "${resolved_item}")
198 endif(EXISTS "${resolved_item}")
201 if(item MATCHES "@executable_path")
203 # @executable_path references are assumed relative to exepath
205 string(REPLACE "@executable_path" "${exepath}" ri "${item}")
206 get_filename_component(ri "${ri}" ABSOLUTE)
209 #message(STATUS "info: embedded item exists (${ri})")
211 set(resolved_item "${ri}")
213 message(STATUS "warning: embedded item does not exist '${ri}'")
214 endif(EXISTS "${ri}")
215 endif(item MATCHES "@executable_path")
219 if(item MATCHES "@loader_path")
221 # @loader_path references are assumed relative to the
222 # PATH of the given "context" (presumably another library)
224 get_filename_component(contextpath "${context}" PATH)
225 string(REPLACE "@loader_path" "${contextpath}" ri "${item}")
226 get_filename_component(ri "${ri}" ABSOLUTE)
229 #message(STATUS "info: embedded item exists (${ri})")
231 set(resolved_item "${ri}")
233 message(STATUS "warning: embedded item does not exist '${ri}'")
234 endif(EXISTS "${ri}")
235 endif(item MATCHES "@loader_path")
239 set(ri "ri-NOTFOUND")
240 find_file(ri "${item}" ${exepath} ${dirs} NO_DEFAULT_PATH)
241 find_file(ri "${item}" ${exepath} ${dirs} /usr/lib)
243 #message(STATUS "info: 'find_file' in exepath/dirs (${ri})")
245 set(resolved_item "${ri}")
246 set(ri "ri-NOTFOUND")
251 if(item MATCHES "[^/]+\\.framework/")
252 set(fw "fw-NOTFOUND")
253 find_file(fw "${item}"
254 "~/Library/Frameworks"
255 "/Library/Frameworks"
256 "/System/Library/Frameworks"
259 #message(STATUS "info: 'find_file' found framework (${fw})")
261 set(resolved_item "${fw}")
262 set(fw "fw-NOTFOUND")
264 endif(item MATCHES "[^/]+\\.framework/")
267 # Using find_program on Windows will find dll files that are in the PATH.
268 # (Converting simple file names into full path names if found.)
272 set(ri "ri-NOTFOUND")
273 find_program(ri "${item}" PATHS "${exepath};${dirs}" NO_DEFAULT_PATH)
274 find_program(ri "${item}" PATHS "${exepath};${dirs}")
276 #message(STATUS "info: 'find_program' in exepath/dirs (${ri})")
278 set(resolved_item "${ri}")
279 set(ri "ri-NOTFOUND")
284 # Provide a hook so that projects can override item resolution
285 # by whatever logic they choose:
287 if(COMMAND gp_resolve_item_override)
288 gp_resolve_item_override("${context}" "${item}" "${exepath}" "${dirs}" resolved_item resolved)
289 endif(COMMAND gp_resolve_item_override)
293 warning: cannot resolve item '${item}'
296 need more directories?
297 need to use InstallRequiredSystemLibraries?
298 run in install tree instead of build tree?
301 #******************************************************************************
302 #warning: cannot resolve item '${item}'
305 # need more directories?
306 # need to use InstallRequiredSystemLibraries?
307 # run in install tree instead of build tree?
309 # context='${context}'
311 # exepath='${exepath}'
313 # resolved_item_var='${resolved_item_var}'
314 #******************************************************************************
318 set(${resolved_item_var} "${resolved_item}" PARENT_SCOPE)
319 endfunction(gp_resolve_item)
322 # gp_resolved_file_type original_file file exepath dirs type_var
324 # Return the type of ${file} with respect to ${original_file}. String
325 # describing type of prerequisite is returned in variable named ${type_var}.
327 # Use ${exepath} and ${dirs} if necessary to resolve non-absolute ${file}
328 # values -- but only for non-embedded items.
330 # Possible types are:
336 function(gp_resolved_file_type original_file file exepath dirs type_var)
337 #message(STATUS "**")
339 if(NOT IS_ABSOLUTE "${original_file}")
340 message(STATUS "warning: gp_resolved_file_type expects absolute full path for first arg original_file")
347 set(resolved_file "${file}")
349 if("${file}" MATCHES "^@(executable|loader)_path")
354 if(NOT IS_ABSOLUTE "${file}")
355 gp_resolve_item("${original_file}" "${file}" "${exepath}" "${dirs}" resolved_file)
358 string(TOLOWER "${original_file}" original_lower)
359 string(TOLOWER "${resolved_file}" lower)
362 if(resolved_file MATCHES "^(/lib/|/lib32/|/lib64/|/usr/lib/|/usr/lib32/|/usr/lib64/|/usr/X11R6/)")
368 if(resolved_file MATCHES "^(/System/Library/|/usr/lib/)")
374 string(TOLOWER "$ENV{SystemRoot}" sysroot)
375 string(REGEX REPLACE "\\\\" "/" sysroot "${sysroot}")
377 string(TOLOWER "$ENV{windir}" windir)
378 string(REGEX REPLACE "\\\\" "/" windir "${windir}")
380 if(lower MATCHES "^(${sysroot}/system|${windir}/system|(.*/)*msvc[^/]+dll)")
386 get_filename_component(original_path "${original_lower}" PATH)
387 get_filename_component(path "${lower}" PATH)
388 if("${original_path}" STREQUAL "${path}")
394 # Return type string based on computed booleans:
406 #message(STATUS "gp_resolved_file_type: '${file}' '${resolved_file}'")
407 #message(STATUS " type: '${type}'")
410 if(NOT IS_ABSOLUTE "${resolved_file}")
411 if(lower MATCHES "^msvc[^/]+dll" AND is_system)
412 message(STATUS "info: non-absolute msvc file '${file}' returning type '${type}'")
414 message(STATUS "warning: gp_resolved_file_type non-absolute file '${file}' returning type '${type}' -- possibly incorrect")
419 set(${type_var} "${type}" PARENT_SCOPE)
421 #message(STATUS "**")
425 # gp_file_type original_file file type_var
427 # Return the type of ${file} with respect to ${original_file}. String
428 # describing type of prerequisite is returned in variable named ${type_var}.
430 # Possible types are:
436 function(gp_file_type original_file file type_var)
437 if(NOT IS_ABSOLUTE "${original_file}")
438 message(STATUS "warning: gp_file_type expects absolute full path for first arg original_file")
441 get_filename_component(exepath "${original_file}" PATH)
444 gp_resolved_file_type("${original_file}" "${file}" "${exepath}" "" type)
446 set(${type_var} "${type}" PARENT_SCOPE)
447 endfunction(gp_file_type)
450 # get_prerequisites target prerequisites_var exclude_system recurse dirs
452 # Get the list of shared library files required by ${target}. The list in
453 # the variable named ${prerequisites_var} should be empty on first entry to
454 # this function. On exit, ${prerequisites_var} will contain the list of
455 # required shared library files.
457 # target is the full path to an executable file
459 # prerequisites_var is the name of a CMake variable to contain the results
461 # exclude_system is 0 or 1: 0 to include "system" prerequisites , 1 to
464 # recurse is 0 or 1: 0 for direct prerequisites only, 1 for all prerequisites
467 # exepath is the path to the top level executable used for @executable_path
468 # replacment on the Mac
470 # dirs is a list of paths where libraries might be found: these paths are
471 # searched first when a target without any path info is given. Then standard
472 # system locations are also searched: PATH, Framework locations, /usr/lib...
474 function(get_prerequisites target prerequisites_var exclude_system recurse exepath dirs)
478 if(NOT IS_ABSOLUTE "${target}")
479 message("warning: target '${target}' is not absolute...")
480 endif(NOT IS_ABSOLUTE "${target}")
482 if(NOT EXISTS "${target}")
483 message("warning: target '${target}' does not exist...")
484 endif(NOT EXISTS "${target}")
486 # <setup-gp_tool-vars>
488 # Try to choose the right tool by default. Caller can set gp_tool prior to
489 # calling this function to force using a different tool.
491 if("${gp_tool}" STREQUAL "")
497 set(gp_tool "dumpbin")
499 endif("${gp_tool}" STREQUAL "")
503 if("${gp_tool}" STREQUAL "ldd")
505 set(gp_regex "^[\t ]*[^\t ]+ => ([^\t ]+).*${eol_char}$")
506 set(gp_regex_cmp_count 1)
508 endif("${gp_tool}" STREQUAL "ldd")
510 if("${gp_tool}" STREQUAL "otool")
511 set(gp_cmd_args "-L")
512 set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
513 set(gp_regex_cmp_count 3)
515 endif("${gp_tool}" STREQUAL "otool")
517 if("${gp_tool}" STREQUAL "dumpbin")
518 set(gp_cmd_args "/dependents")
519 set(gp_regex "^ ([^ ].*[Dd][Ll][Ll])${eol_char}$")
520 set(gp_regex_cmp_count 1)
522 set(ENV{VS_UNICODE_OUTPUT} "") # Block extra output from inside VS IDE.
523 endif("${gp_tool}" STREQUAL "dumpbin")
525 if(NOT gp_tool_known)
526 message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
527 message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
528 message(STATUS "Valid gp_tool values are dumpbin, ldd and otool.")
530 endif(NOT gp_tool_known)
532 set(gp_cmd_paths ${gp_cmd_paths}
533 "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
534 "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
535 "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
536 "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
537 "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
538 "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
543 find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
546 message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
550 if("${gp_tool}" STREQUAL "dumpbin")
551 # When running dumpbin, it also needs the "Common7/IDE" directory in the
552 # PATH. It will already be in the PATH if being run from a Visual Studio
553 # command prompt. Add it to the PATH here in case we are running from a
554 # different command prompt.
556 get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
557 get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
558 if(EXISTS "${gp_cmd_dlls_dir}")
559 # only add to the path if it is not already in the path
560 if(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
561 set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
562 endif(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
563 endif(EXISTS "${gp_cmd_dlls_dir}")
564 endif("${gp_tool}" STREQUAL "dumpbin")
566 # </setup-gp_tool-vars>
568 if("${gp_tool}" STREQUAL "ldd")
569 set(old_ld_env "$ENV{LD_LIBRARY_PATH}")
570 foreach(dir ${exepath} ${dirs})
571 set(ENV{LD_LIBRARY_PATH} "${dir}:$ENV{LD_LIBRARY_PATH}")
573 endif("${gp_tool}" STREQUAL "ldd")
576 # Track new prerequisites at each new level of recursion. Start with an
577 # empty list at each level:
581 # Run gp_cmd on the target:
584 COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
585 OUTPUT_VARIABLE gp_cmd_ov
588 if("${gp_tool}" STREQUAL "ldd")
589 set(ENV{LD_LIBRARY_PATH} "${old_ld_env}")
590 endif("${gp_tool}" STREQUAL "ldd")
593 message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
594 message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
595 message(STATUS "</RawOutput>")
598 get_filename_component(target_dir "${target}" PATH)
600 # Convert to a list of lines:
602 string(REGEX REPLACE ";" "\\\\;" candidates "${gp_cmd_ov}")
603 string(REGEX REPLACE "\n" "${eol_char};" candidates "${candidates}")
605 # Analyze each line for file names that match the regular expression:
607 foreach(candidate ${candidates})
608 if("${candidate}" MATCHES "${gp_regex}")
609 # Extract information from each candidate:
610 string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
612 if(gp_regex_cmp_count GREATER 1)
613 string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
614 string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
615 string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
616 string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
617 endif(gp_regex_cmp_count GREATER 1)
619 if(gp_regex_cmp_count GREATER 2)
620 string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
621 string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
622 string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
623 string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
624 endif(gp_regex_cmp_count GREATER 2)
626 # Use the raw_item as the list entries returned by this function. Use the
627 # gp_resolve_item function to resolve it to an actual full path file if
630 set(item "${raw_item}")
632 # Add each item unless it is excluded:
636 if(${exclude_system})
638 gp_resolved_file_type("${target}" "${item}" "${exepath}" "${dirs}" type)
640 if("${type}" STREQUAL "system")
642 endif("${type}" STREQUAL "system")
643 endif(${exclude_system})
646 list(LENGTH ${prerequisites_var} list_length_before_append)
647 gp_append_unique(${prerequisites_var} "${item}")
648 list(LENGTH ${prerequisites_var} list_length_after_append)
651 # If item was really added, this is the first time we have seen it.
652 # Add it to unseen_prereqs so that we can recursively add *its*
655 # But first: resolve its name to an absolute full path name such
656 # that the analysis tools can simply accept it as input.
658 if(NOT list_length_before_append EQUAL list_length_after_append)
659 gp_resolve_item("${target}" "${item}" "${exepath}" "${dirs}" resolved_item)
660 set(unseen_prereqs ${unseen_prereqs} "${resolved_item}")
661 endif(NOT list_length_before_append EQUAL list_length_after_append)
664 else("${candidate}" MATCHES "${gp_regex}")
666 message(STATUS "ignoring non-matching line: '${candidate}'")
668 endif("${candidate}" MATCHES "${gp_regex}")
669 endforeach(candidate)
671 list(LENGTH ${prerequisites_var} prerequisites_var_length)
672 if(prerequisites_var_length GREATER 0)
673 list(SORT ${prerequisites_var})
674 endif(prerequisites_var_length GREATER 0)
676 set(more_inputs ${unseen_prereqs})
677 foreach(input ${more_inputs})
678 get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse} "${exepath}" "${dirs}")
682 set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
683 endfunction(get_prerequisites)
686 # list_prerequisites target all exclude_system verbose
688 # ARGV0 (target) is the full path to an executable file
690 # optional ARGV1 (all) is 0 or 1: 0 for direct prerequisites only,
691 # 1 for all prerequisites recursively
693 # optional ARGV2 (exclude_system) is 0 or 1: 0 to include "system"
694 # prerequisites , 1 to exclude them
696 # optional ARGV3 (verbose) is 0 or 1: 0 to print only full path
697 # names of prerequisites, 1 to print extra information
699 function(list_prerequisites target)
700 if("${ARGV1}" STREQUAL "")
702 else("${ARGV1}" STREQUAL "")
704 endif("${ARGV1}" STREQUAL "")
706 if("${ARGV2}" STREQUAL "")
707 set(exclude_system 0)
708 else("${ARGV2}" STREQUAL "")
709 set(exclude_system "${ARGV2}")
710 endif("${ARGV2}" STREQUAL "")
712 if("${ARGV3}" STREQUAL "")
714 else("${ARGV3}" STREQUAL "")
715 set(verbose "${ARGV3}")
716 endif("${ARGV3}" STREQUAL "")
720 set(print_count "${verbose}")
721 set(print_prerequisite_type "${verbose}")
722 set(print_target "${verbose}")
725 get_filename_component(exepath "${target}" PATH)
728 get_prerequisites("${target}" prereqs ${exclude_system} ${all} "${exepath}" "")
731 message(STATUS "File '${target}' depends on:")
734 foreach(d ${prereqs})
735 math(EXPR count "${count} + 1")
738 set(count_str "${count}. ")
741 if(print_prerequisite_type)
742 gp_file_type("${target}" "${d}" type)
743 set(type_str " (${type})")
744 endif(print_prerequisite_type)
746 message(STATUS "${count_str}${d}${type_str}")
748 endfunction(list_prerequisites)
751 # list_prerequisites_by_glob glob_arg glob_exp
753 # glob_arg is GLOB or GLOB_RECURSE
755 # glob_exp is a globbing expression used with "file(GLOB" to retrieve a list
756 # of matching files. If a matching file is executable, its prerequisites are
759 # Any additional (optional) arguments provided are passed along as the
760 # optional arguments to the list_prerequisites calls.
762 function(list_prerequisites_by_glob glob_arg glob_exp)
763 message(STATUS "=============================================================================")
764 message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
766 file(${glob_arg} file_list ${glob_exp})
767 foreach(f ${file_list})
768 is_file_executable("${f}" is_f_executable)
770 message(STATUS "=============================================================================")
771 list_prerequisites("${f}" ${ARGN})
773 endif(is_f_executable)
775 endfunction(list_prerequisites_by_glob)