include_directories(${CMAKE_CURRENT_SOURCE_DIR})

## generate log_code.cc, log_print.cc, log_header.cc
set_source_files_properties(
  "${CMAKE_CURRENT_BINARY_DIR}/log_code"
  "${CMAKE_CURRENT_BINARY_DIR}/log_print"
  "${CMAKE_CURRENT_BINARY_DIR}/log_header.h"
  PROPERTIES GENERATED TRUE)

add_executable(logformat logformat.cc)
target_link_libraries(logformat ${LIBTOKUPORTABILITY})

add_custom_command(
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/log_code.cc"
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/log_print.cc"
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/log_header.h"
  COMMAND $<TARGET_FILE:logformat> .
  DEPENDS logformat
  )
add_custom_target(
  generate_log_code
  DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/log_code.cc" "${CMAKE_CURRENT_BINARY_DIR}/log_print.cc" "${CMAKE_CURRENT_BINARY_DIR}/log_header.h"
  )

set(FT_SOURCES
  background_job_manager
  block_allocator
  block_table
  cachetable
  checkpoint
  compress
  dbufio
  fifo
  ft
  ft-cachetable-wrappers
  ft-flusher
  ft-hot-flusher
  ftloader
  ftloader-callback
  ft_msg
  ft_node-serialize
  ft-node-deserialize
  ft-ops
  ft-serialize
  ft-test-helpers
  ft-verify
  key
  kibbutz
  leafentry
  le-cursor
  logcursor
  logfilemgr
  logger
  log_upgrade
  memarena
  mempool
  minicron
  omt
  partitioned_counter
  pqueue
  queue
  quicklz
  recover
  rollback
  rollback-apply
  rollback-ct-callbacks
  roll
  sort
  sub_block
  threadpool
  txn
  txn_manager
  ule
  workqueue
  x1764
  xids
  ybt
  "${CMAKE_CURRENT_BINARY_DIR}/log_code"
  "${CMAKE_CURRENT_BINARY_DIR}/log_print"
  )

add_library(ft SHARED ${FT_SOURCES})
add_library(ft_static STATIC ${FT_SOURCES})
## we're going to link this into libtokudb.so so it needs to have PIC
add_space_separated_property(TARGET ft_static COMPILE_FLAGS -fPIC)
maybe_add_gcov_to_libraries(ft ft_static)

## depend on other generated targets
add_dependencies(ft install_tdb_h generate_log_code build_lzma)
add_dependencies(ft_static install_tdb_h generate_log_code build_lzma)

## link with lzma (which should be static) and link dependers with zlib
target_link_libraries(ft LINK_PRIVATE lzma ${LIBTOKUPORTABILITY})
target_link_libraries(ft LINK_PUBLIC z)
target_link_libraries(ft_static LINK_PRIVATE lzma)

if (CMAKE_C_COMPILER_ID STREQUAL Intel)
  target_link_libraries(ft LINK_PRIVATE -nodefaultlibs)
endif ()

## conditionally use cilk
if (USE_CILK)
  set_property(TARGET ft APPEND PROPERTY COMPILE_DEFINITIONS HAVE_CILK=1)
  set_property(TARGET ft_static APPEND PROPERTY COMPILE_DEFINITIONS HAVE_CILK=1)
  target_link_libraries(ft cilkrts)
  target_link_libraries(ft_static cilkrts)
endif (USE_CILK)

## build the bins in this directory
set(bins
  ftdump
  tdb_logprint
  tdb-recover
  ftverify
  )
foreach(bin ${bins})
  add_executable(${bin} ${bin})
  add_dependencies(${bin} install_tdb_h)
  target_link_libraries(${bin} ft ${LIBTOKUPORTABILITY})

  add_executable(${bin}_static ${bin})
  add_dependencies(${bin}_static install_tdb_h)
  target_link_libraries(${bin}_static ft_static z lzma ${LIBTOKUPORTABILITY}_static ${CMAKE_THREAD_LIBS_INIT} dl)

  add_common_options_to_binary_targets(${bin} ${bin}_static)
endforeach(bin)

# link in math.h library just for this tool.
target_link_libraries(ftverify m)
target_link_libraries(ftverify_static m)

install(
  TARGETS ftdump_static
  DESTINATION bin
  )

add_subdirectory(tests)