#[[

 pip_code_model(<out_var> file0 [file1 ...] [OPTIONS opt0 [opt1 ...] ])

 Generate code model files for source files file0 [file1 ...]

 Options you can see by exec "pip_cmg -h"
 Relative files pathes read from CMAKE_CURRENT_SOURCE_DIR
 You should add ${<out_var>} to your target




 pip_resources(<out_var> file)

 Generate C++ files for resource file
 You should add ${<out_var>} to your target

]]


macro(PIP_EXTRACT_OPTIONS _pip_files _pip_options _abs)
	set(${_pip_files})
	set(${_pip_options})
	set(_PIP_DOING_OPTIONS FALSE)
	foreach(_currentArg ${ARGN})
		if("x${_currentArg}" STREQUAL "xABSOLUTE")
			set(${_abs} TRUE)
		else()
			if("x${_currentArg}" STREQUAL "xOPTIONS")
				set(_PIP_DOING_OPTIONS TRUE)
			else()
				if(_PIP_DOING_OPTIONS)
					list(APPEND ${_pip_options} "${_currentArg}")
				else()
					list(APPEND ${_pip_files} "${_currentArg}")
				endif()
			endif()
		endif()
	endforeach()
endmacro()


macro(pip_code_model RESULT)
	PIP_EXTRACT_OPTIONS(CCM_SRC OPTS ABS ${ARGN})
	#message(STATUS "src = ${CCM_SRC}")
	#message(STATUS "result = ${RESULT}")
	#message(STATUS "options = \"${CCM_OPTS}\"")
	set(CMG_INCLUDES)
	foreach(pi ${PIP_INCLUDES})
		list(APPEND CMG_INCLUDES "-I${pi}")
	endforeach()
	set(CCM_OUT ${CMAKE_CURRENT_BINARY_DIR}/ccm_${PROJECT_NAME}.cpp)
	set(${RESULT} ${${RESULT}} ${CCM_OUT})
	set(CCM_FILES)
	foreach(csrc ${CCM_SRC})
		if (IS_ABSOLUTE "${csrc}")
			list(APPEND CCM_FILES "${csrc}")
		else()
			list(APPEND CCM_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${csrc}")
		endif()
	endforeach()
	#message(STATUS "CCM = ${RESULT}")
	if(NOT DEFINED PIP_DLL_DIR)
		set(PIP_DLL_DIR ${CMAKE_CURRENT_BINARY_DIR})
	endif()
	add_custom_command(OUTPUT ${CCM_OUT}
						COMMAND ${PIP_CMG}
						ARGS -q ${OPTS} -o ${CMAKE_CURRENT_BINARY_DIR}/ccm_${PROJECT_NAME} ${CMG_INCLUDES} ${CCM_FILES}
						DEPENDS ${CCM_SRC}
						WORKING_DIRECTORY ${PIP_DLL_DIR}
						COMMENT "Generating ccm_${PROJECT_NAME}.h, ccm_${PROJECT_NAME}.cpp"
						VERBATIM)
endmacro()


macro(pip_resources RESULT INPUT)
	#message(STATUS "src = ${CCM_SRC}")
	#message(STATUS "result = ${RESULT}")
	#message(STATUS "options = \"${CCM_OPTS}\"")
	get_filename_component(RC_OUT "${INPUT}" NAME_WE)
	set(RC_FILE "pirc_${RC_OUT}.cpp")
	set(SRC_RC_OUT "${RC_OUT}")
	set(RC_OUT ${CMAKE_CURRENT_BINARY_DIR}/${RC_FILE})
	set(${RESULT} ${${RESULT}} ${RC_OUT})
	if(IS_ABSOLUTE "${INPUT}")
		set(RC_FILES "${INPUT}")
	else()
		set(RC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/${INPUT}")
	endif()
	#message(STATUS "CCM = ${RESULT}")
	if(NOT DEFINED PIP_DLL_DIR)
		set(PIP_DLL_DIR ${CMAKE_CURRENT_BINARY_DIR})
	endif()
	set(RC_DEPS ${RC_FILES})
	if(NOT BUILDING_pip)
		execute_process(COMMAND "${PIP_RC}" -l -i "${RC_FILES}"
			WORKING_DIRECTORY ${PIP_DLL_DIR}
			OUTPUT_VARIABLE RC_LIST)
		#message("${RC_LIST}")
		string(REPLACE "\n" ";" RC_LIST "${RC_LIST}")
		list(APPEND RC_DEPS "${RC_LIST}")
	endif()
	#message("PIP_RC: ${PIP_RC}")
	#message("RC_OUT: ${RC_OUT}")
	#message("RC_FILES: ${RC_FILES}")
	#message("PIP_DLL_DIR: ${PIP_DLL_DIR}")
	#message("RC_DEPS: ${RC_DEPS}")
	add_custom_command(OUTPUT ${RC_OUT}
		COMMAND ${PIP_RC}
		ARGS -s -i ${RC_FILES} -o ${RC_OUT} -n ${SRC_RC_OUT}
		DEPENDS ${RC_DEPS}
		WORKING_DIRECTORY ${PIP_DLL_DIR}
		COMMENT "Generating ${RC_FILE}"
		VERBATIM)
endmacro()

