find_package(Qt4)
set(SAFE_CMAKE_REQUIRED_INCLUDES "${CMAKE_REQUIRED_INCLUDES}")
set(SAFE_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
set(CMAKE_REQUIRED_INCLUDES "${QT_INCLUDES}")
set(CMAKE_REQUIRED_LIBRARIES "${QT_QTCORE_LIBRARY}")
CHECK_CXX_SOURCE_COMPILES("#include <QObject>
int main() { QObject o; return 0;}" QT4_USABLE)
mark_as_advanced(QT4_USABLE)
set(CMAKE_REQUIRED_INCLUDES "${SAFE_CMAKE_REQUIRED_INCLUDES}")
set(CMAKE_REQUIRED_LIBRARIES "${SAFE_CMAKE_REQUIRED_LIBRARIES}")

macro(build_example name)
   set(_SRCS)
   set(_LIBS)
   set(_state 1)
   foreach(ARG ${ARGN})
      if(ARG STREQUAL "LIBS")
         set(_state 2)
      elseif(_state EQUAL 1)
         set(_SRCS ${_SRCS} ${ARG})
      elseif(_state EQUAL 2)
         set(_LIBS ${_LIBS} ${ARG})
      endif()
   endforeach()
   add_executable("example_${name}_default" ${_SRCS})
   target_link_libraries("example_${name}_default" Vc ${_LIBS})

   add_executable("example_${name}_scalar" ${_SRCS})
   add_target_property("example_${name}_scalar" COMPILE_FLAGS "-DVC_IMPL=Scalar")
   add_target_property("example_${name}_scalar" LABELS "Scalar")
   add_dependencies(Scalar "example_${name}_scalar")
   target_link_libraries("example_${name}_scalar" Vc ${_LIBS})

   if(USE_SSE2)
      add_executable("example_${name}_sse" ${_SRCS})
      add_target_property("example_${name}_sse" COMPILE_FLAGS "-DVC_IMPL=SSE")
      add_target_property("example_${name}_sse" LABELS "SSE")
      add_dependencies(SSE "example_${name}_sse")
      target_link_libraries("example_${name}_sse" Vc ${_LIBS})
   endif()

   if(USE_AVX)
      add_executable("example_${name}_avx" ${_SRCS})
      add_target_property("example_${name}_avx" COMPILE_FLAGS "-DVC_IMPL=AVX")
      add_target_property("example_${name}_avx" LABELS "AVX")
      add_dependencies(AVX "example_${name}_avx")
      target_link_libraries("example_${name}_avx" Vc ${_LIBS})

      add_target_property("example_${name}_default" LABELS "AVX")
      add_dependencies(AVX "example_${name}_default")
   elseif(USE_SSE2)
      add_target_property("example_${name}_default" LABELS "SSE")
      add_dependencies(SSE "example_${name}_default")
   else()
      add_target_property("example_${name}_default" LABELS "Scalar")
      add_dependencies(Scalar "example_${name}_default")
   endif()
endmacro(build_example)

macro(my_add_subdirectory _name)
   list(FIND disabled_targets "example_${_name}" _disabled)
   if(_disabled EQUAL -1)
      add_subdirectory(${_name})
   endif()
endmacro()

my_add_subdirectory(polarcoord)
my_add_subdirectory(matrix)
my_add_subdirectory(mandelbrot)
my_add_subdirectory(buddhabrot)
my_add_subdirectory(finitediff)
