CMakeVerifyManifest.cmake 3.9 KB

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