1include(FetchContent)
2FetchContent_Declare(
3  googletest
4  URL https://github.com/google/googletest/archive/refs/tags/release-1.12.1.zip
5)
6# For Windows: Prevent overriding the parent project's compiler/linker settings
7set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
8FetchContent_MakeAvailable(googletest)
9
10include(GoogleTest)
11
12function(add_capi_test name)
13  cmake_parse_arguments(PARSE_ARGV 1 arg "" "" "FILES")
14  add_executable(test-${name} ${arg_FILES})
15  target_link_libraries(test-${name} PRIVATE wasmtime-cpp gtest_main)
16  gtest_discover_tests(test-${name}
17    # GitHub Actions on Windows is pretty slow, let's give it lots more time
18    # than the default 5 seconds.
19    DISCOVERY_TIMEOUT 60)
20endfunction()
21
22add_capi_test(tests FILES
23  simple.cc
24  types.cc
25  memory_type.cc
26  val_type.cc
27  global_type.cc
28  table_type.cc
29  func_type.cc
30  import_type.cc
31  export_type.cc
32  extern_type.cc
33  tag_type.cc
34  exception.cc
35  func.cc
36  component/instantiate.cc
37  component/define_module.cc
38  component/lookup_func.cc
39  component/call_func.cc
40  component/values.cc
41  component/linker.cc
42  component/types.cc
43  error.cc
44  config.cc
45  wat.cc
46  module.cc
47  engine.cc
48  trap.cc
49  wasi.cc
50  store.cc
51  val.cc
52  gc.cc
53  table.cc
54  global.cc
55  memory.cc
56  instance.cc
57  linker.cc
58  wasip2.cc
59)
60
61# Create a list of all wasmtime headers with `GLOB_RECURSE`, then emit a file
62# into the current binary directory which tests that if the header is included
63# that the file compiles correctly.
64#
65# Gather everything into a list and then create a test which links all these
66# compilations together. This binary doesn't actually run any tests itself but
67# it does test that all headers compile in isolation at least. Not perfect but
68# hopefully at least something.
69cmake_path(APPEND CMAKE_CURRENT_SOURCE_DIR "../include" OUTPUT_VARIABLE header_root)
70file(GLOB_RECURSE cpp_headers "${header_root}/*.h*")
71foreach(header IN LISTS cpp_headers)
72  cmake_path(GET header EXTENSION header_extension)
73  if(header_extension STREQUAL ".h.in")
74    continue()
75  endif()
76  cmake_path(
77    RELATIVE_PATH header
78    BASE_DIRECTORY ${header_root}
79    OUTPUT_VARIABLE rel_header)
80  cmake_path(APPEND CMAKE_CURRENT_BINARY_DIR "header-test" "${rel_header}.cc"
81    OUTPUT_VARIABLE test_filename)
82  list(APPEND header_tests ${test_filename})
83
84  file(WRITE ${test_filename} "#include <${rel_header}>")
85endforeach()
86add_capi_test(standalone-headers FILES ${header_tests})
87
88# Add a custom test where two files include `wasmtime.hh` and are compiled into
89# the same executable (basically makes sure any defined functions in the header
90# are tagged with `inline`).
91add_capi_test(test-double-include FILES double-include-a.cc double-include-b.cc)
92