# This file contains functions that are used to build
# the CMakeLists.txt for an extension:
# Most of the time, an extension CMakeLists.txt should
# consist in a few lines containing calls to these functions.

macro(gd_add_extension_includes)
	include_directories(${sfml_include_dir})
	include_directories(${GDCORE_include_dir})
endmacro()

#Add common defines for a target that will be a GD extension
function(gd_add_extension_definitions target_name)

	#Define used in GD to check the build type
	IF(CMAKE_BUILD_TYPE MATCHES "Debug")
		add_definitions( -DDEBUG )
	ELSE()
		add_definitions( -DRELEASE )
	ENDIF()

	set(${target_name}_extra_definitions "${${target_name}_extra_definitions} GD_IDE_ONLY=1;" PARENT_SCOPE)

	#Defne used in GD to identify the environment
	IF (EMSCRIPTEN)
		add_definitions( -DEMSCRIPTEN )
	ELSEIF(WIN32)
		add_definitions( -DWINDOWS )
    ELSEIF(APPLE)
    	add_definitions( -DMACOS )
    ELSE()
		add_definitions( -DLINUX )
	ENDIF()

	IF(WIN32) #Windows specific defines
		add_definitions( "-DGD_CORE_API=__declspec(dllimport)" )
		add_definitions( "-DGD_API=__declspec(dllimport)" )
		add_definitions( "-DGD_EXTENSION_API=__declspec(dllexport)" )
		add_definitions( -D__GNUWIN32__ )
	ELSE()

		add_definitions( -DGD_API= )
		add_definitions( -DGD_CORE_API= )
		add_definitions( -DGD_EXTENSION_API= )
	ENDIF(WIN32)
endfunction()

#Add a GD extension target, that will produce the final library file.
function(gd_add_extension_target target_name source_files)
	IF(target_name STREQUAL "")
		MESSAGE(ERROR "You called gd_add_extension_target without specifying a target name")
	ENDIF()

	SET(platform_directory ${ARGV2})
	IF(NOT platform_directory)
		SET(platform_directory "CppPlatform")
	ENDIF()

	IF(EMSCRIPTEN)
		# Emscripten treats all libraries as static libraries
		add_library(${target_name} STATIC ${source_files})
	ELSE()
		add_library(${target_name} SHARED ${source_files})
	ENDIF()
	set_target_properties(${target_name} PROPERTIES PREFIX "")
	set_target_properties(${target_name} PROPERTIES COMPILE_DEFINITIONS "${${target_name}_extra_definitions}")
	set_target_properties(${target_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${GD_base_dir}/Binaries/Output/${CMAKE_BUILD_TYPE}_${CMAKE_SYSTEM_NAME}/${platform_directory}/Extensions")
	set_target_properties(${target_name} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${GD_base_dir}/Binaries/Output/${CMAKE_BUILD_TYPE}_${CMAKE_SYSTEM_NAME}/${platform_directory}/Extensions")
	set_target_properties(${target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${GD_base_dir}/Binaries/Output/${CMAKE_BUILD_TYPE}_${CMAKE_SYSTEM_NAME}/${platform_directory}/Extensions")
	IF(WIN32) #GD extensions have special suffix in their filenames.
		set_target_properties(${target_name} PROPERTIES SUFFIX ".xgdwe")
	ELSEIF(EMSCRIPTEN)
		set_target_properties(${target_name} PROPERTIES SUFFIX ".bc")
	ELSE()
		set_target_properties(${target_name} PROPERTIES SUFFIX ".xgde")
	ENDIF()
endfunction()

#Link default libraries with a target that is a GD extension
function(gd_extension_link_libraries target_name)
	IF(EMSCRIPTEN)
		#Nothing.
	ELSE()
		target_link_libraries(${target_name} GDCore)
		target_link_libraries(${target_name} ${sfml_LIBRARIES})
	ENDIF()
endfunction()

