################################################################################
# libibex
################################################################################

# Generate ibex_Setting.h and add its path to the list of include directories
configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/ibex_Setting.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/ibex_Setting.h
)
list (APPEND IBEX_INCDIRS ${CMAKE_CURRENT_BINARY_DIR})

# Recurse on all subdirectories
set (SUBDIR_LIST arithmetic bisector combinatorial contractor data function
                 numeric parser predicate set solver strategy symbolic system
                 tools)

foreach (subdir ${SUBDIR_LIST})
  add_subdirectory (${subdir})
endforeach()

# Configure flex and bison for the parser
find_package(BISON)
find_package(FLEX)
FLEX_TARGET(Lexer
  ${CMAKE_CURRENT_SOURCE_DIR}/parser/lexer.l
  ${CMAKE_CURRENT_BINARY_DIR}/parser/lexer.lex.cc
  COMPILE_FLAGS "-Pibex")
list (APPEND IBEX_SRC ${FLEX_Lexer_OUTPUTS})
BISON_TARGET(Parser
  ${CMAKE_CURRENT_SOURCE_DIR}/parser/parser.yc
  ${CMAKE_CURRENT_BINARY_DIR}/parser/parser.tab.cc
  COMPILE_FLAGS "--name-prefix=ibex --report=all --file-prefix=parser")
list (APPEND IBEX_SRC ${BISON_Parser_OUTPUTS})

ADD_FLEX_BISON_DEPENDENCY (Lexer Parser)
list (APPEND IBEX_INCDIRS ${CMAKE_CURRENT_BINARY_DIR}/parser)

# Create the target for libibex
add_library (ibex ${IBEX_SRC})
target_include_directories(ibex PUBLIC "$<BUILD_INTERFACE:${IBEX_INCDIRS}>")
target_include_directories(ibex PUBLIC "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
target_include_directories(ibex PUBLIC "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/ibex>")
target_link_libraries (ibex PUBLIC ${INTERVAL_LIB})
target_link_libraries (ibex PUBLIC ${LP_LIB})

################################################################################
# ibex.h
################################################################################
# Set IBEX_HDR and IBEX_INL from IBEX_SRC
foreach (srcfile ${IBEX_SRC})
  if (srcfile MATCHES "\\.h$" OR srcfile MATCHES "\\.hpp$")
    list (APPEND IBEX_HDR ${srcfile})
  elseif (srcfile MATCHES "\\inl$")
    list (APPEND IBEX_INL ${srcfile})
  endif ()
endforeach()

# remove parser.h from IBEX_HDR (internal header, should not be distributed)
list (REMOVE_ITEM IBEX_HDR ${CMAKE_CURRENT_SOURCE_DIR}/parser/parser.h)

#
set (IBEX_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/ibex.h)
file (WRITE ${IBEX_MAIN_HEADER} "/* This file in generated by CMake */\n\n")
file (APPEND ${IBEX_MAIN_HEADER} "#ifndef __IBEX_H__\n#define __IBEX_H__\n\n")
file (APPEND ${IBEX_MAIN_HEADER} "#include <ibex_Setting.h>\n")
foreach (header_path ${IBEX_HDR})
  GET_FILENAME_COMPONENT (header_name ${header_path} NAME)
  file (APPEND ${IBEX_MAIN_HEADER} "#include <${header_name}>\n")
endforeach()
file (APPEND ${IBEX_MAIN_HEADER} "\n#endif /* __IBEX_H__ */\n")

################################################################################
# installation
################################################################################
install (TARGETS ibex DESTINATION ${CMAKE_INSTALL_LIBDIR})
install (FILES ${IBEX_MAIN_HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install (FILES ${IBEX_HDR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ibex)
install (FILES ${IBEX_INL} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ibex)

################################################################################
# binaries
################################################################################
add_subdirectory (bin)
