cmake_minimum_required(VERSION 3.10)
project(jdai VERSION 0.1.1 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_STANDARD 99)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)   # the shared library's C++ and C entry points on Windows

if(MSVC)
    add_compile_options(/utf-8 /bigobj)
    add_definitions(-D_CRT_SECURE_NO_WARNINGS -DNOMINMAX)
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()

option(JDAI_BUILD_TESTS "Build the jdai tests" ON)
option(JDAI_BUILD_TOOLS "Build the jdaiconv command line tool" ON)
option(JDAI_BUILD_SHARED "Build libjdai as a shared library too" ON)
option(JDAI_BUILD_PYTHON "Build the Python module (needs pybind11)" OFF)
option(JDAI_BUILD_BENCH "Build the benchmark (bench/)" OFF)
option(JDAI_BUILD_EXAMPLES "Build the C++ examples (examples/cpp; run by ctest with the tests)" ON)

find_package(Threads REQUIRED)

# bundled third-party C code (licensing.json): LZ4 (BSD-2-Clause), LZMA SDK (public domain),
# Ryu (Apache-2.0 or BSL-1.0); fast_float (header-only) is included by src/json.cpp
file(GLOB JDAI_LZ4_SRC ${CMAKE_CURRENT_SOURCE_DIR}/third_party/lz4/*.c)
file(GLOB JDAI_LZMA_SRC ${CMAKE_CURRENT_SOURCE_DIR}/third_party/lzma-sdk/*.c)
# SwapBytes.c (the SDK's byte-swap utility) is not used by the codecs, and its SIMD code needs GCC 4.9+
list(FILTER JDAI_LZMA_SRC EXCLUDE REGEX "SwapBytes\\.c$")
set(JDAI_RYU_SRC third_party/ryu/ryu/d2s.c third_party/ryu/ryu/f2s.c)
add_library(jdai_thirdparty OBJECT ${JDAI_LZ4_SRC} ${JDAI_LZMA_SRC} ${JDAI_RYU_SRC})
target_include_directories(jdai_thirdparty PRIVATE third_party/lz4 third_party/lzma-sdk third_party/ryu)
if(NOT MSVC)
    target_compile_options(jdai_thirdparty PRIVATE -w)
endif()

set(JDAI_SRC
    src/deflate.cpp
    src/zstd.cpp
    src/lzcodecs.cpp
    src/zmat.cpp
    src/threads.cpp
    src/value.cpp
    src/ndops.cpp
    src/bjdata_read.cpp
    src/bjdata_write.cpp
    src/fileio.cpp
    src/msgpack.cpp
    src/json.cpp
    src/chunks.cpp
    src/jdata.cpp
    src/mmap.cpp
    src/builder.cpp
    src/api.cpp
    src/capi.cpp
    src/jdata_class.cpp
    src/capi_struct.c
)

add_library(jdai_objs OBJECT ${JDAI_SRC})
target_include_directories(jdai_objs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(jdai_objs SYSTEM PRIVATE third_party/lz4 third_party/lzma-sdk third_party/ryu third_party/fast_float/include)
if(NOT MSVC)
    target_compile_options(jdai_objs PRIVATE -Wall -Wextra -pedantic)
endif()

add_library(jdai_static STATIC $<TARGET_OBJECTS:jdai_objs> $<TARGET_OBJECTS:jdai_thirdparty>)
if(MSVC)
    # MSVC names a static library and a DLL's import library both jdai.lib, in the same folder: the
    # shared build's import library would replace the static one, and what links jdai_static
    # would then need jdai.dll at run time
    set_target_properties(jdai_static PROPERTIES OUTPUT_NAME jdai_static)
else()
    set_target_properties(jdai_static PROPERTIES OUTPUT_NAME jdai)
endif()
target_include_directories(jdai_static PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
target_link_libraries(jdai_static PUBLIC Threads::Threads)
add_library(jdai::jdai ALIAS jdai_static)

if(JDAI_BUILD_SHARED)
    add_library(jdai_shared SHARED $<TARGET_OBJECTS:jdai_objs> $<TARGET_OBJECTS:jdai_thirdparty>)
    set_target_properties(jdai_shared PROPERTIES OUTPUT_NAME jdai VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
    target_include_directories(jdai_shared PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>)
    target_link_libraries(jdai_shared PUBLIC Threads::Threads)
    if(WIN32)
        # data (the C interface's JData struct) is exported only when marked: functions are
        # exported by CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS, data needs dllexport here, dllimport in users
        target_compile_definitions(jdai_objs PRIVATE JDAI_SHARED_BUILD)
        target_compile_definitions(jdai_shared INTERFACE JDAI_SHARED)
    endif()
endif()

if(JDAI_BUILD_TOOLS)
    add_executable(jdaiconv tools/jdaiconv.cpp)
    target_link_libraries(jdaiconv PRIVATE jdai_static)
    install(TARGETS jdaiconv RUNTIME DESTINATION bin)
endif()

if(JDAI_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()

if(JDAI_BUILD_EXAMPLES)
    foreach(e hello_jdata arrays user_types)
        add_executable(example_${e} examples/cpp/${e}.cpp)
        target_link_libraries(example_${e} PRIVATE jdai_static)
        if(JDAI_BUILD_TESTS)
            add_test(NAME example_${e} COMMAND example_${e} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
        endif()
    endforeach()
    # the C example, as C99 (JData) and C11 (JDAI_VAL too)
    foreach(std 99 11)
        add_executable(example_c_hello_jdata_c${std} examples/c/hello_jdata.c)
        target_link_libraries(example_c_hello_jdata_c${std} PRIVATE jdai_static)
        set_target_properties(example_c_hello_jdata_c${std} PROPERTIES LINKER_LANGUAGE CXX C_STANDARD ${std} C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
        if(JDAI_BUILD_TESTS)
            add_test(NAME example_c_hello_jdata_c${std} COMMAND example_c_hello_jdata_c${std} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
        endif()
    endforeach()
endif()

if(JDAI_BUILD_PYTHON)
    add_subdirectory(bindings/python)
endif()

if(JDAI_BUILD_BENCH)
    add_executable(bench_jdai bench/bench_jdai.cpp)
    target_link_libraries(bench_jdai PRIVATE jdai_static)
    # neurojson::jdata against nlohmann/json, when its header is found (-DJDAI_NLOHMANN_INCLUDE=dir)
    add_executable(bench_cxx_api bench/bench_cxx_api.cpp)
    target_link_libraries(bench_cxx_api PRIVATE jdai_static)
    find_path(JDAI_NLOHMANN_INCLUDE nlohmann/json.hpp)
    if(JDAI_NLOHMANN_INCLUDE)
        target_include_directories(bench_cxx_api SYSTEM PRIVATE ${JDAI_NLOHMANN_INCLUDE})
        target_compile_definitions(bench_cxx_api PRIVATE JDAI_WITH_NLOHMANN)
    endif()
endif()

if(SKBUILD)
    return()                                # a Python wheel holds only the module
endif()

install(DIRECTORY include/jdai DESTINATION include)
install(TARGETS jdai_static ARCHIVE DESTINATION lib)
if(JDAI_BUILD_SHARED)
    install(TARGETS jdai_shared LIBRARY DESTINATION lib RUNTIME DESTINATION bin ARCHIVE DESTINATION lib)
endif()
