# Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
#
# The MySQL Connector/ODBC is licensed under the terms of the GPLv2
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most 
# MySQL Connectors. There are special exceptions to the terms and 
# conditions of the GPLv2 as it is applied to this software, see the 
# FLOSS License Exception
# <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
#
# This program 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; version 2 of the License.
#
# This program 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, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

##########################################################################

PROJECT(MySQL_Connector_ODBC)

CMAKE_MINIMUM_REQUIRED(VERSION 2.4 FATAL_ERROR)
#SET(CMAKE_VERBOSE_MAKEFILE ON)

if(COMMAND cmake_policy)
       cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)

#-------------- find mysql --------------------
INCLUDE(${CMAKE_SOURCE_DIR}/cmake/FindMySQL.cmake)
#-----------------------------------------------------

#-------------- unixodbc/iodbc/win -------------------
IF(WIN32)
	SET(ODBCLIB odbc32)
	SET(ODBCINSTLIB odbccp32)
ELSE(WIN32)
	INCLUDE(cmake/FindODBC.cmake)
	SET(ODBCLIB odbc)
	SET(ODBCINSTLIB odbcinst)
	# Disabling setup lib build on non-windows platforms for now
	SET(DISABLE_GUI 1)
ENDIF(WIN32)
#-----------------------------------------------------

#------------------ check compatibility---------------
TRY_COMPILE(COMPILE_RESULT ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/cmake/sqlcolattrib1.c)
MESSAGE(STATUS "Checking if SQLColAttribute last arg is compatible with SQLLEN* - ${COMPILE_RESULT}")

IF(COMPILE_RESULT)
	ADD_DEFINITIONS(-DUSE_SQLCOLATTRIBUTE_SQLLEN_PTR)
ELSE(COMPILE_RESULT)
	TRY_COMPILE(COMPILE_RESULT1 ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/cmake/sqlcolattrib2.c)
	MESSAGE(STATUS "Checking if SQLColAttribute last arg is compatible with SQLPOINTER - ${COMPILE_RESULT1}")

	IF(COMPILE_RESULT1)
		ADD_DEFINITIONS(-DUSE_SQLCOLATTRIBUTE_SQLPOINTER)
	ENDIF(COMPILE_RESULT1)
ENDIF(COMPILE_RESULT)

TRY_COMPILE(COMPILE_RESULT ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/cmake/sqlparamopt1.c)
MESSAGE(STATUS "Checking if SQLParamOptions() 2nd and 3rd arg is compatible with SQLULEN - ${COMPILE_RESULT}")

IF(COMPILE_RESULT)
	ADD_DEFINITIONS(-DUSE_SQLPARAMOPTIONS_SQLULEN_PTR)
ELSE(COMPILE_RESULT)
	TRY_COMPILE(COMPILE_RESULT1 ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/cmake/sqlparamopt2.c)
	MESSAGE(STATUS "Checking if SQLParamOptions() 2nd and 3rd arg is compatible with SQLUINTEGER - ${COMPILE_RESULT1}")

	IF(COMPILE_RESULT1)
	ADD_DEFINITIONS(-DUSE_SQLPARAMOPTIONS_SQLUINTEGER_PTR)
	ENDIF(COMPILE_RESULT1)

ENDIF(COMPILE_RESULT)
#-----------------------------------------------------

#------------ build options for windows --------------
IF(WIN32)
	REMOVE_DEFINITIONS(-DUNICODE)
	ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
	ADD_DEFINITIONS(-DENGLISH -DMYODBC_EXPORTS -D_USERDLL)
	ADD_DEFINITIONS(-D_WIN32 -DWIN32 -D_WINDOWS -D__WIN__)

	#Since 5.5.13 libmysql introduces dependency on Secur32.lib
	FILE(STRINGS "${MYSQL_INCLUDE_DIR}\\mysql_version.h" mysql_version REGEX "^\#define[ \t]+MYSQL_VERSION_ID")

	STRING(REGEX REPLACE "^\#define[ \t]+MYSQL_VERSION_ID[ \t]+([0-9]+)" "\\1" MYSQL_CLIENT_VERSION "${mysql_version}")

	IF(MYSQL_CLIENT_VERSION GREATER 50512)
		MESSAGE(STATUS "MySQL client lib(version ${MYSQL_CLIENT_VERSION}) requires Secure32.lib - TRUE")
		SET(SECURE32_LIB Secur32)
	ENDIF(MYSQL_CLIENT_VERSION GREATER 50512)
	

	# edits for all config build flags
	FOREACH(TYPE C CXX)
		# makefiles use blank configuration
		FOREACH(CFG "_DEBUG" "_MINSIZEREL" "_RELEASE" "_RELWITHDEBINFO")
		#FOREACH(CFG "" ${CMAKE_CONFIGURATION_TYPES})
			#MESSAGE("Replacing CMAKE_${TYPE}_FLAGS${CFG}: ${CMAKE_${TYPE}_FLAGS${CFG}}")

			SET(NEW_FLAGS "${CMAKE_${TYPE}_FLAGS${CFG}}")

			# fix up static libc flags
			STRING(REPLACE "/MD" "/MT" NEW_FLAGS "${NEW_FLAGS}")

			# Add some additional help for debug builds
			IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
				STRING(REPLACE "/Zi" "/ZI" NEW_FLAGS "${NEW_FLAGS}")
				SET(NEW_FLAGS "${NEW_FLAGS} /RTC1 /RTCc")
			ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")

			# *FORCE* to override whats already placed into the cache
			SET(CMAKE_${TYPE}_FLAGS${CFG} "${NEW_FLAGS}" CACHE STRING
				"CMAKE_${TYPE}_FLAGS${CFG} (overwritten for odbc)" FORCE)

			#MESSAGE("New       CMAKE_${TYPE}_FLAGS${CFG}: ${CMAKE_${TYPE}_FLAGS${CFG}}")
		ENDFOREACH(CFG)
	ENDFOREACH(TYPE)
ENDIF(WIN32)
#-----------------------------------------------------

SET(EXECUTABLE_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/bin")
SET(LIBRARY_OUTPUT_PATH "${CMAKE_SOURCE_DIR}/lib")

ADD_SUBDIRECTORY(util)
ADD_SUBDIRECTORY(driver)
IF(NOT DISABLE_GUI)
	ADD_SUBDIRECTORY(setupgui)
ENDIF(NOT DISABLE_GUI)

ADD_SUBDIRECTORY(dltest)
ADD_SUBDIRECTORY(installer)
ADD_SUBDIRECTORY(monitor)

IF(NOT DISABLE_GUI)
	#ADD_SUBDIRECTORY(dsn-editor)
ENDIF(NOT DISABLE_GUI)

ADD_SUBDIRECTORY(test)

