CMake/Tests/RunCMake/Instrumentation/verify-snippet.cmake
Martin Duffy 39f365ec3c instrumentation: Unique snippet file names
Updates the hash in snippet file naming to take the ProcessId into account
so that snippets with identical commands don't risk a collision in filename.

Additionally, this adds a `workingDir` to snippet files so that snippets with
identical commands can be more easily differentiated, such as with install
snippets.
2025-03-18 13:23:02 -04:00

116 lines
4.7 KiB
CMake

# Performs generic (non-project specific) validation of v1 Snippet File Contents
function(add_error error)
string(APPEND RunCMake_TEST_FAILED " ${error}\n")
string(APPEND ERROR_MESSAGE " ${error}\n")
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(snippet_error snippet error)
add_error("Error in snippet file ${snippet}:\n${error}")
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(has_key snippet json key)
string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
if (NOT missingKey MATCHES NOTFOUND)
snippet_error("${snippet}" "Missing ${key}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(has_not_key snippet json key)
string(JSON data ERROR_VARIABLE missingKey GET "${json}" ${key})
if (missingKey MATCHES NOTFOUND)
snippet_error("${snippet}" "Has unexpected ${key}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(snippet_has_fields snippet contents)
get_filename_component(filename "${snippet}" NAME)
has_key("${snippet}" "${contents}" role)
has_key("${snippet}" "${contents}" result)
has_key("${snippet}" "${contents}" workingDir)
if (NOT filename MATCHES "^build-*")
has_key("${snippet}" "${contents}" command)
endif()
if (filename MATCHES "^link-*")
has_key("${snippet}" "${contents}" target)
has_key("${snippet}" "${contents}" outputs)
has_key("${snippet}" "${contents}" outputSizes)
has_key("${snippet}" "${contents}" targetType)
has_key("${snippet}" "${contents}" targetLabels)
has_key("${snippet}" "${contents}" config)
elseif (filename MATCHES "^compile-*")
has_key("${snippet}" "${contents}" target)
has_key("${snippet}" "${contents}" outputs)
has_key("${snippet}" "${contents}" outputSizes)
has_key("${snippet}" "${contents}" source)
has_key("${snippet}" "${contents}" language)
has_key("${snippet}" "${contents}" config)
elseif (filename MATCHES "^custom-*")
has_key("${snippet}" "${contents}" outputs)
has_key("${snippet}" "${contents}" outputSizes)
elseif (filename MATCHES "^test-*")
has_key("${snippet}" "${contents}" testName)
has_key("${snippet}" "${contents}" config)
endif()
if(ARGS_DYNAMIC_QUERY)
has_key("${snippet}" "${contents}" dynamicSystemInformation)
string(JSON dynamicSystemInfo ERROR_VARIABLE noInfo GET "${contents}" dynamicSystemInformation)
if (noInfo MATCHES NOTFOUND)
has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
endif()
else()
has_not_key("${snippet}" "${contents}" dynamicSystemInformation)
string(JSON dynamicSystemInfo ERROR_VARIABLE noInfo GET "${contents}" dynamicSystemInformation)
if (noInfo MATCHES NOTFOUND)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
has_not_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
endif()
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(snippet_valid_timing contents)
string(JSON start GET "${contents}" timeStart)
string(JSON duration GET "${contents}" duration)
if (start LESS 0)
snippet_error("${snippet}" "Negative time start: ${start}")
endif()
if (duration LESS 0)
snippet_error("${snippet}" "Negative duration: ${end}")
endif()
return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
endfunction()
function(verify_snippet snippet contents)
snippet_has_fields("${snippet}" "${contents}")
snippet_valid_timing("${contents}")
string(JSON version GET "${contents}" version)
if (NOT version EQUAL 1)
snippet_error("${snippet}" "Version must be 1, got: ${version}")
endif()
string(JSON role GET "${contents}" role)
get_filename_component(filename "${snippet}" NAME)
if (NOT filename MATCHES "^${role}-")
snippet_error("${snippet}" "Role \"${role}\" doesn't match snippet filename")
endif()
string(JSON outputs ERROR_VARIABLE noOutputs GET "${contents}" outputs)
if (NOT outputs MATCHES NOTFOUND)
string(JSON outputSizes ERROR_VARIABLE noOutputSizes GET "${contents}" outputSizes)
list(LENGTH outputs outputsLen)
list(LENGTH outputSizes outputSizesLen)
if (outputSizes MATCHES NOTFOUND OR NOT outputsLen EQUAL outputSizesLen)
snippet_error("${snippet}" "outputs and outputSizes do not match")
endif()
endif()
return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED role)
endfunction()