# Inherit the parent CMake setting
set(CURRENT_SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@")
set(LEMON_EXECUTABLE "@LEMON_EXECUTABLE@")
set(RE2C_EXECUTABLE "@RE2C_EXECUTABLE@")
set(PERPLEX_EXECUTABLE "@PERPLEX_EXECUTABLE@")
set(SYNC_SCRIPT "@SYNC_SCRIPT@")
set(SYNC_TARGET_NAME "@SYNC_TARGET_NAME@")
set(DEBUGGING_GENERATED_SOURCES "@DEBUGGING_GENERATED_SOURCES@")

if(NOT DEBUGGING_GENERATED_SOURCES)
  # Include the file the provides the baseline against which
  # current files will be compared
  include("@BASELINE_INFORMATION_FILE@")

  # Define a variety of convenience routines
  include("@PROJECT_CMAKE_DIR@/Generated_Source_Utils.cmake")

  # The following need to be checked:
  #
  # 1.  baseline input MD5 hashes against the current input
  #     hashes.  If the cached sources were generated using
  #     inputs other than the current inputs, note they are
  #     out of sync but don't stop.  Templates used by perplex
  #     and lemon are part of this group.
  #
  # 2.  baseline cached source MD5 hashes against current
  #     cached source MD5 hashes.  Making sure no changes
  #     have been made to the generated sources.  If the
  #     cached sources need to be updated (see #1, for example)
  #     their MD5 hashes need to be updated at the same time.
  #
  # 3.  MD5 hashes of output generated by the tools against
  #     the MD5 hashes of the equalivent cached sources, if
  #     a) the tool versions are the same b) the input MD5
  #     hash comparisions were the same and c) the baseline
  #     test from #2 passed.  This is done to detect platform
  #     differences in output sources, but is only valid if
  #     the input files are in their "pristine" state and the
  #     toolchain is equalivent to that used for the baseline.

  # Individually verify all of the files in question.
  set(input_files "@INPUT_FILELIST@")
  VERIFY_FILES("${input_files}" 0 input_unchanged)
  set(template_files "@TEMPLATE_FILELIST@")
  VERIFY_FILES("${template_files}" 1 templates_unchanged)
  set(cached_files "@CACHED_FILELIST@")
  VERIFY_FILES("${cached_files}" 1 cached_unchanged)
  if(NOT cached_unchanged)
    message(FATAL_ERROR "Cached generated sources do not match the MD5 hashes present in @BASELINE_INFORMATION_FILE@ - if updating cached sources, remember that the build enforces the requirement that associated MD5 hashes in @BASELINE_INFORMATION_FILE@ are current as well.  Cached generated sources should not be directly edited.")
  endif(NOT cached_unchanged)

  GET_GENERATOR_EXEC_VERSIONS()

  if("${lemon_version}" VERSION_EQUAL "${baseline_lemon_version}" AND "${perplex_version}" VERSION_EQUAL "${baseline_perplex_version}" AND "${re2c_version}" VERSION_EQUAL "${baseline_re2c_version}")
    set(tool_versions_equal 1)
  else()
    set(tool_versions_equal 0)
  endif()

  if(NOT input_unchanged)
    if(templates_unchanged AND tool_versions_equal)
      message("Input files changed - syncing cached outputs")
      execute_process(COMMAND ${CMAKE_COMMAND} -P ${SYNC_SCRIPT} OUTPUT_VARIABLE output) 
    else(templates_unchanged AND tool_versions_equal)
      if(NOT templates_unchanged AND NOT tool_versions_equal)
	message("Input files have been updated, but templates and current tool versions do not match those previously used to generate cached sources.  Automatic syncing will not proceed.")
	message("To force syncing, use the build target ${SYNC_TARGET_NAME}")
      else(NOT templates_unchanged AND NOT tool_versions_equal)
	if(NOT templates_unchanged)
	  message("Input files have been updated, but templates do not match those previously used to generate cached sources.  Automatic syncing will not proceed.")
	  message("To force syncing, use the build target ${SYNC_TARGET_NAME}")
	endif(NOT templates_unchanged)
	if(NOT tool_versions_equal)
	  message("Input files have been updated, but tool versions do not match those previously used to generate cached sources.  Automatic syncing will not proceed.")
	  message("To force syncing, use the build target ${SYNC_TARGET_NAME}")
	endif(NOT tool_versions_equal)
      endif(NOT templates_unchanged AND NOT tool_versions_equal)
    endif(templates_unchanged AND tool_versions_equal)
  else(NOT input_unchanged)
    if(templates_unchanged AND cached_unchanged AND tool_versions_equal)
      # Under these conditions, the uncached generated output should be equal to the cached files.
      # Check if it is - a difference here may indicate a platform-specific behavior in one of the
      # generators.
      set(build_files "@BUILD_OUTPUT_FILELIST@")
      VERIFY_FILES("${build_files}" 1 platform_unchanged)
      if(NOT platform_unchanged)
	message("Note: give these build inputs and tools, source files should be identical to generated files.  Differences were still observed - possible indiciation of platform-specific generator behavior.")
      endif(NOT platform_unchanged)
    endif(templates_unchanged AND cached_unchanged AND tool_versions_equal)
  endif(NOT input_unchanged)
else(NOT DEBUGGING_GENERATED_SOURCES)
  message("\nNote: DEBUGGING_GENERATED_SOURCES is enabled - generated outputs will contain configuration-specific debugging information, so syncing cached output files is not possible.  To restore normal behavior, disable DEBUGGING_GENERATED_SOURCES.\n")
endif(NOT DEBUGGING_GENERATED_SOURCES)

# Local Variables:
# tab-width: 8
# mode: cmake
# indent-tabs-mode: t
# End:
# ex: shiftwidth=2 tabstop=8
