if (${WIN32})
	get_filename_component(MGWDIR ${CMAKE_C_COMPILER} PATH)
	find_library(PIP_LIBRARY pip ${MGWDIR}/../lib)
	set(PIP_INCLUDES ${MGWDIR}/../include/pip)
	set(PIP_CMG ${MGWDIR}/../bin/pip_cmg.exe)
else ()
	find_library(PIP_LIBRARY pip /usr/lib/)
	if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
		find_library(PTHREAD_LIBRARY pthread)
		list(APPEND PIP_LIBRARY ${PTHREAD_LIBRARY})
	endif ()
	set(PIP_INCLUDES /usr/include/pip)
	set(PIP_CMG /usr/bin/pip_cmg)
endif ()

set(PIP_CMG /usr/bin/pip_cmg)

if (NOT PIP_VERSION)
	set(PIP_VERSION_MAJOR 0)
	set(PIP_VERSION_MINOR 4)
	set(PIP_VERSION_PATCH 0)
	set(PIP_VERSION_COUNT 5)
	set(PIP_VERSION_SUFFIX _r)
	set(PIP_VERSION ${PIP_VERSION_MAJOR}.${PIP_VERSION_MINOR}.${PIP_VERSION_PATCH}${PIP_VERSION_SUFFIX}${PIP_VERSION_COUNT} CACHE STRING "VERSION")
	message(STATUS "Found PIP version ${PIP_VERSION}")
endif ()
if (PIP_FIND_VERSION VERSION_GREATER PIP_VERSION)
	message(FATAL_ERROR "PIP version ${PIP_VERSION} is available, but ${PIP_FIND_VERSION} requested!")
endif ()

macro (pip_code_model SRC RESULT)
	set(NEED_PARSE FALSE CACHE BOOL "NEED_PARSE" FORCE)
	if (DEFINED CACHE_CMG_FILES)
		string(REPLACE " " ";" CMG_LIST_C ${CACHE_CMG_FILES})
		foreach (CF ${CMG_LIST_C})
			file (TIMESTAMP ${CF} ATIME)
			if (NOT TIME_${CF} STREQUAL ATIME)
				set(NEED_PARSE TRUE)
				#message(STATUS "MOD cache")
			endif ()
			set(TIME_${CF} ${ATIME} CACHE INTERNAL TIME_${CF} FORCE)
			#message (STATUS ${CF} ": " ${ATIME})
		endforeach (CF)
	else ()
		set(NEED_PARSE TRUE)
		#message(STATUS "NEW cache")
	endif ()
	if (NEED_PARSE)
		message(STATUS "Creating code model based on \"${SRC}\", please wait ... ")
		execute_process(COMMAND ${PIP_CMG} -qP -o ${PROJECT_NAME}_ccm -I${PIP_INCLUDES} ${SRC} OUTPUT_VARIABLE CMG_OUT)
		message(STATUS "Creating code model done, to use it include \"${PROJECT_NAME}_ccm.h\"")
		string(REPLACE "\n" ";" CMG_LIST ${CMG_OUT})
		string(REPLACE "\n" " " CMG_LIST_S ${CMG_OUT})
		set(CACHE_CMG_FILES ${CMG_LIST_S} CACHE INTERNAL "CACHE_CMG_FILES" FORCE)
		foreach (CF ${CMG_LIST})
			file (TIMESTAMP ${CF} ATIME)
			set(TIME_${CF} ${ATIME} CACHE INTERNAL TIME_${CF} FORCE)
		endforeach (CF)
	else ()
		message(STATUS "Code model is up to date :-)")
	endif ()
	#message(STATUS ${CMG_OUT})
	#message(STATUS ${CACHE_CMG_FILES})
	set(${RESULT} ${PROJECT_NAME}_ccm.cpp)
endmacro (pip_code_model)

