PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/cmake/GetPrerequisites_2_8.cmake

https://bitbucket.org/lindenlab/viewer-beta/
CMake | 786 lines | 417 code | 107 blank | 262 comment | 84 complexity | 4974bf0c4612322976c1e3c522ecf7e2 MD5 | raw file
  1# GetPrerequisites.cmake
  2#
  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.)
  5#
  6# It uses various tools to obtain the list of required shared library files:
  7#   dumpbin (Windows)
  8#   ldd (Linux/Unix)
  9#   otool (Mac OSX)
 10#
 11# The following functions are provided by this script:
 12#   gp_append_unique
 13#   is_file_executable
 14#   gp_item_default_embedded_path
 15#     (projects can override with gp_item_default_embedded_path_override)
 16#   gp_resolve_item
 17#     (projects can override with gp_resolve_item_override)
 18#   gp_resolved_file_type
 19#   gp_file_type
 20#   get_prerequisites
 21#   list_prerequisites
 22#   list_prerequisites_by_glob
 23#
 24# Requires CMake 2.6 or greater because it uses function, break, return and
 25# PARENT_SCOPE.
 26
 27#=============================================================================
 28# Copyright 2008-2009 Kitware, Inc.
 29#
 30# Distributed under the OSI-approved BSD License (the "License");
 31# see accompanying file Copyright.txt for details.
 32#
 33# This software is distributed WITHOUT ANY WARRANTY; without even the
 34# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 35# See the License for more information.
 36#=============================================================================
 37# (To distributed this file outside of CMake, substitute the full
 38#  License text for the above reference.)
 39
 40# gp_append_unique list_var value
 41#
 42# Append value to the list variable ${list_var} only if the value is not
 43# already in the list.
 44#
 45function(gp_append_unique list_var value)
 46  set(contains 0)
 47
 48  foreach(item ${${list_var}})
 49    if("${item}" STREQUAL "${value}")
 50      set(contains 1)
 51      break()
 52    endif("${item}" STREQUAL "${value}")
 53  endforeach(item)
 54
 55  if(NOT contains)
 56    set(${list_var} ${${list_var}} "${value}" PARENT_SCOPE)
 57  endif(NOT contains)
 58endfunction(gp_append_unique)
 59
 60
 61# is_file_executable file result_var
 62#
 63# Return 1 in ${result_var} if ${file} is a binary executable.
 64#
 65# Return 0 in ${result_var} otherwise.
 66#
 67function(is_file_executable file result_var)
 68  #
 69  # A file is not executable until proven otherwise:
 70  #
 71  set(${result_var} 0 PARENT_SCOPE)
 72
 73  get_filename_component(file_full "${file}" ABSOLUTE)
 74  string(TOLOWER "${file_full}" file_full_lower)
 75
 76  # If file name ends in .exe on Windows, *assume* executable:
 77  #
 78  if(WIN32)
 79    if("${file_full_lower}" MATCHES "\\.exe$")
 80      set(${result_var} 1 PARENT_SCOPE)
 81      return()
 82    endif("${file_full_lower}" MATCHES "\\.exe$")
 83
 84    # A clause could be added here that uses output or return value of dumpbin
 85    # to determine ${result_var}. In 99%+? practical cases, the exe name
 86    # match will be sufficient...
 87    #
 88  endif(WIN32)
 89
 90  # Use the information returned from the Unix shell command "file" to
 91  # determine if ${file_full} should be considered an executable file...
 92  #
 93  # If the file command's output contains "executable" and does *not* contain
 94  # "text" then it is likely an executable suitable for prerequisite analysis
 95  # via the get_prerequisites macro.
 96  #
 97  if(UNIX)
 98    if(NOT file_cmd)
 99      find_program(file_cmd "file")
100    endif(NOT file_cmd)
101
102    if(file_cmd)
103      execute_process(COMMAND "${file_cmd}" "${file_full}"
104        OUTPUT_VARIABLE file_ov
105        OUTPUT_STRIP_TRAILING_WHITESPACE
106        )
107
108      # Replace the name of the file in the output with a placeholder token
109      # (the string " _file_full_ ") so that just in case the path name of
110      # the file contains the word "text" or "executable" we are not fooled
111      # into thinking "the wrong thing" because the file name matches the
112      # other 'file' command output we are looking for...
113      #
114      string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
115      string(TOLOWER "${file_ov}" file_ov)
116
117      #message(STATUS "file_ov='${file_ov}'")
118      if("${file_ov}" MATCHES "executable")
119        #message(STATUS "executable!")
120        if("${file_ov}" MATCHES "text")
121          #message(STATUS "but text, so *not* a binary executable!")
122        else("${file_ov}" MATCHES "text")
123          set(${result_var} 1 PARENT_SCOPE)
124          return()
125        endif("${file_ov}" MATCHES "text")
126      endif("${file_ov}" MATCHES "executable")
127    else(file_cmd)
128      message(STATUS "warning: No 'file' command, skipping execute_process...")
129    endif(file_cmd)
130  endif(UNIX)
131endfunction(is_file_executable)
132
133
134# gp_item_default_embedded_path item default_embedded_path_var
135#
136# Return the path that others should refer to the item by when the item
137# is embedded inside a bundle.
138#
139# Override on a per-project basis by providing a project-specific
140# gp_item_default_embedded_path_override function.
141#
142function(gp_item_default_embedded_path item default_embedded_path_var)
143
144  # On Windows and Linux, "embed" prerequisites in the same directory
145  # as the executable by default:
146  #
147  set(path "@executable_path")
148  set(overridden 0)
149
150  # On the Mac, relative to the executable depending on the type
151  # of the thing we are embedding:
152  #
153  if(APPLE)
154    #
155    # The assumption here is that all executables in the bundle will be
156    # in same-level-directories inside the bundle. The parent directory
157    # of an executable inside the bundle should be MacOS or a sibling of
158    # MacOS and all embedded paths returned from here will begin with
159    # "@executable_path/../" and will work from all executables in all
160    # such same-level-directories inside the bundle.
161    #
162
163    # By default, embed things right next to the main bundle executable:
164    #
165    set(path "@executable_path/../../Contents/MacOS")
166
167    # Embed .dylibs right next to the main bundle executable:
168    #
169    if(item MATCHES "\\.dylib$")
170      set(path "@executable_path/../MacOS")
171      set(overridden 1)
172    endif(item MATCHES "\\.dylib$")
173
174    # Embed frameworks in the embedded "Frameworks" directory (sibling of MacOS):
175    #
176    if(NOT overridden)
177      if(item MATCHES "[^/]+\\.framework/")
178        set(path "@executable_path/../Frameworks")
179        set(overridden 1)
180      endif(item MATCHES "[^/]+\\.framework/")
181    endif(NOT overridden)
182  endif()
183
184  # Provide a hook so that projects can override the default embedded location
185  # of any given library by whatever logic they choose:
186  #
187  if(COMMAND gp_item_default_embedded_path_override)
188    gp_item_default_embedded_path_override("${item}" path)
189  endif(COMMAND gp_item_default_embedded_path_override)
190
191  set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
192endfunction(gp_item_default_embedded_path)
193
194
195# gp_resolve_item context item exepath dirs resolved_item_var
196#
197# Resolve an item into an existing full path file.
198#
199# Override on a per-project basis by providing a project-specific
200# gp_resolve_item_override function.
201#
202function(gp_resolve_item context item exepath dirs resolved_item_var)
203  set(resolved 0)
204  set(resolved_item "${item}")
205
206  # Is it already resolved?
207  #
208  if(EXISTS "${resolved_item}")
209    set(resolved 1)
210  endif(EXISTS "${resolved_item}")
211
212  if(NOT resolved)
213    if(item MATCHES "@executable_path")
214      #
215      # @executable_path references are assumed relative to exepath
216      #
217      string(REPLACE "@executable_path" "${exepath}" ri "${item}")
218      get_filename_component(ri "${ri}" ABSOLUTE)
219
220      if(EXISTS "${ri}")
221        #message(STATUS "info: embedded item exists (${ri})")
222        set(resolved 1)
223        set(resolved_item "${ri}")
224      else(EXISTS "${ri}")
225        message(STATUS "warning: embedded item does not exist '${ri}'")
226      endif(EXISTS "${ri}")
227    endif(item MATCHES "@executable_path")
228  endif(NOT resolved)
229
230  if(NOT resolved)
231    if(item MATCHES "@loader_path")
232      #
233      # @loader_path references are assumed relative to the
234      # PATH of the given "context" (presumably another library)
235      #
236      get_filename_component(contextpath "${context}" PATH)
237      string(REPLACE "@loader_path" "${contextpath}" ri "${item}")
238      get_filename_component(ri "${ri}" ABSOLUTE)
239
240      if(EXISTS "${ri}")
241        #message(STATUS "info: embedded item exists (${ri})")
242        set(resolved 1)
243        set(resolved_item "${ri}")
244      else(EXISTS "${ri}")
245        message(STATUS "warning: embedded item does not exist '${ri}'")
246      endif(EXISTS "${ri}")
247    endif(item MATCHES "@loader_path")
248  endif(NOT resolved)
249
250  if(NOT resolved)
251    set(ri "ri-NOTFOUND")
252    find_file(ri "${item}" ${exepath} ${dirs} NO_DEFAULT_PATH)
253    find_file(ri "${item}" ${exepath} ${dirs} /usr/lib)
254    if(ri)
255      #message(STATUS "info: 'find_file' in exepath/dirs (${ri})")
256      set(resolved 1)
257      set(resolved_item "${ri}")
258      set(ri "ri-NOTFOUND")
259    endif(ri)
260  endif(NOT resolved)
261
262  if(NOT resolved)
263    if(item MATCHES "[^/]+\\.framework/")
264      set(fw "fw-NOTFOUND")
265      find_file(fw "${item}"
266        "~/Library/Frameworks"
267        "/Library/Frameworks"
268        "/System/Library/Frameworks"
269      )
270      if(fw)
271        #message(STATUS "info: 'find_file' found framework (${fw})")
272        set(resolved 1)
273        set(resolved_item "${fw}")
274        set(fw "fw-NOTFOUND")
275      endif(fw)
276    endif(item MATCHES "[^/]+\\.framework/")
277  endif(NOT resolved)
278
279  # Using find_program on Windows will find dll files that are in the PATH.
280  # (Converting simple file names into full path names if found.)
281  #
282  if(WIN32)
283  if(NOT resolved)
284    set(ri "ri-NOTFOUND")
285    find_program(ri "${item}" PATHS "${exepath};${dirs}" NO_DEFAULT_PATH)
286    find_program(ri "${item}" PATHS "${exepath};${dirs}")
287    if(ri)
288      #message(STATUS "info: 'find_program' in exepath/dirs (${ri})")
289      set(resolved 1)
290      set(resolved_item "${ri}")
291      set(ri "ri-NOTFOUND")
292    endif(ri)
293  endif(NOT resolved)
294  endif(WIN32)
295
296  # Provide a hook so that projects can override item resolution
297  # by whatever logic they choose:
298  #
299  if(COMMAND gp_resolve_item_override)
300    gp_resolve_item_override("${context}" "${item}" "${exepath}" "${dirs}" resolved_item resolved)
301  endif(COMMAND gp_resolve_item_override)
302
303  if(NOT resolved)
304    message(STATUS "
305warning: cannot resolve item '${item}'
306
307  possible problems:
308    need more directories?
309    need to use InstallRequiredSystemLibraries?
310    run in install tree instead of build tree?
311")
312#    message(STATUS "
313#******************************************************************************
314#warning: cannot resolve item '${item}'
315#
316#  possible problems:
317#    need more directories?
318#    need to use InstallRequiredSystemLibraries?
319#    run in install tree instead of build tree?
320#
321#    context='${context}'
322#    item='${item}'
323#    exepath='${exepath}'
324#    dirs='${dirs}'
325#    resolved_item_var='${resolved_item_var}'
326#******************************************************************************
327#")
328  endif(NOT resolved)
329
330  set(${resolved_item_var} "${resolved_item}" PARENT_SCOPE)
331endfunction(gp_resolve_item)
332
333
334# gp_resolved_file_type original_file file exepath dirs type_var
335#
336# Return the type of ${file} with respect to ${original_file}. String
337# describing type of prerequisite is returned in variable named ${type_var}.
338#
339# Use ${exepath} and ${dirs} if necessary to resolve non-absolute ${file}
340# values -- but only for non-embedded items.
341#
342# Possible types are:
343#   system
344#   local
345#   embedded
346#   other
347#
348function(gp_resolved_file_type original_file file exepath dirs type_var)
349  #message(STATUS "**")
350
351  if(NOT IS_ABSOLUTE "${original_file}")
352    message(STATUS "warning: gp_resolved_file_type expects absolute full path for first arg original_file")
353  endif()
354
355  set(is_embedded 0)
356  set(is_local 0)
357  set(is_system 0)
358
359  set(resolved_file "${file}")
360
361  if("${file}" MATCHES "^@(executable|loader)_path")
362    set(is_embedded 1)
363  endif()
364
365  if(NOT is_embedded)
366    if(NOT IS_ABSOLUTE "${file}")
367      gp_resolve_item("${original_file}" "${file}" "${exepath}" "${dirs}" resolved_file)
368    endif()
369
370    string(TOLOWER "${original_file}" original_lower)
371    string(TOLOWER "${resolved_file}" lower)
372
373    if(UNIX)
374      if(resolved_file MATCHES "^(/lib/|/lib32/|/lib64/|/usr/lib/|/usr/lib32/|/usr/lib64/|/usr/X11R6/)")
375        set(is_system 1)
376      endif()
377    endif()
378
379    if(APPLE)
380      if(resolved_file MATCHES "^(/System/Library/|/usr/lib/)")
381        set(is_system 1)
382      endif()
383    endif()
384
385    if(WIN32)
386      string(TOLOWER "$ENV{SystemRoot}" sysroot)
387      string(REGEX REPLACE "\\\\" "/" sysroot "${sysroot}")
388
389      string(TOLOWER "$ENV{windir}" windir)
390      string(REGEX REPLACE "\\\\" "/" windir "${windir}")
391
392      if(lower MATCHES "^(${sysroot}/system|${windir}/system|${sysroot}/syswow|${windir}/syswow|(.*/)*msvc[^/]+dll)")
393        set(is_system 1)
394      endif()
395    endif()
396
397    if(NOT is_system)
398      get_filename_component(original_path "${original_lower}" PATH)
399      get_filename_component(path "${lower}" PATH)
400      if("${original_path}" STREQUAL "${path}")
401        set(is_local 1)
402      endif()
403    endif()
404  endif()
405
406  # Return type string based on computed booleans:
407  #
408  set(type "other")
409
410  if(is_system)
411    set(type "system")
412  elseif(is_embedded)
413    set(type "embedded")
414  elseif(is_local)
415    set(type "local")
416  endif()
417
418  #message(STATUS "gp_resolved_file_type: '${file}' '${resolved_file}'")
419  #message(STATUS "                type: '${type}'")
420
421  if(NOT is_embedded)
422    if(NOT IS_ABSOLUTE "${resolved_file}")
423      if(lower MATCHES "^msvc[^/]+dll" AND is_system)
424        message(STATUS "info: non-absolute msvc file '${file}' returning type '${type}'")
425      else()
426        message(STATUS "warning: gp_resolved_file_type non-absolute file '${file}' returning type '${type}' -- possibly incorrect")
427      endif()
428    endif()
429  endif()
430
431  set(${type_var} "${type}" PARENT_SCOPE)
432
433  #message(STATUS "**")
434endfunction()
435
436
437# gp_file_type original_file file type_var
438#
439# Return the type of ${file} with respect to ${original_file}. String
440# describing type of prerequisite is returned in variable named ${type_var}.
441#
442# Possible types are:
443#   system
444#   local
445#   embedded
446#   other
447#
448function(gp_file_type original_file file type_var)
449  if(NOT IS_ABSOLUTE "${original_file}")
450    message(STATUS "warning: gp_file_type expects absolute full path for first arg original_file")
451  endif()
452
453  get_filename_component(exepath "${original_file}" PATH)
454
455  set(type "")
456  gp_resolved_file_type("${original_file}" "${file}" "${exepath}" "" type)
457
458  set(${type_var} "${type}" PARENT_SCOPE)
459endfunction(gp_file_type)
460
461
462# get_prerequisites target prerequisites_var exclude_system recurse dirs
463#
464# Get the list of shared library files required by ${target}. The list in
465# the variable named ${prerequisites_var} should be empty on first entry to
466# this function. On exit, ${prerequisites_var} will contain the list of
467# required shared library files.
468#
469#  target is the full path to an executable file
470#
471#  prerequisites_var is the name of a CMake variable to contain the results
472#
473#  exclude_system is 0 or 1: 0 to include "system" prerequisites , 1 to
474#   exclude them
475#
476#  recurse is 0 or 1: 0 for direct prerequisites only, 1 for all prerequisites
477#   recursively
478#
479#  exepath is the path to the top level executable used for @executable_path
480#   replacment on the Mac
481#
482#  dirs is a list of paths where libraries might be found: these paths are
483#   searched first when a target without any path info is given. Then standard
484#   system locations are also searched: PATH, Framework locations, /usr/lib...
485#
486function(get_prerequisites target prerequisites_var exclude_system recurse exepath dirs)
487  set(verbose 0)
488  set(eol_char "E")
489
490  if(NOT IS_ABSOLUTE "${target}")
491    message("warning: target '${target}' is not absolute...")
492  endif(NOT IS_ABSOLUTE "${target}")
493
494  if(NOT EXISTS "${target}")
495    message("warning: target '${target}' does not exist...")
496  endif(NOT EXISTS "${target}")
497
498  # <setup-gp_tool-vars>
499  #
500  # Try to choose the right tool by default. Caller can set gp_tool prior to
501  # calling this function to force using a different tool.
502  #
503  if("${gp_tool}" STREQUAL "")
504    set(gp_tool "ldd")
505    if(APPLE)
506      set(gp_tool "otool")
507    endif(APPLE)
508    if(WIN32)
509      set(gp_tool "dumpbin")
510    endif(WIN32)
511  endif("${gp_tool}" STREQUAL "")
512
513  set(gp_tool_known 0)
514
515  if("${gp_tool}" STREQUAL "ldd")
516    set(gp_cmd_args "")
517    set(gp_regex "^[\t ]*[^\t ]+ => ([^\t ]+).*${eol_char}$")
518    set(gp_regex_cmp_count 1)
519    set(gp_tool_known 1)
520  endif("${gp_tool}" STREQUAL "ldd")
521
522  if("${gp_tool}" STREQUAL "otool")
523    set(gp_cmd_args "-L")
524    set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
525    set(gp_regex_cmp_count 3)
526    set(gp_tool_known 1)
527  endif("${gp_tool}" STREQUAL "otool")
528
529  if("${gp_tool}" STREQUAL "dumpbin")
530    set(gp_cmd_args "/dependents")
531    set(gp_regex "^    ([^ ].*[Dd][Ll][Ll])${eol_char}$")
532    set(gp_regex_cmp_count 1)
533    set(gp_tool_known 1)
534    set(ENV{VS_UNICODE_OUTPUT} "") # Block extra output from inside VS IDE.
535  endif("${gp_tool}" STREQUAL "dumpbin")
536
537  if(NOT gp_tool_known)
538    message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
539    message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
540    message(STATUS "Valid gp_tool values are dumpbin, ldd and otool.")
541    return()
542  endif(NOT gp_tool_known)
543
544  set(gp_cmd_paths ${gp_cmd_paths}
545    "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
546    "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
547    "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
548    "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
549    "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
550    "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
551    "/usr/local/bin"
552    "/usr/bin"
553    )
554
555  find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
556
557  if(NOT gp_cmd)
558    message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
559    return()
560  endif(NOT gp_cmd)
561
562  if("${gp_tool}" STREQUAL "dumpbin")
563    # When running dumpbin, it also needs the "Common7/IDE" directory in the
564    # PATH. It will already be in the PATH if being run from a Visual Studio
565    # command prompt. Add it to the PATH here in case we are running from a
566    # different command prompt.
567    #
568    get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
569    get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
570    if(EXISTS "${gp_cmd_dlls_dir}")
571      # only add to the path if it is not already in the path
572      if(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
573        set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
574      endif(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
575    endif(EXISTS "${gp_cmd_dlls_dir}")
576  endif("${gp_tool}" STREQUAL "dumpbin")
577  #
578  # </setup-gp_tool-vars>
579
580  if("${gp_tool}" STREQUAL "ldd")
581    set(old_ld_env "$ENV{LD_LIBRARY_PATH}")
582    foreach(dir ${exepath} ${dirs})
583      set(ENV{LD_LIBRARY_PATH} "${dir}:$ENV{LD_LIBRARY_PATH}")
584    endforeach(dir)
585  endif("${gp_tool}" STREQUAL "ldd")
586
587
588  # Track new prerequisites at each new level of recursion. Start with an
589  # empty list at each level:
590  #
591  set(unseen_prereqs)
592
593  # Run gp_cmd on the target:
594  #
595  execute_process(
596    COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
597    OUTPUT_VARIABLE gp_cmd_ov
598    )
599
600  if("${gp_tool}" STREQUAL "ldd")
601    set(ENV{LD_LIBRARY_PATH} "${old_ld_env}")
602  endif("${gp_tool}" STREQUAL "ldd")
603
604  if(verbose)
605    message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
606    message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
607    message(STATUS "</RawOutput>")
608  endif(verbose)
609
610  get_filename_component(target_dir "${target}" PATH)
611
612  # Convert to a list of lines:
613  #
614  string(REGEX REPLACE ";" "\\\\;" candidates "${gp_cmd_ov}")
615  string(REGEX REPLACE "\n" "${eol_char};" candidates "${candidates}")
616
617  # Analyze each line for file names that match the regular expression:
618  #
619  foreach(candidate ${candidates})
620  if("${candidate}" MATCHES "${gp_regex}")
621    # Extract information from each candidate:
622    string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
623
624    if(gp_regex_cmp_count GREATER 1)
625      string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
626      string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
627      string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
628      string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
629    endif(gp_regex_cmp_count GREATER 1)
630
631    if(gp_regex_cmp_count GREATER 2)
632      string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
633      string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
634      string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
635      string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
636    endif(gp_regex_cmp_count GREATER 2)
637
638    # Use the raw_item as the list entries returned by this function. Use the
639    # gp_resolve_item function to resolve it to an actual full path file if
640    # necessary.
641    #
642    set(item "${raw_item}")
643
644    # Add each item unless it is excluded:
645    #
646    set(add_item 1)
647
648    if(${exclude_system})
649      set(type "")
650      gp_resolved_file_type("${target}" "${item}" "${exepath}" "${dirs}" type)
651      if("${type}" STREQUAL "system")
652        set(add_item 0)
653      endif("${type}" STREQUAL "system")
654    endif(${exclude_system})
655
656    if(add_item)
657      list(LENGTH ${prerequisites_var} list_length_before_append)
658      gp_append_unique(${prerequisites_var} "${item}")
659      list(LENGTH ${prerequisites_var} list_length_after_append)
660
661      if(${recurse})
662        # If item was really added, this is the first time we have seen it.
663        # Add it to unseen_prereqs so that we can recursively add *its*
664        # prerequisites...
665        #
666        # But first: resolve its name to an absolute full path name such
667        # that the analysis tools can simply accept it as input.
668        #
669        if(NOT list_length_before_append EQUAL list_length_after_append)
670          gp_resolve_item("${target}" "${item}" "${exepath}" "${dirs}" resolved_item)
671          set(unseen_prereqs ${unseen_prereqs} "${resolved_item}")
672        endif(NOT list_length_before_append EQUAL list_length_after_append)
673      endif(${recurse})
674    endif(add_item)
675  else("${candidate}" MATCHES "${gp_regex}")
676    if(verbose)
677      message(STATUS "ignoring non-matching line: '${candidate}'")
678    endif(verbose)
679  endif("${candidate}" MATCHES "${gp_regex}")
680  endforeach(candidate)
681
682  list(LENGTH ${prerequisites_var} prerequisites_var_length)
683  if(prerequisites_var_length GREATER 0)
684    list(SORT ${prerequisites_var})
685  endif(prerequisites_var_length GREATER 0)
686  if(${recurse})
687    set(more_inputs ${unseen_prereqs})
688    foreach(input ${more_inputs})
689      get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse} "${exepath}" "${dirs}")
690    endforeach(input)
691  endif(${recurse})
692
693  set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
694endfunction(get_prerequisites)
695
696
697# list_prerequisites target all exclude_system verbose
698#
699#  ARGV0 (target) is the full path to an executable file
700#
701#  optional ARGV1 (all) is 0 or 1: 0 for direct prerequisites only,
702#   1 for all prerequisites recursively
703#
704#  optional ARGV2 (exclude_system) is 0 or 1: 0 to include "system"
705#   prerequisites , 1 to exclude them
706#
707#  optional ARGV3 (verbose) is 0 or 1: 0 to print only full path
708#   names of prerequisites, 1 to print extra information
709#
710function(list_prerequisites target)
711  if("${ARGV1}" STREQUAL "")
712    set(all 1)
713  else("${ARGV1}" STREQUAL "")
714    set(all "${ARGV1}")
715  endif("${ARGV1}" STREQUAL "")
716
717  if("${ARGV2}" STREQUAL "")
718    set(exclude_system 0)
719  else("${ARGV2}" STREQUAL "")
720    set(exclude_system "${ARGV2}")
721  endif("${ARGV2}" STREQUAL "")
722
723  if("${ARGV3}" STREQUAL "")
724    set(verbose 0)
725  else("${ARGV3}" STREQUAL "")
726    set(verbose "${ARGV3}")
727  endif("${ARGV3}" STREQUAL "")
728
729  set(count 0)
730  set(count_str "")
731  set(print_count "${verbose}")
732  set(print_prerequisite_type "${verbose}")
733  set(print_target "${verbose}")
734  set(type_str "")
735
736  get_filename_component(exepath "${target}" PATH)
737
738  set(prereqs "")
739  get_prerequisites("${target}" prereqs ${exclude_system} ${all} "${exepath}" "")
740
741  if(print_target)
742    message(STATUS "File '${target}' depends on:")
743  endif(print_target)
744
745  foreach(d ${prereqs})
746    math(EXPR count "${count} + 1")
747
748    if(print_count)
749      set(count_str "${count}. ")
750    endif(print_count)
751
752    if(print_prerequisite_type)
753      gp_file_type("${target}" "${d}" type)
754      set(type_str " (${type})")
755    endif(print_prerequisite_type)
756
757    message(STATUS "${count_str}${d}${type_str}")
758  endforeach(d)
759endfunction(list_prerequisites)
760
761
762# list_prerequisites_by_glob glob_arg glob_exp
763#
764#  glob_arg is GLOB or GLOB_RECURSE
765#
766#  glob_exp is a globbing expression used with "file(GLOB" to retrieve a list
767#   of matching files. If a matching file is executable, its prerequisites are
768#   listed.
769#
770# Any additional (optional) arguments provided are passed along as the
771# optional arguments to the list_prerequisites calls.
772#
773function(list_prerequisites_by_glob glob_arg glob_exp)
774  message(STATUS "=============================================================================")
775  message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
776  message(STATUS "")
777  file(${glob_arg} file_list ${glob_exp})
778  foreach(f ${file_list})
779    is_file_executable("${f}" is_f_executable)
780    if(is_f_executable)
781      message(STATUS "=============================================================================")
782      list_prerequisites("${f}" ${ARGN})
783      message(STATUS "")
784    endif(is_f_executable)
785  endforeach(f)
786endfunction(list_prerequisites_by_glob)