# needs QTDEFINES_FILE set to work correctly.

if(NOT QT_DEFINES_FOUND)
#Threshold for tests
set(QT_NO_DEBUG 16)
set(QT_NO_ACCESSIBILITY 16)
set(QT_NO_ACTION 5)
set(QT_NO_BUTTONGROUP 12)
set(QT_NO_CHECKBOX 10)
set(QT_NO_COLORDIALOG 12)
set(QT_NO_COMBOBOX 10)
set(QT_NO_CURSOR 5)
set(QT_NO_DATASTREAM 5)
set(QT_NO_DATETIMEEDIT 12)
set(QT_NO_DIAL 10)
set(QT_NO_DIALOG 12)
set(QT_NO_DIR 5)
set(QT_NO_DOM 5)
set(QT_NO_DRAGANDDROP 5)
set(QT_NO_DRAWUTIL 0)
set(QT_NO_ERRORMESSAGE 13)
set(QT_NO_FILEDIALOG 13)
set(QT_NO_FONTDATABASE 5)
set(QT_NO_FONTDIALOG 12)
set(QT_NO_FRAME 10)
set(QT_NO_FTP 9)
set(QT_NO_GROUPBOX 12)
set(QT_NO_LABEL 10)
set(QT_NO_LAYOUT 10)
set(QT_NO_LCDNUMBER 12)
set(QT_NO_LINEEDIT 12)
set(QT_NO_LISTVIEW 13)
set(QT_NO_MAINWINDOW 13)
set(QT_NO_MATRIX 0)
set(QT_NO_MENUBAR 13)
set(QT_NO_MESSAGEBOX 13)
set(QT_NO_MOVIE 5)
set(QT_NO_NETWORK 5)
set(QT_NO_PALETTE 5)
set(QT_NO_PICTURE 5)
set(QT_NO_PRINTER 5)
set(QT_NO_PRINTDIALOG 13)
set(QT_NO_PROCESS 5)
set(QT_NO_PROGRESSBAR 12)
set(QT_NO_PROGRESSDIALOG 13)
set(QT_NO_PROPERTIES 0)
set(QT_NO_PUSHBUTTON 12)
set(QT_NO_QUUID_STRING 8)
set(QT_NO_RADIOBUTTON 12)
set(QT_NO_SCROLLBAR 12)
set(QT_NO_SETTINGS 5)
set(QT_NO_SIGNALMAPPER 0)
set(QT_NO_SIZEGRIP 10)
set(QT_NO_SLIDER 12)
set(QT_NO_SOUND 5)
set(QT_NO_STRINGLIST 0)
set(QT_NO_STYLE_PLASTIQUE 16)
set(QT_NO_STYLE_S60 16)
set(QT_NO_STYLE_WINDOWSXP 16)
set(QT_NO_STYLE_WINDOWS 16)
set(QT_NO_STYLE_MAC 16)
set(QT_NO_STYLE_CLEANLOOKS 16)
set(QT_NO_TABBAR 10)
set(QT_NO_TABLEVIEW 16)
set(QT_NO_TABWIDGET 10)
set(QT_NO_TEXTBROWSER 14)
set(QT_NO_TEXTCODEC 1)
set(QT_NO_TEXTCODECPLUGIN 6)
set(QT_NO_TEXTEDIT 13)
set(QT_NO_TEXTSTREAM 5)
set(QT_NO_THREAD 5)
set(QT_NO_TOOLBAR 10)
set(QT_NO_TOOLBUTTON 12)
set(QT_NO_TOOLTIP 10)
set(QT_NO_VARIANT 0)
set(QT_NO_WHATSTHIS 10)
set(QT_NO_WHEELEVENT 1)
set(QT_NO_XML 5)

#all tests
set(define_tests QT_NO_DEBUG QT_NO_ACCESSIBILITY QT_NO_ACTION QT_NO_BUTTONGROUP QT_NO_CHECKBOX
QT_NO_COLORDIALOG QT_NO_COMBOBOX QT_NO_CURSOR QT_NO_DATASTREAM QT_NO_DATETIMEEDIT
QT_NO_DIAL QT_NO_DIALOG QT_NO_DIR QT_NO_DOM QT_NO_DRAGANDDROP QT_NO_DRAWUTIL
QT_NO_ERRORMESSAGE QT_NO_FILEDIALOG QT_NO_FONTDATABASE QT_NO_FONTDIALOG
QT_NO_FRAME QT_NO_FTP QT_NO_GROUPBOX QT_NO_LABEL QT_NO_LAYOUT QT_NO_LCDNUMBER
QT_NO_LINEEDIT QT_NO_LISTVIEW QT_NO_MAINWINDOW QT_NO_MATRIX QT_NO_MENUBAR
QT_NO_MESSAGEBOX QT_NO_MOVIE QT_NO_NETWORK QT_NO_PALETTE QT_NO_PICTURE
QT_NO_PRINTER QT_NO_PRINTDIALOG QT_NO_PROCESS QT_NO_PROGRESSBAR
QT_NO_PROGRESSDIALOG QT_NO_PROPERTIES QT_NO_PUSHBUTTON QT_NO_QUUID_STRING
QT_NO_RADIOBUTTON QT_NO_SCROLLBAR QT_NO_SETTINGS QT_NO_SIGNALMAPPER
QT_NO_SIZEGRIP QT_NO_SLIDER QT_NO_SOUND QT_NO_STRINGLIST QT_NO_STYLE_PLASTIQUE QT_NO_STYLE_S60
QT_NO_STYLE_WINDOWSXP QT_NO_STYLE_WINDOWS QT_NO_STYLE_MAC QT_NO_STYLE_CLEANLOOKS
QT_NO_TABBAR QT_NO_TABLEVIEW QT_NO_TABWIDGET QT_NO_TEXTBROWSER QT_NO_TEXTCODEC
QT_NO_TEXTCODECPLUGIN QT_NO_TEXTEDIT QT_NO_TEXTSTREAM QT_NO_THREAD QT_NO_TOOLBAR
QT_NO_TOOLBUTTON QT_NO_TOOLTIP QT_NO_VARIANT QT_NO_WHATSTHIS QT_NO_WHEELEVENT
QT_NO_XML)

# macro is defined in qt-copy/src/qbase.pri but couldn't be found by qtguess, so I append it manually
list(APPEND qtdefines "QT_NO_CAST_TO_ASCII")
list(APPEND qtdefines "QT_NO_CAST_FROM_ASCII")
list(APPEND qtdefines "QT_GUI_LIB")

# check for MSVC compiler
if (MSVC)
  list(APPEND qtdefines "Q_CC_MSVC")
endif (MSVC)

foreach(test_entry ${define_tests})
  if(${${test_entry}} GREATER ${qt_test_threshold})
    list(APPEND test_targets ${test_entry})
  endif(${${test_entry}} GREATER ${qt_test_threshold})
endforeach(test_entry ${define_tests})

file(STRINGS "${QT_QTCORE_INCLUDE_DIR}/qglobal.h" qglobal_h NEWLINE_CONSUME)
file(STRINGS "${QT_QTCORE_INCLUDE_DIR}/qconfig.h" qconfig_h NEWLINE_CONSUME)
file(STRINGS "${QT_QTCORE_INCLUDE_DIR}/qfeatures.h" qfeatures_h NEWLINE_CONSUME)
set(qtheaders "${qglobal_h}\n${qconfig_h}\n${qfeatures}")
string(REGEX REPLACE "\n" ";" qtheaders "${qtheaders}")

file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/qtdefines.cpp ${CMAKE_CURRENT_BINARY_DIR}/qtdefines)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/qtdefines.cpp "#include <cstdio>\n#include \"qglobal.h\"\nint main(int argc, char** argv)\n{\n")
foreach( line ${qtheaders} )
  if(line MATCHES "^#[ ]*define[ ]+(QT_[A-Z_0-9]+)[ ]*$")
    #ifdef DEF; printf "DEF"; #endif
    file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/qtdefines.cpp "#ifdef ${CMAKE_MATCH_1}\n#ifdef QTDEF_MULTI\nprintf(\";\");\n#endif\n#ifndef QTDEF_MULTI\n#define QTDEF_MULTI\n#endif\nprintf(\"${CMAKE_MATCH_1}\");\n#endif\n")
  endif(line MATCHES "^#[ ]*define[ ]+(QT_[A-Z_0-9]+)[ ]*$")
endforeach(line)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/qtdefines.cpp "return 0;\n}\n")

try_run(qtdefines_run qtdefines_compile ${CMAKE_CURRENT_BINARY_DIR}/qtdefines
  ${CMAKE_CURRENT_BINARY_DIR}/qtdefines.cpp
  CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${QT_INCLUDE_DIR};${QT_QTCORE_INCLUDE_DIR};${CMAKE_SYSTEM_INCLUDE_PATH}"
  RUN_OUTPUT_VARIABLE qtdefines_output)
if(${qtdefines_run} EQUAL 0)
  list(REMOVE_DUPLICATES qtdefines_output)
  list(REMOVE_ITEM qtdefines_output "QT_DEBUG")
  # message(STATUS "Got defines from Qt headers ${qtdefines_output}")
  list(APPEND qtdefines ${qtdefines_output})
  if (qtdefines_output)
    list(REMOVE_ITEM test_targets ${qtdefines_output})
  endif (qtdefines_output)
endif(${qtdefines_run} EQUAL 0)

message(STATUS "Checking how Qt was built...")
message(STATUS "Threshold is set to ${qt_test_threshold}")

include (HandleImportedTargetsInCMakeRequiredLibraries)
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_SYSTEM_INCLUDE_PATH} ${QT_INCLUDE_DIR})
set(CMAKE_REQUIRED_LIBRARIES ${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTNETWORK_LIBRARY} ${QT_QTOPENGL_LIBRARY} ${QT_QTXML_LIBRARY} ${QT_QTSVG_LIBRARY})
HANDLE_IMPORTED_TARGETS_IN_CMAKE_REQUIRED_LIBRARIES(_CMAKE_REQUIRED_LIBRARIES)

foreach(test_target ${test_targets})
  file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/test-${test_target})
  
  try_compile(test_result "${CMAKE_CURRENT_BINARY_DIR}/test-${test_target}"
    ${CMAKE_CURRENT_SOURCE_DIR}/tests/test.cpp
    COMPILE_DEFINITIONS -DTEST_${test_target}
    CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${CMAKE_REQUIRED_INCLUDES}"
    "-DLINK_LIBRARIES:STRING=${_CMAKE_REQUIRED_LIBRARIES}")
  if(test_result)
    message(STATUS "${test_target} *Undefined*")
  else(test_result)
    message(STATUS "${test_target} [Defined]")
    list(APPEND qtdefines ${test_target})
  endif(test_result)
endforeach(test_target ${test_targets})

set(WITH_QT3_SUPPORT OFF CACHE BOOL "Include Qt3 Support members in QtCore and QtGui smoke modules")
if(WITH_QT3_SUPPORT)
    list(APPEND qtdefines "QT3_SUPPORT")
endif(WITH_QT3_SUPPORT)

# string(REGEX REPLACE ";" "\n" qtdefines ${qtdefines})
# file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/../qtdefines-ng ${qtdefines})
file(REMOVE "${QTDEFINES_FILE}")
foreach(definition ${qtdefines})
  file(APPEND "${QTDEFINES_FILE}" "${definition}\n")
endforeach(definition ${qtdefines})
set(QT_DEFINES_FOUND true CACHE BOOL "Test results that describe how Qt was built")
endif(NOT QT_DEFINES_FOUND)
