##
## This file is part of ufo-serge filter set.
## Copyright (C) 2016 Serge Cohen
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
## Serge Cohen <serge.cohen@synchrotron-soleil.fr>
##

set(contrib_sxc_filter_SRCS
   ufo-backproject-irregular-task.c
   ufo-bin-voxels-task.c
   ufo-destrip-faster-task.c
   ufo-destrip-sinogram-task.c
   ufo-med-mad-reject-2d-task.c
   ufo-med-mad-reject-task.c
   ufo-ocl-1liner-task.c
   ufo-rotate-reframe-task.c
   ufo-smooth-pad-task.c
   ufo-stat-monitor-task.c
   )

set(contrib_sxc_aux_SRCS
    ufo-sxc-common.c)

file(GLOB contrib_sxc_filter_KERNELS "kernels/*.cl")

include(ConfigurePaths)
include(PkgConfigVars)
include(CheckCSourceCompiles)

configure_paths(CONTRIB_SXC_FILTERS)

find_package(OpenCL REQUIRED)
find_package(PkgConfig REQUIRED)

pkg_check_modules(UFO ufo>=${PKG_UFO_CORE_MIN_REQUIRED} REQUIRED)
pkg_check_variable(ufo plugindir)
pkg_check_variable(ufo kerneldir)

add_definitions("-std=c99 -Wall -fPIC")
add_definitions(-DG_LOG_DOMAIN="Ufo")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}
                    ${CMAKE_CURRENT_BINARY_DIR}
                    ${OPENCL_INCLUDE_DIRS}
                    ${UFO_INCLUDE_DIRS})

link_directories(${UFO_LIBRARY_DIRS} ${OPENCL_LIBRARY_DIRS})

set(CMAKE_REQUIRED_INCLUDES ${OPENCL_INCLUDE_DIRS})

set(ufofilter_LIBS
    m
    ${UFO_LIBRARIES}
    ${OpenCL_LIBRARIES})

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -pedantic -Wall -Wextra -fPIC -Wno-unused-parameter -Wno-deprecated-declarations")
set(CL_TARGET_OPENCL_VERSION "120")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/config.h)

# build static auxiliary library first
add_library(contrib_sxc_aux STATIC ${contrib_sxc_aux_SRCS})

foreach(_src ${contrib_sxc_filter_SRCS})
    # find plugin suffix
    string(REGEX REPLACE "ufo-([^ \\.]+)-task.*" "\\1" task "${_src}")

    # build string to get auxalleanous sources
    string(REPLACE "-" "_" _aux ${task})
    string(TOUPPER ${_aux} _aux_upper)

    # build string to get miscalleanous sources
    string(REPLACE "-" "_" _misc ${task})

    # create an option name and add this to disable filters
    set(_aux_src "${_aux}_aux_SRCS")
    set(_aux_libs "${_aux}_aux_LIBS")

    set(target "ufofilter${task}")

    # build single shared library per filter
    if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        add_library(${target} MODULE ${_src} ${${_aux_src}})
    else()
        add_library(${target} SHARED ${_src} ${${_aux_src}})
    endif()

    target_link_libraries(${target} ${ufofilter_LIBS} ${${_aux_libs}} contrib_sxc_aux)

    list(APPEND all_targets ${target})

    install(TARGETS ${target}
            ARCHIVE DESTINATION ${UFO_PLUGINDIR}
            LIBRARY DESTINATION ${UFO_PLUGINDIR})
endforeach()


# copy kernels
foreach(_kernel ${contrib_sxc_filter_KERNELS})
    install(FILES ${_kernel} DESTINATION ${UFO_KERNELDIR})
endforeach()
