Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CMakeVerifyManifest.cmake
1 # CMakeVerifyManifest.cmake
2 #
3 # This script is used to verify that embeded manifests and
4 # side by side manifests for a project match.  To run this
5 # script, cd to a directory and run the script with cmake -P.
6 # On the command line you can pass in versions that are OK even
7 # if not found in the .manifest files. For example,
8 # cmake -Dallow_versions=8.0.50608.0 -PCmakeVerifyManifest.cmake
9 # could be used to allow an embeded manifest of 8.0.50608.0
10 # to be used in a project even if that version was not found
11 # in the .manifest file.
12
13 # This script first recursively globs *.manifest files from
14 # the current directory.  Then globs *.exe and *.dll.  Each
15 # .exe and .dll is scanned for embeded manifests and the versions
16 # of CRT are compared to those found in the .manifest files
17 # from the first glob.
18
19 #=============================================================================
20 # Copyright 2008-2009 Kitware, Inc.
21 #
22 # Distributed under the OSI-approved BSD License (the "License");
23 # see accompanying file Copyright.txt for details.
24 #
25 # This software is distributed WITHOUT ANY WARRANTY; without even the
26 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 # See the License for more information.
28 #=============================================================================
29 # (To distribute this file outside of CMake, substitute the full
30 #  License text for the above reference.)
31
32
33 # crt_version:
34 # function to extract the CRT version from a file
35 # this can be passed a .exe, .dll, or a .manifest file
36 # it will put the list of versions found into the variable
37 # specified by list_var
38 function(crt_version file list_var)
39   file(STRINGS "${file}" strings REGEX "Microsoft.VC...CRT" NEWLINE_CONSUME)
40   foreach(s ${strings})
41     set(has_match 1)
42     string(REGEX
43       REPLACE ".*<assembly.*\"Microsoft.VC...CRT\".*version=\"([^\"]*)\".*</assembly>.*$" "\\1"
44       version "${s}")
45     if(NOT "${version}" STREQUAL "")
46       list(APPEND version_list ${version})
47     else()
48       message(FATAL_ERROR "Parse error could not find version in [${s}]")
49     endif()
50   endforeach()
51   if(NOT DEFINED has_match)
52     message("Information: no embeded manifest in: ${file}")
53     return()
54   endif()
55   list(APPEND version_list ${${list_var}})
56   list(REMOVE_DUPLICATES version_list)
57   if(version_list)
58     set(${list_var} ${version_list} PARENT_SCOPE)
59   endif()
60 endfunction()
61 set(fatal_error FALSE)
62
63 # check_version:
64 #
65 # test a file against the shipped manifest versions
66 # for a directory
67 function(check_version file manifest_versions)
68   set(manifest_versions ${manifest_versions} ${allow_versions})
69   # collect versions for a given file
70   crt_version(${file} file_versions)
71   # see if the versions
72   foreach(ver ${file_versions})
73     list(FIND manifest_versions "${ver}" found_version)
74     if("${found_version}" EQUAL -1)
75       message("ERROR: ${file} uses ${ver} not found in shipped manifests:[${manifest_versions}].")
76       set(fatal_error TRUE PARENT_SCOPE)
77     endif()
78   endforeach()
79   list(LENGTH file_versions len)
80   if(${len} GREATER 1)
81     message("WARNING: found more than one version of MICROSOFT.VC80.CRT referenced in ${file}: [${file_versions}]")
82   endif()
83 endfunction()
84
85 # collect up the versions of CRT that are shipped
86 # in .manifest files
87 set(manifest_version_list )
88 file(GLOB_RECURSE manifest_files "*.manifest")
89 foreach(f ${manifest_files})
90   crt_version("${f}" manifest_version_list)
91 endforeach()
92 list(LENGTH manifest_version_list LEN)
93 if(LEN EQUAL 0)
94   message(FATAL_ERROR "No .manifest files found, no version check can be done.")
95 endif()
96 message("Versions found in ${manifest_files}: ${manifest_version_list}")
97 if(DEFINED allow_versions)
98   message("Extra versions allowed: ${allow_versions}")
99 endif()
100
101 # now find all .exe and .dll files
102 # and call check_version on each of them
103 file(GLOB_RECURSE exe_files "*.exe")
104 file(GLOB_RECURSE dll_files "*.dll")
105 set(exe_files ${exe_files} ${dll_files})
106 foreach(f ${exe_files})
107   check_version(${f} "${manifest_version_list}")
108 endforeach()
109
110 # report a fatal error if there were any so that cmake will return
111 # a non zero value
112 if(fatal_error)
113   message(FATAL_ERROR "This distribution embeds dll "
114     " versions that it does not ship, and may not work on other machines.")
115 endif()