### CMakeLists.txt to build PASTIX matrix_drivers library
cmake_minimum_required(VERSION 2.8)

include_directories( ${CMAKE_BINARY_DIR}/sopalin/src)
include_directories( ${CMAKE_BINARY_DIR}/common/src)
set (modules blend common fax kass order perf sopalin symbol matrix_drivers)
foreach (mod ${modules})
  include_directories(${CMAKE_SOURCE_DIR}/${mod}/src)
endforeach()
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

# api_str_to_int
add_custom_command(
  OUTPUT  api_str_to_int.c
  COMMAND ${CMAKE_COMMAND}
  -DSRC=${CMAKE_SOURCE_DIR}/common/src/api.h
  -DDST=api_str_to_int.c
  -P ${CMAKE_SOURCE_DIR}/utils/src/genapi.cmake
  DEPENDS
  ${CMAKE_SOURCE_DIR}/utils/src/genapi.cmake
  ${CMAKE_SOURCE_DIR}/common/src/api.h
)

set(MATRIX_DRIVERS_SRC
  api_str_to_int.c
  skitf.f
  iohb.c
  mmio.c
  common_drivers.c
  get_options.c)

set(MATRIX_DRIVERS_SRC_FLOAT
  read_matrix.c
  rsaread.c
  hbread.c
  chbread.c
  mmread.c
  mmdread.c
  petscread.c
  cccread.c
  olafread.c
  cscdread.c
  peerread.c
  threefilesread.c
  fdupread.c
  laplacian.c)

# generate new files for each type of float
foreach(_src_list MATRIX_DRIVERS_SRC)
  foreach(_f ${${_src_list}_FLOAT})
    get_filename_component(_fulldir ${_f} PATH)
    string(REPLACE ${CMAKE_SOURCE_DIR} "" _dirtmp "${_fulldir}")
    string(REPLACE ${CMAKE_BINARY_DIR} "" _dir "${_dirtmp}")
    get_filename_component(_name ${_f} NAME)
    array2d_begin_loop( advanced "${type_defines_array}" 2 "type;define" )
    while(advanced)
      set(_outfile ${CMAKE_CURRENT_BINARY_DIR}/${_dir}/${type}_${_name})
      FILE(WRITE  ${_outfile}
	"
#define FORGET_TYPE
${define}
#include \"${_name}\"
")
      list(APPEND ${_src_list} ${_outfile})
      array2d_advance()
    endwhile()
    list(APPEND ${_src_list} ${_f})
  endforeach()
endforeach()

add_library(matrix_drivers ${MATRIX_DRIVERS_SRC})
add_dependencies(matrix_drivers generate_headers)

install(TARGETS matrix_drivers
	RUNTIME DESTINATION bin
	LIBRARY DESTINATION lib
	ARCHIVE DESTINATION lib
	PUBLIC_HEADER DESTINATION include)

install(FILES read_matrix.h get_options.h
	DESTINATION include)
