##################################################################################
# MIT License                                                                    #
#                                                                                #
# Copyright (c) 2018 Maxime Pinard                                               #
#                                                                                #
# Permission is hereby granted, free of charge, to any person obtaining a copy   #
# of this software and associated documentation files (the "Software"), to deal  #
# in the Software without restriction, including without limitation the rights   #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      #
# copies of the Software, and to permit persons to whom the Software is          #
# furnished to do so, subject to the following conditions:                       #
#                                                                                #
# The above copyright notice and this permission notice shall be included in all #
# copies or substantial portions of the Software.                                #
#                                                                                #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  #
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  #
# SOFTWARE.                                                                      #
##################################################################################

# Functions summary:
# - cmutils_enable_ide_folders()
# - cmutils_disable_in_sources_build()
# - cmutils_set_classic_output_folders()

# include guard
if(CMUTILS_CONFIG_INCLUDED)
	return()
endif()
set(CMUTILS_CONFIG_INCLUDED ON)

## cmutils_enable_ide_folders()
# Enable IDE folders and put predefined CMake targets in _CMake folder
macro(cmutils_enable_ide_folders)
	if(ARGC GREATER 0)
		message(FATAL_ERROR "Too many arguments")
	endif()

	set_property(GLOBAL PROPERTY USE_FOLDERS ON)
	set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "_CMake")
	message(STATUS "[cmutils] Enabled IDE folders")
endmacro()

## cmutils_disable_in_sources_build()
# Don't allow to build in sources otherwise a makefile not generated by CMake can be overridden.
macro(cmutils_disable_in_sources_build)
	if(ARGC GREATER 0)
		message(FATAL_ERROR "Too many arguments")
	endif()

	set(CMAKE_DISABLE_SOURCE_CHANGES ON)
	set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)
	message(STATUS "[cmutils] Disabled in-sources build")
endmacro()

## cmutils_set_classic_output_folders()
# Set the runtime, library and archive output directory to classic folders build/bin and build/bin.
macro(cmutils_set_classic_output_folders)
	if(ARGC GREATER 0)
		message(FATAL_ERROR "Too many arguments")
	endif()

	# Runtime output directory: build/bin
	file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/bin)
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/bin)
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR}/build/bin)
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_CURRENT_BINARY_DIR}/build/bin)
	set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR}/build/bin)

	# Library output directory: build/bin
	file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR}/build/lib)

	# Archive output directory: build/lib
	set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_CURRENT_BINARY_DIR}/build/lib)
	set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_BINARY_DIR}/build/lib)

	message(STATUS "[cmutils] Set classic output folders build/bin and build/lib")
endmacro()
