#
# Takin
# @author Tobias Weber <tobias.weber@tum.de>
# @date 2013-2017
# @license GPLv2
#

project(takin)
cmake_minimum_required(VERSION 3.0)

#set(CMAKE_VERBOSE_MAKEFILE TRUE)
set(CMAKE_BUILD_TYPE Release)


list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}" "." "${PROJECT_SOURCE_DIR}/tlibs")
set(CMAKE_BINARY_DIR "${CMAKE_SOURCE_DIR}")
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/bin")


set(BOOST_COMPS
	iostreams system filesystem regex program_options)

find_package(PythonInterp 2.7)
find_package(PythonLibs 2.7)
if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
	list(APPEND BOOST_COMPS python)
endif()

find_package(Boost REQUIRED COMPONENTS iostreams system filesystem regex python program_options REQUIRED)
find_package(Threads REQUIRED)
find_package(Rt REQUIRED)
find_package(Mp REQUIRED)
find_package(tlibs REQUIRED)
find_package(Qt4 REQUIRED)
find_package(PNG REQUIRED)
#find_package(JPEG REQUIRED)
#find_package(TIFF REQUIRED)
find_package(Qwt REQUIRED)
find_package(Clipper REQUIRED)

list(APPEND CMAKE_MODULE_PATH "${tlibs_SHARED_DIRS}")
find_package(Minuit2 REQUIRED)


#message("Project source dir: ${PROJECT_SOURCE_DIR}")
#message("CMake source dir: ${CMAKE_SOURCE_DIR}")
#message("Binary dir: ${CMAKE_BINARY_DIR}")
message("Install path prefix: ${CMAKE_INSTALL_PREFIX}")
message("Using qwt version ${QWT_MAJOR_VERSION}")
message("Qt moc: ${QT_MOC_EXECUTABLE}\nQt uic: ${QT_UIC_EXECUTABLE}")

add_definitions(-DNDEBUG)
add_definitions(-DUSE_NET -DUSE_IOSTR -DUSE_BOOST_REX -DUSE_GIL -DUSE_CIF)
add_definitions(-DQT_VER=4 -DQWT_VER=${QWT_MAJOR_VERSION})
add_definitions(-DNO_LAPACK -DNO_CLP)
add_definitions(-DNO_JPEG -DNO_TIFF)
add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")
add_definitions(${Boost_CXX_FLAGS})


include_directories("${PROJECT_SOURCE_DIR}" ".")
include_directories("${Boost_INCLUDE_DIRS}/..")
include_directories("${tlibs_INCLUDE_DIRS}/..")
include_directories("${PNG_INCLUDE_DIRS}")
#include_directories("${JPEG_INCLUDE_DIRS} ${TIFF_INCLUDE_DIRS}")
include_directories("${QWT_INCLUDE_DIRS}")


# py scripting support
if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
	message("Enabling py scripting support.")

	add_definitions(-DUSE_PY)
	include_directories("${PYTHON_INCLUDE_DIRS}")

	set(SRCS_PY tools/monteconvo/sqw_py.cpp)
	set(LIBS_PY ${PYTHON_LIBRARIES})
else()
	message("Disabling py scripting support.")

	add_definitions(-DNO_PY)
endif()


# fitting support
if(Minuit2_FOUND)
	message("Enabling fitting support.")
	include_directories("${Minuit2_INCLUDE_DIRS}")
	set(LIBS_FIT ${Minuit2_LIBRARIES})
else()
	message("Disabling fitting support.")
	add_definitions(-DNO_FIT)
endif()


if(X11_FOUND)
	include_directories(${X11_X11_INCLUDE_PATH})
else()
	set(X11_X11_LIB "")
endif()


# 3d support
if(OPENGL_FOUND AND FREETYPE_FOUND)
	message("Enabling 3D support. Inc: \"${OPENGL_INCLUDE_DIR}\".")

	include_directories("${FREETYPE_INCLUDE_DIRS}")
	if(NOT "${OPENGL_INCLUDE_DIR}" STREQUAL "")
		include_directories("${OPENGL_INCLUDE_DIR}")
		include_directories("${OPENGL_INCLUDE_DIR}/GL" "${OPENGL_INCLUDE_DIR}/OpenGL")
	else()
		message("Empty OGL dirs, guessing some defaults.")
		include_directories("/usr/include/GL" "/usr/include/OpenGL")
	endif()

	add_definitions(-DUSE_3D)

	set(SRCS_3D
		libs/plotgl.cpp libs/plotgl2.cpp tools/taz/recip3d.cpp
		tools/taz/real3d.cpp tools/taz/bz3d.cpp dialogs/EllipseDlg3D.cpp
	)
	set(LIBS_3D
		${FREETYPE_LIBRARY}
		Qt4::QtOpenGL ${OPENGL_LIBRARIES}
		${X11_X11_LIB}
	)
else()
	message("Disabling 3D support: OGL: ${OPENGL_FOUND}, FT: ${FREETYPE_FOUND}")

	add_definitions(-DNO_3D)
endif()


set(CMAKE_AUTOMOC OFF)
set(CMAKE_AUTOUIC OFF)

add_definitions(-std=c++11)
#add_definitions(-march=native -s)



# -----------------------------------------------------------------------------
# gentab
# -----------------------------------------------------------------------------
add_executable(gentab 
	tools/gentab/gentab.cpp
	libs/spacegroups/spacegroup_clp.cpp libs/spacegroups/crystalsys.cpp
)

target_link_libraries(gentab 
	${Boost_LIBRARIES} ${LIBS_PY} ${Clipper_LIBRARIES} 
	${tlibs_LIBRARIES}
	Qt4::QtCore Qt4::QtGui
)

add_custom_command(TARGET gentab POST_BUILD
	COMMAND cd ${CMAKE_BINARY_DIR} && ./gentab
	MAIN_DEPENDENCY gentab)
# -----------------------------------------------------------------------------



# -----------------------------------------------------------------------------
# takin
# -----------------------------------------------------------------------------
add_executable(takin 
	tools/taz/taz_main.cpp
	tools/taz/taz.cpp tools/taz/tas_layout.cpp tools/taz/tof_layout.cpp
	tools/taz/taz_crys.cpp
	tools/taz/taz_file.cpp tools/taz/taz_export.cpp
	tools/taz/tas_layout.cpp tools/taz/scattering_triangle.cpp
	tools/taz/real_lattice.cpp tools/taz/proj_lattice.cpp
	tools/taz/nicos.cpp tools/taz/sics.cpp tools/taz/taz_net.cpp

	tools/res/r0.cpp
	tools/res/cn.cpp tools/res/pop.cpp tools/res/eck.cpp tools/res/viol.cpp tools/res/simple.cpp
	tools/res/ResoDlg.cpp tools/res/ResoDlg_file.cpp

	tools/monteconvo/ConvoDlg.cpp tools/monteconvo/ConvoDlg_file.cpp
	tools/monteconvo/ConvoDlg_sim.cpp tools/monteconvo/ConvoDlg_fit.cpp
	tools/convofit/convofit_import.cpp
	tools/monteconvo/SqwParamDlg.cpp tools/monteconvo/TASReso.cpp
	tools/monteconvo/sqw.cpp tools/monteconvo/sqwbase.cpp tools/monteconvo/sqwfactory.cpp
	${SRCS_PY}

	tools/convofit/scan.cpp
	tools/scanviewer/scanviewer.cpp tools/scanviewer/FitParamDlg.cpp
	tools/scanpos/ScanPosDlg.cpp
	tools/powderfit/PowderFitDlg.cpp

	dialogs/SettingsDlg.cpp dialogs/FilePreviewDlg.cpp
	dialogs/GotoDlg.cpp dialogs/DWDlg.cpp dialogs/DynPlaneDlg.cpp
	dialogs/NeutronDlg.cpp dialogs/TOFDlg.cpp
	dialogs/SpurionDlg.cpp dialogs/PowderDlg.cpp
	dialogs/DispDlg.cpp dialogs/FavDlg.cpp
	dialogs/RecipParamDlg.cpp dialogs/RealParamDlg.cpp
	dialogs/SrvDlg.cpp dialogs/NetCacheDlg.cpp dialogs/ScanMonDlg.cpp
	dialogs/EllipseDlg.cpp tools/ffact/FormfactorDlg.cpp
	dialogs/AtomsDlg.cpp dialogs/AboutDlg.cpp
	dialogs/DeadAnglesDlg.cpp dialogs/LogDlg.cpp

	libs/spacegroups/spacegroup.cpp tools/sglist/SgListDlg.cpp
	libs/globals.cpp libs/globals_qt.cpp libs/spacegroups/crystalsys.cpp
	libs/formfactors/formfact.cpp libs/qt/qthelper.cpp libs/qt/qwthelper.cpp
	libs/qt/recent.cpp

	libcrystal/version.cpp

	${SRCS_3D}
)

target_link_libraries(takin 
	${LIBS_FIT} ${Boost_LIBRARIES} ${tlibs_LIBRARIES}
	Threads::Threads ${Mp_LIBRARIES} ${Rt_LIBRARIES} 
	Qt4::QtCore Qt4::QtGui Qt4::QtSvg #Qt4::QtOpenGL
	${LIBS_PY} ${LIBS_3D}
	${PNG_LIBRARIES}
#	${JPEG_LIBRARIES} ${TIFF_LIBRARIES}
	${QWT_LIBRARIES}
)


# pre-build
add_custom_target(prebuild-script
	COMMAND ${PROJECT_SOURCE_DIR}/prebuild.sh ${QT_UIC_EXECUTABLE} ${QT_MOC_EXECUTABLE} ${PROJECT_SOURCE_DIR}
)


# post-build
#add_custom_command(TARGET takin POST_BUILD
#	COMMAND qcollectiongenerator ${PROJECT_SOURCE_DIR}/doc/takin.qhcp -o ${PROJECT_SOURCE_DIR}/doc/takin.qhc
#		&& cp -v ${PROJECT_SOURCE_DIR}/doc/takin.qhc ${PROJECT_SOURCE_DIR}/res/
#		&& cp -v ${PROJECT_SOURCE_DIR}/doc/takin.qch ${PROJECT_SOURCE_DIR}/res/
#	MAIN_DEPENDENCY takin)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
	add_custom_command(TARGET takin POST_BUILD
		COMMAND strip -v $<TARGET_FILE:takin>
		MAIN_DEPENDENCY takin
	)
endif()


add_dependencies(takin prebuild-script)
add_dependencies(takin gentab)
# -----------------------------------------------------------------------------



# -----------------------------------------------------------------------------
# convofit, convoseries
# -----------------------------------------------------------------------------
add_executable(convofit 
	tools/res/r0.cpp
	tools/res/cn.cpp tools/res/pop.cpp tools/res/eck.cpp tools/res/viol.cpp 
	#tools/res/simple.cpp

	tools/monteconvo/TASReso.cpp
	tools/monteconvo/sqw.cpp tools/monteconvo/sqwbase.cpp tools/monteconvo/sqwfactory.cpp
	tools/monteconvo/sqw_py.cpp # tools/monteconvo/sqw_proc.cpp

	tools/convofit/convofit.cpp tools/convofit/convofit_import.cpp
	tools/convofit/model.cpp tools/convofit/scan.cpp
	tools/convofit/convofit_main.cpp
)

set_target_properties(convofit PROPERTIES COMPILE_FLAGS "-DNO_QT")

target_link_libraries(convofit
	${LIBS_FIT}
	${tlibs_LIBRARIES} Threads::Threads ${Mp_LIBRARIES} ${Rt_LIBRARIES}
	${Boost_LIBRARIES} ${LIBS_PY}
)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
	add_custom_command(TARGET convofit POST_BUILD
		COMMAND strip -v $<TARGET_FILE:convofit>
		MAIN_DEPENDENCY convofit
	)
endif()
# -----------------------------------------------------------------------------

add_executable(convoseries
	tools/convofit/scanseries.cpp
)

set_target_properties(convoseries PROPERTIES COMPILE_FLAGS "-DNO_QT")

target_link_libraries(convoseries
	${tlibs_LIBRARIES} ${Boost_LIBRARIES} ${LIBS_PY}
)

if(CMAKE_BUILD_TYPE STREQUAL "Release")
	add_custom_command(TARGET convoseries POST_BUILD
		COMMAND strip -v $<TARGET_FILE:convoseries>
		MAIN_DEPENDENCY convoseries
	)
endif()
# -----------------------------------------------------------------------------




# -----------------------------------------------------------------------------
# install
# -----------------------------------------------------------------------------
install(TARGETS takin DESTINATION bin)
install(TARGETS convofit convoseries DESTINATION bin)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/examples ${PROJECT_SOURCE_DIR}/doc
	DESTINATION share/takin)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/res/data ${PROJECT_SOURCE_DIR}/res/doc ${PROJECT_SOURCE_DIR}/res/icons
	DESTINATION share/takin/res)
install(FILES ${PROJECT_SOURCE_DIR}/COPYING ${PROJECT_SOURCE_DIR}/AUTHORS 
	${PROJECT_SOURCE_DIR}/LICENSES ${PROJECT_SOURCE_DIR}/LITERATURE
	DESTINATION share/takin)
# -----------------------------------------------------------------------------
