
###############################################################################
# the macros in charge of dispatching to the language specific macros

macro(itk_wrap_modules_all_generators)
#!  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
#!    INIT_itk_wrap_module_java()
#!  endif()

  if(ITK_WRAP_PYTHON)
    itk_wrap_modules_python()
  endif()
  if(ITK_WRAP_TCL)
    itk_wrap_modules_tcl()
  endif()
  if(ITK_WRAP_JAVA)
    itk_wrap_modules_java()
  endif()
  if(ITK_WRAP_RUBY)
    itk_wrap_modules_ruby()
  endif()
endmacro()

macro(itk_end_wrap_modules_all_generators)
  if(ITK_WRAP_PYTHON)
    itk_end_wrap_modules_python()
  endif()
  if(ITK_WRAP_TCL)
    itk_end_wrap_modules_tcl()
  endif()
  if(ITK_WRAP_JAVA)
    itk_end_wrap_modules_java()
  endif()
  if(ITK_WRAP_RUBY)
    itk_end_wrap_modules_ruby()
  endif()
endmacro()


macro(itk_wrap_module_all_generators library_name)
  if(ITK_WRAP_EXPLICIT)
    itk_wrap_module_explicit("${library_name}")
  endif()
  if(ITK_WRAP_GCCXML)
    itk_wrap_module_gccxml("${library_name}")
  endif()
  if(ITK_WRAP_SWIGINTERFACE)
    itk_wrap_module_swig_interface("${library_name}")
  endif()
  if(ITK_WRAP_DOC)
    itk_wrap_module_DOC("${library_name}")
  endif()
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_wrap_module_python("${library_name}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_wrap_module_tcl("${library_name}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_wrap_module_ruby("${library_name}")
  endif()
  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
    itk_wrap_module_java("${library_name}")
  endif()
endmacro()

macro(itk_end_wrap_module_all_generators)
  if(ITK_WRAP_EXPLICIT)
    itk_end_wrap_module_explicit()
  endif()
  if(ITK_WRAP_GCCXML)
    itk_end_wrap_module_gccxml()
  endif()
  if(ITK_WRAP_SWIGINTERFACE)
    itk_end_wrap_module_swig_interface()
  endif()
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_end_wrap_module_python()
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_end_wrap_module_tcl()
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_end_wrap_module_ruby()
  endif()
  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
    itk_end_wrap_module_java()
  endif()
  if(ITK_WRAP_DOC)
    itk_end_wrap_module_DOC()
  endif()
endmacro()


macro(itk_wrap_submodule_all_generators module)
  if(ITK_WRAP_EXPLICIT)
    itk_wrap_submodule_explicit("${module}")
  endif()
  if(ITK_WRAP_GCCXML)
    itk_wrap_submodule_gccxml("${module}")
  endif()
  if(ITK_WRAP_SWIGINTERFACE)
    itk_wrap_submodule_swig_interface("${module}")
  endif()
  if(ITK_WRAP_DOC)
    itk_wrap_submodule_DOC("${module}")
  endif()
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_wrap_submodule_python("${module}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_wrap_submodule_tcl("${module}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_wrap_submodule_ruby("${module}")
  endif()
  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
    itk_wrap_submodule_java("${module}")
  endif()
endmacro()

macro(itk_end_wrap_submodule_all_generators module)
  if(ITK_WRAP_EXPLICIT)
    itk_end_wrap_submodule_explicit("${module}")
  endif()
  if(ITK_WRAP_GCCXML)
    itk_end_wrap_submodule_gccxml("${module}")
  endif()
  if(ITK_WRAP_SWIGINTERFACE)
    itk_end_wrap_submodule_swig_interface("${module}")
  endif()
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_end_wrap_submodule_python("${module}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_end_wrap_submodule_tcl("${module}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_end_wrap_submodule_ruby("${module}")
  endif()
  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
    itk_end_wrap_submodule_java("${module}")
  endif()
  if(ITK_WRAP_DOC)
    itk_end_wrap_submodule_DOC()
  endif()
endmacro()


macro(itk_wrap_class_all_generators class)
endmacro()

macro(itk_wrap_named_class_all_generators class swig_name)
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_wrap_named_class_python("${class}" "${swig_name}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_wrap_named_class_tcl("${class}" "${swig_name}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_wrap_named_class_ruby("${class}" "${swig_name}")
  endif()
  if(ITK_WRAP_DOC)
    itk_wrap_named_class_DOC("${class}" "${swig_name}")
  endif()
endmacro()

macro(itk_end_wrap_class_all_generators)
endmacro()


macro(itk_wrap_simple_class_all_generators class)
endmacro()


macro(itk_wrap_named_simple_class_all_generators class)
endmacro()


macro(itk_wrap_include_all_generators inc)
  if(ITK_WRAP_EXPLICIT)
    itk_wrap_include_explicit("${inc}")
  endif()
  if(ITK_WRAP_GCCXML)
    itk_wrap_include_gccxml("${inc}")
  endif()
  if(ITK_WRAP_SWIGINTERFACE)
    itk_wrap_include_swig_interface("${inc}")
  endif()
endmacro()

macro(itk_wrap_template_all_generators name types)
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_wrap_template_python("${name}" "${types}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_wrap_template_tcl("${name}" "${types}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_wrap_template_ruby("${name}" "${types}")
  endif()
endmacro()


macro(itk_wrap_one_type_all_generators wrap_method wrap_class swig_name template_params)
  if(ITK_WRAP_EXPLICIT)
    itk_wrap_one_type_explicit("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_GCCXML)
    itk_wrap_one_type_gccxml("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_SWIGINTERFACE)
    itk_wrap_one_type_swig_interface("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_DOC)
    itk_wrap_one_type_DOC("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_wrap_one_type_python("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_wrap_one_type_tcl("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_wrap_one_type_ruby("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
    itk_wrap_one_type_java("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif()
endmacro()


macro(itk_wrap_simple_type_all_generators wrap_class swig_name)
  if(ITK_WRAP_GCCXML)
    itk_wrap_simple_type_gccxml("${wrap_class}" "${swig_name}")
  endif()
  if(ITK_WRAP_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    itk_wrap_simple_type_python("${wrap_class}" "${swig_name}")
  endif()
  if(ITK_WRAP_TCL AND WRAPPER_LIBRARY_TCL)
    itk_wrap_simple_type_tcl("${wrap_class}" "${swig_name}")
  endif()
  if(ITK_WRAP_RUBY AND WRAPPER_LIBRARY_RUBY)
    itk_wrap_simple_type_ruby("${wrap_class}" "${swig_name}")
  endif()
  if(ITK_WRAP_JAVA AND WRAPPER_LIBRARY_JAVA)
    itk_wrap_simple_type_java("${wrap_class}" "${swig_name}")
  endif()
endmacro()



###############################################################################
# ccache management. This option is shared by most of generators, so put it here.
option(ITK_USE_CCACHE "Use ccache to cache swig/gccxml/... output and speedup the rebuild." OFF)
mark_as_advanced(ITK_USE_CCACHE)
if(ITK_USE_CCACHE)
  find_program(CCACHE_EXECUTABLE NAMES ccache-swig ccache DOC "ccache executable.")
endif()



###############################################################################
# build the generators list

if(EXTERNAL_WRAP_ITK_PROJECT)
  # generators there have been turned on while building wrapitk
  set(default ON)
else()
  # set some of them to on by default
  option(ITK_WRAP_GCCXML "Build gccxml files." ON)
  option(ITK_WRAP_SWIGINTERFACE "Build swig interfaces." ON)
  set(default OFF)
endif()

set(WRAP_ITK_GENERATORS CACHE INTERNAL "Internal generators list.")
set(WRAP_ITK_UNUSED_GENERATORS CACHE INTERNAL "Internal unused generators list.")
file(GLOB local_files *)
# filter and reorder the list
set(generators )
foreach(f ${local_files})
  file(RELATIVE_PATH lang "${CMAKE_CURRENT_SOURCE_DIR}" "${f}")
  if(IS_DIRECTORY "${f}")
    if("${lang}" MATCHES "^GccXML$|^SwigInterface$|^Doc$")
      # put those generators before the others
      set(generators ${lang} ${generators})
    else()
      set(generators ${generators} ${lang})
    endif()
  endif()
endforeach()

foreach(lang ${generators})
  string(TOUPPER "${lang}" lang_upper)

  # add the language to the options
  option(ITK_WRAP_${lang_upper} "Build swig ${lang} wrapper support." ${default})

  # add the language to the list, if the user have turn it on, and include
  # the subdirectory
  if(ITK_WRAP_${lang_upper})
    set(WRAP_ITK_GENERATORS ${WRAP_ITK_GENERATORS} "${lang}")
    add_subdirectory("${lang}")
  else()
    set(WRAP_ITK_UNUSED_GENERATORS ${WRAP_ITK_UNUSED_GENERATORS} "${lang}")
  endif()
endforeach()
set(WRAP_ITK_GENERATORS ${WRAP_ITK_GENERATORS} CACHE INTERNAL "Internal generators list.")
set(WRAP_ITK_UNUSED_GENERATORS ${WRAP_ITK_UNUSED_GENERATORS} CACHE INTERNAL "Internal unused generators list.")

# The include_directories are added here to be used by
# Wrapping/ConfigureWrapping.cmake
foreach(lang ${generators})
  string(TOUPPER "${lang}" lang_upper)
  if(ITK_WRAP_${lang_upper})
    get_directory_property(inc DIRECTORY "${lang}" INCLUDE_DIRECTORIES)
    include_directories(${inc})
  endif()
endforeach()

if(NOT EXTERNAL_WRAP_ITK_PROJECT)
  install(FILES CMakeLists.txt itk.i
    DESTINATION "${WRAP_ITK_INSTALL_PREFIX}/Configuration/Generators"
  )
endif()
