cmake_minimum_required(VERSION 3.12)
project(read_table)
option(PRINT_DATA "Print out the table data. Requires arrow-glib" ON)
option(VERBOSE "Enable for more diagnostics messages." OFF)
add_executable(read_table read_table.c arrow.c kernel_utils.c)
target_compile_definitions(read_table PUBLIC DEFINE_DEFAULT_ENGINE_BASE)
target_include_directories(read_table PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../../target/ffi-headers")
target_link_directories(read_table PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../../target/debug")
target_link_libraries(read_table PUBLIC delta_kernel_ffi)
target_compile_options(read_table PUBLIC)

# Add the test
include(CTest)
set(TestRunner "../../../tests/read-table-testing/run_test.sh")
set(DatPath "../../../../acceptance/tests/dat/out/reader_tests/generated")
set(ExpectedPath "../../../tests/read-table-testing/expected-data")
set(KernelTestPath "../../../../kernel/tests/data")
add_test(NAME read_and_print_all_prim COMMAND ${TestRunner} ${DatPath}/all_primitive_types/delta/ ${ExpectedPath}/all-prim-types.expected)
add_test(NAME read_and_print_basic_partitioned COMMAND ${TestRunner} ${DatPath}/basic_partitioned/delta/ ${ExpectedPath}/basic-partitioned.expected)
add_test(NAME read_and_print_with_dv_small COMMAND ${TestRunner} ${KernelTestPath}/table-with-dv-small/ ${ExpectedPath}/table-with-dv-small.expected)

if(WIN32)
  set(CMAKE_C_FLAGS_DEBUG "/MT")
  target_link_libraries(read_table PUBLIC ws2_32 userenv bcrypt ncrypt crypt32 secur32 ntdll RuntimeObject)
endif(WIN32)

if(MSVC)
  target_compile_options(read_table PRIVATE /W3 /WX)
else()
  # no-strict-prototypes because arrow headers have fn defs without prototypes
  target_compile_options(read_table PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-strict-prototypes -g -fsanitize=address)
  target_link_options(read_table PRIVATE -g -fsanitize=address)
endif()

if(VERBOSE)
  target_compile_definitions(read_table PUBLIC VERBOSE)
endif(VERBOSE)

if(PRINT_DATA)
  include(FindPkgConfig)
  pkg_check_modules(GLIB REQUIRED glib-2.0)
  pkg_check_modules(ARROW_GLIB REQUIRED arrow-glib)
  target_include_directories(read_table PUBLIC ${ARROW_GLIB_INCLUDE_DIRS})
  target_link_directories(read_table PUBLIC ${ARROW_GLIB_LIBRARY_DIRS})
  target_link_libraries(read_table PUBLIC ${ARROW_GLIB_LIBRARIES})
  target_compile_options(read_table PUBLIC ${ARROW_GLIB_CFLAGS_OTHER})
  target_compile_definitions(read_table PUBLIC PRINT_ARROW_DATA)
endif(PRINT_DATA)
