CMakeVerifyManifest.cmake 4.2 KB

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