File size: 2,192 Bytes
9422383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Verify the deployment boundary of a built native runtime binary. readelf
# inspects the dynamic section without loading the library and confirms that it
# names only the expected system dependencies. nm confirms that the library
# exports exactly the public C ABI. CTest invokes this script for the library
# produced by the CMake target.

if(NOT DEFINED NATIVE_RUNTIME OR NOT EXISTS "${NATIVE_RUNTIME}")
  message(FATAL_ERROR "Native runtime does not exist: ${NATIVE_RUNTIME}")
endif()

find_program(READELF_EXECUTABLE readelf REQUIRED)
find_program(NM_EXECUTABLE nm REQUIRED)

# Dynamic-library boundary
execute_process(
  COMMAND "${READELF_EXECUTABLE}" --dynamic --wide "${NATIVE_RUNTIME}"
  OUTPUT_VARIABLE dynamic_section
  COMMAND_ERROR_IS_FATAL ANY)
set(expected_libraries
  libc.so.6
  libgcc_s.so.1
  libm.so.6
  libstdc++.so.6)
set(actual_libraries)
string(REPLACE "\n" ";" dynamic_section_lines "${dynamic_section}")
foreach(line IN LISTS dynamic_section_lines)
  if(line MATCHES "\\(NEEDED\\).*\\[([^]]+)\\]")
    list(APPEND actual_libraries "${CMAKE_MATCH_1}")
  endif()
endforeach()
list(SORT actual_libraries)
if(NOT actual_libraries STREQUAL expected_libraries)
  message(FATAL_ERROR
    "Unexpected native runtime dependencies. "
    "Expected: ${expected_libraries}. Actual: ${actual_libraries}")
endif()

# Public-symbol boundary
execute_process(
  COMMAND "${NM_EXECUTABLE}" -D --defined-only "${NATIVE_RUNTIME}"
  OUTPUT_VARIABLE nm_output
  COMMAND_ERROR_IS_FATAL ANY)
string(REGEX MATCHALL "[0-9A-Fa-f]+ [A-Za-z] ([A-Za-z0-9_]+)" symbol_rows "${nm_output}")
set(actual_exports)
foreach(row IN LISTS symbol_rows)
  string(REGEX REPLACE ".* " "" symbol "${row}")
  list(APPEND actual_exports "${symbol}")
endforeach()
list(SORT actual_exports)
set(expected_exports
  y26_native_arena_bytes
  y26_native_calls
  y26_native_class_count
  y26_native_create
  y26_native_destroy
  y26_native_execute
  y26_native_graphs
  y26_native_input_bytes
  y26_native_output_bytes
  y26_native_preprocess_image_no_resize
  y26_native_resolution)
if(NOT actual_exports STREQUAL expected_exports)
  message(FATAL_ERROR "Unexpected native runtime exports: ${actual_exports}")
endif()