###
#
# PastixGenHeaders: generate some headers which depends on the config
#
###
FILE(WRITE ${CMAKE_BINARY_DIR}/append.cmake "
   FILE(WRITE \${DST} \"\")
   string(REPLACE \" \" \";\" SRC_LIST \${SRC})
   foreach(f \${SRC_LIST})
     FILE(READ \${f} _src)
     FILE(APPEND \${DST} \"\${_src}\")
     FILE(APPEND \${DST} \"\\n\")
   endforeach()
   ")

configure_file(sopalin/src/pastix.h.in ${CMAKE_BINARY_DIR}/sopalin/src/pastix.h.top)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/sopalin/src/pastix.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/pastix.h.top;${CMAKE_SOURCE_DIR}/sopalin/src/pastix.h;${CMAKE_SOURCE_DIR}/common/src/api.h"
  -DDST=${CMAKE_BINARY_DIR}/sopalin/src/pastix.h
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/sopalin/src/pastix.h.top ${CMAKE_SOURCE_DIR}/sopalin/src/pastix.h ${CMAKE_SOURCE_DIR}/common/src/api.h ${CMAKE_BINARY_DIR}/append.cmake)
install(FILES
  ${CMAKE_BINARY_DIR}/sopalin/src/pastix.h
  sopalin/src/cscd_utils.h
  DESTINATION "include")

configure_file(sopalin/src/pastix_fortran.h.in ${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h.top)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h.top;${CMAKE_SOURCE_DIR}/common/src/pastix_fortran.h"
  -DDST="${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h"
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h.top ${CMAKE_SOURCE_DIR}/common/src/pastix_fortran.h
  DEPENDS ${CMAKE_BINARY_DIR}/append.cmake ${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h.top ${CMAKE_SOURCE_DIR}/common/src/pastix_fortran.h)
install(FILES ${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h
  DESTINATION "include")

set(MURGE_HDRS
  ${CMAKE_SOURCE_DIR}/murge/include/smurge.h
  ${CMAKE_SOURCE_DIR}/murge/include/dmurge.h
  ${CMAKE_SOURCE_DIR}/murge/include/cmurge.h
  ${CMAKE_SOURCE_DIR}/murge/include/zmurge.h
  )
add_custom_command(OUTPUT
  ${MURGE_HDRS}
  COMMAND ${CMAKE_MAKE_PROGRAM} -C ${CMAKE_SOURCE_DIR}/murge arithmetics
  DEPENDS ${CMAKE_SOURCE_DIR}/murge/include/murge.h ${CMAKE_SOURCE_DIR}/murge/Makefile)
add_custom_target(murge_headers ALL SOURCES ${MURGE_HDRS})

if ( PASTIX_FLOAT_TYPE STREQUAL "Z" OR PASTIX_FLOAT_TYPE STREQUAL "C" )
  FILE(WRITE ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h "
      #ifndef TYPE_COMPLEX
      #  define TYPE_COMPLEX
      #endif
      ")
else()
  FILE(WRITE ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h "")
endif()

if ( PASTIX_FLOAT_TYPE STREQUAL "Z" OR PASTIX_FLOAT_TYPE STREQUAL "D" )
  FILE(APPEND ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h "
      #ifndef PREC_DOUBLE
      #  define PREC_DOUBLE
      #endif
      ")
else()
  FILE(WRITE ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h "")
endif()

if(PASTIX_INT64)
  FILE(APPEND ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h "
      #ifndef INTSSIZE64
      #  define INTSSIZE64
      #endif")
else(PASTIX_INT64)
  FILE(APPEND ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h "
      #ifndef INTSIZE32
      #  define INTSIZE32
      #endif")
endif(PASTIX_INT64)

add_custom_command(OUTPUT murge/include/murge.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h;${CMAKE_SOURCE_DIR}/murge/include/murge.h;${CMAKE_SOURCE_DIR}/sopalin/src/murge_pastix.h"
  -DDST="${CMAKE_BINARY_DIR}/murge/include/murge.h"
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/append.cmake ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h ${CMAKE_SOURCE_DIR}/murge/include/murge.h ${CMAKE_SOURCE_DIR}/sopalin/src/murge_pastix.h)
add_custom_target(murge_h ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/murge/include/murge.h)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/murge/include/smurge.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h;${CMAKE_SOURCE_DIR}/murge/include/smurge.h;${CMAKE_SOURCE_DIR}/sopalin/src/smurge_pastix.h"
  -DDST="${CMAKE_BINARY_DIR}/murge/include/smurge.h"
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/append.cmake ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h ${CMAKE_SOURCE_DIR}/murge/include/smurge.h ${CMAKE_SOURCE_DIR}/sopalin/src/smurge_pastix.h)
add_custom_target(smurge_h ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/murge/include/smurge.h)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/murge/include/dmurge.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h;${CMAKE_SOURCE_DIR}/murge/include/dmurge.h;${CMAKE_SOURCE_DIR}/sopalin/src/dmurge_pastix.h"
  -DDST="${CMAKE_BINARY_DIR}/murge/include/dmurge.h"
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/append.cmake ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h ${CMAKE_SOURCE_DIR}/murge/include/dmurge.h ${CMAKE_SOURCE_DIR}/sopalin/src/dmurge_pastix.h)
add_custom_target(dmurge_h ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/murge/include/dmurge.h)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/murge/include/cmurge.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h;${CMAKE_SOURCE_DIR}/murge/include/cmurge.h;${CMAKE_SOURCE_DIR}/sopalin/src/cmurge_pastix.h"
  -DDST="${CMAKE_BINARY_DIR}/murge/include/cmurge.h"
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/append.cmake ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h ${CMAKE_SOURCE_DIR}/murge/include/cmurge.h ${CMAKE_SOURCE_DIR}/sopalin/src/cmurge_pastix.h)
add_custom_target(cmurge_h ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/murge/include/cmurge.h)

add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/murge/include/zmurge.h
  COMMAND ${CMAKE_COMMAND}
  -DSRC="${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h;${CMAKE_SOURCE_DIR}/murge/include/zmurge.h;${CMAKE_SOURCE_DIR}/sopalin/src/zmurge_pastix.h"
  -DDST="${CMAKE_BINARY_DIR}/murge/include/zmurge.h"
  -P ${CMAKE_BINARY_DIR}/append.cmake
  DEPENDS ${CMAKE_BINARY_DIR}/append.cmake ${CMAKE_BINARY_DIR}/sopalin/src/murge_types.h ${CMAKE_SOURCE_DIR}/murge/include/zmurge.h ${CMAKE_SOURCE_DIR}/sopalin/src/zmurge_pastix.h)
add_custom_target(zmurge_h ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/murge/include/zmurge.h)

install(FILES ${CMAKE_BINARY_DIR}/murge/include/murge.h
  ${CMAKE_BINARY_DIR}/murge/include/smurge.h
  ${CMAKE_BINARY_DIR}/murge/include/dmurge.h
  ${CMAKE_BINARY_DIR}/murge/include/cmurge.h
  ${CMAKE_BINARY_DIR}/murge/include/zmurge.h
  DESTINATION "include")

add_custom_target(generate_headers ALL
  DEPENDS
  ${CMAKE_BINARY_DIR}/sopalin/src/pastix.h
  ${CMAKE_BINARY_DIR}/sopalin/src/pastix_fortran.h)
if(NOT PASTIX_WITH_MPI)
  add_dependencies(generate_headers pastix_nompi_header)
endif()