cmake_minimum_required(VERSION 3.1.0)

if(POLICY CMP0078)
    cmake_policy(SET CMP0078 NEW)
endif()
if(POLICY CMP0086)
    cmake_policy(SET CMP0086 NEW)
endif()
if(POLICY CMP0127)
    cmake_policy(SET CMP0127 NEW)
endif()

project( MathGL2 )

set(mgl_clean_files )
set(MGL_DEP_LIBS)
add_definitions(-DMGL_SRC)

#add_definitions(-DZLIB_WINAPI)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${MathGL2_SOURCE_DIR}/scripts)

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are:
		None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif(NOT CMAKE_BUILD_TYPE)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(MathGL_VERSION_MAJOR 8)
set(MathGL_VERSION_MINOR 0)
set(MathGL_PATCH_VERSION 1)
set(MathGL_VERSION ${MathGL_VERSION_MAJOR}.${MathGL_VERSION_MINOR}.${MathGL_PATCH_VERSION})
set(MathGL_SOVERSION ${MathGL_VERSION_MAJOR})
string(TIMESTAMP MathGL_DATE UTC)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
	option(enable-lcov "Enable coverage." OFF)
	if(enable-lcov)
		include(CodeCoverage)
		APPEND_COVERAGE_COMPILER_FLAGS()
	endif(enable-lcov)
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
	unset(enable-lcov CACHE)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

#set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)
set(MathGL_INSTALL_LIB_DIR "lib" CACHE PATH "Installation directory for libraries")
set(MathGL_INSTALL_BIN_DIR "bin" CACHE PATH "Installation directory for executables")
set(MathGL_INSTALL_INCLUDE_DIR "include" CACHE PATH "Installation directory for headers")
if(WIN32 AND NOT CYGWIN)
	set(DEF_INSTALL_CMAKE_DIR cmake)
else()
	set(DEF_INSTALL_CMAKE_DIR lib/cmake/mathgl2)
endif()
set(MathGL_INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
# Make relative paths absolute (needed later on)
foreach(p LIB BIN INCLUDE CMAKE)
	set(var MathGL_INSTALL_${p}_DIR)
	if(NOT IS_ABSOLUTE "${${var}}")
		set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
	endif()
endforeach()

macro(mgl_po_src)
	set(l_files )
	foreach(src_file ${ARGN})
		get_filename_component(fp ${src_file} ABSOLUTE)
		file(RELATIVE_PATH rel ${CMAKE_SOURCE_DIR} ${fp})
		list(APPEND l_files "${rel}")
	endforeach(src_file ${ARGN})
	set(po_files ${po_files} ${l_files} PARENT_SCOPE)
endmacro(mgl_po_src)

function(mgl_add_lib mgl_tmp_lib)
	if(${mgl_tmp_lib} MATCHES mgl)
		set(mgllib mgl)
		set(mgllib2 mgl2)
	else(${mgl_tmp_lib} MATCHES mgl)
		set(mgllib mgl-${mgl_tmp_lib})
		set(mgllib2 mgl2-${mgl_tmp_lib})
	endif(${mgl_tmp_lib} MATCHES mgl)
	set(mgl_src_lst ${ARGV})
	list(REMOVE_AT mgl_src_lst 0)
	add_library(${mgllib} SHARED ${mgl_src_lst})
	add_library(${mgllib}-static STATIC ${mgl_src_lst})
	set_target_properties(${mgllib} PROPERTIES SOVERSION ${MathGL_SOVERSION})
	set_target_properties(${mgllib} PROPERTIES VERSION ${MathGL_VERSION})
	set_target_properties(${mgllib} PROPERTIES DEFINE_SYMBOL "mgl_EXPORTS")
	set_target_properties(${mgllib} PROPERTIES C_VISIBILITY_PRESET hidden)
	set_target_properties(${mgllib} PROPERTIES CXX_VISIBILITY_PRESET hidden)
	set_target_properties(${mgllib} PROPERTIES VISIBILITY_INLINES_HIDDEN 1)
	target_compile_definitions(${mgllib}-static PUBLIC MGL_STATIC_DEFINE)
	if(MSVC)
		set(mgl_lib_static "-static")
		set (CMAKE_DEBUG_POSTFIX "d")
		set_target_properties(${mgllib} PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
		set_target_properties(${mgllib}-static PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
#		if(CMAKE_BUILD_TYPE STREQUAL "Debug")
#			set(mgl_lib_end "d")
#		else(CMAKE_BUILD_TYPE STREQUAL "Debug")
#			set(mgl_lib_end)
#		endif(CMAKE_BUILD_TYPE STREQUAL "Debug")
	elseif(MSVC)
		set(mgl_lib_static)
		set_target_properties(${mgllib} PROPERTIES CLEAN_DIRECT_OUTPUT 1)
	set_target_properties(${mgllib}-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
	endif(MSVC)
	if(enable-mgl2)
			set_target_properties(${mgllib} PROPERTIES OUTPUT_NAME "${mgllib2}${mgl_lib_end}")
			set_target_properties(${mgllib}-static PROPERTIES OUTPUT_NAME "${mgllib2}${mgl_lib_static}${mgl_lib_end}")
	else(enable-mgl2)
			set_target_properties(${mgllib} PROPERTIES OUTPUT_NAME "${mgllib}${mgl_lib_end}")
			set_target_properties(${mgllib}-static PROPERTIES OUTPUT_NAME "${mgllib}${mgl_lib_static}${mgl_lib_end}")
	endif(enable-mgl2)

	install(
			TARGETS ${mgllib} ${mgllib}-static
			EXPORT MathGLTargets
			RUNTIME DESTINATION ${MathGL_INSTALL_BIN_DIR}
			ARCHIVE DESTINATION ${MathGL_INSTALL_LIB_DIR}
			LIBRARY DESTINATION ${MathGL_INSTALL_LIB_DIR}
	)
endfunction(mgl_add_lib mgl_tmp_lib)

MACRO(MGL_DEPENDENT_OPTION option doc default depends1 force1 depends2 force2)
  IF(${option}_ISSET MATCHES "^${option}_ISSET$")
    SET(${option}_AVAILABLE 1)
    IF(${force1})
    FOREACH(d ${depends1})
      STRING(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
      IF(${CMAKE_DEPENDENT_OPTION_DEP})
      ELSE(${CMAKE_DEPENDENT_OPTION_DEP})
          SET(${option}_AVAILABLE 0)
          SET(depends1_AVAILABLE 1)
      ENDIF(${CMAKE_DEPENDENT_OPTION_DEP})
    ENDFOREACH(d)
    ENDIF(${force1})
    IF(${force2})
    FOREACH(d ${depends2})
      STRING(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
      IF(${CMAKE_DEPENDENT_OPTION_DEP})
      ELSE(${CMAKE_DEPENDENT_OPTION_DEP})
          SET(${option}_AVAILABLE 0)
          SET(depends2_AVAILABLE 1)
      ENDIF(${CMAKE_DEPENDENT_OPTION_DEP})
    ENDFOREACH(d)
    ENDIF(${force2})
    IF(${option}_AVAILABLE)
      OPTION(${option} "${doc}" "${default}")
      SET(${option} "${${option}}" CACHE BOOL "${doc}" FORCE)
    ELSE(${option}_AVAILABLE)
      IF(${option} MATCHES "^${option}$")
      ELSE(${option} MATCHES "^${option}$")
        SET(${option} "${${option}}" CACHE INTERNAL "${doc}")
      ENDIF(${option} MATCHES "^${option}$")
        IF(depends1_AVAILABLE)
          SET(${option} OFF)
        ELSEIF(depends2_AVAILABLE)
          SET(${option} ON)
        ENDIF(depends1_AVAILABLE)
    ENDIF(${option}_AVAILABLE)
  ELSE(${option}_ISSET MATCHES "^${option}_ISSET$")
    SET(${option} "${${option}_ISSET}")
  ENDIF(${option}_ISSET MATCHES "^${option}_ISSET$")
ENDMACRO(MGL_DEPENDENT_OPTION)
include(CMakeDependentOption)

string(TIMESTAMP MGL_NIGHT "%d.%m.%y")

if(WIN32)
	option(enable-dep-dll "Enable copying off all dependent dll libraries to the install directory" OFF)
endif(WIN32)

option(enable-double "Enable double precision in MathGL library" ON)
option(enable-mpi "Enable mpi")
option(enable-opengl "Enable OpenGL support" ON)
option(enable-all-docs "Enable all documentation building")
#option(enable-doc "Enable documentation building")
option(enable-all "Enable all core features")
option(enable-all-widgets "Enable all Widgets")
option(enable-all-swig "Enable all SWIG based interfaces")
option(enable-rvalue "Enable move constructor support (need C++11)" OFF)
option(enable-pthread "Enable POSIX threads support" OFF)
if(MSVC)
	option(enable-pthr-widget "Enable POSIX threads for widgets" OFF)
else(MSVC)
	option(enable-pthr-widget "Enable POSIX threads for widgets" ON)
endif(MSVC)
option(enable-openmp "Enable OpenMP support" ON)
option(disable-C99complex "Enable C99 complex number support" OFF)

if(enable-pthread AND enable-openmp)
	message(SEND_ERROR "You can't enable POSIX threads and OpenMP at the same time!")
endif(enable-pthread AND enable-openmp)

option(enable-lgpl "Enable only LGPL part of MathGL")
option(enable-mgl2 "Use names 'libmgl2-*' instead of 'libmgl-*'")
option(enable-ltdl "Enable loading modules support")
option(enable-doc-site "Enable HTML documentation for website")
#CMAKE_DEPENDENT_OPTION(enable-doc-site "Enable HTML documentation for website" OFF "NOT enable-all-docs" ON)
CMAKE_DEPENDENT_OPTION(enable-doc-html "Enable HTML documentation" OFF "NOT enable-all-docs" ON)
CMAKE_DEPENDENT_OPTION(enable-doc-info "Enable INFO documentation" OFF "NOT enable-all-docs" ON)
CMAKE_DEPENDENT_OPTION(enable-doc-pdf-ru "Enable Russian PDF documentation" OFF "NOT enable-all-docs" ON)
CMAKE_DEPENDENT_OPTION(enable-doc-pdf-en "Enable English PDF documentation" OFF "NOT enable-all-docs" ON)
#CMAKE_DEPENDENT_OPTION(enable-doc-prc "Enable PDF samples for HTML docs" OFF "NOT enable-all-docs" ON)
#CMAKE_DEPENDENT_OPTION(enable-doc-json "Enable JSON samples for HTML docs" OFF "NOT enable-all-docs" ON)
option(enable-texi2html "Use texi2html (obsolete package) instead of texi2any" OFF)
CMAKE_DEPENDENT_OPTION(enable-mgltex "Enable installation of mgltex package (MGL scripts in LATEX document)" OFF "NOT enable-lgpl" OFF)

CMAKE_DEPENDENT_OPTION(enable-zlib "Enable zlib support" ON "NOT enable-all" ON)
CMAKE_DEPENDENT_OPTION(enable-png "Enable png support" ON "NOT enable-all" ON)
CMAKE_DEPENDENT_OPTION(enable-jpeg "Enable jpeg support" OFF "NOT enable-all" ON)
MGL_DEPENDENT_OPTION(enable-gsl "Enable gsl support" OFF "NOT enable-lgpl" ON "NOT enable-all" ON)
MGL_DEPENDENT_OPTION(enable-hdf4 "Enable hdf4 support" OFF "NOT enable-lgpl" ON "NOT enable-all" ON)
MGL_DEPENDENT_OPTION(enable-hdf5 "Enable hdf5 support" OFF "NOT enable-lgpl" ON "NOT enable-all" ON)
CMAKE_DEPENDENT_OPTION(enable-pdf "Enable pdf support" OFF "NOT enable-all" ON)
CMAKE_DEPENDENT_OPTION(enable-gif "Enable gif support" OFF "NOT enable-all" ON)
CMAKE_DEPENDENT_OPTION(enable-arma "Enable Armadillo support" OFF "NOT enable-all" ON)
CMAKE_DEPENDENT_OPTION(enable-glut "Enable glut support" OFF "NOT enable-all-widgets" ON)
CMAKE_DEPENDENT_OPTION(enable-fltk "Enable fltk widget" OFF "NOT enable-all-widgets" ON)
CMAKE_DEPENDENT_OPTION(enable-wx "Enable wxWidget widget" OFF "NOT enable-all-widgets" ON)
#CMAKE_DEPENDENT_OPTION(enable-qt "Enable Qt4 and/or Qt5 widget(s)" OFF "NOT enable-all-widgets" ON)
CMAKE_DEPENDENT_OPTION(enable-qt "Enable Qt5 widget" OFF "NOT enable-all-widgets" ON)
#option(enable-qt5 "Enable Qt5 widget" OFF)
CMAKE_DEPENDENT_OPTION(enable-qt5asqt "Set Qt5 as default libmgl-qt" OFF "enable-qt5" OFF)
CMAKE_DEPENDENT_OPTION(enable-freetype "Enable freetype support" OFF "NOT enable-all" ON)
set(PY3VERSION_DOTTED "3.9" CACHE STRING "Used python version")
string(REPLACE "." "" PY3VERSION_NODOT ${PY3VERSION_DOTTED})

# msvc fwprintf print char* for the specifier of "%s" format
if(MSVC AND MSVC_VERSION GREATER 1899)
	SET(CMAKE_CXX_FLAGS "/EHsc -D_CRT_STDIO_ISO_WIDE_SPECIFIERS ${CMAKE_CXX_FLAGS}")
	SET(CMAKE_C_FLAGS "-D_CRT_STDIO_ISO_WIDE_SPECIFIERS ${CMAKE_C_FLAGS}")
endif(MSVC AND MSVC_VERSION GREATER 1899)

include(CheckCXXSourceCompiles)

if(${CMAKE_SIZEOF_VOID_P} EQUAL 4)
	set(CMAKE_REQUIRED_FLAGS -msse2 -mfpmath=sse)
	CHECK_CXX_SOURCE_COMPILES("
		int main(void){}" mgl_sse)
	if(mgl_sse)
		SET(CMAKE_CXX_FLAGS "-msse2 -mfpmath=sse ${CMAKE_CXX_FLAGS}")
		SET(CMAKE_C_FLAGS "-msse2 -mfpmath=sse ${CMAKE_C_FLAGS}")
	endif(mgl_sse)
	unset(CMAKE_REQUIRED_FLAGS)
endif(${CMAKE_SIZEOF_VOID_P} EQUAL 4)

#if(enable-qt)
#	if(Qt4_FOUND)
#		set(enable-qt4 ON)
#		find_package(Qt5 COMPONENTS Widgets)
#			set(enable-qt5 ON)
#		endif(Qt5Widgets_FOUND)
#	else(Qt4_FOUND)
#		set(enable-qt5 ON)
#	endif(Qt4_FOUND)
#endif(enable-qt)
if(enable-qt)
	find_package(Qt5 COMPONENTS Widgets)
	set(enable-qt5 ON)
	if(NOT Qt5Widgets_FOUND)
		message(SEND_ERROR "Couldn't find Qt5 widgets.")
	endif(NOT Qt5Widgets_FOUND)
endif(enable-qt)

if(enable-qt4 OR enable-qt5)
	set(QT_ENABLED ON)
	if(enable-qt4asqt AND enable-qt5asqt)
		message(SEND_ERROR "You cannot make Qt4 and Qt5 as qt at the same time.")
	endif(enable-qt4asqt AND enable-qt5asqt)
	if(enable-qt4 AND NOT enable-qt5)
		set(enable-qt4asqt TRUE)
	endif(enable-qt4 AND NOT enable-qt5)
	if(enable-qt5 AND NOT enable-qt4)
		set(enable-qt5asqt TRUE)
	endif(enable-qt5 AND NOT enable-qt4)
# 	if(NOT enable-opengl)
# 		message(SEND_ERROR "You cannot build MathGL with Qt4 or Qt5 without OpenGL enabled.")
# 	endif(NOT enable-opengl)
endif(enable-qt4 OR enable-qt5)

CMAKE_DEPENDENT_OPTION(enable-json-sample "Enable JSON sample (WebKit variant is the default)." ON "QT_ENABLED" OFF)
CMAKE_DEPENDENT_OPTION(enable-json-sample-we "Enable JSON sample (WebEngine variant)." OFF "enable-json-sample" OFF)
MGL_DEPENDENT_OPTION(enable-python "Enable python interface" OFF "NOT enable-lgpl" ON "NOT enable-all-swig" ON)
MGL_DEPENDENT_OPTION(enable-lua "Enable Lua (v.5.1) interface" OFF "NOT enable-lgpl" ON "NOT enable-all-swig" ON)
MGL_DEPENDENT_OPTION(enable-octave "Enable octave interface" OFF "NOT enable-lgpl" ON "NOT enable-all-swig" ON)
MGL_DEPENDENT_OPTION(enable-octave-install "Octave interface will install for all users" ON "NOT enable-lgpl" ON "NOT enable-all-swig" ON)

include_directories( ${MathGL2_SOURCE_DIR}/include ${MathGL2_BINARY_DIR}/include)
set(MGL_INCLUDE_PATH "${CMAKE_INSTALL_PREFIX}/include/mgl2")
set(MGL_CGI_PATH_INSTALL "lib/cgi-bin" CACHE STRING "Set CGI install directory")
#set(MGL_CGI_PATH_INSTALL "share/mathgl" CACHE STRING "Set CGI install directory")
set(MGL_CGI_PATH "${CMAKE_INSTALL_PREFIX}/${MGL_CGI_PATH_INSTALL}")
set(MGL_DEF_FONT "STIX" CACHE STRING "Set default font name")

if(NOT WIN32)
#	set(MGL_CGI_PATH "${CMAKE_INSTALL_PREFIX}/share/mathgl")
	set(MGL_DATA_PATH "${CMAKE_INSTALL_PREFIX}/share/mathgl")
	set(MGL_DOC_PATH "${CMAKE_INSTALL_PREFIX}/share/doc/mathgl")
	set(MGL_MAN_PATH "${CMAKE_INSTALL_PREFIX}/share/man")
	set(MGL_INFO_PATH "${CMAKE_INSTALL_PREFIX}/share/info")
	set(MGL_FONT_PATH "${MGL_DATA_PATH}/fonts")
else(NOT WIN32)
	set(MGL_FONT_PATH "${CMAKE_INSTALL_PREFIX}/fonts")
endif(NOT WIN32)

include(CheckFunctionExists)
include(CMakePushCheckState)
include(TestBigEndian)

TEST_BIG_ENDIAN(WORDS_BIGENDIAN)
if(WORDS_BIGENDIAN)
	ADD_DEFINITIONS(-DWORDS_BIGENDIAN)
endif(WORDS_BIGENDIAN)

CHECK_FUNCTION_EXISTS(sin MGL_SIN)
CHECK_FUNCTION_EXISTS(memrchr HAVE_MEMRCHR)
if(NOT MGL_SIN)
	cmake_push_check_state()
	set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} m)
	CHECK_FUNCTION_EXISTS(sin MGL_SIN_M)
	cmake_pop_check_state()
	if(MGL_SIN_M)
		set(M_LIB m)
	elseif(MGL_SIN_M)
		message(SEND_ERROR "Math library not found")
	endif(MGL_SIN_M)
endif(NOT MGL_SIN)
set(MGL_DEP_LIBS ${M_LIB} ${MGL_DEP_LIBS})

if(HAVE_MEMRCHR)
	ADD_DEFINITIONS(-DHAVE_MEMRCHR)
endif(HAVE_MEMRCHR)

include(CheckTypeSize)
check_type_size("long" SIZEOF_LONG)

if(NOT disable-C99complex)
# 	unset(MGL_HAVE_C99_COMPLEX)
	CHECK_CXX_SOURCE_COMPILES(
	"#include <complex>
	#include <complex.h>
	int main(int argc, char *args[])
	{std::complex<double> c(2.0, 1.0);
	double _Complex i=1.0i;
	double _Complex *a = reinterpret_cast<double _Complex *>(&c);
	std::complex<double> b(*a);return 0;}" MGL_HAVE_C99_COMPLEX)
	if(NOT MGL_HAVE_C99_COMPLEX)
		set(MGL_HAVE_C99_COMPLEX 0)
	endif(NOT MGL_HAVE_C99_COMPLEX)
else(NOT disable-C99complex)
	set(MGL_HAVE_C99_COMPLEX 0)
endif(NOT disable-C99complex)

#unset(MGL_HAVE_NAN_INF)
CHECK_CXX_SOURCE_COMPILES(
"#include <math.h>
int main(){double a=NAN, b=INFINITY;return 0;}" MGL_HAVE_NAN_INF)
if(NOT MGL_HAVE_NAN_INF)
	set(MGL_HAVE_NAN_INF 0)
endif(NOT MGL_HAVE_NAN_INF)

#unset(MGL_HAVE_ATTRIBUTE)
CHECK_CXX_SOURCE_COMPILES(
"int __attribute__((pure)) test1() {return 0;}
int __attribute__((const)) test2(int x) {return x*x;}
int main(int argc, char* argv[]) {return 0;}" MGL_HAVE_ATTRIBUTE)
if(NOT MGL_HAVE_ATTRIBUTE)
	set(MGL_HAVE_ATTRIBUTE 0)
endif(NOT MGL_HAVE_ATTRIBUTE)

if(NOT MSVC AND enable-rvalue)
	SET(CMAKE_CXX_FLAGS "-std=gnu++11 ${CMAKE_CXX_FLAGS}")
	unset(MGL_HAVE_RVAL CACHE)
	CHECK_CXX_SOURCE_COMPILES(
	"struct test { test() {} test(test&& a){} };
	int main() { test t;	return 0; }" MGL_HAVE_RVAL)
	if(NOT MGL_HAVE_RVAL)
		message(SEND_ERROR "Couldn't enable rvalue.")
#		set(MGL_HAVE_RVAL 0)
	endif(NOT MGL_HAVE_RVAL)
else(NOT MSVC AND enable-rvalue)
	set(MGL_HAVE_RVAL 0)
endif(NOT MSVC AND enable-rvalue)



CHECK_CXX_SOURCE_COMPILES(
"#include <getopt.h>
int main(int argc, char *args[]) {
int ch = getopt(argc, args, \"1:2:3:4:5:6:7:8:9:hno:L:C:A:s:S:q:\"); return 0; }" MGL_HAVE_GETOPT)
if(NOT MGL_HAVE_GETOPT)
	include_directories(${MathGL2_SOURCE_DIR}/addons/getopt)
	set(getopt_lib-static getopt-static)
endif(NOT MGL_HAVE_GETOPT)

if(enable-double)
	set(MGL_USE_DOUBLE 1)
else(enable-double)
	set(MGL_USE_DOUBLE 0)
endif(enable-double)

if(enable-qt4 OR enable-qt5)
	set(MGL_HAVE_QT 1)
endif(enable-qt4 OR enable-qt5)

if(enable-mpi)
	set(MGL_HAVE_MPI 1)
	find_package(MPI REQUIRED)
	set(CMAKE_CXX_COMPILE_FLAGS ${CMAKE_CXX_COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
	set(CMAKE_CXX_LINK_FLAGS ${CMAKE_CXX_LINK_FLAGS} ${MPI_LINK_FLAGS})
	include_directories(${MPI_INCLUDE_PATH})
else(enable-mpi)
	set(MGL_HAVE_MPI 0)
endif(enable-mpi)

if(enable-pthr-widget OR enable-pthread)
	if(enable-pthread)
		set(MGL_HAVE_PTHREAD 1)
	else(enable-pthread)
		set(MGL_HAVE_PTHREAD 0)
	endif(enable-pthread)
	# cmake v.3.1
	set(MGL_HAVE_PTHR_WIDGET 1)
	set(CMAKE_THREAD_PREFER_PTHREAD ON)
	set(THREADS_PREFER_PTHREAD_FLAG ON)
	find_package(Threads REQUIRED)
	# was for v.2.8.12
	#	include(FindThreads)
	if(NOT CMAKE_USE_PTHREADS_INIT)
		message(SEND_ERROR "Couldn't find POSIX threads library.")
	endif(NOT CMAKE_USE_PTHREADS_INIT)
	# cmake v.3.1
	set(MGL_DEP_LIBS Threads::Threads ${MGL_DEP_LIBS})
	# was for v.2.8.12
	#	set(MGL_DEP_LIBS ${CMAKE_THREAD_LIBS_INIT} ${MGL_DEP_LIBS})
else(enable-pthr-widget OR enable-pthread)
	set(MGL_HAVE_PTHR_WIDGET 0)
	set(MGL_HAVE_PTHREAD 0)
endif(enable-pthr-widget OR enable-pthread)

if(enable-arma)
	find_package(Armadillo)
	if(ARMADILLO_FOUND)
		set(MGL_HAVE_ARMA 1)
	else(ARMADILLO_FOUND)
		set(MGL_HAVE_ARMA 0)
	endif(ARMADILLO_FOUND)
else(enable-arma)
	set(MGL_HAVE_ARMA 0)
endif(enable-arma)


if(enable-openmp)
	find_package(OpenMP)
	if(OPENMP_FOUND)
		set(MGL_HAVE_OMP 1)
		set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
		set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
		set (MGL_DEP_LIBS OpenMP::OpenMP_CXX ${MGL_DEP_LIBS})
	else(OPENMP_FOUND)
		message(SEND_ERROR "Couldn't find OpenMP. You can enable POSIX threads instead.")
		set(MGL_HAVE_OMP 0)
	endif(OPENMP_FOUND)
else(enable-openmp)
	set(MGL_HAVE_OMP 0)
endif(enable-openmp)

if(enable-gsl)
	set(MGL_HAVE_GSL 1)
	find_library(GSL_LIB gsl)
	find_library(GSL_CBLAS_LIB gslcblas)
	find_path(GSL_INCLUDE_DIR gsl/gsl_fft_complex.h)
	if(NOT GSL_LIB OR NOT GSL_CBLAS_LIB OR NOT GSL_INCLUDE_DIR)
		message(SEND_ERROR "${GSL_LIB}")
		message(SEND_ERROR "${GSL_CBLAS_LIB}")
		message(SEND_ERROR "${GSL_INCLUDE_DIR}")
		message(SEND_ERROR "Couldn't find GSL libraries.")
	else(NOT GSL_LIB OR NOT GSL_CBLAS_LIB OR NOT GSL_INCLUDE_DIR)
		set(CMAKE_REQUIRED_INCLUDES ${GSL_INCLUDE_DIR})
		set(CMAKE_REQUIRED_LIBRARIES ${GSL_LIB} ${GSL_CBLAS_LIB})
		CHECK_CXX_SOURCE_COMPILES("#include <gsl/gsl_multifit_nlin.h>
		int main(){gsl_multifit_fdfsolver *s=0;gsl_matrix *J = 0;
		gsl_multifit_fdfsolver_jac(s, J);}" MGL_HAVE_GSL2)
		unset(CMAKE_REQUIRED_INCLUDES)
		unset(CMAKE_REQUIRED_LIBRARIES)
	endif(NOT GSL_LIB OR NOT GSL_CBLAS_LIB OR NOT GSL_INCLUDE_DIR)
	set(MGL_DEP_LIBS ${GSL_LIB} ${GSL_CBLAS_LIB} ${MGL_DEP_LIBS})
	include_directories(${GSL_INCLUDE_DIR})
else(enable-gsl)
	set(MGL_HAVE_GSL 0)
endif(enable-gsl)

if(enable-all OR enable-ltdl)
	set(MGL_HAVE_LTDL 1)
	find_library(LTDL_LIB ltdl)
	find_path(LTDL_INCLUDE_DIR ltdl.h)
	if(NOT LTDL_LIB OR NOT LTDL_INCLUDE_DIR)
		message(SEND_ERROR "${LTDL_LIB}")
		message(SEND_ERROR "${LTDL_INCLUDE_DIR}")
		message(SEND_ERROR "Couldn't find LTDL library.")
	endif(NOT LTDL_LIB OR NOT LTDL_INCLUDE_DIR)
	set(MGL_DEP_LIBS ${LTDL_LIB} ${MGL_DEP_LIBS})
	include_directories(${LTDL_INCLUDE_DIR})
else(enable-all OR enable-ltdl)
	set(MGL_HAVE_LTDL 0)
endif(enable-all OR enable-ltdl)

if(enable-hdf4)
	set(MGL_HAVE_HDF4 1)
	find_library(HDF4_LIB NAMES df hdf)
	find_library(HDF4MF_LIB mfhdf)
	find_path(HDF4_INCLUDE_DIR mfhdf.h PATH_SUFFIXES hdf)
	if(NOT HDF4_LIB OR NOT HDF4MF_LIB OR NOT HDF4_INCLUDE_DIR)
		message(SEND_ERROR "${HDF4_LIB}")
		message(SEND_ERROR "${HDF4MF_LIB}")
		message(SEND_ERROR "${HDF4_INCLUDE_DIR}")
		message(SEND_ERROR "Couldn't find HDF4 libraries.")
	endif(NOT HDF4_LIB OR NOT HDF4MF_LIB OR NOT HDF4_INCLUDE_DIR)
	set(MGL_DEP_LIBS ${HDF4MF_LIB} ${HDF4_LIB} ${MGL_DEP_LIBS})
	include_directories(${HDF4_INCLUDE_DIR})
else(enable-hdf4)
	set(MGL_HAVE_HDF4 0)
endif(enable-hdf4)

if(enable-hdf5)
	set(MGL_HAVE_HDF5 1)
	find_package(HDF5)
	if(NOT HDF5_FOUND)
		find_package(HDF5 NAMES hdf5 COMPONENTS C shared static)
		if(NOT HDF5_FOUND)
			message(SEND_ERROR "Couldn't find HDF5 library.")
		endif(NOT HDF5_FOUND)
	endif(NOT HDF5_FOUND)
	
	string(REPLACE "." ";" HDF_VERSION_LIST ${HDF5_VERSION})
	list(GET HDF_VERSION_LIST 0 HDF_VERSION_MAJOR)
	list(GET HDF_VERSION_LIST 1 HDF_VERSION_MINOR)

	set(MGL_DEP_LIBS ${HDF5_LIBRARIES} ${HDF5_C_SHARED_LIBRARY} ${MGL_DEP_LIBS})
	include_directories(${HDF5_INCLUDE_DIRS})
else(enable-hdf5)
	set(MGL_HAVE_HDF5 0)
	set(HDF_VERSION_MAJOR 0)
	set(HDF_VERSION_MINOR 0)
endif(enable-hdf5)

if(enable-jpeg)
	set(MGL_HAVE_JPEG 1)
	include(FindJPEG)
	if(NOT JPEG_FOUND)
		message(SEND_ERROR "Couldn't find JPEG library.")
	endif(NOT JPEG_FOUND)
	set(MGL_DEP_LIBS ${JPEG_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${JPEG_INCLUDE_DIR})
else(enable-jpeg)
	set(MGL_HAVE_JPEG 0)
endif(enable-jpeg)


if(enable-zlib)
	set(MGL_HAVE_ZLIB 1)
	include(FindZLIB)
	if(NOT ZLIB_FOUND)
		message(SEND_ERROR "Couldn't find ZLib library.")
	endif(NOT ZLIB_FOUND)
	set(MGL_DEP_LIBS ${ZLIB_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${ZLIB_INCLUDE_DIR})
else(enable-zlib)
	set(MGL_HAVE_ZLIB 0)
endif(enable-zlib)

if(enable-freetype)
	set(MGL_HAVE_FREETYPE 1)
	include(FindFreetype)
	if(NOT FREETYPE_FOUND)
		message(SEND_ERROR "Couldn't find FreeType library.")
	endif(NOT FREETYPE_FOUND)
	set(MGL_DEP_LIBS ${FREETYPE_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${FREETYPE_INCLUDE_DIRS})
else(enable-freetype)
	set(MGL_HAVE_FREETYPE 0)
endif(enable-freetype)

if(enable-png)
	set(MGL_HAVE_PNG 1)
	if(NOT MGL_HAVE_ZLIB)
		message(SEND_ERROR "You have to enable ZLib if you plan to use PNG export.")
	endif(NOT MGL_HAVE_ZLIB)
	include(FindPNG)
	if(NOT PNG_FOUND)
		message(SEND_ERROR "Couldn't find PNG library.")
	endif(NOT PNG_FOUND)
	set(MGL_DEP_LIBS ${PNG_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${PNG_INCLUDE_DIR})
else(enable-png)
	set(MGL_HAVE_PNG 0)
endif(enable-png)


if(enable-pdf)
	set(MGL_HAVE_PDF 1)
	if(NOT MGL_HAVE_PNG)
		message(SEND_ERROR "You have to enable PNG if you plan to use PDF export.")
	endif(NOT MGL_HAVE_PNG)
	find_library(HPDF_LIB hpdf)
	if(NOT HPDF_LIB)
		message(SEND_ERROR "Couldn't find libHaru or libhpdf.")
	endif(NOT HPDF_LIB)
	find_path(HPDF_INCLUDE_DIR hpdf_u3d.h)
	if(NOT HPDF_INCLUDE_DIR)
		message(SEND_ERROR "Couldn't find headers of 3d-enabled version of libhpdf.")
	endif(NOT HPDF_INCLUDE_DIR)
	include_directories(${HPDF_INCLUDE_DIR})
	set(MGL_DEP_LIBS ${HPDF_LIB} ${MGL_DEP_LIBS})
else(enable-pdf)
	set(MGL_HAVE_PDF 0)
endif(enable-pdf)

if(enable-gif)
	set(MGL_HAVE_GIF 1)
	include(FindGIF)
	if(NOT GIF_FOUND)
		message(SEND_ERROR "Couldn't find GIF library.")
	endif(NOT GIF_FOUND)
	set(MGL_DEP_LIBS ${GIF_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${GIF_INCLUDE_DIR})
else(enable-gif)
	set(MGL_HAVE_GIF 0)
endif(enable-gif)

if(enable-opengl)
	set(MGL_HAVE_OPENGL 1)
	set(OpenGL_GL_PREFERENCE LEGACY)
	include(FindOpenGL)
	if(NOT OPENGL_FOUND)
		message(SEND_ERROR "Couldn't find OpenGL libraries.")
	endif(NOT OPENGL_FOUND)
	set(MGL_DEP_LIBS ${OPENGL_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${OPENGL_INCLUDE_DIR} )
else(enable-opengl)
	set(MGL_HAVE_OPENGL 0)
endif(enable-opengl)

if(enable-glut)
	set(MGL_HAVE_GLUT 1)
	if(NOT MGL_HAVE_OPENGL)
		message(SEND_ERROR "You have to enable OpenGL if you plan to use GLUT.")
	endif(NOT MGL_HAVE_OPENGL)
	include(FindGLUT)
	if(NOT GLUT_FOUND)
		message(SEND_ERROR "Couldn't find GLUT library.")
	endif(NOT GLUT_FOUND)
else(enable-glut)
	set(MGL_HAVE_GLUT 0)
endif(enable-glut)

if(enable-fltk)
	set(MGL_HAVE_FLTK 1)
	FIND_PACKAGE(FLTK)
	if(NOT FLTK_FOUND)
		message(SEND_ERROR "Couldn't find FLTK library.")
	else(NOT FLTK_FOUND)
		include_directories(${FLTK_INCLUDE_DIR})
		CHECK_CXX_SOURCE_COMPILES(
		"#include <FL/Fl_Copy_Surface.H>
		int main(){return 0;}" MGL_HAVE_FL_COPY)
	endif(NOT FLTK_FOUND)
	if(NOT MGL_HAVE_FL_COPY)
		set(MGL_HAVE_FL_COPY 0)
	endif(NOT MGL_HAVE_FL_COPY)
else(enable-fltk)
	set(MGL_HAVE_FLTK 0)
endif(enable-fltk)

if(enable-wx)
	set(MGL_HAVE_WX 1)
	FIND_PACKAGE(wxWidgets COMPONENTS base core gl)
	if(NOT wxWidgets_FOUND)
		message(SEND_ERROR "Couldn't find wxWidgets library.")
	endif(NOT wxWidgets_FOUND)
else(enable-wx)
	set(MGL_HAVE_WX 0)
endif(enable-wx)


find_program(findxgettext xgettext)
find_program(findmsgmerge msgmerge)
find_program(findmsgfmt msgfmt)
find_program(findmsgcat msgcat)
if(NOT findmsgfmt OR NOT findxgettext OR NOT findmsgmerge OR NOT findmsgcat )
	message("Building of translation files was disabled, because xgettext, msgmerge, msgcat or msgfmt was not found. Current translations will be just coped.")
	set(USE_GETTEXT 0)
else(NOT findmsgfmt  OR NOT findxgettext OR NOT findmsgmerge OR NOT findmsgcat )
	set(USE_GETTEXT 1)
endif(NOT findmsgfmt  OR NOT findxgettext OR NOT findmsgmerge OR NOT findmsgcat )
find_package(Intl)
if(NOT Intl_FOUND)
	set(MGL_USE_LIBINTL 0)
	message("Gettext and translations was fully disabled, because libintl was not found.")
else(NOT Intl_FOUND)
	set(MGL_USE_LIBINTL 1)
	set(MGL_DEP_LIBS ${Intl_LIBRARIES} ${MGL_DEP_LIBS})
	include_directories(${Intl_INCLUDE_DIRS})
endif(NOT Intl_FOUND)
set(po_files )
if(WIN32)
	set(USE_GETTEXT 0)
	set(MGL_USE_LIBINTL 0)
endif(WIN32)

if(enable-doc-info)
	set(MGL_HAVE_DOC_INFO 1)
	find_program(findmi makeinfo)
	if(NOT findmi)
		message(SEND_ERROR "Couldn't find makeinfo needed for documentation building.")
	endif(NOT findmi)
else(enable-doc-info)
	set(MGL_HAVE_DOC_INFO 0)
endif(enable-doc-info)

if(enable-doc-html OR enable-doc-site)
	if(enable-texi2html)
		find_program(findth texi2html)
		if(NOT findth)
			message(SEND_ERROR "Couldn't find texi2html needed for documentation building.")
		endif(NOT findth)
	else(enable-texi2html)
		find_program(findth texi2any)
		if(NOT findth)
			message(SEND_ERROR "Couldn't find texi2any needed for documentation building.")
		endif(NOT findth)
	endif(enable-texi2html)
endif(enable-doc-html OR enable-doc-site)

if(enable-texi2html)
	set(site_en ${CMAKE_BINARY_DIR}/texinfo/doc_en/doc_en.html)
	set(site_ru ${CMAKE_BINARY_DIR}/texinfo/doc_ru/doc_ru.html)
	set(th_opt )
else(enable-texi2html)
	set(th_opt --html)
	set(site_en ${CMAKE_BINARY_DIR}/texinfo/doc_en/index.html)
	set(site_ru ${CMAKE_BINARY_DIR}/texinfo/doc_ru/index.html)
endif(enable-texi2html)

if(enable-doc-html)
	set(MGL_HAVE_DOC_HTML 1)
else(enable-doc-html)
	set(MGL_HAVE_DOC_HTML 0)
endif(enable-doc-html)

if(enable-doc-site)
	set(MGL_HAVE_DOC_SITE 1)
else(enable-doc-site)
	set(MGL_HAVE_DOC_SITE 0)
endif(enable-doc-site)

if(enable-doc-pdf-ru)
	set(MGL_HAVE_DOC_PDF_RU 1)
	find_program(findtp texi2pdf)
	if(NOT findtp)
		message(SEND_ERROR "Couldn't find texi2pdf needed for documentation building.")
	endif(NOT findtp)
else(enable-doc-pdf-ru)
	set(MGL_HAVE_DOC_PDF_RU 0)
endif(enable-doc-pdf-ru)

if(enable-doc-pdf-en)
	set(MGL_HAVE_DOC_PDF_EN 1)
	find_program(findtp texi2pdf)
	if(NOT findtp)
		message(SEND_ERROR "Couldn't find texi2pdf needed for documentation building.")
	endif(NOT findtp)
else(enable-doc-pdf-en)
	set(MGL_HAVE_DOC_PDF_EN 0)
endif(enable-doc-pdf-en)

if(enable-doc-site)
	set(MGL_HAVE_DOC_JSON 1)
else(enable-doc-site)
	set(MGL_HAVE_DOC_JSON 0)
endif(enable-doc-site)

if(enable-doc-site)
	set(MGL_HAVE_DOC_PRC 1)
	if(NOT enable-pdf)
		message(SEND_ERROR "You need to enable pdf support for MathGL.")
	endif(NOT enable-pdf)
else(enable-doc-site)
	set(MGL_HAVE_DOC_PRC 0)
endif(enable-doc-site)

if(UNIX)
	add_definitions(-DNO_COLOR_ARRAY)
endif(UNIX)

if(WIN32)
	add_definitions(-DWIN32)
endif(WIN32)

if(NOT MGL_HAVE_GETOPT)
	add_subdirectory( addons/getopt )
endif(NOT MGL_HAVE_GETOPT)

add_subdirectory( src )
add_subdirectory( widgets )
add_subdirectory( include )
if(NOT enable-lgpl)
	if(MGL_HAVE_QT)
		add_subdirectory( udav )
		if(enable-json-sample)
			add_subdirectory( json )
		endif(enable-json-sample)
	endif(MGL_HAVE_QT)
	if(enable-python OR enable-lua OR enable-octave)
		add_subdirectory( lang )
	endif(enable-python OR enable-lua OR enable-octave)
add_subdirectory( utils )
add_subdirectory( examples )
if(NOT WIN32)
	add_subdirectory( fonts )
endif(NOT WIN32)
add_subdirectory( mgllab )
endif(NOT enable-lgpl)

if(NOT MSVC AND NOT BORLAND)

	if(MGL_HAVE_DOC_HTML OR MGL_HAVE_DOC_SITE OR MGL_HAVE_DOC_INFO OR MGL_HAVE_DOC_PDF_RU OR MGL_HAVE_DOC_PDF_EN )
		add_subdirectory( texinfo )
	endif(MGL_HAVE_DOC_HTML OR MGL_HAVE_DOC_SITE OR MGL_HAVE_DOC_INFO OR MGL_HAVE_DOC_PDF_RU OR MGL_HAVE_DOC_PDF_EN )

endif(NOT MSVC AND NOT BORLAND)

if(enable-mgltex)
	add_subdirectory( mgltex )
endif(enable-mgltex)

if(MGL_USE_LIBINTL)
	if(USE_GETTEXT)
		set(mgl_tl_list )
		add_custom_command(OUTPUT mathgl.pot
			COMMAND ${findxgettext} -s --keyword=_ -C -c --package-name=MathGL2 --package-version=${MathGL_VERSION} -o ${MathGL2_BINARY_DIR}/mathgl.pot ${po_files}
			WORKING_DIRECTORY ${MathGL2_SOURCE_DIR}/
			DEPENDS ${po_files} )
		set(mgl_clean_files mathgl.pot ${mgl_clean_files})
		foreach(tl ru es) # en)
			add_custom_command(OUTPUT mathgl_${tl}.po.done
				COMMAND ${findmsgmerge} -U mathgl_${tl}.po ${MathGL2_BINARY_DIR}/mathgl.pot
				COMMAND ${CMAKE_COMMAND} -E touch ${MathGL2_BINARY_DIR}/mathgl_${tl}.po.done
				WORKING_DIRECTORY ${MathGL2_SOURCE_DIR}/
				DEPENDS mathgl.pot mathgl_${tl}.po)
			add_custom_command(OUTPUT mathgl_${tl}.mo
				COMMAND ${findmsgfmt} -o mathgl_${tl}.mo ${MathGL2_SOURCE_DIR}/mathgl_${tl}.po
				DEPENDS mathgl_${tl}.po.done)
			install( FILES ${MathGL2_BINARY_DIR}/mathgl_${tl}.mo DESTINATION "${CMAKE_INSTALL_PREFIX}/share/locale/${tl}/LC_MESSAGES/" RENAME mathgl.mo )
			set(mgl_tl_list mathgl_${tl}.mo ${mgl_tl_list})
			set(mgl_clean_files mathgl_${tl}.mo mathgl_${tl}.po.done ${mgl_clean_files})
		endforeach(tl)
		add_custom_target(mgl_translations ALL DEPENDS ${mgl_tl_list})
	else(USE_GETTEXT)
		foreach(tl ru es) # en)
			install( FILES ${MathGL2_SOURCE_DIR}/translations/mathgl_${tl}.mo DESTINATION "${CMAKE_INSTALL_PREFIX}/share/locale/${tl}/LC_MESSAGES/" RENAME mathgl.mo )
		endforeach(tl)
	endif(USE_GETTEXT)
endif(MGL_USE_LIBINTL)

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${mgl_clean_files}")

# WARNING!!! DO NOT ADD ANYTHING AFTER THIS LINE!
add_subdirectory ( scripts )
