
function(remove_generator_macros t m)
	while(1)
		list(FIND CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS ${m} GEN_MACRO_INDEX)
		if( ${GEN_MACRO_INDEX} GREATER -1 )
			message("remove ${t} ${m} ${GEN_MACRO_INDEX}")
			list(REMOVE_AT CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS ${GEN_MACRO_INDEX})
			list(REMOVE_AT CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS ${GEN_MACRO_INDEX})
			SET(CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS ${CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS} CACHE INTERNAL "")
		else()
			break()
		endif()				
	endwhile()
endfunction()	

function(add_generator_macros t m v)
	list(APPEND CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS ${m} ${v})
	SET(CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS ${CMAKE_EXTRA_GENERATOR_${t}_SYSTEM_DEFINED_MACROS} CACHE INTERNAL "")
endfunction()	

function(include_what_you_use target dirs)
  if (CLANG AND ("${target}" IN_LIST KYTY_IWYU))
    find_program (CLANG_IWYU_EXE NAMES "include-what-you-use")
    if (CLANG_IWYU_EXE)
		set_target_properties(${target} PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "${CLANG_IWYU_EXE};-Xiwyu;--cxx17ns;-Qunused-arguments;-Werror")
    endif()
  endif()
endfunction()

function(include_what_you_use_with_mappings target dirs mappings)
  if (CLANG AND ("${target}" IN_LIST KYTY_IWYU))
    find_program (CLANG_IWYU_EXE NAMES "include-what-you-use")
    if (CLANG_IWYU_EXE)
		foreach(map ${mappings})
			list(APPEND mapdirs ";-Xiwyu;--mapping_file=${map}")
		endforeach()
		set_target_properties(${target} PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "${CLANG_IWYU_EXE};${mapdirs};-Xiwyu;--cxx17ns;-Qunused-arguments;-Werror")
    endif()
  endif()
endfunction()

function(clang_tidy_check target config headers dirs)
  if (CLANG AND ("${target}" IN_LIST KYTY_CLANG_TYDY) AND NOT MSVC)
    find_program (CLANG_TIDY_EXE NAMES "clang-tidy")
    if (CLANG_TIDY_EXE)
		foreach(dir ${dirs})
			list(APPEND incdirs "-extra-arg=-I${dir}")
		endforeach()
		foreach(header ${headers})
			list(APPEND filter "(${header}.*)")
		endforeach()
		string(REPLACE ";" "|" filter "${filter}")
		if ("${config}" STREQUAL "")
			set_target_properties(${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-warnings-as-errors=*;-header-filter=${filter};-extra-arg=-std=c++17;${incdirs}")	
		else()		
			set_target_properties(${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-config=${config};-warnings-as-errors=*;-header-filter=${filter};-extra-arg=-std=c++17;${incdirs}")	
		endif()
    endif()
  endif()
endfunction()

function(clang_tidy_fix target config headers dirs)
  if (CLANG AND ("${target}" IN_LIST KYTY_CLANG_TYDY))
    find_program (CLANG_TIDY_EXE NAMES "clang-tidy")
    if (CLANG_TIDY_EXE)
		foreach(dir ${dirs})
			list(APPEND incdirs "-extra-arg=-I${dir}")
		endforeach()
		foreach(header ${headers})
			list(APPEND filter "(${header}.*)")
		endforeach()
		string(REPLACE ";" "|" filter "${filter}")
		if ("${config}" STREQUAL "")
			set_target_properties(${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-fix;-format-style=file;-header-filter=${filter};-extra-arg=-std=c++17;${incdirs}")	
		else()	
			set_target_properties(${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};-fix;-format-style=file;-config=${config};-header-filter=${filter};-extra-arg=-std=c++17;${incdirs}")	
		endif()
    endif()
  endif()
endfunction()

macro(config_compiler_and_linker)

set(KYTY_WARNINGS_ARE_ERRORS ON)

set(KYTY_CPP_FLAGS "")
set(KYTY_CPP_FLAGS_RELEASE "")
set(KYTY_CPP_FLAGS_DEBUG "")
set(KYTY_EXE_LINKER_FLAGS "")
set(KYTY_EXE_LINKER_FLAGS_RELEASE "")
set(KYTY_EXE_LINKER_FLAGS_DEBUG "")

SET(CMAKE_NINJA_FORCE_RESPONSE_FILE 1 CACHE INTERNAL "")

if(MSVC)
	if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
		string(REGEX REPLACE "/W[0-4]" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
	else()
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
	endif()
	
	string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
	string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
	string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
	string(REGEX REPLACE "/MD" "/MT" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
	string(REGEX REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
	string(REGEX REPLACE "/MD" "/MT" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
			
	if (CLANG)
		set(KYTY_CPP_FLAGS "${KYTY_CPP_FLAGS} /utf-8 /Oy- /wd4244 /wd4305 /wd4800 /wd4345")	
	else()
		set(KYTY_CPP_FLAGS "${KYTY_CPP_FLAGS} /Zc:__cplusplus /utf-8 /Oy- /wd4244 /wd4305 /wd4800 /wd4345")	
	endif()
	
	set(KYTY_EXE_LINKER_FLAGS_RELEASE "/OPT:NOREF")
  
	if(KYTY_WARNINGS_ARE_ERRORS)
		set(KYTY_CPP_FLAGS "${KYTY_CPP_FLAGS} /WX")
	endif()
endif()

if(MINGW)

	if (CLANG)
	    set(KYTY_CPP_FLAGS "${KYTY_CPP_FLAGS} -fno-rtti -fno-exceptions -fcolor-diagnostics -finput-charset=UTF-8 -fexec-charset=UTF-8 -g -static -fno-strict-aliasing -fno-omit-frame-pointer -Wall -fmessage-length=0")
	else()
		set(KYTY_CPP_FLAGS "${KYTY_CPP_FLAGS} -fno-exceptions -fdiagnostics-color=always -finput-charset=UTF-8 -fexec-charset=UTF-8 -static-libgcc -static-libstdc++ -g -fno-strict-aliasing -fno-omit-frame-pointer -Wall -fmessage-length=0")
	endif()
	
	set(KYTY_CPP_FLAGS_DEBUG "${KYTY_CPP_FLAGS_DEBUG} -O0")

    if(KYTY_WARNINGS_ARE_ERRORS)
        set(KYTY_CPP_FLAGS "${KYTY_CPP_FLAGS} -Werror")
    endif()	

	set(KYTY_EXE_LINKER_FLAGS "${KYTY_EXE_LINKER_FLAGS} -g")
	
	unset(CMAKE_CXX_STANDARD_LIBRARIES CACHE)
	unset(CMAKE_C_STANDARD_LIBRARIES CACHE)
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${KYTY_CPP_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KYTY_CPP_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${KYTY_CPP_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${KYTY_CPP_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${KYTY_CPP_FLAGS_DEBUG}")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${KYTY_CPP_FLAGS_DEBUG}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${KYTY_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${KYTY_EXE_LINKER_FLAGS_RELEASE}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${KYTY_EXE_LINKER_FLAGS_DEBUG}")

endmacro()