
set(example_src
  ArrayHandle.cxx
  ArrayHandleAdapt.cxx
  ArrayHandleCast.cxx
  ArrayHandleCompositeVector.cxx
  ArrayHandleConstant.cxx
  ArrayHandleCoordinateSystems.cxx
  ArrayHandleCounting.cxx
  ArrayHandleDerived.cxx
  ArrayHandleDiscard.cxx
  ArrayHandleExtractComponent.cxx
  ArrayHandleGroupVec.cxx
  ArrayHandleImplicit.cxx
  ArrayHandlePermutation.cxx
  ArrayHandleSwizzle.cxx
  ArrayHandleTransform.cxx
  ArrayHandleZip.cxx
  BasicGlut.cxx
  CellEdgesFaces.cxx
  CellOperations.cxx
  CellShapes.cxx
  ColorTables.cxx
  CoreDataTypes.cxx
  CustomDeviceAdapter.cxx
  DataSetCreation.cxx
  DeviceAdapterTag.cxx
  DeviceAdapterAlgorithms.cxx
  DynamicArrayHandle.cxx
  EnvironmentModifierMacros.cxx
  ErrorHandling.cxx
  FilterDataSetWithField.cxx
  FractalWorklets.cxx
  FunctionInterface.cxx
  GenerateMeshCombineLike.cxx
  GenerateMeshConstantShape.cxx
  GenerateMeshHash.cxx
  GenerateMeshVariableShape.cxx
  IO.cxx
  ListTags.cxx
  Matrix.cxx
  NewtonsMethod.cxx
  OtherGlut.cxx
  ProvidedFilters.cxx
  RuntimeDeviceTracker.cxx
  ScatterCounting.cxx
  ScatterUniform.cxx
  SumOfAngles.cxx
  SimpleHistogram.cxx
  Timer.cxx
  Traits.cxx
  TransferringArguments.cxx
  TriangleQuality.cxx
  TryExecute.cxx
  UsePointElevationWorklet.cxx
  UseWorkletMapCellToPoint.cxx
  UseWorkletMapField.cxx
  UseWorkletMapPointToCell.cxx
  UseWorkletReduceByKey.cxx
  )

# Set up compiling and testing of examples.
if (BUILD_EXAMPLES)
  find_package(VTKm REQUIRED
    COMPONENTS Serial TBB Rendering OpenGL GLUT
    )
  include_directories(${VTKm_INCLUDE_DIRS})

  if(VTKm_VERSION_FULL MATCHES "[0-9]+.[0-9]+.[0-9]+-.*")
    # If we are in between VTK-m versions, change the version to give the
    # commit we are currently documenting.
    set(VTKm_GUIDE_VERSION ${VTKm_VERSION_FULL} PARENT_SCOPE)
  else()
    # If we are at an actual release (as far as we can tell), make sure the
    # version matches up.
    if(NOT VTKm_GUIDE_VERSION STREQUAL VTKm_VERSION)
      message(SEND_ERROR "VTKm_GUIDE_VERSION seems to be set wrong. Change in CMakeLists.txt")
    endif()
  endif()

  set(test_prog ExampleTests)
  create_test_sourcelist(test_src ${test_prog}.cxx ${example_src})
  add_executable(${test_prog} ${test_src})
  target_include_directories(${test_prog} PRIVATE ${VTKm_INCLUDE_DIRS})
  target_link_libraries(${test_prog} ${VTKm_LIBRARIES})
  target_compile_options(${test_prog} PRIVATE ${VTKm_COMPILE_OPTIONS})

  foreach (test ${example_src})
    get_filename_component(tname ${test} NAME_WE)
    add_test(NAME ${tname}
      COMMAND ${test_prog} ${tname} --no-interaction
      )
  endforeach()

  if(NOT WIN32)
    execute_process(
      COMMAND ${CMAKE_COMMAND} -E
        create_symlink ${CMAKE_SOURCE_DIR}/data ${CMAKE_CURRENT_BINARY_DIR}/data
      )
  else()
    execute_process(
      COMMAND ${CMAKE_COMMAND} -E
        copy_directory ${CMAKE_SOURCE_DIR}/data ${CMAKE_CURRENT_BINARY_DIR}/data
      )
  endif()
endif()

# Set up targets to extract examples from code
latex_get_output_path(output_dir)
set(scan_target_files)
foreach(src_file ${example_src})
  set(known_out_file ${output_dir}/${src_file}-scanned)
  add_custom_command(OUTPUT ${known_out_file}
    COMMAND ${CMAKE_COMMAND}
      -D INPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/${src_file}
      -D OUTPUT_DIRECTORY=${output_dir}
      -P ${CMAKE_CURRENT_SOURCE_DIR}/ExtractExample.cmake
    COMMAND ${CMAKE_COMMAND} -E touch ${known_out_file}
    DEPENDS ${src_file} ExtractExample.cmake
    COMMENT "Extracting examples from ${src_file}"
    )
  list(APPEND scan_target_files ${known_out_file})
endforeach()

set(example_listings ${scan_target_files} PARENT_SCOPE)

# This target never really needs to be called. However, its existance means
# the make targets for all the files in ${scan_target_files} will be be
# created. If you leave it out, it means that targets made in other scopes
# may not find the files.
add_custom_target(ExtractExamples
  DEPENDS ${scan_target_files}
  SOURCES ExtractExample.cmake
  )
