
# ==============================================================================
# For convenience we define the sources as a variable. You can add 
# header files and cpp/c files and CMake will sort them out
# ==============================================================================

#file (GLOB INTERNAL_HEADER_FILES ${PRODUCT_ROOT}/source/*.hpp ${PRODUCT_ROOT}/source/*.incl_cpp ${PRODUCT_ROOT}/build/*.h)
file (GLOB INTERNAL_HEADER_COMMONCODE_NMDS ${PRODUCT_ROOT}/XMPFiles/source/NativeMetadataSupport/*.h)
source_group("Header Files\\Internal Headers\\Common Code\\NativeMetadataSupport" FILES ${INTERNAL_HEADER_COMMONCODE_NMDS})

list (APPEND INTERNAL_HEADER_COMMONCODE
	${XMPROOT_DIR}/source/Endian.h
	${XMPROOT_DIR}/source/SafeStringAPIs.h
	${XMPROOT_DIR}/source/SafeTypes.h
	${XMPROOT_DIR}/source/SuppressSAL.h
	${XMPROOT_DIR}/build/XMP_BuildInfo.h
	${SOURCE_ROOT}/XMPFiles.hpp
	${SOURCE_ROOT}/XMPFiles_Impl.hpp
	)
source_group("Header Files\\Internal Headers\\Common Code" FILES ${INTERNAL_HEADER_COMMONCODE})

file (GLOB INTERNAL_HEADER_FILEHANDLERS ${SOURCE_ROOT}/FileHandlers/*.hpp)
list (REMOVE_ITEM INTERNAL_HEADER_FILEHANDLERS
	${SOURCE_ROOT}/FileHandlers/AIFF_Handler.hpp
	)
source_group("Header Files\\Internal Headers\\File Handlers" FILES ${INTERNAL_HEADER_FILEHANDLERS})

file (GLOB INTERNAL_HEADER_FORMATSUPPORT_AIFF ${SOURCE_ROOT}/FormatSupport/AIFF/*.h)
source_group("Header Files\\Internal Headers\\Format Support\\AIFF" FILES ${INTERNAL_HEADER_FORMATSUPPORT_AIFF})

file (GLOB INTERNAL_HEADER_FORMATSUPPORT_IFF ${SOURCE_ROOT}/FormatSupport/IFF/*.h)
source_group("Header Files\\Internal Headers\\Format Support\\IFF" FILES ${INTERNAL_HEADER_FORMATSUPPORT_IFF})

file (GLOB INTERNAL_HEADER_FORMATSUPPORT_WAVE ${SOURCE_ROOT}/FormatSupport/WAVE/*.h)
source_group("Header Files\\Internal Headers\\Format Support\\WAVE" FILES ${INTERNAL_HEADER_FORMATSUPPORT_WAVE})

file (GLOB INTERNAL_HEADER_FORMATSUPPORT ${SOURCE_ROOT}/FormatSupport/*.hpp)

list (REMOVE_ITEM INTERNAL_HEADER_FORMATSUPPORT
#	${SOURCE_ROOT}/source/MD5.cpp
	${XMPROOT_DIR}/source/UnicodeConversions.cpp
	)
source_group("Header Files\\Internal Headers\\Format Support" FILES ${INTERNAL_HEADER_FORMATSUPPORT})

file (GLOB INTERNAL_HEADER_PLUGINHANDLER ${SOURCE_ROOT}/PluginHandler/*.h)
source_group("Header Files\\Internal Headers\\PluginHandler" FILES ${INTERNAL_HEADER_PLUGINHANDLER})

list (APPEND PUBLIC_HEADER_CLIENTGLUE
	${XMPROOT_DIR}/public/include/client-glue/TXMPFiles.incl_cpp
	${XMPROOT_DIR}/public/include/client-glue/WXMP_Common.hpp
	${XMPROOT_DIR}/public/include/client-glue/WXMPFiles.hpp
	)
source_group("Header Files\\Public Headers\\Client Glue" FILES ${PUBLIC_HEADER_CLIENTGLUE})
list (APPEND PUBLIC_HEADER
	${XMPROOT_DIR}/public/include/TXMPFiles.hpp
	${XMPROOT_DIR}/public/include/TXMPIterator.hpp
	${XMPROOT_DIR}/public/include/TXMPMeta.hpp
	${XMPROOT_DIR}/public/include/TXMPUtils.hpp
	${XMPROOT_DIR}/public/include/XMP.hpp
	${XMPROOT_DIR}/public/include/XMP.incl_cpp
	${XMPROOT_DIR}/public/include/XMP_Const.h
	${XMPROOT_DIR}/public/include/XMP_Environment.h
	${XMPROOT_DIR}/public/include/XMP_IO.hpp
	${XMPROOT_DIR}/public/include/XMP_Version.h
	)
source_group("Header Files\\Public Headers" FILES ${PUBLIC_HEADER})

file (GLOB HEADERFILES_THIRDPARTY_ZLIB ${XMPROOT_DIR}/third-party/zlib/*.h)
list (REMOVE_ITEM HEADERFILES_THIRDPARTY_ZLIB
	${CMAKE_CURRENT_SOURCE_DIR}/${XMPROOT_DIR}/third-party/zlib/gzguts.h
	)
source_group("Header Files\\ThirdParty\\zlib" FILES ${HEADERFILES_THIRDPARTY_ZLIB})

list (APPEND FILES_THIRDPARTY_EXPAT
	${TP_EXPAT_PATH}/xmlparse.c
	${TP_EXPAT_PATH}/xmlrole.c
	${TP_EXPAT_PATH}/xmltok.c
	)
list (APPEND FILES_THIRDPARTY_EXPAT ${RESOURCE_ROOT}/../../XMPCore/resource/${XMP_PLATFORM_SHORT}/expat_config.h)
source_group("Source Files\\ThirdParty\\expat" FILES ${FILES_THIRDPARTY_EXPAT})

list (APPEND HEADERFILES
	${XMPROOT_DIR}/source/Host_IO.hpp
	${XMPROOT_DIR}/source/XIO.hpp
	${XMPROOT_DIR}/source/IOUtils.hpp
	${XMPROOT_DIR}/source/XMPFiles_IO.hpp
	)
source_group("Header Files" FILES ${HEADERFILES})

#resource files
file (GLOB RESOURCE_FILES ${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}.*)
if(WIN32 AND ${XMP_BUILD_STATIC})
	list (REMOVE_ITEM RESOURCE_FILES ${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}.rc)
endif()
source_group("Resource Files" FILES ${RESOURCE_FILES})

#source files
file (GLOB SOURCEFILES_COMMONCODE_NMDS ${SOURCE_ROOT}/NativeMetadataSupport/*.cpp)
source_group("Source Files\\Common Code\\NativeMetadataSupport" FILES ${SOURCEFILES_COMMONCODE_NMDS})

list (APPEND SOURCEFILES_COMMONCODE
	${TP_ZUID_PATH}/MD5.cpp
	${XMPROOT_DIR}/source/UnicodeConversions.cpp
	${SOURCE_ROOT}/HandlerRegistry.cpp
	${XMPROOT_DIR}/source/SafeStringAPIs.cpp
	${XMPROOT_DIR}/source/PerfUtils.cpp
	${SOURCE_ROOT}/WXMPFiles.cpp
	${XMPROOT_DIR}/source/XIO.cpp
	${XMPROOT_DIR}/source/IOUtils.cpp
	${XMPROOT_DIR}/source/XML_Node.cpp
	${XMPROOT_DIR}/source/XMP_LibUtils.cpp
	${XMPROOT_DIR}/source/XMP_ProgressTracker.cpp
	${SOURCE_ROOT}/XMPFiles.cpp
	${SOURCE_ROOT}/XMPFiles_Impl.cpp
	${XMPROOT_DIR}/source/XMPFiles_IO.cpp
	)
if(UNIX)
	list(APPEND SOURCEFILES_COMMONCODE ${XMPROOT_DIR}/source/Host_IO-POSIX.cpp)
else()
	list(APPEND SOURCEFILES_COMMONCODE ${XMPROOT_DIR}/source/Host_IO-Win.cpp)
endif()
source_group("Source Files\\Common Code" FILES ${SOURCEFILES_COMMONCODE})

file (GLOB SOURCEFILES_FILEHANDLERS ${SOURCE_ROOT}/FileHandlers/*.cpp)
list (APPEND SOURCEFILES_FILEHANDLERS
	${SOURCE_ROOT}/FileHandlers/AIFF_Handler.hpp
	)

source_group("Source Files\\File Handlers" FILES ${SOURCEFILES_FILEHANDLERS})

file (GLOB SOURCEFILES_FORMATSUPPORT_AIFF ${SOURCE_ROOT}/FormatSupport/AIFF/*.cpp)
source_group("Source Files\\Format Support\\AIFF" FILES ${SOURCEFILES_FORMATSUPPORT_AIFF})

file (GLOB SOURCEFILES_FORMATSUPPORT_IFF ${SOURCE_ROOT}/FormatSupport/IFF/*.cpp)
source_group("Source Files\\Format Support\\IFF" FILES ${SOURCEFILES_FORMATSUPPORT_IFF})

file (GLOB SOURCEFILES_FORMATSUPPORT_WAVE ${SOURCE_ROOT}/FormatSupport/WAVE/*.cpp)
source_group("Source Files\\Format Support\\WAVE" FILES ${SOURCEFILES_FORMATSUPPORT_WAVE})

file (GLOB SOURCEFILES_FORMATSUPPORT ${SOURCE_ROOT}/FormatSupport/*.cpp)

source_group("Source Files\\Format Support" FILES ${SOURCEFILES_FORMATSUPPORT})

if (NOT APPLE_IOS AND NOT CMAKE_WINUNIVERSAL )
	file (GLOB SOURCEFILES_PLUGINHANDLER ${SOURCE_ROOT}/PluginHandler/*.cpp)
	list (REMOVE_ITEM SOURCEFILES_PLUGINHANDLER
		${SOURCE_ROOT}/PluginHandler/OS_Utils_Linux.cpp
		${SOURCE_ROOT}/PluginHandler/OS_Utils_WIN.cpp
		${SOURCE_ROOT}/PluginHandler/OS_Utils_Mac.cpp
		${SOURCE_ROOT}/PluginHandler/OS_Utils_Android.cpp
		)
	if (UNIX)
		if (APPLE)
			list (APPEND SOURCEFILES_PLUGINHANDLER ${SOURCE_ROOT}/PluginHandler/OS_Utils_Mac.cpp)
		elseif(ANDROID)
			list (APPEND SOURCEFILES_PLUGINHANDLER ${SOURCE_ROOT}/PluginHandler/OS_Utils_ANDROID.cpp)
		else()
			list (APPEND SOURCEFILES_PLUGINHANDLER ${SOURCE_ROOT}/PluginHandler/OS_Utils_Linux.cpp)
		endif()
	else()
			list (APPEND SOURCEFILES_PLUGINHANDLER ${SOURCE_ROOT}/PluginHandler/OS_Utils_WIN.cpp)
	endif()

	source_group("Source Files\\PluginHandler" FILES ${SOURCEFILES_PLUGINHANDLER})
endif()

#Core & Common public source files
#if(NOT ANDROID)
if (INCLUDE_CPP_DOM_SOURCE)
file (GLOB PUBLIC_XMPCOMMON_CLIENTGLUE_FILES ${XMPROOT_DIR}/public/include/XMPCommon/source/*.*)
source_group("Source Files\\Public\\XMPCommon" FILES ${PUBLIC_XMPCOMMON_CLIENTGLUE_FILES})

file (GLOB PUBLIC_XMPCORE_CLIENTGLUE_FILES ${XMPROOT_DIR}/public/include/XMPCore/source/*.*)
source_group("Source Files\\Public\\XMPCore" FILES ${PUBLIC_XMPCORE_CLIENTGLUE_FILES})
endif()
#endif()
list (APPEND HEADERFILES_THIRDPARTY_ZLIB
	${XMPROOT_DIR}/third-party/zlib/adler32.c
	${XMPROOT_DIR}/third-party/zlib/compress.c
	${XMPROOT_DIR}/third-party/zlib/crc32.c
	${XMPROOT_DIR}/third-party/zlib/deflate.c
	${XMPROOT_DIR}/third-party/zlib/infback.c
	${XMPROOT_DIR}/third-party/zlib/inffast.c
	${XMPROOT_DIR}/third-party/zlib/inflate.c
	${XMPROOT_DIR}/third-party/zlib/inftrees.c
	${XMPROOT_DIR}/third-party/zlib/trees.c
	${XMPROOT_DIR}/third-party/zlib/uncompr.c
	${XMPROOT_DIR}/third-party/zlib/zutil.c
	)
source_group("Source Files\\ThirdParty\\zlib" FILES ${HEADERFILES_THIRDPARTY_ZLIB})

list(APPEND SOURCE_FILES
	${INTERNAL_HEADER_COMMONCODE_NMDS}
	${INTERNAL_HEADER_COMMONCODE}
	${INTERNAL_HEADER_FILEHANDLERS}
	${INTERNAL_HEADER_FORMATSUPPORT_AIFF}
	${INTERNAL_HEADER_FORMATSUPPORT_IFF}
	${INTERNAL_HEADER_FORMATSUPPORT_WAVE}
	${INTERNAL_HEADER_FORMATSUPPORT}
	${INTERNAL_HEADER_PLUGINHANDLER}
	${PUBLIC_HEADER_CLIENTGLUE}
	${PUBLIC_HEADER}
	${HEADERFILES_THIRDPARTY_ZLIB}
	${HEADERFILES}
	${RESOURCE_FILES}
	${SOURCEFILES_COMMONCODE_NMDS}
	${SOURCEFILES_COMMONCODE}
	${SOURCEFILES_FILEHANDLERS}
	${SOURCEFILES_FORMATSUPPORT_AIFF}
	${SOURCEFILES_FORMATSUPPORT_IFF}
	${SOURCEFILES_FORMATSUPPORT_WAVE}
	${SOURCEFILES_FORMATSUPPORT}
	${SOURCEFILES_PLUGINHANDLER}
	${PUBLIC_XMPCOMMON_CLIENTGLUE_FILES}
	${PUBLIC_XMPCORE_CLIENTGLUE_FILES}
	${HEADERFILES_THIRDPARTY_ZLIB}
	${FILES_THIRDPARTY_EXPAT}
	)

# include directories
include_directories(${XMPROOT_DIR})
include_directories(${XMPROOT_DIR}/public/include)
include_directories(${XMPROOT_DIR}/public/include/client-glue)

include_directories(${TP_EXPAT_PATH})
include_directories(${XMPROOT_DIR}/third-party/expat/public/lib)
include_directories(${XMPROOT_DIR}/XMPFilesPlugins/api/source)
include_directories(${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT})

#additional link directory
link_directories(${OUTPUT_DIR})

# ==============================================================================
# Define what to do, lib, exe, etc
if (UNIX AND APPLE AND NOT ${XMP_BUILD_STATIC})
	# preprocess Info.plist
	if(NOT APPLE_IOS)
		add_custom_target(${TARGET_NAME}InfoPlist
			COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
			COMMAND if [ $(CONFIGURATION) != Debug ]; then
				${GCCTOOL} -E -P -x c ${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}.plist
				-F${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/
				-DPRODUCT_NAME=${TARGET_NAME} -DMAC_ENV=1 -DNDEBUG=1
				-include ${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}PList.h
				-o ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Info.plist
				\; else
				${GCCTOOL} -E -P -x c ${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}.plist
				-F${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/
				-DPRODUCT_NAME=${TARGET_NAME} -DMAC_ENV=1 -DDEBUG=1
				-include ${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}PList.h
				-o ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Info.plist
				\; fi
			COMMAND rm -f ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../CMakeFiles/${TARGET_NAME}.dir/Info.plist
			COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Info.plist ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../CMakeFiles/${TARGET_NAME}.dir/Info.plist
			COMMENT "Preprocessing Info-plist"
		)
	else()
		add_custom_target(${TARGET_NAME}InfoPlist
			COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
			COMMAND if [ $(CONFIGURATION) != Debug ]; then
				${GCCTOOL} -E -P -x c ${RESOURCE_ROOT}/ios/${TARGET_NAME}.plist
				-F${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/
				-DPRODUCT_NAME=${TARGET_NAME} -DIOS_ENV=1 -DNDEBUG=1
				-include ${RESOURCE_ROOT}/ios/${TARGET_NAME}PList.h
				-o ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Info.plist
				\; else
				${GCCTOOL} -E -P -x c ${RESOURCE_ROOT}/ios/${TARGET_NAME}.plist
				-F${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/
				-DPRODUCT_NAME=${TARGET_NAME} -DIOS_ENV=1 -DDEBUG=1
				-include ${RESOURCE_ROOT}/ios/${TARGET_NAME}PList.h
				-o ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Info.plist
				\; fi
			COMMAND rm -f ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../CMakeFiles/${TARGET_NAME}.dir/Info.plist
			COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Info.plist ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/../CMakeFiles/${TARGET_NAME}.dir/Info.plist
			COMMENT "Preprocessing Info-plist"
		)
	endif()
	set(DEPENDENCY_LIST "ALL:${TARGET_NAME}InfoPlist" "DLL:XMPCore")
else ()
	if(CMAKE_WINUNIVERSAL )
		add_definitions(-DWIN_UNIVERSAL_ENV -DWIN_ENV)
	endif()
	set(DEPENDENCY_LIST "DLL:XMPCore")
endif()

AddLibraryAndDependencies(${TARGET_NAME} ${XMP_BUILD_STATIC} YES "SHARED" SOURCE_FILES DEPENDENCY_LIST)

# ==============================================================================
# Link dependencies

if(WIN32)
	target_link_libraries(
		${TARGET_NAME}
		${LIB_ADOBEXMP}
		${XMP_PLATFORM_LINK}
	)
else(WIN32)
	if(UNIX AND NOT APPLE)
		target_link_libraries(
			${TARGET_NAME}
            ${LIB_ADOBEXMP}
		)
	endif()
endif()

set(FRAMEWORK_LIST "ALL:CoreFoundation" "Mac:CoreServices" "ALL:${LIB_ADOBEXMP}" "ALL:${XMP_PLATFORM_LINK}")
AddMacFramework(${TARGET_NAME} FRAMEWORK_LIST)

if(UNIX)
	if (NOT APPLE)
		SetPlatformLinkFlags(${TARGET_NAME} "-Xlinker --version-script -Xlinker \"${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}.exp\"" "")
	else()
		if(APPLE_IOS AND NOT XMP_BUILD_STATIC)
			set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_DYLIB_INSTALL_NAME_BASE "@rpath")
			set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.adobe.Adobe${TARGET_NAME}")
			set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_INSTALL_PATH "@rpath")
		endif()
		if(XMP_BUILD_STATIC AND NOT APPLE_IOS)
			set_target_properties(${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_SYMBOLS_PRIVATE_EXTERN "YES")
		endif()
		set_target_properties(${TARGET_NAME} PROPERTIES BUILD_WITH_INSTALL_RPATH ON BUILD_WITH_INSTALL_NAME_DIR ON INSTALL_NAME_DIR "@executable_path/../Frameworks")
		SetPlatformLinkFlags(${TARGET_NAME} "-exported_symbols_list \"${RESOURCE_ROOT}/${XMP_PLATFORM_SHORT}/${TARGET_NAME}.exp\"" "${XMPFILES_LIB}")
	endif()
else()
	SetPlatformLinkFlags(${TARGET_NAME} "" "${XMPFILES_LIB}")
endif()

set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME  ${XMPFILES_LIB})

# ==============================================================================
# Define output for this project
SetOutputPath(${OUTPUT_DIR} 0)

# ==============================================================================
# Post build
# ==============================================================================

if(UNIX AND NOT ANDROID)
	#hack for unix to rename the output static library. cmake add lib and extenstion as .a, rename it
	if (NOT APPLE)
		if (${XMP_BUILD_STATIC})
			add_custom_command (TARGET ${TARGET_NAME}
				POST_BUILD
				COMMAND  mv ${OUTPUT_DIR}/lib${XMPFILES_LIB}.a  ${OUTPUT_DIR}/${XMPFILES_LIB}.ar
			)
		else()
			if((${CMAKE_BUILD_TYPE} MATCHES "Debug") OR (${CMAKE_BUILD_TYPE} MATCHES "debug") )
				add_custom_command (TARGET ${TARGET_NAME}
					POST_BUILD
					COMMAND ls -l ${OUTPUT_DIR}/lib${XMPFILES_LIB}.so
				)
			else()
				add_custom_command (TARGET ${TARGET_NAME}
					POST_BUILD
					COMMAND strip ${OUTPUT_DIR}/lib${XMPFILES_LIB}.so
					COMMAND ls -l ${OUTPUT_DIR}/lib${XMPFILES_LIB}.so
				)
			endif()
		endif()

	
	endif()

	
else()
	set_target_properties(${TARGET_NAME} PROPERTIES PROJECT_LABEL ${PROJECT_LABEL_STR})

endif()

message (STATUS "===========================================================================")
message (STATUS " ${PROJECT_NAME} ")
message (STATUS "===========================================================================")
message (STATUS " OUTPUT_DIR = ${OUTPUT_DIR}")
