RunCPackVerifyResult.cmake 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # prevent older policies from interfering with this script
  2. cmake_policy(PUSH)
  3. cmake_policy(VERSION ${CMAKE_VERSION})
  4. include(CMakeParseArguments)
  5. message(STATUS "=============================================================================")
  6. message(STATUS "CTEST_FULL_OUTPUT (Avoid ctest truncation of output)")
  7. message(STATUS "")
  8. if(NOT CPackComponentsDEB_BINARY_DIR)
  9. message(FATAL_ERROR "CPackComponentsDEB_BINARY_DIR not set")
  10. endif()
  11. if(NOT CPackGen)
  12. message(FATAL_ERROR "CPackGen not set")
  13. endif()
  14. message("CMAKE_CPACK_COMMAND = ${CMAKE_CPACK_COMMAND}")
  15. if(NOT CMAKE_CPACK_COMMAND)
  16. message(FATAL_ERROR "CMAKE_CPACK_COMMAND not set")
  17. endif()
  18. if(NOT CPackDEBConfiguration)
  19. message(FATAL_ERROR "CPackDEBConfiguration not set")
  20. endif()
  21. # run cpack with some options and returns the list of files generated
  22. # -output_expected_file: list of files that match the pattern
  23. function(run_cpack output_expected_file CPack_output_parent CPack_error_parent)
  24. set(options "EXPECT_FAILURE")
  25. set(oneValueArgs "EXPECTED_FILE_MASK" "CONFIG_VERBOSE")
  26. set(multiValueArgs "CONFIG_ARGS")
  27. cmake_parse_arguments(run_cpack_deb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  28. # clean-up previously CPack generated files
  29. if(${run_cpack_deb_EXPECTED_FILE_MASK})
  30. file(GLOB expected_file "${${run_cpack_deb_EXPECTED_FILE_MASK}}")
  31. if (expected_file)
  32. file(REMOVE "${expected_file}")
  33. endif()
  34. endif()
  35. message("config_args = ${run_cpack_deb_CONFIG_ARGS}")
  36. message("config_verbose = ${run_cpack_deb_CONFIG_VERBOSE}")
  37. set(_backup_lang "$ENV{LANG}")
  38. set(_backup_lc_all "$ENV{LC_ALL}")
  39. set(ENV{LANG} "C")
  40. set(ENV{LC_ALL} "C")
  41. execute_process(COMMAND ${CMAKE_CPACK_COMMAND} ${run_cpack_deb_CONFIG_VERBOSE} -G ${CPackGen} -C "${CONFIG}" ${run_cpack_deb_CONFIG_ARGS}
  42. RESULT_VARIABLE CPack_result
  43. OUTPUT_VARIABLE CPack_output
  44. ERROR_VARIABLE CPack_error
  45. WORKING_DIRECTORY ${CPackComponentsDEB_BINARY_DIR})
  46. set(ENV{LANG} "${_backup_lang}")
  47. set(ENV{LC_ALL} "${_backup_lc_all}")
  48. set(${CPack_output_parent} ${CPack_output} PARENT_SCOPE)
  49. set(${CPack_error_parent} ${CPack_error} PARENT_SCOPE)
  50. if (CPack_result AND NOT run_cpack_deb_EXPECT_FAILURE)
  51. message(FATAL_ERROR "error: CPack execution went wrong!, CPack_output=${CPack_output}, CPack_error=${CPack_error}")
  52. elseif (NOT CPack_result AND run_cpack_deb_EXPECT_FAILURE)
  53. message(FATAL_ERROR "error: CPack execution succeeded although failure was expected!, CPack_output=${CPack_output}, CPack_error=${CPack_error}")
  54. else ()
  55. message(STATUS "CPack_output=${CPack_output}")
  56. message(STATUS "CPack_error=${CPack_error}")
  57. endif()
  58. if(run_cpack_deb_EXPECTED_FILE_MASK)
  59. file(GLOB _output_expected_file "${run_cpack_deb_EXPECTED_FILE_MASK}")
  60. set(${output_expected_file} "${_output_expected_file}" PARENT_SCOPE)
  61. endif()
  62. endfunction()
  63. # This function runs lintian on a .deb and returns its output
  64. function(run_lintian lintian_output)
  65. set(${lintian_output} "" PARENT_SCOPE)
  66. find_program(LINTIAN_EXECUTABLE lintian)
  67. if(LINTIAN_EXECUTABLE)
  68. set(options "")
  69. set(oneValueArgs "FILENAME")
  70. set(multiValueArgs "")
  71. cmake_parse_arguments(run_lintian_deb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  72. if(NOT run_lintian_deb_FILENAME)
  73. message(FATAL_ERROR "error: run_lintian needs FILENAME to be set")
  74. endif()
  75. # run dpkg-deb
  76. execute_process(COMMAND ${LINTIAN_EXECUTABLE} ${run_lintian_deb_FILENAME}
  77. WORKING_DIRECTORY "${CPACK_TEMPORARY_DIRECTORY}"
  78. OUTPUT_VARIABLE LINTIAN_OUTPUT
  79. RESULT_VARIABLE LINTIAN_RESULT
  80. ERROR_VARIABLE LINTIAN_ERROR
  81. OUTPUT_STRIP_TRAILING_WHITESPACE )
  82. set(${lintian_output} "${LINTIAN_OUTPUT}" PARENT_SCOPE)
  83. else()
  84. message(FATAL_ERROR "run_lintian called without lintian executable being present")
  85. endif()
  86. endfunction()
  87. # Higher level lintian check that parse the output for errors and required strings
  88. function(lintian_check_specific_errors output_errors)
  89. set(${output_errors} "" PARENT_SCOPE)
  90. set(ERROR_ACC)
  91. set(options "")
  92. set(oneValueArgs "FILENAME")
  93. set(multiValueArgs "ERROR_REGEX_STRINGS")
  94. cmake_parse_arguments(lintian_check_specific_errors_deb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  95. set(lintian_output)
  96. run_lintian(lintian_output FILENAME ${lintian_check_specific_errors_deb_FILENAME})
  97. message(STATUS "Lintian output is ''${lintian_output}'")
  98. # regex to avoid
  99. foreach(_s IN LISTS lintian_check_specific_errors_deb_ERROR_REGEX_STRINGS)
  100. if(_s STREQUAL "")
  101. continue()
  102. endif()
  103. string(REGEX MATCHALL "${_s}" "_TMP_CHECK_ERROR" "${lintian_output}")
  104. if(NOT _TMP_CHECK_ERROR STREQUAL "")
  105. string(APPEND ERROR_ACC "\nlintian: ${_f}: output contains an undesirable regex:\n\t${_TMP_CHECK_ERROR}")
  106. endif()
  107. endforeach()
  108. set(${output_errors} "${ERROR_ACC}" PARENT_SCOPE)
  109. endfunction()
  110. # This function runs dpkg-deb on a .deb and returns its output
  111. # the default behavior it to run "--info" on the specified Debian package
  112. # ACTION is one of the option accepted by dpkg-deb
  113. function(run_dpkgdeb dpkg_deb_output)
  114. set(${dpkg_deb_output} "" PARENT_SCOPE)
  115. find_program(DPKGDEB_EXECUTABLE dpkg-deb)
  116. if(DPKGDEB_EXECUTABLE)
  117. set(options "")
  118. set(oneValueArgs "FILENAME" "ACTION")
  119. set(multiValueArgs "")
  120. cmake_parse_arguments(run_dpkgdeb_deb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  121. if(NOT run_dpkgdeb_deb_FILENAME)
  122. message(FATAL_ERROR "error: run_dpkgdeb needs FILENAME to be set")
  123. endif()
  124. if(NOT run_dpkgdeb_deb_ACTION)
  125. set(run_dpkgdeb_deb_ACTION "--info")
  126. endif()
  127. # run dpkg-deb
  128. execute_process(COMMAND ${DPKGDEB_EXECUTABLE} ${run_dpkgdeb_deb_ACTION} ${run_dpkgdeb_deb_FILENAME}
  129. WORKING_DIRECTORY "${CPACK_TEMPORARY_DIRECTORY}"
  130. OUTPUT_VARIABLE DPKGDEB_OUTPUT
  131. RESULT_VARIABLE DPKGDEB_RESULT
  132. ERROR_VARIABLE DPKGDEB_ERROR
  133. OUTPUT_STRIP_TRAILING_WHITESPACE )
  134. if(NOT DPKGDEB_RESULT EQUAL "0")
  135. message(FATAL_ERROR "Error '${DPKGDEB_RESULT}' returned by dpkg-deb: '${DPKGDEB_ERROR}'")
  136. endif()
  137. set(${dpkg_deb_output} "${DPKGDEB_OUTPUT}" PARENT_SCOPE)
  138. else()
  139. message(FATAL_ERROR "run_dpkgdeb called without dpkg-deb executable being present")
  140. endif()
  141. endfunction()
  142. # returns a particular line of the metadata of the .deb, for checking
  143. # a previous call to run_dpkgdeb should provide the DPKGDEB_OUTPUT entry.
  144. function(dpkgdeb_return_specific_metaentry output)
  145. set(${output} "" PARENT_SCOPE)
  146. set(options "")
  147. set(oneValueArgs "DPKGDEB_OUTPUT" "METAENTRY")
  148. set(multiValueArgs "")
  149. cmake_parse_arguments(dpkgdeb_return_specific_metaentry_deb "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
  150. if(NOT dpkgdeb_return_specific_metaentry_deb_METAENTRY)
  151. message(FATAL_ERROR "error: dpkgdeb_return_specific_metaentry needs METAENTRY to be set")
  152. endif()
  153. string(REGEX MATCH "${dpkgdeb_return_specific_metaentry_deb_METAENTRY}([^\r\n]*)" _TMP_STR "${dpkgdeb_return_specific_metaentry_deb_DPKGDEB_OUTPUT}")
  154. #message("################ _TMP_STR = ${CMAKE_MATCH_1} ##################")
  155. if(NOT "${CMAKE_MATCH_1}" STREQUAL "")
  156. string(STRIP "${CMAKE_MATCH_1}" _TMP_STR)
  157. set(${output} "${_TMP_STR}" PARENT_SCOPE)
  158. endif()
  159. endfunction()
  160. function(get_package_description DPKG_OUTPUT RESULT_VAR)
  161. string(UUID uuid NAMESPACE 00000000-0000-0000-0000-000000000000 TYPE SHA1)
  162. string(REPLACE ";" "${uuid}" DPKG_OUTPUT "${DPKG_OUTPUT}")
  163. string(REPLACE "\n" ";" DPKG_OUTPUT "${DPKG_OUTPUT}")
  164. unset(_actual_description)
  165. set(_parse_description FALSE)
  166. foreach(_line IN LISTS DPKG_OUTPUT)
  167. if(_line MATCHES " Description:.*")
  168. set(_parse_description TRUE)
  169. string(REPLACE " Description: " "" _line "${_line}")
  170. list(APPEND _actual_description "${_line}")
  171. elseif(_parse_description)
  172. if(_line MATCHES " [A-Z][A-Za-z\-]+: .*")
  173. set(_parse_description FALSE)
  174. else()
  175. list(APPEND _actual_description "${_line}")
  176. endif()
  177. endif()
  178. endforeach()
  179. list(JOIN _actual_description "\n" _actual_description)
  180. set(${RESULT_VAR} "${_actual_description}" PARENT_SCOPE)
  181. endfunction()
  182. cmake_policy(POP)