#
# CMakeD - CMake module for D Language
#
# Copyright (c) 2015, Ferdinand Thiessen <rpm@fthiessen.de>
# Copyright (c) 2007, Selman Ulug <selman.ulug@gmail.com>
#                     Tim Burrell <tim.burrell@gmail.com>
#
# All rights reserved.
#
# See LICENSE for details.
#
# Modified from CMake 2.6.5 CMakeCInformation.cmake
# See http://www.cmake.org/HTML/Copyright.html for details
#

include(CMakeCommonLanguageInclude)

# some compilers use different extensions, so set the extension here
# first so it can be overridden by the compiler specific file
if(UNIX)
	set(CMAKE_D_OUTPUT_EXTENSION .o)
else()
	set(CMAKE_D_OUTPUT_EXTENSION .obj)
endif()

# Compiler specific
if(CMAKE_D_COMPILER_ID)
	include(Compiler/${CMAKE_D_COMPILER_ID}-D OPTIONAL)

# Platform specific
	include(Platform/${CMAKE_SYSTEM_NAME}-${CMAKE_D_COMPILER_ID} OPTIONAL)
endif()

# This should be included before the _INIT variables are
# used to initialize the cache.  Since the rule variables 
# have if blocks on them, users can still define them here.
# But, it should still be after the platform file so changes can
# be made to those values.

if(CMAKE_USER_MAKE_RULES_OVERRIDE)
	include(${CMAKE_USER_MAKE_RULES_OVERRIDE})
endif(CMAKE_USER_MAKE_RULES_OVERRIDE)

if(CMAKE_USER_MAKE_RULES_OVERRIDE_D)
	include(${CMAKE_USER_MAKE_RULES_OVERRIDE_D})
endif(CMAKE_USER_MAKE_RULES_OVERRIDE_D)

# for most systems a module is the same as a shared library
# so unless the variable CMAKE_MODULE_EXISTS is set just
# copy the values from the LIBRARY variables
IF(NOT CMAKE_MODULE_EXISTS)
  SET(CMAKE_SHARED_MODULE_D_FLAGS ${CMAKE_SHARED_LIBRARY_D_FLAGS})
  SET(CMAKE_SHARED_MODULE_CREATE_D_FLAGS ${CMAKE_SHARED_LIBRARY_CREATE_D_FLAGS})
ENDIF(NOT CMAKE_MODULE_EXISTS)

SET (CMAKE_D_FLAGS "$ENV{CFLAGS} ${CMAKE_D_FLAGS_INIT}" CACHE STRING
     "Flags for D compiler.")

IF(NOT CMAKE_NOT_USING_CONFIG_FLAGS)
# default build type is none
  IF(NOT CMAKE_NO_BUILD_TYPE)
    SET (CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE_INIT} CACHE STRING 
      "Choose the type of build, options are: None(CMAKE_D_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")
  ENDIF(NOT CMAKE_NO_BUILD_TYPE)
  SET (CMAKE_D_FLAGS_DEBUG "${CMAKE_D_FLAGS_DEBUG_INIT}" CACHE STRING
    "Flags used by the compiler during debug builds.")
  SET (CMAKE_D_FLAGS_MINSIZEREL "${CMAKE_D_FLAGS_MINSIZEREL_INIT}" CACHE STRING
    "Flags used by the compiler during release minsize builds.")
  SET (CMAKE_D_FLAGS_RELEASE "${CMAKE_D_FLAGS_RELEASE_INIT}" CACHE STRING
    "Flags used by the compiler during release builds (/MD /Ob1 /Oi /Ot /Oy /Gs will produce slightly less optimized but smaller files).")
  SET (CMAKE_D_FLAGS_RELWITHDEBINFO "${CMAKE_D_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING
    "Flags used by the compiler during Release with Debug Info builds.")
ENDIF(NOT CMAKE_NOT_USING_CONFIG_FLAGS)

# now define the following rule variables

# CMAKE_D_CREATE_SHARED_LIBRARY
# CMAKE_D_CREATE_SHARED_MODULE
# CMAKE_D_CREATE_STATIC_LIBRARY
# CMAKE_D_COMPILE_OBJECT
# CMAKE_D_LINK_EXECUTABLE

# variables supplied by the generator at use time
# <TARGET>
# <TARGET_BASE> the target without the suffix
# <OBJECTS>
# <OBJECT>
# <LINK_LIBRARIES>
# <FLAGS>
# <LINK_FLAGS>

# D compiler information
# <CMAKE_D_COMPILER>  
# <CMAKE_SHARED_LIBRARY_CREATE_D_FLAGS>
# <CMAKE_SHARED_MODULE_CREATE_D_FLAGS>
# <CMAKE_D_LINK_FLAGS>

# Static library tools
# <CMAKE_AR> 
# <CMAKE_RANLIB>

IF("$ENV{D_PATH}" STREQUAL "")
	STRING(LENGTH ${CMAKE_D_COMPILER} CMAKE_D_COMPILER_LENGTH)
	IF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
		MATH(EXPR CMAKE_D_COMPILER_LENGTH "${CMAKE_D_COMPILER_LENGTH} - 12")
	ELSE(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
		MATH(EXPR CMAKE_D_COMPILER_LENGTH "${CMAKE_D_COMPILER_LENGTH} - 8")
	ENDIF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
	STRING(SUBSTRING ${CMAKE_D_COMPILER} 0 ${CMAKE_D_COMPILER_LENGTH} D_PATH)
ELSE("$ENV{D_PATH}" STREQUAL "")
	SET(D_PATH "$ENV{D_PATH}")
ENDIF("$ENV{D_PATH}" STREQUAL "")
MESSAGE(STATUS "D Compiler Install Prefix (use D_PATH env var to override): ${D_PATH}")

# create a D shared library
IF(NOT CMAKE_D_CREATE_SHARED_LIBRARY)
  IF(CMAKE_COMPILER_IS_GDC)
  	  SET(CMAKE_D_CREATE_SHARED_LIBRARY
  	    "<CMAKE_D_COMPILER> <CMAKE_SHARED_LIBRARY_D_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_D_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_D_FLAG><TARGET_SONAME> ${CMAKE_OUTPUT_D_FLAG}<TARGET> <OBJECTS> <LINK_LIBRARIES>")
  ELSE(CMAKE_COMPILER_IS_GDC)
  	  SET(CMAKE_D_CREATE_SHARED_LIBRARY
  	    "<CMAKE_D_COMPILER> <CMAKE_SHARED_LIBRARY_D_FLAGS> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_D_FLAGS> <CMAKE_SHARED_LIBRARY_SONAME_D_FLAG><TARGET_SONAME> ${CMAKE_OUTPUT_D_FLAG}<TARGET> <OBJECTS> <LINK_LIBRARIES> ${DSTDLIB_FLAGS}")
  ENDIF(CMAKE_COMPILER_IS_GDC)
ENDIF(NOT CMAKE_D_CREATE_SHARED_LIBRARY)

# create a D shared module just copy the shared library rule
IF(NOT CMAKE_D_CREATE_SHARED_MODULE)
  SET(CMAKE_D_CREATE_SHARED_MODULE ${CMAKE_D_CREATE_SHARED_LIBRARY})
ENDIF(NOT CMAKE_D_CREATE_SHARED_MODULE)

# create a D static library
IF(NOT CMAKE_D_CREATE_STATIC_LIBRARY)
	if(CMAKE_D_COMPILER_ID STREQUAL "DMD")
		if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
			set(CMAKE_D_CREATE_STATIC_LIBRARY
				"<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS>"
				"<CMAKE_RANLIB> <TARGET>")
		else(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
			set(CMAKE_D_CREATE_STATIC_LIBRARY
				"${DMD_LIBRARIAN} -c -p256 <TARGET> <OBJECTS>")
		endif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
	else(CMAKE_D_COMPILER_ID STREQUAL "DMD")
		if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    		set(CMAKE_D_CREATE_STATIC_LIBRARY
				"<CMAKE_AR> cr <TARGET>.lib <LINK_FLAGS> <OBJECTS> "
				"<CMAKE_RANLIB> <TARGET>.lib "
				"<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS> "
				"<CMAKE_RANLIB> <TARGET> "
			)
		else(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    		set(CMAKE_D_CREATE_STATIC_LIBRARY
				"<CMAKE_AR> cr <TARGET> <LINK_FLAGS> <OBJECTS> "
				"<CMAKE_RANLIB> <TARGET> "
			)
		endif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
	endif(CMAKE_D_COMPILER_ID STREQUAL "DMD")
endif(NOT CMAKE_D_CREATE_STATIC_LIBRARY)

# compile a D file into an object file
IF(NOT CMAKE_D_COMPILE_OBJECT)
    SET(CMAKE_D_COMPILE_OBJECT
      "<CMAKE_D_COMPILER> <FLAGS> ${CMAKE_OUTPUT_D_FLAG}<OBJECT> -c <SOURCE>")
ENDIF(NOT CMAKE_D_COMPILE_OBJECT)

IF(NOT CMAKE_D_LINK_EXECUTABLE)
  IF(CMAKE_COMPILER_IS_GDC)
    SET(CMAKE_D_LINK_EXECUTABLE
      "<CMAKE_D_COMPILER> <FLAGS> <CMAKE_D_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> ${CMAKE_OUTPUT_D_FLAG}<TARGET> <LINK_LIBRARIES> ${DSTDLIB_FLAGS} ${DSTDLIB_TYPE}")
  ELSE(CMAKE_COMPILER_IS_GDC)
    IF(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
      SET(CMAKE_D_LINK_EXECUTABLE
	"gcc ${DLINK_FLAGS} <CMAKE_D_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> -lpthread -lm ${DSTDLIB_FLAGS}")
    ELSE(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
      SET(CMAKE_D_LINK_EXECUTABLE
	"<CMAKE_D_COMPILER> <FLAGS> <CMAKE_D_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> ${CMAKE_OUTPUT_D_FLAG}<TARGET> <LINK_LIBRARIES>")
    ENDIF(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
  ENDIF(CMAKE_COMPILER_IS_GDC)
ENDIF(NOT CMAKE_D_LINK_EXECUTABLE)

MARK_AS_ADVANCED(
CMAKE_D_FLAGS
CMAKE_D_FLAGS_DEBUG
CMAKE_D_FLAGS_MINSIZEREL
CMAKE_D_FLAGS_RELEASE
CMAKE_D_FLAGS_RELWITHDEBINFO
)
SET(CMAKE_D_INFORMATION_LOADED 1)
