cmake_minimum_required(VERSION 3.4)
project(sqlite3 C)

include(conanbuildinfo.cmake)
conan_basic_setup()

# Add some options from https://sqlite.org/compile.html
option(SQLITE3_BUILD_EXECUTABLE "Build sqlite command line utility for accessing SQLite databases")
option(ENABLE_JSON1 "Enable JSON SQL functions")
option(ENABLE_COLUMN_METADATA "Enable additional APIs that provide convenient access to meta-data about tables and queries")
option(ENABLE_DBSTAT_VTAB "Enable the DBSTAT virtual table")
option(ENABLE_EXPLAIN_COMMENTS "Enable SQLite to insert comment text into the output of EXPLAIN")
option(ENABLE_FTS3 "Enable version 3 of the full-text search engine")
option(ENABLE_FTS3_PARENTHESIS "Kodifies the query pattern parser in FTS3 such that it supports operators AND and NOT (in addition to the usual OR and NEAR) and also allows query expressions to contain nested parenthesis")
option(ENABLE_FTS4 "Enable version 3 and 4 of the full-text search engine")
option(ENABLE_FTS5 "Enable version 5 of the full-text search engine")
option(ENABLE_SOUNDEX "Enable the soundex() SQL function")
option(ENABLE_PREUPDATE_HOOK "Enables APIs to handle any change to a rowid table")
option(ENABLE_RTREE "Enable support for the R*Tree index extension")
option(ENABLE_UNLOCK_NOTIFY "Enable support for the unlock notify API")
option(ENABLE_DEFAULT_SECURE_DELETE "Turns on secure deletion by default")
option(USE_ALLOCA "The alloca() memory allocator will be used in a few situations where it is appropriate.")
option(OMIT_LOAD_EXTENSION "Omits the entire extension loading mechanism from SQLite")
option(HAVE_FDATASYNC "Use fdatasync() instead of fsync() on unix systems")
option(HAVE_GMTIME_R "Use the threadsafe gmtime_r()")
option(HAVE_LOCALTIME_R "Use the threadsafe localtime_r()")
option(HAVE_POSIX_FALLOCATE "Use posix_fallocate()")
option(HAVE_STRERROR_R "Use strerror_r()")
option(HAVE_USLEEP "Use usleep() system call to implement the xSleep method")
option(DISABLE_GETHOSTUUID "Disable function gethostuuid")
set(MAX_BLOB_SIZE CACHE STRING "Set the maximum number of bytes in a string or BLOB")

add_library(${PROJECT_NAME} source_subfolder/sqlite3.c)
if (WIN32 AND MSVC AND BUILD_SHARED_LIBS)
    set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif(MSVC AND BUILD_SHARED_LIBS)
if(ENABLE_JSON1)
    target_compile_definitions(${PROJECT_NAME} PRIVATE SQLITE_ENABLE_JSON1)
endif()
if(ENABLE_COLUMN_METADATA)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_COLUMN_METADATA)
endif()
if(ENABLE_DBSTAT_VTAB)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_DBSTAT_VTAB)
endif()
if(ENABLE_EXPLAIN_COMMENTS)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_EXPLAIN_COMMENTS)
endif()
if(ENABLE_FTS3)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_FTS3)
endif()
if(ENABLE_FTS3_PARENTHESIS)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_FTS3_PARENTHESIS)
endif()
if(ENABLE_FTS4)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_FTS4)
endif()
if(ENABLE_FTS5)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_FTS5)
endif()
if(ENABLE_PREUPDATE_HOOK)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_PREUPDATE_HOOK)
endif()
if(ENABLE_RTREE)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_RTREE)
endif()
if(ENABLE_UNLOCK_NOTIFY)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_ENABLE_UNLOCK_NOTIFY)
endif()
if(ENABLE_DEFAULT_SECURE_DELETE)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_SECURE_DELETE)
endif()
if(ENABLE_SOUNDEX)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_SOUNDEX)
endif()
if(USE_ALLOCA)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_USE_ALLOCA)
endif()
if(OMIT_LOAD_EXTENSION)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_OMIT_LOAD_EXTENSION)
endif()
if(HAVE_FDATASYNC)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_FDATASYNC)
endif()
if(HAVE_GMTIME_R)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_GMTIME_R)
endif()
if(HAVE_LOCALTIME_R)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_LOCALTIME_R)
endif()
if(HAVE_POSIX_FALLOCATE)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_POSIX_FALLOCATE)
endif()
if(HAVE_STRERROR_R)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_STRERROR_R)
endif()
if(HAVE_USLEEP)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_USLEEP)
endif()
if(DISABLE_GETHOSTUUID)
    target_compile_definitions(${PROJECT_NAME}  PRIVATE HAVE_GETHOSTUUID=0)
endif()
target_compile_definitions(${PROJECT_NAME}  PRIVATE SQLITE_THREADSAFE=${THREADSAFE})
if(MAX_BLOB_SIZE)
    target_compile_definitions(${PROJECT_NAME} PRIVATE SQLITE_MAX_LENGTH=${MAX_BLOB_SIZE})
endif()

if(THREADSAFE)
    find_package(Threads REQUIRED)
    target_link_libraries(${PROJECT_NAME}  PRIVATE Threads::Threads)
endif()

if(NOT OMIT_LOAD_EXTENSION)
    target_link_libraries(${PROJECT_NAME}  PRIVATE ${CMAKE_DL_LIBS})
endif()

if(ENABLE_FTS5)
    find_library(MATH_LIBRARY m)
    if(MATH_LIBRARY)
        target_link_libraries(${PROJECT_NAME}  PRIVATE ${MATH_LIBRARY})
    endif()
endif()

install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)

install(DIRECTORY source_subfolder/ DESTINATION include
        FILES_MATCHING PATTERN "*.h")

if(SQLITE3_BUILD_EXECUTABLE)
    add_executable(sqlite3-bin source_subfolder/shell.c)
    target_link_libraries(sqlite3-bin PRIVATE ${PROJECT_NAME})
    set_target_properties(sqlite3-bin PROPERTIES OUTPUT_NAME "sqlite3")
    install(TARGETS sqlite3-bin RUNTIME DESTINATION bin)
endif()
