Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
ctest: Improve CMake scripts
Browse files Browse the repository at this point in the history
Do not use CMAKE_ARGV<n> variables.
Use dedicated variables instead.
  • Loading branch information
RoEdAl committed May 7, 2024
1 parent 97c2327 commit d12f214
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 40 deletions.
18 changes: 8 additions & 10 deletions cmake/test/AST_BUILDOPT_SUM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)

IF(NOT DEFINED CMAKE_ARGV3)
IF(NOT DEFINED MODULE_PATH)
MESSAGE(FATAL_ERROR "Library not specified")
ENDIF()

IF(NOT DEFINED CMAKE_ARGV4)
IF(NOT DEFINED AST_BUILDOPT_SUM)
MESSAGE(FATAL_ERROR "Checksum not specified")
ENDIF()

MESSAGE(DEBUG "DLL:\t${CMAKE_ARGV3}")
STRING(TOLOWER "${CMAKE_ARGV4}" AST_BUILDOPT_SUM)
MESSAGE(DEBUG "DLL:\t${MODULE_PATH}")
MESSAGE(DEBUG "CHECKSUM:\t${AST_BUILDOPT_SUM}")

EXECUTE_PROCESS(
COMMAND grep -Eao "[0-9abcdef]{32}" ${CMAKE_ARGV3}
COMMAND grep -Eao "[0-9abcdef]{32}" ${MODULE_PATH}
OUTPUT_VARIABLE CHECKSUM_CANDIDATES_ML
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY
Expand All @@ -27,20 +26,19 @@ EXECUTE_PROCESS(
STRING(REGEX MATCHALL "[^\n\r]+" CHECKSUM_CANDIDATES ${CHECKSUM_CANDIDATES_ML})
LIST(LENGTH CHECKSUM_CANDIDATES CHECKSUM_CANDIDATES_CNT)
IF (${CHECKSUM_CANDIDATES_CNT} EQUAL 0)
MESSAGE(FATAL_ERROR "Could not find checksums in ${${CMAKE_ARGV3}}")
MESSAGE(FATAL_ERROR "Could not find checksums in ${MODULE_PATH}")
ENDIF()

SET(TEST_PASSED)
FOREACH(l IN LISTS CHECKSUM_CANDIDATES)
IF(${l} STREQUAL ${AST_BUILDOPT_SUM})
MESSAGE(STATUS "AST_BUILDOPT_SUM: ${l} ✓")
MESSAGE(STATUS "[checksum] ${l}✓")
SET(TEST_PASSED ON)
ELSE()
MESSAGE(STATUS "AST_BUILDOPT_SUM: ${l} ⍻")
MESSAGE(STATUS "[checksum] ${l}⍻")
ENDIF()
ENDFOREACH()

IF(NOT TEST_PASSED)
MESSAGE(FATAL_ERROR "Checksum ${AST_BUILDOPT_SUM} not found")
MESSAGE(FATAL_ERROR "Checksum not found")
ENDIF()

31 changes: 31 additions & 0 deletions cmake/test/arch-specific.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
# arch-specific
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)

IF(NOT DEFINED CMAKE_READELF)
MESSAGE(FATAL_ERROR "readelf utility not specified")
ENDIF()

IF(NOT DEFINED MODULE_PATH)
MESSAGE(FATAL_ERROR "Library not specified")
ENDIF()

EXECUTE_PROCESS(
COMMAND ${CMAKE_READELF} -AW ${MODULE_PATH}
OUTPUT_VARIABLE ARCH_SPECIFIC_NL
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY
TIMEOUT 15
)

IF(ARCH_SPECIFIC_NL)
STRING(REGEX MATCHALL "[^\n\r]+" ARCH_SPECIFIC ${ARCH_SPECIFIC_NL})
FOREACH(l IN LISTS ARCH_SPECIFIC)
IF("${l}" MATCHES "^[\t ]*(.+):[\t ]*\"(.+)\"$")
MESSAGE(STATUS "[arch-specific] ${CMAKE_MATCH_1}: ${CMAKE_MATCH_2}")
ELSEIF("${l}" MATCHES "^[\t ]*(.+):[\t ]*(.+)$")
MESSAGE(STATUS "[arch-specific] ${CMAKE_MATCH_1}: ${CMAKE_MATCH_2}")
ENDIF()
ENDFOREACH()
ENDIF()
16 changes: 8 additions & 8 deletions cmake/test/comment-section.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)

IF(NOT DEFINED CMAKE_ARGV3)
IF(NOT DEFINED CMAKE_READELF)
MESSAGE(FATAL_ERROR "readelf not specified")
ENDIF()

IF(NOT DEFINED CMAKE_ARGV4)
IF(NOT DEFINED MODULE_PATH)
MESSAGE(FATAL_ERROR "Library not specified")
ENDIF()

IF(NOT DEFINED CMAKE_ARGV5)
IF(NOT DEFINED COMPILER_VERSION)
MESSAGE(FATAL_ERROR "Compiler version not specified")
ENDIF()

EXECUTE_PROCESS(
COMMAND ${CMAKE_ARGV3} -p ".comment" -W ${CMAKE_ARGV4}
COMMAND ${CMAKE_READELF} -p ".comment" -W ${MODULE_PATH}
OUTPUT_VARIABLE COMMENTS_NL
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY
TIMEOUT 15
)

STRING(REPLACE "." "\\." ${CMAKE_ARGV5} GCC_VERSION_MATCHER)
STRING(REPLACE "." "\\." ${COMPILER_VERSION} GCC_VERSION_MATCHER)
SET(TEST_PASSED)
SET(COMMENT_LINES)
STRING(REGEX MATCHALL "[^\n\r]+" COMMENTS ${COMMENTS_NL})
Expand All @@ -36,14 +36,14 @@ FOREACH(l IN LISTS COMMENTS)
IF("${COMMENT_LINE}" MATCHES "GCC\\:" AND "${COMMENT_LINE}" MATCHES "${GCC_VERSION_MATCHER}")
SET(TEST_PASSED ON)
LIST(APPEND COMMENT_LINES "${COMMENT_LINE}")
MESSAGE(STATUS "Comment: ${COMMENT_LINE} ✓")
MESSAGE(STATUS "[comment] ${COMMENT_LINE}✓")
ELSE()
MESSAGE(VERBOSE "Comment: ${COMMENT_LINE}")
MESSAGE(VERBOSE "[comment] ${COMMENT_LINE}")
ENDIF()
ENDFOREACH()

LIST(LENGTH COMMENT_LINES COMMENT_LINES_CNT)
MESSAGE(STATUS "Status: ${COMMENT_LINES_CNT} comment(s) found")
MESSAGE(STATUS "[status] comments:${COMMENT_LINES_CNT}")

IF(NOT TEST_PASSED)
MESSAGE(FATAL_ERROR "Check failed")
Expand Down
30 changes: 30 additions & 0 deletions cmake/test/dump-header.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# dump-header
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)

IF(NOT DEFINED CMAKE_READELF)
MESSAGE(FATAL_ERROR "readelf utility not specified")
ENDIF()

IF(NOT DEFINED MODULE_PATH)
MESSAGE(FATAL_ERROR "Library not specified")
ENDIF()

EXECUTE_PROCESS(
COMMAND ${CMAKE_READELF} -hW ${MODULE_PATH}
OUTPUT_VARIABLE DUMP_HEADER_NL
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY
TIMEOUT 15
)

STRING(REGEX MATCHALL "[^\n\r]+" DUMP_HEADER ${DUMP_HEADER_NL})
FOREACH(l IN LISTS DUMP_HEADER)
IF("${l}" MATCHES "^[\t ]*(.+):[\t ]*\"(.+)\"$")
MESSAGE(STATUS "[header] ${CMAKE_MATCH_1}: ${CMAKE_MATCH_2}")
ELSEIF("${l}" MATCHES "^[\t ]*(.+):[\t ]*(.+)$")
MESSAGE(STATUS "[header] ${CMAKE_MATCH_1}: ${CMAKE_MATCH_2}")
ENDIF()
ENDFOREACH()

25 changes: 11 additions & 14 deletions cmake/test/needed-libs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#
CMAKE_MINIMUM_REQUIRED(VERSION 3.20)

CMAKE_POLICY(SET CMP0057 NEW)
SET(ALLOWED_LIBS libasound libsqlite3 libc libpthread)
SET(ALLOWED_LIBS libasound libsqlite3 libc libpthread libgcc_s)

FUNCTION(GetNeededLib NEEDED_LIB)
STRING(REGEX REPLACE "^\\[" "" NEEDED_LIB1 ${NEEDED_LIB})
Expand All @@ -25,18 +24,16 @@ FUNCTION(CheckLibrary LIB_NAME OUT_VAR)
SET("${OUT_VAR}_RESULT" PARENT_SCOPE)
ENDFUNCTION()

IF(NOT DEFINED CMAKE_ARGV3)
MESSAGE(FATAL_ERROR "readelf not specified")
IF(NOT DEFINED CMAKE_READELF)
MESSAGE(FATAL_ERROR "readelf utility not specified")
ENDIF()

IF(NOT DEFINED CMAKE_ARGV4)
IF(NOT DEFINED MODULE_PATH)
MESSAGE(FATAL_ERROR "Library not specified")
ENDIF()

MESSAGE(DEBUG "DLL:\t${CMAKE_ARGV4}")

EXECUTE_PROCESS(
COMMAND ${CMAKE_ARGV3} -d -W ${CMAKE_ARGV4}
COMMAND ${CMAKE_READELF} -d -W ${MODULE_PATH}
OUTPUT_VARIABLE NEEDED_LIBRARIES_NL
OUTPUT_STRIP_TRAILING_WHITESPACE
COMMAND_ERROR_IS_FATAL ANY
Expand All @@ -56,21 +53,21 @@ FOREACH(l IN LISTS NEEDED_LIBRARIES)
CheckLibrary(${NEEDED_LIB} LIB_CHECK)
IF(LIB_CHECK_RESULT)
LIST(APPEND EXPECTED_LIBS ${LIB_CHECK_RESULT})
MESSAGE(STATUS "${LIB_CHECK_RESULT} ✓")
MESSAGE(STATUS "[lib] ${LIB_CHECK_RESULT}✓")
ELSE()
LIST(APPEND UNEXPECTED_LIBS ${NEEDED_LIB})
MESSAGE(STATUS "${NEEDED_LIB} ⍻")
MESSAGE(STATUS "[lib] ${NEEDED_LIB}⍻")
ENDIF()
ENDFOREACH()

LIST(LENGTH UNEXPECTED_LIBS UNEXPECTED_LIBS_CNT)
LIST(LENGTH EXPECTED_LIBS EXPECTED_LIBS_CNT)
MESSAGE(STATUS "Status - expected:${EXPECTED_LIBS_CNT} unexpected:${UNEXPECTED_LIBS_CNT}")
MESSAGE(STATUS "[status] expected:${EXPECTED_LIBS_CNT}, unexpected:${UNEXPECTED_LIBS_CNT}")

IF(${UNEXPECTED_LIBS_CNT} GREATER 0)
MESSAGE(FATAL_ERROR "There are ${UNEXPECTED_LIBS_CNT} unexpected libraries")
MESSAGE(FATAL_ERROR "Unexpected libraries found")
ENDIF()

IF(${EXPECTED_LIBS_CNT} EQUAL 0)
MESSAGE(FATAL_ERROR "Internal error - zero libraries checked")
ENDIF()
MESSAGE(FATAL_ERROR "Internal error — zero libraries checked")
ENDIF()
34 changes: 26 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ INSTALL(

IF(IS_GIT_REPO)
IF(DEFINED CMAKE_READELF)
ADD_TEST(NAME "Load library" COMMAND ${CMAKE_READELF} -h -W $<TARGET_FILE:asterisk-chan-quectel>)
ADD_TEST(NAME "Load library" COMMAND ${CMAKE_COMMAND}
-DCMAKE_READELF=${CMAKE_READELF}
-DMODULE_PATH=$<TARGET_FILE:asterisk-chan-quectel>
-P ${CMAKE_SOURCE_DIR}/cmake/test/dump-header.cmake
)
ADD_TEST(NAME "Check library dependencies" COMMAND ${CMAKE_COMMAND}
-DCMAKE_READELF=${CMAKE_READELF}
-DMODULE_PATH=$<TARGET_FILE:asterisk-chan-quectel>
-P ${CMAKE_SOURCE_DIR}/cmake/test/needed-libs.cmake
${CMAKE_READELF}
$<TARGET_FILE:asterisk-chan-quectel>
)
ADD_TEST(NAME "Check comment section" COMMAND ${CMAKE_COMMAND}
-DCMAKE_READELF=${CMAKE_READELF}
-DMODULE_PATH=$<TARGET_FILE:asterisk-chan-quectel>
-DCOMPILER_VERSION=${CMAKE_C_COMPILER_VERSION}
-P ${CMAKE_SOURCE_DIR}/cmake/test/comment-section.cmake
${CMAKE_READELF}
$<TARGET_FILE:asterisk-chan-quectel>
${CMAKE_C_COMPILER_VERSION}
)
SET_TESTS_PROPERTIES(
"Load library"
Expand All @@ -71,13 +75,27 @@ IF(IS_GIT_REPO)
TIMEOUT 20
WORKING_DIRECTORY $<TARGET_FILE_DIR:asterisk-chan-quectel>
)
IF(CMAKE_CROSSCOMPILING)
ADD_TEST(NAME "Check architecture-specific metadata" COMMAND ${CMAKE_COMMAND}
-DCMAKE_READELF=${CMAKE_READELF}
-DMODULE_PATH=$<TARGET_FILE:asterisk-chan-quectel>
-P ${CMAKE_SOURCE_DIR}/cmake/test/arch-specific.cmake
)
SET_TESTS_PROPERTIES(
"Check architecture-specific metadata"
PROPERTIES
LABELS "DLL;READELF"
TIMEOUT 20
WORKING_DIRECTORY $<TARGET_FILE_DIR:asterisk-chan-quectel>
)
ENDIF()
ELSE()
MESSAGE(WARNING Cannot find readelf utility)
ENDIF()
ADD_TEST(NAME "Check AST_BUILDOPT_SUM" COMMAND ${CMAKE_COMMAND}
-DMODULE_PATH=$<TARGET_FILE:asterisk-chan-quectel>
-DAST_BUILDOPT_SUM=${AST_BUILDOPT_SUM}
-P ${CMAKE_SOURCE_DIR}/cmake/test/AST_BUILDOPT_SUM.cmake
$<TARGET_FILE:asterisk-chan-quectel>
${AST_BUILDOPT_SUM}
)
SET_TESTS_PROPERTIES(
"Check AST_BUILDOPT_SUM"
Expand Down

0 comments on commit d12f214

Please sign in to comment.