File size: 1,524 Bytes
be11144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Inputs:
#
# Variable             | Type     | Doc
# ---------------------|----------|--------------------------------------
# EXAMPLE_EXECUTABLE   | FilePath | Path to example executable
# FILECHECK_ENABLED    | Boolean  | Run FileCheck comparison test
# FILECHECK_EXECUTABLE | FilePath | Path to the LLVM FileCheck utility
# REFERENCE_FILE       | FilePath | Path to the FileCheck reference file

if (FILECHECK_ENABLED)
  if (NOT EXISTS "${REFERENCE_FILE}")
    message(FATAL_ERROR
      "FileCheck requested for '${EXAMPLE_EXECUTABLE}', but reference file "
      "does not exist at '${REFERENCE_FILE}`."
    )
  endif()

  # If the reference file is empty, validate that the example doesn't
  # produce any output.
  file(SIZE "${REFERENCE_FILE}" file_size)
  message("${REFERENCE_FILE}: ${file_size} bytes")

  if (file_size EQUAL 0)
    set(check_empty_output TRUE)
    set(filecheck_command)
  else()
    set(check_empty_output FALSE)
    set(filecheck_command COMMAND "${FILECHECK_EXECUTABLE}" "${REFERENCE_FILE}")
  endif()
endif()

execute_process(
  COMMAND "${EXAMPLE_EXECUTABLE}"
  ${filecheck_command}
  RESULT_VARIABLE exit_code
  OUTPUT_VARIABLE stdout
  ERROR_VARIABLE stderr
)

if (NOT 0 EQUAL exit_code)
  message(FATAL_ERROR "${EXAMPLE_EXECUTABLE} failed (${exit_code}):\n${stderr}")
endif()

if (check_empty_output)
  string(LENGTH "${stdout}" stdout_size)
  if (NOT stdout_size EQUAL 0)
    message(FATAL_ERROR "${EXAMPLE_EXECUTABLE}: output received, but not expected:\n${stdout}")
  endif()
endif()