if (${WIN32})
	find_package(MinGW REQUIRED)
	find_library(PIP_LIBRARY pip ${MINGW_LIB})
	set(PIP_INCLUDES ${MINGW_INCLUDE}/pip)
	set(PIP_CMG ${MINGW_BIN}/pip_cmg.exe)
else ()
	find_library(PIP_LIBRARY pip /usr/lib/)
	if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
		find_library(PTHREAD_LIBRARY pthread)
		find_library(DL_LIBRARY dl)
		list(APPEND PIP_LIBRARY ${PTHREAD_LIBRARY} ${DL_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)
	file(STRINGS "${PIP_INCLUDES}/pip_version_str.h" _VERSION REGEX "\".*\"")
	string(REGEX MATCH "\".*\"" _VERSION ${_VERSION})
	string(LENGTH ${_VERSION} SL)
	math(EXPR SL '${SL}-2')
	string(SUBSTRING ${_VERSION} 1 ${SL} _VERSION)
	set(PIP_VERSION ${_VERSION} 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(RESULT "")
	set(OPTIONS "")
	set(_IS_OPT false)
	foreach (_A ${ARGN})
		#message(STATUS ${_A})
		if (_IS_OPT)
			set (OPTIONS ${_A})
		else ()
			if ("${_A}" STREQUAL "OPTIONS")
				set(_IS_OPT true)
			else ()
				set (RESULT ${_A})
			endif ()
		endif ()
	endforeach()
	#message(STATUS "src = ${CMAKE_CURRENT_SOURCE_DIR}/${SRC}")
	#message(STATUS "result = ${RESULT}")
	#message(STATUS "options = ${OPTIONS}")
	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 ()
	set(${RESULT} ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_ccm.cpp)
	#message(STATUS "Res = ${${RESULT}}")
	if (NOT EXISTS ${${RESULT}})
		set(NEED_PARSE TRUE)
	endif ()
	if (NEED_PARSE)
		message(STATUS "Creating code model based on \"${SRC}\", please wait ... ")
		#message(STATUS "exec \"-qP ${OPTIONS} -o ${PROJECT_NAME}_ccm -I${PIP_INCLUDES} ${SRC}\"")
		execute_process(COMMAND ${PIP_CMG} -qP ${OPTIONS} -o ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_ccm -I${PIP_INCLUDES} ${CMAKE_CURRENT_SOURCE_DIR}/${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})
endmacro (pip_code_model)

