# Copyright (c) 2017, Intel Corporation
# 
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

FILE(GLOB_RECURSE CIF_HEADERS RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/.." "*.h")

if(MSVC OR MINGW)
  SET(OS "win")
else()
  SET(OS "lin")
endif(MSVC OR MINGW)

MACRO(MAKE_ABSOLUTE_PATHS ROOT_DIR RELATIVE_PATHS_LIST DST_ABSOLUTE_PATHS_LIST)
  SET(RET_LIST "")
  FOREACH(IT ${RELATIVE_PATHS_LIST})
    SET(ABS_PATH ${ROOT_DIR}/${IT})
    SET(RET_LIST "${RET_LIST};${ABS_PATH}")
  ENDFOREACH()

  SET(${DST_ABSOLUTE_PATHS_LIST} ${RET_LIST})
ENDMACRO(MAKE_ABSOLUTE_PATHS)

MACRO(SET_CIF_FILTERS_SINGLE FILES_RELATIVE BASE_DIR FILTER_BASE)
    SET(ROOT_PATH "${BASE_DIR}cif/")
    GET_FILENAME_COMPONENT(ROOT_PATH_ABSOLUTE "${ROOT_PATH}" ABSOLUTE)
    FOREACH(IT ${FILES_RELATIVE})
        GET_FILENAME_COMPONENT(PATH_WITHOUT_FILENAME ${IT} DIRECTORY)
        GET_FILENAME_COMPONENT(PATH_WITHOUT_FILENAME_ABSOLUTE "${PATH_WITHOUT_FILENAME}" ABSOLUTE)
        STRING(REPLACE "${ROOT_PATH_ABSOLUTE}" "" PARTIAL_PATH ${PATH_WITHOUT_FILENAME_ABSOLUTE})
        STRING(REPLACE "/" "\\" FILTER ${PARTIAL_PATH})
        SET(FILTER "${FILTER_BASE}${FILTER}")
        SOURCE_GROUP("${FILTER}" FILES "${IT}")
    ENDFOREACH(IT)
ENDMACRO(SET_CIF_FILTERS_SINGLE)

MACRO(SET_CIF_FILTERS SRC_CIF_SOURCES_RELATIVE SRC_CIF_HEADERS_RELATIVE BASE_DIR)
    SET_CIF_FILTERS_SINGLE("${SRC_CIF_SOURCES_RELATIVE}" "${BASE_DIR}" "cif\\\\sources\\\\") 
    SET_CIF_FILTERS_SINGLE("${SRC_CIF_HEADERS_RELATIVE}" "${BASE_DIR}" "cif\\\\headers\\\\")
ENDMACRO(SET_CIF_FILTERS)

MACRO(CONCAT_LIST DST_LIST SRC_LIST SRC_PREFIX)
    SET(RET_LIST "")
    FOREACH(IT ${SRC_LIST})
        SET(RET_LIST "${RET_LIST};${SRC_PREFIX}${IT}")
    ENDFOREACH(IT)
    SET(${DST_LIST} ${RET_LIST})
ENDMACRO(CONCAT_LIST)

MACRO(GET_CIF_FILES_RELATIVE DST_CIF_SOURCES_EXPORT DST_CIF_SOURCES_IMPORT DST_CIF_HEADERS BASE_DIR)
    CONCAT_LIST(RET_CIF_SOURCES_EXPORT "${CIF_SOURCES_EXPORT}" "${BASE_DIR}")
    CONCAT_LIST(RET_CIF_SOURCES_IMPORT "${CIF_SOURCES_IMPORT}" "${BASE_DIR}")
    CONCAT_LIST(RET_CIF_HEADERS "${CIF_HEADERS}" "${BASE_DIR}")
    SET(${DST_CIF_SOURCES_EXPORT} ${RET_CIF_SOURCES_EXPORT})
    SET(${DST_CIF_SOURCES_IMPORT} ${RET_CIF_SOURCES_IMPORT})
    SET(${DST_CIF_HEADERS} ${RET_CIF_HEADERS})
ENDMACRO(GET_CIF_FILES_RELATIVE)

SET(CIF_BUILTINS_SOURCES cif/builtins/memory/buffer/impl/buffer_impl.cpp
                         
   )

SET(CIF_SOURCES_EXPORT "${CIF_BUILTINS_SOURCES}"
                       cif/builtins/builtins_registry.cpp
                       cif/export/cif_main.cpp
                       cif/export/registry.cpp
                       cif/helpers/error.cpp
    )
                       
SET(CIF_SOURCES_IMPORT cif/import/cif_main.cpp
                       cif/os/${OS}/${OS}_library_handle.cpp
                       cif/helpers/error.cpp
    )
    
SET(CIF_SOURCES "${CIF_SOURCES_IMPORT};${CIF_SOURCES_EXPORT}")

MAKE_ABSOLUTE_PATHS("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CIF_SOURCES_EXPORT}" CIF_SOURCES_EXPORT_ABSOLUTE_PATH)
MAKE_ABSOLUTE_PATHS("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CIF_SOURCES_IMPORT}" CIF_SOURCES_IMPORT_ABSOLUTE_PATH)
SET(CIF_SOURCES_ABSOLUTE_PATH "${CIF_SOURCES_EXPORT_ABSOLUTE_PATH};${CIF_SOURCES_IMPORT_ABSOLUTE_PATH}")
MAKE_ABSOLUTE_PATHS("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CIF_HEADERS}" CIF_HEADERS_ABSOLUTE_PATH)

# Export these to parent CMake
SET(CIF_HEADERS ${CIF_HEADERS} PARENT_SCOPE)
SET(CIF_SOURCES_EXPORT ${CIF_SOURCES_EXPORT} PARENT_SCOPE)
SET(CIF_SOURCES_IMPORT ${CIF_SOURCES_IMPORT} PARENT_SCOPE)
SET(CIF_SOURCES ${CIF_SOURCES} PARENT_SCOPE)
SET(CIF_SOURCES_EXPORT_ABSOLUTE_PATH ${CIF_SOURCES_EXPORT_ABSOLUTE_PATH} PARENT_SCOPE)
SET(CIF_SOURCES_IMPORT_ABSOLUTE_PATH ${CIF_SOURCES_IMPORT_ABSOLUTE_PATH} PARENT_SCOPE)
SET(CIF_SOURCES_ABSOLUTE_PATH ${CIF_SOURCES_ABSOLUTE_PATH} PARENT_SCOPE)
SET(CIF_HEADERS_ABSOLUTE_PATH ${CIF_HEADERS_ABSOLUTE_PATH} PARENT_SCOPE)

SET(CIF_FOUND TRUE PARENT_SCOPE)
SET(CIF_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.." PARENT_SCOPE)
