###############################################################################
# 
#  Copyright (2008) Alexander Stukowski
#
#  This file is part of OVITO (Open Visualization Tool).
#
#  OVITO is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  OVITO is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

# This plugin relies on the AtomViz plugin.
IF(NOT OVITO_BUILD_PLUGIN_ATOMVIZ)
	MESSAGE(FATAL_ERROR "To build the CrystalAnalysis plugin the AtomViz plugin has to be enabled too. Please set the OVITO_BUILD_PLUGIN_ATOMVIZ option to ON.")
ENDIF(NOT OVITO_BUILD_PLUGIN_ATOMVIZ)

# Find GNU Scientific Library (GSL)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
FIND_PACKAGE(GSL)
IF(NOT GSL_FOUND)
	MESSAGE(FATAL_ERROR "The GNU Scientific Library (GSL) was not found on your system but is required for the Crystal Analysis plugin. Please install this library on your system or specify the location of the library manually.")
ENDIF(NOT GSL_FOUND)
INCLUDE_DIRECTORIES(${GSL_INCLUDE_DIR})

# Find Computational Geometry Algorithms Library (CGAL)
#FIND_PACKAGE(CGAL)
#IF(NOT CGAL_FOUND)
#	MESSAGE(FATAL_ERROR "The Computational Geometry Algorithms Library (CGAL) was not found on your system but is required for the Crystal Analysis plugin. Please install this library on your system or specify the location of the library manually.")
#ENDIF(NOT CGAL_FOUND)
#INCLUDE_DIRECTORIES(${CGAL_INCLUDE_DIR})

# Enable precompiled headers when using the GCC compiler.
IF(OVITO_USE_PRECOMPILED_HEADERS AND CMAKE_COMPILER_IS_GNUCXX)
	INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ENDIF(OVITO_USE_PRECOMPILED_HEADERS AND CMAKE_COMPILER_IS_GNUCXX)

# This is needed to export the symbols in this shared library.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMAKING_MODULE_CRYSTALANALYSIS")

# The header files that need to be processed by the QT meta-object compiler.
SET(CrystalAnalysis_MOC_HDRS
	grains/AnalyzeMicrostructureModifier.h
	deformation/CalculateExtrinsicStrainModifier.h
	deformation/CalculateIntrinsicStrainModifier.h
#	alphashape/AlphaShapeUtility.h
)

# The regular source files of this library.
SET(CrystalAnalysis_SRCS
	grains/AnalyzeMicrostructureModifier.cpp
	grains/FindGrains.cpp
	deformation/CalculateExtrinsicStrainModifier.cpp
	deformation/CalculateIntrinsicStrainModifier.cpp
#	alphashape/AlphaShapeUtility.cpp
)

# AlphaShapeUtility.cpp implicitely includes "/usr/include/c++/4.3/ext/hash_set" which uses depreciated functions. Turn off the compiler warings.
#SET_SOURCE_FILES_PROPERTIES(alphashape/AlphaShapeUtility.cpp PROPERTIES COMPILE_FLAGS "-Wno-deprecated")

# Generate precompiled header
IF(OVITO_USE_PRECOMPILED_HEADERS AND MSVC)
	SET_SOURCE_FILES_PROPERTIES(${AtomViz_SRCS} PROPERTIES COMPILE_FLAGS "/Yucore/Core.h /Fpcrystalanalysis.pch")
	SET_SOURCE_FILES_PROPERTIES(PrecompiledHeader.cpp PROPERTIES COMPILE_FLAGS "/Yccore/Core.h /Fpcrystalanalysis.pch")
ENDIF(OVITO_USE_PRECOMPILED_HEADERS AND MSVC)

# Add optional source files needed for scripting of the plugin.
IF(OVITO_BUILD_PLUGIN_SCRIPTING)
	SET(CrystalAnalysis_SRCS ${CrystalAnalysis_SRCS} python/PythonInterface.cpp)
ENDIF(OVITO_BUILD_PLUGIN_SCRIPTING)

# Use the QT meta-object compiler to generate additional code.
QT4_WRAP_CPP(CrystalAnalysis_MOC_SRCS ${CrystalAnalysis_MOC_HDRS})

# Generate plugin module.
ADD_LIBRARY(CrystalAnalysis 
	PrecompiledHeader.cpp	 
	${CrystalAnalysis_SRCS}
	${CrystalAnalysis_MOC_SRCS}	
)

IF(OVITO_USE_PRECOMPILED_HEADERS AND CMAKE_COMPILER_IS_GNUCXX)
	ADD_PRECOMPILED_HEADER(CrystalAnalysis ${CMAKE_SOURCE_DIR}/src/core/Core.h)
ENDIF(OVITO_USE_PRECOMPILED_HEADERS AND CMAKE_COMPILER_IS_GNUCXX)

# Link required libraries
TARGET_LINK_LIBRARIES(CrystalAnalysis Base Core Mesh AtomViz)

# Link the target against the Qt libraries. 
TARGET_LINK_LIBRARIES(CrystalAnalysis ${QT_LIBRARIES})

# Add GSL library
TARGET_LINK_LIBRARIES(CrystalAnalysis ${GSL_LIBRARIES})

# Add CGAL library
#TARGET_LINK_LIBRARIES(CrystalAnalysis ${CGAL_LIBRARIES})

# Add Python libraries for scripting
IF(OVITO_BUILD_PLUGIN_SCRIPTING)
	TARGET_LINK_LIBRARIES(CrystalAnalysis Scripting ${BOOST_PYTHON_LIB} ${PYTHON_UTIL_LIBRARY} ${PYTHON_LIBRARIES})
ENDIF(OVITO_BUILD_PLUGIN_SCRIPTING)

# Build the resource files for this plugin.
QT4_COMPILE_RESOURCES(CrystalAnalysis ${OVITO_PLUGINS_DIRECTORY}/crystalanalysis.rcc resources/crystalanalysis.qrc)

# Assign an absolute install path to this dynamic link library.
IF(APPLE)
	SET_TARGET_PROPERTIES(CrystalAnalysis PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
ENDIF(APPLE)

# Copy plugin manifest to destination directory.
CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/resources/CrystalAnalysis.manifest.xml" 
               "${OVITO_PLUGINS_DIRECTORY}/CrystalAnalysis.manifest.xml")

# Install script files for this plugin.
IF(OVITO_BUILD_PLUGIN_SCRIPTING)
	COPY_DIRECTORY_WITHOUT_SVN("${CMAKE_CURRENT_SOURCE_DIR}/resources/scripts" "${OVITO_SHARE_DIRECTORY}/scripts/crystalanalysis")
	INSTALL(DIRECTORY "${OVITO_SHARE_DIRECTORY}/scripts/crystalanalysis" DESTINATION "${OVITO_RELATIVE_SHARE_DIRECTORY}/scripts/" PATTERN ".svn" EXCLUDE)
ENDIF(OVITO_BUILD_PLUGIN_SCRIPTING)

# This plugin will be part of the installation package.
IF(NOT OVITO_MONOLITHIC_BUILD)
	INSTALL(TARGETS CrystalAnalysis DESTINATION "${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
ENDIF(NOT OVITO_MONOLITHIC_BUILD)
INSTALL(FILES "${OVITO_PLUGINS_DIRECTORY}/CrystalAnalysis.manifest.xml" DESTINATION "${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
INSTALL(FILES "${OVITO_PLUGINS_DIRECTORY}/crystalanalysis.rcc" DESTINATION "${OVITO_RELATIVE_PLUGINS_DIRECTORY}")
